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 * @author rodrigo.hjort
11 */
12 public class Pagina {
13
14 public static final String SIM = "S";
15 public static final String NAO = "N";
16
17 /**
18 * <b>Exemplo:</b><br>
19 * int codEnsino = Pagina.getParam(request, "codEnsino");<br>
20 * int codModalidade = Pagina.getParam(request, "codModalidade");<br>
21 * int codFormaOrganiz = Pagina.getParam(request, "codFormaOrganizCurso");<b>
22 *
23 * @author N/C
24 * @since N/C
25 * @version N/C
26 * @param HttpServletRequest request - objeto "request" da página JSP
27 * @param String nomeParam - nome do parâmetro desejado
28 * @return int - valor do inteiro<br>
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 * <b>Exemplo:</b><br>
46 * int codEnsino = Pagina.getParam(request, "codEnsino");<br>
47 * int codModalidade = Pagina.getParam(request, "codModalidade");<br>
48 * int codFormaOrganiz = Pagina.getParam(request, "codFormaOrganizCurso");<br>
49 *
50 * @author N/C
51 * @since N/C
52 * @version N/C
53 * @param HttpServletRequest request - objeto "request" da página JSP
54 * @param String nomeParam - nome do parâmetro desejado
55 * @return long - valor do long
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 * <b>Exemplo:</b><br>
74 * int codEnsino = Pagina.getParam(request, "codEnsino");<br>
75 * int codModalidade = Pagina.getParam(request, "codModalidade");<br>
76 * int codFormaOrganiz = Pagina.getParam(request, "codFormaOrganizCurso");<br>
77 *
78 * @author N/C
79 * @since N/C
80 * @version N/C
81 * @param HttpServletRequest request - objeto "request" da página JSP
82 * @param String nomeParam - nome do parâmetro desejado
83 * @return Short - valor do inteiro
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 * Obtem dados de um campo especificado. Caso o retorno seja nulo retorna vazio.<br>
102 *
103 * @author N/C
104 * @since N/C
105 * @version N/C
106 * @param HttpServletRequest request - objeto "request" da página JSP
107 * @param String nomeParam - nome do parâmetro desejado
108 * @return String - valor do texto
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 * Obtem dados de um campo TEXTO especificado. Caso o retorno seja nulo retorna vazio.<br>
126 *
127 * @author N/C
128 * @since N/C
129 * @version N/C
130 * @param HttpServletRequest request - objeto "request" da página JSP
131 * @param String nomeParam - nome do parâmetro desejado
132 * @return String - valor do texto
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 * Obtem codigo de atributo de um campo tipo TEXTO especificado. Caso o retorno seja nulo retorna vazio.<br>
165 *
166 * @author N/C
167 * @since N/C
168 * @version N/C
169 * @param HttpServletRequest request - objeto "request" da página JSP
170 * @param String nomeParam - nome do parâmetro desejado
171 * @return String - valor do texto
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 * Retorna a string com os "\" trocados por "'".<br>
192 * Se a string for "" retorna nulo.<br>
193 *
194 * @author N/C
195 * @since N/C
196 * @version N/C
197 * @param HttpServletRequest request
198 * @param String param
199 * @return String
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 * <b>Exemplo:</b>
215 * int codEnsino = Pagina.getParam(request, "codEnsino");<br>
216 * int codModalidade = Pagina.getParam(request, "codModalidade");<br>
217 * int codFormaOrganiz = Pagina.getParam(request, "codFormaOrganizCurso");<br>
218 *
219 * @author N/C
220 * @since N/C
221 * @version N/C
222 * @param HttpServletRequest request - objeto "request" da página JSP
223 * @param String nomeParam - nome do parâmetro desejado
224 * @return Date
225 */
226 public static final Date getParamDataBanco(HttpServletRequest request, String nomeParam) {
227
228 String valor = request.getParameter(nomeParam);//.toString();
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 * <b>Exemplo:</b><br>
241 * boolean docCertNasc = Pagina.getParamBool(request, "docCertNasc");<br>
242 * boolean docHistorico = Pagina.getParamBool(request, "docHistorico");<br>
243 *
244 * @author N/C
245 * @since N/C
246 * @version N/C
247 * @param HttpServletRequest request - objeto "request" da página JSP
248 * @param String nomeParam - nome do parâmetro desejado
249 * @return boolean
250 */
251 public static final boolean getParamBool(HttpServletRequest request, String nomeParam) {
252 return("true".equals(getParamStr(request, nomeParam)));
253 }
254
255 /**
256 * @author Robson André da Costa
257 * @param request
258 * @param nomeParam
259 * @param strDefault
260 * @return
261 * @since 16/10/2007
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 * Se objeto for nulo retorna uma String em branco, do contrario troca todos os "\" da string por "'".<br>
281 *
282 * @author N/C
283 * @since N/C
284 * @version N/C
285 * @param Object objeto - get do objeto, String ou Número
286 * @return String - se nulo retorna uma String em branco
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 * Retorna String em formatacao monetaria.<br>
297 * Se objeto for nulo ou "" retorna uma string em branco.<br>
298 *
299 * @author N/C
300 * @since N/C
301 * @version N/C
302 * @param Object objeto - get do objeto, String ou Número
303 * @return String - se nulo retorna uma String em branco
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 * Retorna um número com formatação das casas decimais (sem unidades de milhar).<br>
315 * Se objeto for nulo ou "", retorna uma string em branco.<br>
316 * Ex.:<br>
317 * "#0.00"
318 *
319 * @author N/C
320 * @since N/C
321 * @version N/C
322 * @param Object objeto - get do objeto, String ou Número
323 * @return String - se nulo retorna uma String em branco
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 * Se o item informado for quantidade, retorna um número sem formatação nenhuma<br>
339 * senão se for valor ele retorna o numero formatado com casas decimais.
340 * Se objeto for nulo ou "", retorna uma string em branco.<br>
341 * Ex. qtd:<br>
342 * "#000"
343 * Ex. valor:<br>
344 * "#0.00"
345 *
346 * @author luanaoliveira
347 * @since N/C
348 * @version N/C
349 * @param Object objeto - get do objeto, String ou Número
350 * @param String indQtd - indicador se é qtd(Q) ou valor(V)
351 * @return String - se nulo retorna uma String em branco
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 * Formata o numero com casas decimais.<br>
362 * Se objeto for nulo ou "" retorna uma string em branco.<br>
363 * ex.:<br>
364 * "###,###,##0.##"
365 *
366 * @author N/C
367 * @since N/C
368 * @version N/C
369 * @param Object objeto - get do objeto, String ou Número
370 * @return String - se nulo retorna uma String em branco
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 * Retorna numero sem casas decimais.<br>
381 * Se objeto for nulo ou "" retorna string em branco.<br>
382 * Ex.:<br>
383 * "###,###,##0"
384 *
385 * @author N/C
386 * @since N/C
387 * @version N/C
388 * @param Object objeto - get do objeto, String ou Número
389 * @return String - se nulo retorna uma String em branco
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 * Converte um Date para uma string.
400 * Se objeto for nulo retorna uma string em branco.<br>
401 *
402 * @author N/C
403 * @since N/C
404 * @version N/C
405 * @param Date data - get de uma data
406 * @return String - se nulo retorna uma String em branco
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 * <b>Exemplo:</b><br>
417 * Utilizado para radiobutton comparando o valor do objeto a uma determinada String:<br>
418 * Ativo Sim ou Não<br>
419 * "S" ou "N" == getIndAtivo()<br>
420 *
421 * @author N/C
422 * @since N/C
423 * @version N/C
424 * @param Object objeto - get do objeto a ser comparado
425 * @param String str - valor para ser comparado
426 * @return String - checked - se valores iguais
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 * <b>Exemplo:</b><br>
439 * Utilizado para checkbox comparando o valor do objeto a uma determinada Lista de Valores:<br>
440 * Ativo Sim ou Não<br>
441 *
442 * @author N/C
443 * @since N/C
444 * @version N/C
445 * @param Object objeto - get do objeto a ser comparado
446 * @param String str - valor para ser comparado
447 * @return String - checked - se valores iguais
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 * <b>Exemplo:</b><br>
464 * Utilizado para select comparando o valor do objeto a uma determinada String:<br>
465
466 *
467 * @author N/C
468 * @since N/C
469 * @version N/C
470 * @param Object objeto - get do objeto a ser comparado
471 * @param String str - valor para ser comparado
472 * @return String - select - se valores iguais
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 * <b>Exemplo:</b><br>
485 * Utilizado para checkbox comparando o valor do objeto a uma determinada String:<br>
486 * Ativo Sim ou Não<br>
487 * "S" ou "N" == getIndAtivo()<br>
488 *
489 * @author N/C
490 * @since N/C
491 * @version N/C
492 * @param Object objeto - get do objeto a ser comparado
493 * @param String str - valor para ser comparado
494 * @return String - value e checked(se valores iguais)
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 * Metodo para retornar valor < 10 com Zero À Esquerda, utilizado para mostrar horas e minutos<br>
509 * Ex.: 07 <br>
510 *
511 * @author N/C
512 * @since N/C
513 * @version N/C
514 * @param String valor
515 * @return String
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 }