1
2
3
4
5 package ecar.taglib.util;
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 public class CheckListTag implements Tag{
34
35
36
37
38 private String nome;
39
40
41
42 private String objeto;
43
44
45
46 private Collection selected;
47
48
49
50 private String label;
51
52
53
54 private String value;
55
56
57
58 private String order;
59
60
61
62
63 private String filters;
64
65
66
67
68 private String scripts;
69
70
71
72
73
74 private Collection colecao;
75
76
77
78
79 private Collection objetosExcluidos;
80
81 private PageContext page = null;
82
83
84
85
86
87
88
89
90
91
92 public int doStartTag() throws JspException {
93 JspWriter writer = this.page.getOut();
94 StringBuffer s = new StringBuffer();
95 try {
96
97 List itensLista = new ArrayList();
98
99
100 Object obj = Class.forName(getObjeto()).newInstance();
101 Dao dao = new Dao();
102 Session session = dao.getSession();
103
104 if (getColecao() == null){
105
106 Criteria select = session.createCriteria( Class.forName(getObjeto()) );
107
108
109 if(filters != null && !"".equals(filters)){
110 String parametros[] = filters.split(";");
111 for(int i = 0; i < parametros.length; i++){
112 String[] chaveValor = parametros[i].split("=");
113 select.add(Expression.like(chaveValor[0], chaveValor[1]));
114 }
115 }
116
117
118 select.addOrder(Order.asc(order));
119 itensLista = select.list();
120 }else{
121 itensLista.addAll(getColecao());
122 }
123
124
125 Iterator it = itensLista.iterator();
126
127 while(it.hasNext()){
128 obj = it.next();
129 if( (objetosExcluidos == null) || ( objetosExcluidos != null && !objetosExcluidos.contains(obj) ) ){
130 s.append("<input type=\"checkbox\" class=\"form_check_radio\" name=\"").append(getNome()).append("\" value=\"");
131
132 Object value = Class.forName(getObjeto()).getMethod("get"+ Util.primeiraLetraToUpperCase(getValue()), null).invoke(obj, null);
133 s.append(value);
134 s.append("\"");
135 if(getSelected()!= null){
136 Iterator itValue = getSelected().iterator();
137 while(itValue.hasNext()){
138 if( (itValue.next().toString()).equals(value.toString())){
139 s.append(" checked ");
140 }
141 }
142 }
143 if(scripts!= null && !"".equals(scripts))
144 s.append(" ").append(scripts);
145 s.append(">");
146 if (!this.isMultiLabel())
147 {
148
149 s.append(Class.forName(getObjeto()).getMethod("get"+ Util.primeiraLetraToUpperCase(getLabel()), null).invoke(obj, null));
150 }
151 else
152 {
153 Iterator itLabels = this.getLabels().iterator();
154 while (itLabels.hasNext())
155 {
156 s.append(Class.forName(getObjeto()).getMethod("get"+ Util.primeiraLetraToUpperCase((String)itLabels.next()), null).invoke(obj, null));
157 s.append(" - ");
158 }
159 s.deleteCharAt(s.length()-2);
160 }
161 s.append("<br>");
162 }
163 }
164 writer.print(s.toString());
165 } catch (Exception e) {
166 Logger logger = Logger.getLogger(this.getClass());
167 logger.error(e);
168 try{
169 writer.print("Erro na geração da CheckList: " + e.getMessage());
170 logger.error(e);
171 }catch(IOException ioE){
172 logger.error(ioE);
173 }
174 }
175 return Tag.EVAL_BODY_INCLUDE;
176 }
177
178
179
180
181
182
183
184
185
186 public List getLabels()
187 {
188 String labelAux = label;
189 List labels = new ArrayList();
190 while(labelAux.lastIndexOf(",") != -1){
191 labels.add(labelAux.substring(0, labelAux.indexOf(",")));
192 labelAux = labelAux.substring(labelAux.indexOf(",") + 1);
193 }
194 labels.add(labelAux);
195
196 return labels;
197 }
198
199
200
201
202
203
204
205
206
207 public boolean isMultiLabel()
208 {
209 if (this.getLabel().lastIndexOf(",") == -1)
210 return false;
211 return true;
212 }
213
214
215
216
217
218
219
220
221
222 public void setPageContext(PageContext arg0) {
223 this.page = arg0;
224 }
225
226
227
228
229
230
231
232
233
234 public void setParent(Tag arg0) {
235 }
236
237
238
239
240
241
242
243
244
245 public Tag getParent() {
246 return null;
247 }
248
249
250
251
252
253
254
255
256
257
258 public int doEndTag() throws JspException {
259 return Tag.EVAL_PAGE;
260 }
261
262
263
264
265
266
267
268
269 public void release() {
270
271 }
272
273
274
275
276
277
278
279
280
281 public PageContext getPage() {
282 return page;
283 }
284
285
286
287
288
289
290
291
292 public void setPage(PageContext page) {
293 this.page = page;
294 }
295
296
297
298
299
300
301
302
303 public String getFilters() {
304 return filters;
305 }
306
307
308
309
310
311
312
313
314
315 public void setFilters(String filters) {
316 this.filters = filters;
317 }
318
319
320
321
322
323
324
325
326
327 public String getLabel() {
328 return label;
329 }
330
331
332
333
334
335
336
337
338
339 public void setLabel(String label) {
340 this.label = label;
341 }
342
343
344
345
346
347
348
349
350
351 public String getNome() {
352 return nome;
353 }
354
355
356
357
358
359
360
361
362
363 public void setNome(String nome) {
364 this.nome = nome;
365 }
366
367
368
369
370
371
372
373
374
375 public String getObjeto() {
376 return objeto;
377 }
378
379
380
381
382
383
384
385
386
387 public void setObjeto(String objeto) {
388 this.objeto = objeto;
389 }
390
391
392
393
394
395
396
397
398
399 public String getOrder() {
400 return order;
401 }
402
403
404
405
406
407
408
409
410
411 public void setOrder(String order) {
412 this.order = order;
413 }
414
415
416
417
418
419
420
421
422
423 public String getValue() {
424 return value;
425 }
426
427
428
429
430
431
432
433
434
435 public void setValue(String value) {
436 this.value = value;
437 }
438
439
440
441
442
443
444
445
446
447 public Collection getSelected() {
448 return selected;
449 }
450
451
452
453
454
455
456
457
458
459 public void setSelected(Collection selected) {
460 this.selected = selected;
461 }
462
463
464
465
466
467
468
469
470
471 public String getScripts() {
472 return scripts;
473 }
474
475
476
477
478
479
480
481
482
483 public void setScripts(String scripts) {
484 this.scripts = scripts;
485 }
486
487
488
489
490
491
492
493
494
495 public Collection getObjetosExcluidos() {
496 return objetosExcluidos;
497 }
498
499
500
501
502
503
504
505
506
507 public void setObjetosExcluidos(Collection objetosExcluidos) {
508 this.objetosExcluidos = objetosExcluidos;
509 }
510
511
512
513
514
515
516
517
518
519 public Collection getColecao() {
520 return colecao;
521 }
522
523
524
525
526
527
528
529
530
531 public void setColecao(Collection colecao) {
532 this.colecao = colecao;
533 }
534 }