1
2
3
4
5 package ecar.taglib.combos;
6
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import javax.servlet.jsp.JspException;
14 import javax.servlet.jsp.JspWriter;
15 import javax.servlet.jsp.PageContext;
16 import javax.servlet.jsp.tagext.Tag;
17
18 import org.apache.log4j.Logger;
19 import org.hibernate.Criteria;
20 import org.hibernate.Session;
21 import org.hibernate.criterion.Expression;
22 import org.hibernate.criterion.Order;
23
24 import comum.database.Dao;
25 import comum.util.Util;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ComboTag implements Tag {
44
45
46
47
48 private String selected;
49
50
51
52
53 private String nome;
54
55
56
57
58
59
60 private String objeto;
61
62
63
64
65 private String label;
66
67
68
69
70 private String value;
71
72
73
74
75
76 private String order;
77
78
79
80
81
82
83
84 private String filters;
85
86
87
88
89
90 private String scripts;
91
92
93
94
95 private String style;
96
97
98
99
100
101
102 private Collection colecao;
103
104
105
106
107 private Collection objetosExcluidos;
108
109
110
111
112
113
114 private String option;
115
116
117
118
119 private String textoPadrao;
120
121 private PageContext page = null;
122
123 private Logger logger = null;
124
125 private Boolean ignorarTagSelect = new Boolean(false);
126
127 private JspWriter writerParametro = null;
128
129 public ComboTag() {
130 super();
131 this.logger = Logger.getLogger(this.getClass());
132 }
133
134 public ComboTag(JspWriter writer) {
135 this.writerParametro = writer;
136 }
137
138
139
140
141
142
143
144
145
146
147 public int doStartTag() throws JspException {
148 JspWriter writer = null;
149 if(writerParametro != null) {
150 writer = writerParametro;
151 } else {
152 writer = this.page.getOut();
153 }
154
155 StringBuffer s = new StringBuffer();
156 try {
157
158 Object obj = Class.forName(getObjeto()).newInstance();
159 Dao dao = new Dao();
160 Session session = dao.getSession();
161
162 List lista = new ArrayList();
163
164 if (getColecao() != null) {
165
166 lista.addAll(getColecao());
167
168 } else {
169
170
171 Criteria select = session.createCriteria(Class
172 .forName(getObjeto()));
173
174
175 if (filters != null && !"".equals(filters)) {
176 String parametros[] = filters.split(";");
177 for (int i = 0; i < parametros.length; i++) {
178 String[] chaveValor = parametros[i].split("=");
179 select.add(Expression
180 .like(chaveValor[0], chaveValor[1]));
181 }
182 }
183
184
185 select.addOrder(Order.asc(order));
186 lista = select.list();
187 }
188
189
190 Iterator it = lista.iterator();
191
192 if(!getIgnorarTagSelect().booleanValue()) {
193
194 s.append("<select name=\""+nome+"\" id=\""+nome+"\" ");
195
196 if(scripts != null && !"".equals(scripts))
197 s.append( scripts );
198
199 if (style != null && !"".equals(style))
200 s.append(" style=\"").append(style).append("\"");
201 s.append(">");
202 }
203
204 if (getOption() == null){
205 if (getTextoPadrao() != null)
206 s.append("<option value=\"\">").append(getTextoPadrao()).append("</option>");
207 else
208 s.append("<option value=\"\"></option>");
209 }
210
211 while (it.hasNext()) {
212 obj = it.next();
213 if ((objetosExcluidos == null)
214 || (objetosExcluidos != null && !objetosExcluidos
215 .contains(obj))) {
216 s.append("<option value=\"");
217
218
219
220
221 Object value = Class.forName(getObjeto()).getMethod(
222 "get" + Util.primeiraLetraToUpperCase(getValue()),
223 null).invoke(obj, null);
224 s.append(value);
225 s.append("\"");
226 if ((value.toString()).equals(selected))
227 s.append(" selected ");
228 s.append(">");
229
230
231
232
233 if (!this.isMultiLabel())
234 {
235 s.append(Class.forName(getObjeto()).getMethod(
236 "get" + Util.primeiraLetraToUpperCase(getLabel()),
237 null).invoke(obj, null));
238 }
239 else
240 {
241 Iterator itLabels = this.getLabels().iterator();
242 while (itLabels.hasNext())
243 {
244 s.append(Class.forName(getObjeto()).getMethod(
245 "get" + Util.primeiraLetraToUpperCase((String)itLabels.next()),
246 null).invoke(obj, null));
247 s.append(" - ");
248 }
249 s.deleteCharAt(s.length()-2);
250 }
251 s.append("</option>");
252 }
253 }
254 if(!getIgnorarTagSelect().booleanValue()) {
255 s.append("</select>");
256 }
257 writer.print(s.toString());
258 } catch (Exception e) {
259 this.logger.error(e);
260 try {
261 writer.print("Erro na geração da Combo: " + e.getMessage());
262 this.logger.error(e);
263 } catch (IOException ioE) {
264 this.logger.error(ioE);
265 }
266
267 }
268 return Tag.EVAL_BODY_INCLUDE;
269 }
270
271
272
273
274
275
276
277
278
279 public void setPageContext(PageContext arg0) {
280 this.page = arg0;
281 }
282
283
284
285
286
287
288
289
290
291 public void setParent(Tag arg0) {
292 }
293
294
295
296
297
298
299
300
301
302 public Tag getParent() {
303 return null;
304 }
305
306
307
308
309
310
311
312
313
314 public String getScripts() {
315 return scripts;
316 }
317
318
319
320
321
322
323
324
325
326 public void setScripts(String scripts) {
327 this.scripts = scripts;
328 }
329
330
331
332
333
334
335
336
337
338 public String getValue() {
339 return value;
340 }
341
342
343
344
345
346
347
348
349
350 public void setValue(String value) {
351 this.value = value;
352 }
353
354
355
356
357
358
359
360
361
362
363 public int doEndTag() throws JspException {
364 return Tag.EVAL_PAGE;
365 }
366
367
368
369
370
371
372
373
374 public void release() {
375 this.selected = null;
376 }
377
378
379
380
381
382
383
384
385
386 public String getSelected() {
387 return selected;
388 }
389
390
391
392
393
394
395
396
397
398 public void setSelected(String string) {
399 selected = string;
400 }
401
402
403
404
405
406
407
408
409
410 public String getObjeto() {
411 return objeto;
412 }
413
414
415
416
417
418
419
420
421
422 public void setObjeto(String objeto) {
423 this.objeto = objeto;
424 }
425
426
427
428
429
430
431
432
433
434 public PageContext getPage() {
435 return page;
436 }
437
438
439
440
441
442
443
444
445
446 public void setPage(PageContext page) {
447 this.page = page;
448 }
449
450
451
452
453
454
455
456
457
458 public String getFilters() {
459 return filters;
460 }
461
462
463
464
465
466
467
468
469
470 public void setFilters(String filters) {
471 this.filters = filters;
472 }
473
474
475
476
477
478
479
480
481
482 public String getLabel() {
483 return label;
484 }
485
486
487
488
489
490
491
492
493
494 public void setLabel(String label) {
495 this.label = label;
496 }
497
498
499
500
501
502
503
504
505
506 public List getLabels()
507 {
508 String labelAux = this.label;
509 List labels = new ArrayList();
510 while(labelAux.lastIndexOf(",") != -1){
511 labels.add(labelAux.substring(0, labelAux.indexOf(",")));
512 labelAux = labelAux.substring(labelAux.indexOf(",") + 1);
513 }
514 labels.add(labelAux);
515
516 return labels;
517 }
518
519
520
521
522
523
524
525
526
527 public boolean isMultiLabel()
528 {
529 if (this.getLabel().lastIndexOf(",") == -1)
530 return false;
531 return true;
532 }
533
534
535
536
537
538
539
540
541
542 public String getOrder() {
543 return order;
544 }
545
546
547
548
549
550
551
552
553
554 public void setOrder(String order) {
555 this.order = order;
556 }
557
558
559
560
561
562
563
564
565
566 public String getNome() {
567 return nome;
568 }
569
570
571
572
573
574
575
576
577
578 public void setNome(String nome) {
579 this.nome = nome;
580 }
581
582
583
584
585
586
587
588
589
590 public String getStyle() {
591 return style;
592 }
593
594
595
596
597
598
599
600
601
602 public void setStyle(String style) {
603 this.style = style;
604 }
605
606
607
608
609
610
611
612
613
614 public Collection getColecao() {
615 return colecao;
616 }
617
618
619
620
621
622
623
624
625
626 public void setColecao(Collection colecao) {
627 this.colecao = colecao;
628 }
629
630
631
632
633
634
635
636
637
638 public Collection getObjetosExcluidos() {
639 return objetosExcluidos;
640 }
641
642
643
644
645
646
647
648
649
650 public void setObjetosExcluidos(Collection objetosExcluidos) {
651 this.objetosExcluidos = objetosExcluidos;
652 }
653
654
655
656
657
658
659
660
661
662 public String getOption() {
663 return option;
664 }
665
666
667
668
669
670
671
672
673 public void setOption(String option) {
674 this.option = option;
675 }
676
677
678
679
680
681
682
683
684 public String getTextoPadrao() {
685 return textoPadrao;
686 }
687
688
689
690
691
692
693
694
695 public void setTextoPadrao(String textoPadrao) {
696 this.textoPadrao = textoPadrao;
697 }
698
699
700
701
702
703
704
705
706
707 public Boolean getIgnorarTagSelect() {
708 return ignorarTagSelect;
709 }
710
711
712
713
714
715
716
717
718
719 public void setIgnorarTagSelect(Boolean ignorarTagSelect) {
720 this.ignorarTagSelect = ignorarTagSelect;
721 }
722 }