View Javadoc

1   /*
2    * Criado em 08/12/2004
3    *
4    */
5   package ecar.taglib.util;
6   
7   import java.util.Collection;
8   import java.util.Iterator;
9   
10  import javax.servlet.jsp.JspException;
11  import javax.servlet.jsp.JspWriter;
12  import javax.servlet.jsp.PageContext;
13  import javax.servlet.jsp.tagext.Tag;
14  
15  import org.apache.log4j.Logger;
16  
17  import comum.util.Util;
18  
19  import ecar.dao.ConfiguracaoDao;
20  import ecar.pojo.AtributoLivre;
21  import ecar.pojo.ConfiguracaoCfg;
22  import ecar.pojo.SisAtributoSatb;
23  import ecar.pojo.SisGrupoAtributoSga;
24  
25  
26  /**
27   * Taglib que gera uma lista de Opções para um controle HTML.<br>
28   * 
29   * @author felipev
30   */
31  public class Options implements Tag {
32  
33      /**
34       * Collection com as opções a serem mostradas no Controle
35       */
36      private Collection options;
37  
38      /**
39       * Define o atributo dos objetos da Collection que será acessado para
40       * definir os Values das Opções
41       */
42      private String valor;
43  
44      /**
45       * Define se uma opção vazia deve ser incluída na lista
46       */
47      private boolean opcaoVazia;
48  
49      /**
50       * Define o atributo dos objetos da Collection que será acessado para
51       * definir o Label das Opções
52       */
53      private String label;
54      
55      /**
56       * Atributo que define se as opções do nível de planejamento é para ser checkbox (false = DEFAULT)
57       * Utilizado no cadastro de usuários
58       */
59      private Boolean nivelPlanejamentoCheckBox = new Boolean(false);
60  
61      private PageContext page = null;
62  
63      private Tag parent;
64      
65      private String imagem;
66      
67      private String dica;
68      
69      private String nome;
70      
71      private String contexto;
72  
73      public static final int COMBOBOX = 1;
74      public static final int CHECKBOX = 2;
75      public static final int LISTBOX = 3;
76      public static final int RADIO_BUTTON = 4;
77      public static final int TEXT = 5;
78      public static final int IMAGEM = 6;   
79      public static final int MULTITEXTO = 7;
80      public static final int VALIDACAO = 8;
81  
82      public static final long MAXLENGTH = 200;
83      private JspWriter writerParametro = null;
84      
85      /**
86       * @author N/C
87  	 * @since N/C
88  	 * @version N/C
89       */
90      public Options() {
91      	
92      }
93      
94      /**
95       * Atribui valor especificado para JspWriter writerParametro.<br>
96       * 
97       * @author N/C
98  	 * @since N/C
99  	 * @version N/C
100      * @param JspWriter writer
101      */
102     public Options(JspWriter writer) {
103     	this.writerParametro = writer;
104     }
105 
106     /**
107      * Inicializa a montagem da tag para ser adicionada na tela de HTML.<br>
108      * 
109      * @author N/C
110 	 * @since N/C
111 	 * @version N/C
112 	 * @return int
113 	 * @throws JspException
114      */
115     public int doStartTag() throws JspException {
116     	JspWriter writer = null;
117     	if(writerParametro != null) {
118     		writer = writerParametro;
119     	} else {
120         	writer = this.page.getOut();
121     	}
122         StringBuffer s = new StringBuffer();
123         Input tagInput = (Input) getParent();
124         try {
125             ConfiguracaoCfg configuracao = new ConfiguracaoDao(null).getConfiguracao();
126 
127             if (opcaoVazia) {
128                 if (tagInput.getTipo() == LISTBOX) {
129                     s.append("<option value=\"\"></option>");
130                 }
131             } else {
132                 /* comboBox sempre inicia com option vazia */
133                 if (tagInput.getTipo() == COMBOBOX) {
134                     s.append("<option value=\"\"></option>");
135                 }
136 
137             }
138 
139             Iterator it = options.iterator();
140             int countOptions = 0;
141 
142             while (it.hasNext()) {
143             	
144             	countOptions++;
145             	
146                 String sValue = "";
147                 String sLabel = "";
148                 /**
149                  * invoca o Método getXXXX para recuperar o valor do atributo
150                  * value *
151                  */
152                 Object obj = it.next();
153                 sValue = obj
154                         .getClass()
155                         .getMethod(
156                                 "get"
157                                         + Util
158                                                 .primeiraLetraToUpperCase(getValor()),
159                                 null).invoke(obj, null).toString();
160                 sLabel = obj
161                         .getClass()
162                         .getMethod(
163                                 "get"
164                                         + Util
165                                                 .primeiraLetraToUpperCase(getLabel()),
166                                 null).invoke(obj, null).toString();
167 
168                 int tipo = tagInput.getTipo(); 
169                 
170                 if(getNivelPlanejamentoCheckBox().booleanValue()
171                 		&& tagInput.getSisGrupoAtributoSga() != null
172                 		&& configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrNvPlan() != null 
173                 		&& tagInput.getSisGrupoAtributoSga().equals(configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrNvPlan())
174                 		&& tipo != CHECKBOX) {
175                 	tipo = CHECKBOX;
176                 }
177                 
178                 switch (tipo) {
179                 case COMBOBOX:
180                     addOpcaoComboBox(sValue, sLabel, obj, s, tipo, countOptions);
181                     break;
182                 case LISTBOX:
183                     addOpcaoComboBox(sValue, sLabel, obj, s, tipo, countOptions);
184                     break;
185                 case CHECKBOX:
186                     addOpcaoCheckBox(sValue, sLabel, obj, s, countOptions);
187                     break;
188                 case RADIO_BUTTON:
189                     addOpcaoRadioButton(sValue, sLabel, obj, s, countOptions);
190                     break;
191                 case MULTITEXTO:
192                     addOpcaoMultiTexto(sValue, sLabel, obj, s, countOptions);
193                     break;
194                 }
195 
196             }
197 
198             writer.print(s.toString());
199         } catch (Exception e) {
200         	Logger logger = Logger.getLogger(this.getClass());
201         	logger.error(e);
202         }
203         return Tag.EVAL_BODY_INCLUDE;
204     }
205 
206     /**
207      * Adiciona Opção comboBox.<br>
208      * 
209      * @author N/C
210 	 * @since N/C
211 	 * @version N/C
212      * @param String sValue
213      * @param String sLabel
214      * @param Object obj
215      * @param StringBuffer s
216      */
217     public void addOpcaoComboBox(String sValue, String sLabel, Object obj,
218             StringBuffer s, int tipo, int countOptions) {
219         Input tagInput = (Input) getParent();
220         s.append("<option value=\"").append(sValue).append("\"");
221         
222         if (tagInput.getSelecionados() != null)
223             if (tagInput.atributoSelecionado((SisAtributoSatb)obj))
224                 s.append(" selected ");
225         s.append(">");
226         s.append(sLabel);
227         s.append("</option>");
228         
229     }
230 
231     /**
232      * Adiciona opção CheckBox.<br>
233      * 
234      * @author N/C
235 	 * @since N/C
236 	 * @version N/C
237      * @param String sValue
238      * @param String sLabel
239      * @param Object obj
240      * @param StringBuffer s
241      */
242     public void addOpcaoCheckBox(String sValue, String sLabel, Object obj,
243             StringBuffer s, int countOptions) {
244         try{
245 	    	Input tagInput = (Input) getParent();
246 	        ConfiguracaoDao configuracaoDao = new ConfiguracaoDao(null);
247 	        SisGrupoAtributoSga sgaNvPl = configuracaoDao.getConfiguracao().getSisGrupoAtributoSgaByCodSgaGrAtrNvPlan();
248 	        
249 	        SisAtributoSatb satb = (SisAtributoSatb) obj;
250 	        if(sgaNvPl != null && satb.getSisGrupoAtributoSga().equals(sgaNvPl) ){
251 	        	s.append("<img src=\"").append(getImagem() + satb.getAtribInfCompSatb()).append("\">");	        	
252 	        }
253 	        s.append("<input type=\"checkbox\" class=\"form_check_radio\"  name=\"").append(tagInput.getNome())
254 	                .append("\" value=\"").append(sValue).append("\"");
255 	        if (tagInput.getSelecionados() != null)
256 	            if (tagInput.atributoSelecionado(satb))
257 	                s.append(" checked ");
258 	        if (tagInput.getDisabled() != null && "S".equals(tagInput.getDisabled()))    
259 	               s.append(" disabled "); 
260 	        s.append(">");	        
261 	        s.append(sLabel);
262 	        if (countOptions == 1){
263 	        	if( dica != null && !"".equals(dica) ){
264 	            	if (contexto != null){
265 	            		s.append(Util.getTagDica(nome, contexto, dica));
266 	            	}
267 	            }
268 	        }
269 	        s.append("<br>\n"); 
270         }catch(Exception e){
271         	Logger logger = Logger.getLogger(this.getClass());
272         	logger.error(e);	
273         }
274     }
275 
276     /**
277      * Adiciona opção multiTexto.<br>
278      * 
279      * @author N/C
280 	 * @since N/C
281 	 * @version N/C
282      * @param String sValue
283      * @param String sLabel
284      * @param Object obj
285      * @param StringBuffer s
286      */
287     public void addOpcaoMultiTexto(String sValue, String sLabel, Object obj,
288             StringBuffer s, int countOptions) {
289         Input tagInput = (Input) getParent();
290         s.append("\n<td class=\"").append(tagInput.getClassLabel()).append("\">");
291         s.append(sLabel);        
292         s.append("</td>\n<td><input type=\"text\"  name=\"").append(tagInput.getNome())
293         		.append("_").append(sValue).append("\"");  
294         s.append(" size = \"").append(tagInput.getSize()).append("\" maxlength = \"").append(MAXLENGTH).append("\" ");
295         if (tagInput.getSelecionados() != null && !tagInput.getSelecionados().isEmpty())
296         {
297         	Iterator iMulti = tagInput.getSelecionados().iterator();
298         	while (iMulti.hasNext())
299         	{
300         		AtributoLivre atribLivre = (AtributoLivre) iMulti.next();
301         		if(atribLivre.getSisAtributoSatb().equals(obj))
302         			s.append(" value=\"").append(atribLivre.getInformacao()).append("\"");
303         	}
304         }
305         if (tagInput.getDisabled() != null && "S".equals(tagInput.getDisabled()))    
306                s.append(" disabled "); 
307         s.append(">");
308         if (countOptions == 1){
309         	if( dica != null && !"".equals(dica) ){
310             	if (contexto != null){
311             		s.append(Util.getTagDica(nome, contexto, dica));
312             	}
313             }
314         }
315         s.append("</td></tr>");        
316         s.append("\n<tr>");		
317     }
318     
319     /**
320      * Adiciona opção RadioButton.<br>
321      * 
322      * @author N/C
323 	 * @since N/C
324 	 * @version N/C
325      * @param String sValue
326      * @param String sLabel
327      * @param Object obj
328      * @param StringBuffer s
329      */
330     public void addOpcaoRadioButton(String sValue, String sLabel, Object obj,
331             StringBuffer s, int countOptions) {
332         
333         try{
334         	Input tagInput = (Input) getParent();
335         	ConfiguracaoDao configuracaoDao = new ConfiguracaoDao(null);
336 	        SisGrupoAtributoSga sgaNvPl = configuracaoDao.getConfiguracao().getSisGrupoAtributoSgaByCodSgaGrAtrNvPlan();
337 	        
338 	        SisAtributoSatb satb = (SisAtributoSatb) obj;
339 	        if(sgaNvPl != null && satb.getSisGrupoAtributoSga().equals(sgaNvPl) ){
340 	        	s.append("<img src=\"").append(getImagem() + satb.getAtribInfCompSatb()).append("\">");
341 	        }
342 	        s.append("<input type=\"radio\" class=\"form_check_radio\" name=\"").append(tagInput.getNome())
343 	                .append("\" value=\"").append(sValue).append("\"");
344 	        if (tagInput.getSelecionados() != null)
345 	            if (tagInput.atributoSelecionado((SisAtributoSatb)obj))
346 	                s.append(" checked");
347 	        if (tagInput.getDisabled() != null &&  "S".equals(tagInput.getDisabled()))    
348 	            s.append(" disabled ");    
349 	        s.append(">");
350 	        s.append(sLabel);
351 	        if (countOptions == 1){
352 	        	s.append("&nbsp;<input type=\"button\" name=\"buttonLimpar\" value=\"Limpar\" class=\"botao\" ")
353                 .append("onclick=\"limparRadioButtons(document.getElementsByName('" + nome + "'));\"");
354 	        	s.append(">");
355 	        	if( dica != null && !"".equals(dica) ){
356 	            	if (contexto != null){
357 	            		s.append(Util.getTagDica(nome, contexto, dica));
358 	            	}
359 	            }
360 	        }
361 	        s.append("<br>\n");
362         }catch(Exception e){
363         	Logger logger = Logger.getLogger(this.getClass());
364         	logger.error(e);	
365         }
366     }
367 
368     /**
369      * Atribui valor especificado para PageContext page.<br>
370      * 
371      * @author N/C
372 	 * @since N/C
373 	 * @version N/C
374 	 * @param PageContext arg0
375      */
376     public void setPageContext(PageContext arg0) {
377         this.page = arg0;
378     }
379 
380     /**
381      * Atribui valor especificado para Tag parent.<br>
382      * 
383      * @author N/C
384 	 * @since N/C
385 	 * @version N/C
386 	 * @param Tag arg0
387      */
388     public void setParent(Tag arg0) {
389         parent = arg0;
390     }
391 
392     /**
393      * Retorna Tag parent.<br>
394      * 
395      * @author N/C
396 	 * @since N/C
397 	 * @version N/C
398 	 * @return Tag
399      */
400     public Tag getParent() {
401         return parent;
402     }
403 
404     /**
405      * Encerra Tag.<br>
406      * 
407      * @author N/C
408 	 * @since N/C
409 	 * @version N/C
410 	 * @return int
411 	 * @throws JspException
412      */
413     public int doEndTag() throws JspException {
414         return Tag.EVAL_PAGE;
415     }
416 
417     /**
418      * 
419      * 
420      * @author N/C
421 	 * @since N/C
422 	 * @version N/C
423      */
424     public void release() {
425         //this.selected = null;
426     }
427 
428     /**
429      * Retorna PageContext page.<br>
430      * 
431      * @author N/C
432 	 * @since N/C
433 	 * @version N/C
434      * @return PageContext - (Returns the page)
435      */
436     public PageContext getPage() {
437         return page;
438     }
439 
440     /**
441      * Atribui valor especificado para PageContext page.<br>
442      * 
443      * @author N/C
444 	 * @since N/C
445 	 * @version N/C
446      * @param PageContext page - (The page to set)
447      */
448     public void setPage(PageContext page) {
449         this.page = page;
450     }
451 
452     /**
453      * Retorna String label.<br>
454      * 
455      * @author N/C
456 	 * @since N/C
457 	 * @version N/C
458      * @return String - (Returns the label)
459      */
460     public String getLabel() {
461         return label;
462     }
463 
464     /**
465      * Atribui valor especificado para String label.<br>
466      * 
467      * @author N/C
468 	 * @since N/C
469 	 * @version N/C
470      * @param String label - (The label to set)
471      */
472     public void setLabel(String label) {
473         this.label = label;
474     }
475 
476     /**
477      * Retorna Collection options
478      * 
479      * @author N/C
480 	 * @since N/C
481 	 * @version N/C
482      * @return Collection - (Returns the options)
483      */
484     public Collection getOptions() {
485         return options;
486     }
487 
488     /**
489      * Atribui valor especificado para Collection options.<br>
490      * 
491      * @author N/C
492 	 * @since N/C
493 	 * @version N/C
494      * @param Collection options - (The options to set)
495      */
496     public void setOptions(Collection options) {
497         this.options = options;
498     }
499 
500     /**
501      * Retorna String valor.<br>
502      * 
503      * @author N/C
504 	 * @since N/C
505 	 * @version N/C
506      * @return String - (Returns the valor)
507      */
508     public String getValor() {
509         return valor;
510     }
511 
512     /**
513      * Atribui valor especificado para String valor.<br>
514      * 
515      * @author N/C
516 	 * @since N/C
517 	 * @version N/C
518      * @param String valor - (The valor to set)
519      */
520     public void setValor(String valor) {
521         this.valor = valor;
522     }
523 
524     /**
525      * Retorna boolean opcaoVazia.<br>
526      * 
527      * @author N/C
528 	 * @since N/C
529 	 * @version N/C
530      * @return boolean - (Returns the opcaoVazia)
531      */
532     public boolean isOpcaoVazia() {
533         return opcaoVazia;
534     }
535 
536     /**
537      * Atribui valor especificado para boolean opcaoVazia.<br>
538      * 
539      * @author N/C
540 	 * @since N/C
541 	 * @version N/C
542      * @param boolean opcaoVazia - (The opcaoVazia to set)
543      */
544     public void setOpcaoVazia(boolean opcaoVazia) {
545         this.opcaoVazia = opcaoVazia;
546     }
547 
548     /**
549      * Retorna Boolean nivelPlanejamentoCheckBox.<br>
550      * 
551      * @author N/C
552 	 * @since N/C
553 	 * @version N/C
554      * @return Boolean
555      */
556 	public Boolean getNivelPlanejamentoCheckBox() {
557 		return nivelPlanejamentoCheckBox;
558 	}
559 
560 	/**
561 	 * Atribui valor especificado para Boolean nivelPlanejamentoCheckBox.<br>
562 	 * 
563 	 * @author N/C
564 	 * @since N/C
565 	 * @version N/C
566 	 * @param Boolean nivelPlanejamentoCheckBox
567 	 */
568 	public void setNivelPlanejamentoCheckBox(Boolean nivelPlanejamentoCheckBox) {
569 		this.nivelPlanejamentoCheckBox = nivelPlanejamentoCheckBox;
570 	}
571 
572 	public String getImagem() {
573 		return imagem;
574 	}
575 
576 	public void setImagem(String imagem) {
577 		this.imagem = imagem;
578 	}
579 
580 	public String getDica() {
581 		return dica;
582 	}
583 
584 	public void setDica(String dica) {
585 		this.dica = dica;
586 	}
587 
588 	public String getNome() {
589 		return nome;
590 	}
591 
592 	public void setNome(String nome) {
593 		this.nome = nome;
594 	}
595 
596 	public String getContexto() {
597 		return contexto;
598 	}
599 
600 	public void setContexto(String contexto) {
601 		this.contexto = contexto;
602 	}
603 }