1 package comum.util;
2
3 import java.util.Date;
4 import java.util.Enumeration;
5 import java.util.List;
6
7 import javax.servlet.http.HttpServletRequest;
8
9
10
11
12 public class Pagina {
13
14 public static final String SIM = "S";
15 public static final String NAO = "N";
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public static final int getParamInt(HttpServletRequest request, String nomeParam) {
31 String valor = request.getParameter(nomeParam);
32 if (((valor == null) || ("".equals(valor))) && (request.getAttribute(nomeParam) != null))
33 valor = request.getAttribute(nomeParam).toString();
34 if ((valor != null) && (!"".equals(valor))) {
35 try {
36 return(Integer.parseInt(valor));
37 } catch (Exception e) {
38 return(0);
39 }
40 } else
41 return(0);
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public static final long getParamLong(HttpServletRequest request, String nomeParam) {
58 String valor = request.getParameter(nomeParam);
59 if (((valor == null) || ("".equals(valor))) && (request.getAttribute(nomeParam) != null))
60 valor = request.getAttribute(nomeParam).toString();
61 if ((valor != null) && (!"".equals(valor))) {
62 try {
63 return(Long.parseLong(valor));
64 } catch (Exception e) {
65 return(0);
66 }
67 } else
68 return(0);
69 }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public static final Short getParamShort(HttpServletRequest request, String nomeParam) {
86 String valor = request.getParameter(nomeParam);
87 if (((valor == null) || ("".equals(valor))) && (request.getAttribute(nomeParam) != null))
88 valor = request.getAttribute(nomeParam).toString();
89 if ((valor != null) && (!"".equals(valor))) {
90 try {
91 return(Short.decode(valor));
92 } catch (Exception e) {
93 return(Short.decode("0"));
94 }
95 } else
96 return(Short.decode("0"));
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 public static final String getParamStr(HttpServletRequest request, String nomeParam) {
111 String valor = request.getParameter(nomeParam);
112 if (((valor == null) || ("".equals(valor))) && (request.getAttribute(nomeParam) != null))
113 valor = request.getAttribute(nomeParam).toString();
114 if (valor != null) {
115 try {
116 return(valor.toString().replaceAll("\"","'"));
117 } catch (Exception e) {
118 return("");
119 }
120 } else
121 return("");
122 }
123
124
125
126
127
128
129
130
131
132
133
134 public static final String getParamStr(HttpServletRequest request, String nomeParam, boolean arg) {
135
136 String codigo = null;
137
138 Enumeration<?> parametros = request.getParameterNames();
139
140 while(parametros.hasMoreElements()) {
141 String[] p = ((String) parametros.nextElement()).split(":");
142 if(p[0].equals(nomeParam.trim())) {
143 codigo = p[1];
144 break;
145 }
146 }
147
148 String valor = request.getParameter(nomeParam + ":" + codigo);
149
150
151 if (((valor == null) || ("".equals(valor))) && (request.getAttribute(nomeParam) != null))
152 valor = request.getAttribute(nomeParam).toString();
153 if (valor != null) {
154 try {
155 return(valor.toString().replaceAll("\"","'"));
156 } catch (Exception e) {
157 return("");
158 }
159 } else
160 return("");
161 }
162
163
164
165
166
167
168
169
170
171
172
173 public static final String getParamStrInputText(HttpServletRequest request, String nomeParam) {
174
175 String codigo = null;
176
177 Enumeration<?> parametros = request.getParameterNames();
178
179 while(parametros.hasMoreElements()) {
180 String[] p = ((String) parametros.nextElement()).split(":");
181 if(p[0].equals(nomeParam.trim())) {
182 codigo = p[1];
183 break;
184 }
185 }
186
187 return codigo;
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201 public static final String getParam(HttpServletRequest request, String param) {
202 if (param != null && !"".equalsIgnoreCase(param))
203 {
204 String valor = request.getParameter(param);
205 if (valor == null || "".equals(valor.trim())) {
206 return null;
207 } else
208 return valor.replaceAll("\"","'").trim();
209 }
210 return null;
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 public static final Date getParamDataBanco(HttpServletRequest request, String nomeParam) {
227
228 String valor = request.getParameter(nomeParam);
229 if ((valor != null) && (!"".equals(valor))) {
230 try {
231 return(new Date(valor.substring(6)+"/"+valor.substring(3,5)+"/"+valor.substring(0,2)));
232 } catch (Exception e) {
233 return(null);
234 }
235 } else
236 return(null);
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251 public static final boolean getParamBool(HttpServletRequest request, String nomeParam) {
252 return("true".equals(getParamStr(request, nomeParam)));
253 }
254
255
256
257
258
259
260
261
262
263 public static final String getParamOrDefault(HttpServletRequest request, String nomeParam, String strDefault){
264 try{
265 if(request == null)
266 return strDefault;
267 else{
268 String valor = request.getParameter(nomeParam);
269 if (valor == null || valor.trim() == "")
270 return strDefault;
271 else
272 return valor;
273 }
274 }catch(NullPointerException e){
275 return strDefault;
276 }
277 }
278
279
280
281
282
283
284
285
286
287
288 public static final String trocaNull(Object objeto){
289 if(objeto == null)
290 return "";
291 else
292 return objeto.toString().trim().replaceAll("\"","'");
293 }
294
295
296
297
298
299
300
301
302
303
304
305 public static final String trocaNullMoeda(Object objeto){
306 if(objeto == null || "".equals(objeto.toString()))
307 return "";
308 else
309 return Util.formataMoeda(Double.valueOf(objeto.toString().trim()).doubleValue());
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325 public static final String trocaNullNumeroDecimalSemMilhar(Object objeto){
326 if(objeto == null || "".equals(objeto.toString()))
327 return "";
328 else {
329 String valor = Util.formataNumeroDecimalSemMilhar(Double.valueOf(objeto.toString().trim()).doubleValue());
330 return valor.replaceAll("\\.", ",");
331 }
332
333 }
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 public static final String trocaNullQtdValor(Object objeto, String indQtd){
354 if(objeto == null || "".equals(objeto.toString()))
355 return "";
356 else
357 return Util.formataQtdValor(Double.valueOf(objeto.toString().trim()).doubleValue(), indQtd);
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372 public static final String trocaNullNumeroDecimal(Object objeto){
373 if(objeto == null || "".equals(objeto.toString()))
374 return "";
375 else
376 return Util.formataNumeroDecimal(Double.valueOf(objeto.toString().trim()).doubleValue());
377 }
378
379
380
381
382
383
384
385
386
387
388
389
390
391 public static final String trocaNullNumeroSemDecimal(Object objeto){
392 if(objeto == null || "".equals(objeto.toString()))
393 return "";
394 else
395 return Util.formataNumeroSemDecimal(Double.valueOf(objeto.toString().trim()).doubleValue());
396 }
397
398
399
400
401
402
403
404
405
406
407
408 public static final String trocaNullData(Date data){
409 if(data == null)
410 return "";
411 else
412 return Data.parseDate(data);
413 }
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428 public static final String isChecked(Object objeto, String str){
429 String chk;
430 chk = "";
431 if (objeto != null && str.equalsIgnoreCase(objeto.toString()))
432 chk = "checked";
433
434 return chk;
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450 public static final String isChecked(Object objeto, List<Object> listValores){
451 String chk;
452 chk = "";
453 if (listValores==null||listValores.size()==0 )
454 return chk;
455 else if (listValores.contains(objeto))
456 chk = "checked";
457 return chk;
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474 public static final String isSelected (Object objeto, String str){
475 String select;
476 select = "";
477 if (objeto != null && str.equalsIgnoreCase(objeto.toString()))
478 select = "selected";
479
480 return select;
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496 public static final String isBoxChecked(Object objeto, String str){
497 String chk;
498 chk = "";
499 if (objeto != null) {
500 chk += " value=\"" + objeto.toString() + "\"";
501 if(str.equalsIgnoreCase(objeto.toString()))
502 chk += " checked=\"checked\" ";
503 }
504 return chk;
505 }
506
507
508
509
510
511
512
513
514
515
516
517 public static final String getZeroAEsquerda(String valor){
518 if (!"".equals(valor)){
519 int valorInt = (Integer.valueOf (valor)).intValue();
520
521 if (valorInt < 10)
522 valor = "0" + valor;
523 }
524
525 return valor;
526 }
527
528 }