1
2
3
4 package ecar.dao;
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15
16 import javax.servlet.http.HttpServletRequest;
17
18 import org.hibernate.HibernateException;
19 import org.hibernate.Query;
20 import org.hibernate.Transaction;
21
22 import comum.database.Dao;
23 import comum.util.Data;
24 import comum.util.Pagina;
25 import comum.util.Util;
26
27 import ecar.bean.IettArfBean;
28 import ecar.exception.ECARException;
29 import ecar.login.SegurancaECAR;
30 import ecar.pojo.AcompRealFisicoArf;
31 import ecar.pojo.AcompReferenciaAref;
32 import ecar.pojo.AcompReferenciaItemAri;
33 import ecar.pojo.ConfiguracaoCfg;
34 import ecar.pojo.ExercicioExe;
35 import ecar.pojo.ItemEstrtIndResulIettr;
36 import ecar.pojo.ItemEstruturaIett;
37 import ecar.pojo.SisAtributoSatb;
38 import ecar.pojo.SituacaoSit;
39 import ecar.pojo.TipoAcompanhamentoTa;
40 import ecar.util.Dominios;
41
42
43
44
45 public class AcompRealFisicoDao extends Dao {
46
47
48
49 public AcompRealFisicoDao(HttpServletRequest request) {
50 super();
51 this.request = request;
52 }
53
54
55
56
57
58
59
60 public AcompRealFisicoArf buscar(Long codArf) throws ECARException {
61 return (AcompRealFisicoArf) super.buscar(AcompRealFisicoArf.class, codArf);
62 }
63
64
65
66
67
68
69
70
71
72
73 public AcompRealFisicoArf buscarPorIettir(Long mesArf, Long anoArf, Long codIettir) throws ECARException {
74 try {
75 StringBuilder query = new StringBuilder("select ARF from AcompRealFisicoArf as ARF").append(" where ARF.itemEstrtIndResulIettr.codIettir = :codIettir ").append(" and ARF.mesArf = :mesArf ").append(" and ARF.anoArf = :anoArf");
76
77 Query q = this.getSession().createQuery(query.toString());
78
79 q.setLong("codIettir", codIettir.longValue());
80 q.setLong("mesArf", mesArf.longValue());
81 q.setLong("anoArf", anoArf.longValue());
82 q.setMaxResults(1);
83
84 Object arf = q.uniqueResult();
85
86 if (arf != null) {
87 return (AcompRealFisicoArf) arf;
88 }
89 else {
90 return null;
91 }
92 } catch (Exception e) {
93 this.logger.error(e);
94 throw new ECARException(e);
95 }
96 }
97
98
99
100
101
102
103
104
105
106 public List buscarPorIett(Long codIett, Long mesArf, Long anoArf) throws ECARException {
107 try {
108 StringBuilder query = new StringBuilder("select ARF from AcompRealFisicoArf as ARF").append(" where ARF.itemEstruturaIett.codIett = :codIett").append(" and ARF.itemEstruturaIett.indAtivoIett = :ativo").append(" and ARF.mesArf = :mesArf").append(" and ARF.anoArf = :anoArf");
109
110 Query q = this.getSession().createQuery(query.toString());
111
112 q.setLong("codIett", codIett.longValue());
113 q.setLong("mesArf", mesArf.longValue());
114 q.setLong("anoArf", anoArf.longValue());
115 q.setString("ativo", "S");
116
117 return q.list();
118 } catch (Exception e) {
119 this.logger.error(e);
120 throw new ECARException(e);
121 }
122 }
123
124
125
126
127
128
129
130
131
132
133 public double getQuantidadeRealizada(AcompReferenciaItemAri acompReferenciaItem, ItemEstrtIndResulIettr itemEstrtIndResul, String radAcumulados) throws ECARException, HibernateException {
134 StringBuilder query = new StringBuilder("select sum(acompRealFisico.qtdRealizadaArf) from AcompRealFisicoArf as acompRealFisico");
135 query.append(" where acompRealFisico.itemEstrtIndResulIettr.codIettir = :codIettir");
136
137 if ("P".equalsIgnoreCase(radAcumulados)) {
138 query.append(" and (acompRealFisico.anoArf < :anoArf").append(" or (acompRealFisico.mesArf <= :mesArf").append(" and acompRealFisico.anoArf = :anoArf))");
139 }
140
141 Query q = this.getSession().createQuery(query.toString());
142
143 q.setLong("codIettir", itemEstrtIndResul.getCodIettir().longValue());
144
145 if ("P".equalsIgnoreCase(radAcumulados)) {
146 q.setLong("anoArf", Long.valueOf(acompReferenciaItem.getAcompReferenciaAref().getAnoAref()).longValue());
147 q.setLong("mesArf", Long.valueOf(acompReferenciaItem.getAcompReferenciaAref().getMesAref()).longValue());
148 }
149 q.setMaxResults(1);
150
151 Double total = (Double) q.uniqueResult();
152 if (total != null)
153 return total.doubleValue();
154 else
155 return 0;
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 public double getQuantidadeRealizadaNaoAcumulados(AcompReferenciaItemAri ari, ItemEstrtIndResulIettr iettr, String acumulados, String porExercicioOuTotal) throws ECARException {
270 try {
271
272 if ("N".equals(iettr.getIndValorFinalIettr())) {
273 return 0;
274 }
275
276
277 Set setRealizadosFisicos = iettr.getAcompRealFisicoArfs();
278
279 if (setRealizadosFisicos != null) {
280 Iterator it = setRealizadosFisicos.iterator();
281
282
283
284
285 if ("T".equals(porExercicioOuTotal)) {
286
287
288
289 if ("P".equals(acumulados)) {
290
291
292 long anoMesAcompanhamento = Long.parseLong(ari.getAcompReferenciaAref().getAnoAref() + ari.getAcompReferenciaAref().getMesAref());
293
294 Set setRealizadosFisicosTotalAtePeriodo = new HashSet();
295 it = setRealizadosFisicos.iterator();
296 while (it.hasNext()) {
297 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
298 String anoMes = String.valueOf(arf.getAnoArf());
299
300 if (arf.getMesArf() < 10) {
301 anoMes += "0";
302 }
303 anoMes += String.valueOf(arf.getMesArf());
304
305 if (Long.parseLong(anoMes) <= anoMesAcompanhamento) {
306 setRealizadosFisicosTotalAtePeriodo.add(arf);
307 }
308 }
309 it = setRealizadosFisicosTotalAtePeriodo.iterator();
310 }
311
312 }
313 else if ("E".equals(porExercicioOuTotal)) {
314
315 List mesesAnos = null;
316
317
318
319 if ("P".equals(acumulados)) {
320 mesesAnos = new ExercicioDao(request).getMesesDentroDoExercicio(ari.getAcompReferenciaAref().getExercicioExe(), Integer.valueOf(ari.getAcompReferenciaAref().getMesAref()).intValue(), Integer.valueOf(ari.getAcompReferenciaAref().getAnoAref()).intValue());
321 }
322 else {
323 mesesAnos = new ExercicioDao(request).getMesesDentroDoExercicio(ari.getAcompReferenciaAref().getExercicioExe());
324 }
325
326 if (mesesAnos != null && !mesesAnos.isEmpty()) {
327 List anoMesExercicio = new ArrayList();
328
329 Iterator itMeses = mesesAnos.iterator();
330 while (itMeses.hasNext()) {
331 String mesAno = (String) itMeses.next();
332
333 String mes = mesAno.split("-")[0];
334 String ano = mesAno.split("-")[1];
335
336 if (Integer.valueOf(mes).intValue() < 10) {
337 mes = "0" + mes;
338 }
339 anoMesExercicio.add(ano + mes);
340 }
341
342 Collections.sort(anoMesExercicio, new Comparator() {
343 public int compare(Object o1, Object o2) {
344 Integer num1 = Integer.valueOf(o1.toString());
345 Integer num2 = Integer.valueOf(o2.toString());
346
347 return num1.compareTo(num2);
348 }
349 });
350
351 long periodoInicial = Long.parseLong(anoMesExercicio.get(0).toString());
352 long periodoFinal = Long.parseLong(anoMesExercicio.get((anoMesExercicio.size() - 1)).toString());
353
354
355 Set setRealizadosFisicosPorExercicio = new HashSet();
356 it = setRealizadosFisicos.iterator();
357 while (it.hasNext()) {
358 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
359 String anoMes = String.valueOf(arf.getAnoArf());
360
361 if (arf.getMesArf() < 10) {
362 anoMes += "0";
363 }
364 anoMes += String.valueOf(arf.getMesArf());
365
366 if (Long.parseLong(anoMes) >= periodoInicial && Long.parseLong(anoMes) <= periodoFinal) {
367 setRealizadosFisicosPorExercicio.add(arf);
368 }
369 }
370 it = setRealizadosFisicosPorExercicio.iterator();
371 }
372 else {
373 throw new ECARException("Não existe exercício cadastrado para o período do acompanhamento");
374 }
375
376 }
377
378
379
380 if ("M".equals(iettr.getIndValorFinalIettr())) {
381 double maior = 0;
382 while (it.hasNext()) {
383 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
384 if (arf.getQtdRealizadaArf() != null && arf.getQtdRealizadaArf().doubleValue() > maior) {
385 maior = arf.getQtdRealizadaArf().doubleValue();
386 }
387 }
388 return maior;
389 }
390 else if ("U".equals(iettr.getIndValorFinalIettr())) {
391 long maiorAnoMes = 0;
392 double ultimo = 0;
393
394 while (it.hasNext()) {
395 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
396 String anoMes = String.valueOf(arf.getAnoArf());
397
398 if (arf.getMesArf() < 10) {
399 anoMes += "0";
400 }
401 anoMes += String.valueOf(arf.getMesArf());
402
403 if (Long.parseLong(anoMes) > maiorAnoMes) {
404 maiorAnoMes = Long.parseLong(anoMes);
405 if (arf.getQtdRealizadaArf() != null) {
406 ultimo = arf.getQtdRealizadaArf().doubleValue();
407 }
408 else {
409 ultimo = 0;
410 }
411 }
412 }
413 return ultimo;
414 }
415
416 }
417
418 return 0;
419 } catch (Exception e) {
420 this.logger.error(e);
421 throw new ECARException(e);
422 }
423 }
424
425
426
427
428
429
430
431 public void setAcompRealFisico(HttpServletRequest request, AcompRealFisicoArf acompRealFisicoArf) throws ECARException {
432
433 int countSelecionado = Pagina.getParamInt(request, "hidContSelecionado");
434
435
436
437 if (countSelecionado > 0) {
438
439 if (acompRealFisicoArf.getItemEstrtIndResulIettr() != null && (acompRealFisicoArf.getItemEstrtIndResulIettr().getIndTipoAtualizacaoRealizado() == null) || (acompRealFisicoArf.getItemEstrtIndResulIettr().getIndTipoAtualizacaoRealizado() != null && !acompRealFisicoArf.getItemEstrtIndResulIettr().getIndTipoAtualizacaoRealizado().equals("A"))) {
440
441 if (!"".equals(Pagina.getParamStr(request, "qtdRealizadaArf" + countSelecionado))) {
442 acompRealFisicoArf.setQtdRealizadaArf(Double.valueOf(Util.formataNumero(Pagina.getParamStr(request, "qtdRealizadaArf" + countSelecionado))));
443 }
444 else {
445
446 }
447 }
448
449 if (!"".equals(Pagina.getParamStr(request, "situacaoSit" + countSelecionado))) {
450 acompRealFisicoArf.setSituacaoSit((SituacaoSit) buscar(SituacaoSit.class, Long.valueOf(Pagina.getParamStr(request, "situacaoSit" + countSelecionado))));
451 }
452 else {
453
454 }
455
456 acompRealFisicoArf.setUsuarioUltManut(((SegurancaECAR) request.getSession().getAttribute("seguranca")).getUsuario());
457 acompRealFisicoArf.setDataUltManut(Data.getDataAtual());
458
459 }
460
461 }
462
463
464
465
466
467
468
469 public void alterar(AcompRealFisicoArf acompRealFisicoArf) throws ECARException {
470 Transaction tx = null;
471
472 try {
473 ArrayList objetos = new ArrayList();
474 super.inicializarLogBean();
475
476 tx = session.beginTransaction();
477
478 session.update(acompRealFisicoArf);
479 objetos.add(acompRealFisicoArf);
480
481
482
483 if (acompRealFisicoArf.getSituacaoSit() != null && "S".equals(acompRealFisicoArf.getSituacaoSit().getIndConcluidoSit())) {
484 List arfsPosteriores = this.getArfsPosteriores(acompRealFisicoArf);
485
486 if (arfsPosteriores != null && !arfsPosteriores.isEmpty()) {
487 for (Iterator it = arfsPosteriores.iterator(); it.hasNext();) {
488 AcompRealFisicoArf arfExc = (AcompRealFisicoArf) it.next();
489 session.delete(arfExc);
490 }
491 }
492 }
493
494 tx.commit();
495
496 if (super.logBean != null) {
497 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
498 super.logBean.setOperacao("ALT");
499 Iterator itObj = objetos.iterator();
500
501 while (itObj.hasNext()) {
502 super.logBean.setObj(itObj.next());
503 super.loggerAuditoria.info(logBean.toString());
504 }
505 }
506 } catch (HibernateException e) {
507 if (tx != null)
508 try {
509 tx.rollback();
510 } catch (HibernateException r) {
511 this.logger.error(r);
512 throw new ECARException("erro.hibernateException");
513 }
514 this.logger.error(e);
515 throw new ECARException("erro.hibernateException");
516 }
517 }
518
519
520
521
522
523
524
525
526
527 public void atualizar(HttpServletRequest request) throws ECARException {
528 Transaction tx = null;
529
530 try {
531 ArrayList objetos = new ArrayList();
532 super.inicializarLogBean();
533
534 tx = session.beginTransaction();
535 int cont = 1;
536 int numReg = Pagina.getParamInt(request, "hidCont");
537 while (cont <= numReg) {
538 AcompRealFisicoArf acompRealFisico = new AcompRealFisicoArf();
539
540
541 acompRealFisico = this.buscar(Long.valueOf(Pagina.getParamStr(request, "codArf" + cont)));
542
543
544 if (!"".equals(Pagina.getParamStr(request, "qtdRealizadaArf" + cont))) {
545 acompRealFisico.setQtdRealizadaArf(Double.valueOf(Util.formataNumero(Pagina.getParamStr(request, "qtdRealizadaArf" + cont))));
546 }
547 else {
548
549 }
550
551 if (!"".equals(Pagina.getParamStr(request, "situacaoSit" + cont))) {
552 acompRealFisico.setSituacaoSit((SituacaoSit) buscar(SituacaoSit.class, Long.valueOf(Pagina.getParamStr(request, "situacaoSit" + cont))));
553 }
554 else {
555
556 }
557
558 acompRealFisico.setUsuarioUltManut(((SegurancaECAR) request.getSession().getAttribute("seguranca")).getUsuario());
559 acompRealFisico.setDataUltManut(Data.getDataAtual());
560
561 session.update(acompRealFisico);
562 objetos.add(acompRealFisico);
563
564
565
566 if (acompRealFisico.getSituacaoSit() != null && "S".equals(acompRealFisico.getSituacaoSit().getIndConcluidoSit())) {
567 List arfsPosteriores = this.getArfsPosteriores(acompRealFisico);
568
569 if (arfsPosteriores != null && !arfsPosteriores.isEmpty()) {
570 for (Iterator it = arfsPosteriores.iterator(); it.hasNext();) {
571 AcompRealFisicoArf arfExc = (AcompRealFisicoArf) it.next();
572 session.delete(arfExc);
573 }
574 }
575 }
576
577 cont++;
578 }
579 tx.commit();
580
581 if (super.logBean != null) {
582 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
583 super.logBean.setOperacao("ALT");
584 Iterator itObj = objetos.iterator();
585
586 while (itObj.hasNext()) {
587 super.logBean.setObj(itObj.next());
588 super.loggerAuditoria.info(logBean.toString());
589 }
590 }
591 } catch (HibernateException e) {
592 if (tx != null)
593 try {
594 tx.rollback();
595 } catch (HibernateException r) {
596 this.logger.error(r);
597 throw new ECARException("erro.hibernateException");
598 }
599 this.logger.error(e);
600 throw new ECARException("erro.hibernateException");
601 }
602 }
603
604
605
606
607
608
609
610 public void setAcompRealFisicoFilho(HttpServletRequest request, AcompRealFisicoArf acompRealFisicoArf) throws ECARException {
611
612 int countSelecionado = Pagina.getParamInt(request, "hidContSelecionado");
613
614
615 int countFilhoSelecionado = Pagina.getParamInt(request, "hidContFilhoSelecionado");
616
617 if (countSelecionado > 0) {
618
619
620
621 if (acompRealFisicoArf.getItemEstrtIndResulIettr() != null && acompRealFisicoArf.getItemEstrtIndResulIettr().getIndTipoAtualizacaoRealizado() != null && (!acompRealFisicoArf.getItemEstrtIndResulIettr().getIndTipoAtualizacaoRealizado().equals("A"))) {
622 if (!"".equals(Pagina.getParamStr(request, "qtdRealizadaArf" + countSelecionado + "-" + countFilhoSelecionado))) {
623 acompRealFisicoArf.setQtdRealizadaArf(Double.valueOf(Util.formataNumero(Pagina.getParamStr(request, "qtdRealizadaArf" + countSelecionado + "-" + countFilhoSelecionado))));
624 }
625 else {
626 acompRealFisicoArf.setQtdRealizadaArf(null);
627 }
628 }
629
630
631 if (!"".equals(Pagina.getParamStr(request, "situacaoSit" + countSelecionado + "-" + countFilhoSelecionado))) {
632 acompRealFisicoArf.setSituacaoSit((SituacaoSit) buscar(SituacaoSit.class, Long.valueOf(Pagina.getParamStr(request, "situacaoSit" + countSelecionado + "-" + countFilhoSelecionado))));
633 }
634 else {
635 acompRealFisicoArf.setSituacaoSit(null);
636 }
637
638 acompRealFisicoArf.setUsuarioUltManut(((SegurancaECAR) request.getSession().getAttribute("seguranca")).getUsuario());
639 acompRealFisicoArf.setDataUltManut(Data.getDataAtual());
640
641 }
642 }
643
644
645
646
647
648
649
650
651
652
653
654
655 public void atualizarFilho(HttpServletRequest request) throws ECARException {
656 Transaction tx = null;
657
658 try {
659 ArrayList objetos = new ArrayList();
660 super.inicializarLogBean();
661 tx = session.beginTransaction();
662
663 int numero = Pagina.getParamInt(request, "numFilho");
664
665 int numReg;
666
667
668
669
670
671
672 numReg = Pagina.getParamInt(request, "hidContFilhos" + String.valueOf(numero));
673
674 for (int j = 1; j <= numReg; j++) {
675 AcompRealFisicoArf acompRealFisico = new AcompRealFisicoArf();
676
677
678 acompRealFisico = this.buscar(Long.valueOf(Pagina.getParamStr(request, "codArf" + j + "-" + numero)));
679
680
681 if (!"".equals(Pagina.getParamStr(request, "qtdRealizadaArf" + j + "-" + numero))) {
682 acompRealFisico.setQtdRealizadaArf(Double.valueOf(Util.formataNumero(Pagina.getParamStr(request, "qtdRealizadaArf" + j + "-" + numero))));
683 }
684 else {
685 acompRealFisico.setQtdRealizadaArf(null);
686 }
687
688
689 if (!"".equals(Pagina.getParamStr(request, "situacaoSit" + j + "-" + numero))) {
690 acompRealFisico.setSituacaoSit((SituacaoSit) buscar(SituacaoSit.class, Long.valueOf(Pagina.getParamStr(request, "situacaoSit" + j + "-" + numero))));
691 }
692 else {
693 acompRealFisico.setSituacaoSit(null);
694 }
695
696 acompRealFisico.setUsuarioUltManut(((SegurancaECAR) request.getSession().getAttribute("seguranca")).getUsuario());
697 acompRealFisico.setDataUltManut(Data.getDataAtual());
698
699 session.update(acompRealFisico);
700 objetos.add(acompRealFisico);
701
702
703
704 if (acompRealFisico.getSituacaoSit() != null && "S".equals(acompRealFisico.getSituacaoSit().getIndConcluidoSit())) {
705 List arfsPosteriores = this.getArfsPosteriores(acompRealFisico);
706
707 if (arfsPosteriores != null && !arfsPosteriores.isEmpty()) {
708 for (Iterator it = arfsPosteriores.iterator(); it.hasNext();) {
709 AcompRealFisicoArf arfExc = (AcompRealFisicoArf) it.next();
710 session.delete(arfExc);
711 }
712 }
713 }
714
715 }
716
717 tx.commit();
718
719 if (super.logBean != null) {
720 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
721 super.logBean.setOperacao("ALT");
722 Iterator itObj = objetos.iterator();
723
724 while (itObj.hasNext()) {
725 super.logBean.setObj(itObj.next());
726 super.loggerAuditoria.info(logBean.toString());
727 }
728 }
729 } catch (HibernateException e) {
730 if (tx != null)
731 try {
732 tx.rollback();
733 } catch (HibernateException r) {
734 this.logger.error(r);
735 throw new ECARException("erro.hibernateException");
736 }
737 this.logger.error(e);
738 throw new ECARException("erro.hibernateException");
739 }
740 }
741
742
743
744
745
746
747
748
749
750
751
752
753 public double getQtdRealizadaExercicioNaoAcumulavel(ExercicioExe exercicio, ItemEstrtIndResulIettr indResul, AcompReferenciaAref aref) throws ECARException {
754 try {
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800 List valores = this.getListaRealizadoExercicioByIettrAndExe(exercicio, exercicio, indResul, aref);
801
802
803
804
805
806 Double valor = this.getSomaValoresArfs(indResul, valores);
807
808 if (valor != null)
809 return valor.doubleValue();
810 else
811 return 0;
812
813
814
815 } catch (HibernateException e) {
816 this.logger.error(e);
817 throw new ECARException(e);
818 }
819
820 }
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840 public Double getSomaValoresArfs(ItemEstrtIndResulIettr itemEstrtIndResul, List<Double> valores) {
841 Double retorno = new Double(0);
842 double total = 0;
843 double maior = 0;
844
845 String indAcumulavel = itemEstrtIndResul.getIndAcumulavelIettr();
846 String indValorFinal = itemEstrtIndResul.getIndValorFinalIettr();
847
848 for (Double valor : valores) {
849 if ("S".equals(indAcumulavel) && valor != null) {
850 total += valor.doubleValue();
851 }
852 else {
853 if ("M".equals(indValorFinal)) {
854 if (valor != null && valor.doubleValue() > maior) {
855 maior = valor.doubleValue();
856 total = maior;
857 }
858 }
859 else if ("U".equals(indValorFinal) && valor != null) {
860
861
862
863
864 total = valor.doubleValue();
865 break;
866 }
867 }
868 }
869 retorno = Double.valueOf(total);
870
871 return retorno;
872 }
873
874
875
876
877
878
879
880
881
882
883 public double getQtdRealizadaExercicio(AcompReferenciaItemAri acompReferenciaItem, ItemEstrtIndResulIettr indResul, String radAcumulados) throws HibernateException {
884 double total = 0;
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928 if ("P".equalsIgnoreCase(radAcumulados)) {
929 total = this.getQtdRealizadaExercicio(acompReferenciaItem.getAcompReferenciaAref().getExercicioExe(), indResul, acompReferenciaItem.getAcompReferenciaAref());
930 }
931 else {
932 total = this.getQtdRealizadaExercicio(acompReferenciaItem.getAcompReferenciaAref().getExercicioExe(), indResul, null);
933 }
934
935 return total;
936 }
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991 public double getQtdRealizadaExercicio(ExercicioExe exercicio, ItemEstrtIndResulIettr indResul, AcompReferenciaAref aref) {
992
993
994
995
996
997 return this.getRealizadoExercicioByIettrAndExe(exercicio, exercicio, indResul, aref);
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 public double getRealizadoExercicioByIettrAndExe(ExercicioExe exercicioExeInicial, ExercicioExe exercicioExeFinal, ItemEstrtIndResulIettr iettr, AcompReferenciaAref aref) {
1013
1014 StringBuilder select = new StringBuilder();
1015
1016 select.append("select sum(ARF.qtdRealizadaArf) from AcompRealFisicoArf as ARF");
1017 select.append(" where ARF.itemEstrtIndResulIettr.codIettir = :iettir");
1018 select.append(" and (ARF.anoArf > :anoInicial or (ARF.anoArf = :anoInicial and ARF.mesArf >= :mesInicial))");
1019 select.append(" and (ARF.anoArf < :anoFinal or (ARF.anoArf = :anoFinal and ARF.mesArf <= :mesFinal))");
1020
1021 long anoInicial = Data.getAno(exercicioExeInicial.getDataInicialExe());
1022 long anoFinal = Data.getAno(exercicioExeFinal.getDataFinalExe());
1023 long mesInicial = Data.getMes(exercicioExeInicial.getDataInicialExe()) + 1;
1024 long mesFinal = Data.getMes(exercicioExeFinal.getDataFinalExe()) + 1;
1025
1026
1027
1028
1029
1030
1031 if (aref != null) {
1032 if (exercicioExeInicial.equals(aref.getExercicioExe())) {
1033
1034 mesInicial = Data.getMes(aref.getExercicioExe().getDataInicialExe()) + 1;
1035 anoInicial = Data.getAno(aref.getExercicioExe().getDataInicialExe());
1036 mesFinal = Long.parseLong(aref.getMesAref());
1037 anoFinal = Long.parseLong(aref.getAnoAref());
1038 }
1039 else if (exercicioExeInicial.getDataInicialExe().after(aref.getExercicioExe().getDataFinalExe())) {
1040
1041 return 0;
1042 }
1043 }
1044
1045 Query q = this.session.createQuery(select.toString());
1046
1047 q.setLong("iettir", iettr.getCodIettir().longValue());
1048 q.setLong("anoInicial", anoInicial);
1049 q.setLong("mesInicial", mesInicial);
1050 q.setLong("anoFinal", anoFinal);
1051 q.setLong("mesFinal", mesFinal);
1052
1053 q.setMaxResults(1);
1054
1055 Double retorno = (Double) q.uniqueResult();
1056
1057 if (retorno != null)
1058 return retorno.doubleValue();
1059
1060 return 0;
1061 }
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 public List getListaRealizadoExercicioByIettrAndExe(ExercicioExe exercicioExeInicial, ExercicioExe exercicioExeFinal, ItemEstrtIndResulIettr iettr, AcompReferenciaAref aref) {
1074
1075 StringBuilder select = new StringBuilder();
1076
1077 select.append("select ARF.qtdRealizadaArf from AcompRealFisicoArf as ARF");
1078 select.append(" where ARF.itemEstrtIndResulIettr.codIettir = :iettir");
1079 select.append(" and (ARF.anoArf > :anoInicial or (ARF.anoArf = :anoInicial and ARF.mesArf >= :mesInicial))");
1080 select.append(" and (ARF.anoArf < :anoFinal or (ARF.anoArf = :anoFinal and ARF.mesArf <= :mesFinal))");
1081 select.append(" order by ARF.anoArf desc, ARF.mesArf desc");
1082
1083 long anoInicial = Data.getAno(exercicioExeInicial.getDataInicialExe());
1084 long anoFinal = Data.getAno(exercicioExeFinal.getDataFinalExe());
1085 long mesInicial = Data.getMes(exercicioExeInicial.getDataInicialExe()) + 1;
1086 long mesFinal = Data.getMes(exercicioExeFinal.getDataFinalExe()) + 1;
1087
1088
1089
1090
1091
1092
1093 if (aref != null) {
1094 if (exercicioExeInicial.equals(aref.getExercicioExe())) {
1095
1096 mesInicial = Data.getMes(aref.getExercicioExe().getDataInicialExe()) + 1;
1097 anoInicial = Data.getAno(aref.getExercicioExe().getDataInicialExe());
1098 mesFinal = Long.parseLong(aref.getMesAref());
1099 anoFinal = Long.parseLong(aref.getAnoAref());
1100 }
1101 else if (exercicioExeInicial.getDataInicialExe().after(aref.getExercicioExe().getDataFinalExe())) {
1102
1103 return new ArrayList();
1104 }
1105 }
1106
1107 Query q = this.session.createQuery(select.toString());
1108
1109 q.setLong("iettir", iettr.getCodIettir().longValue());
1110 q.setLong("anoInicial", anoInicial);
1111 q.setLong("mesInicial", mesInicial);
1112 q.setLong("anoFinal", anoFinal);
1113 q.setLong("mesFinal", mesFinal);
1114
1115 return q.list();
1116 }
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 public Double getQtdRealizadaMesAno(ItemEstrtIndResulIettr indResul, Long mes, Long ano) throws ECARException {
1171 try {
1172 StringBuilder baseQuery = new StringBuilder("select sum(arf.qtdRealizadaArf) from AcompRealFisicoArf as arf").append(" where arf.itemEstrtIndResulIettr.codIettir = ?").append(" and arf.anoArf=? and arf.mesArf=?");
1173
1174 Query q = session.createQuery(baseQuery.toString());
1175 q.setLong(0, indResul.getCodIettir().longValue());
1176 q.setLong(1, ano.longValue());
1177 q.setLong(2, mes.longValue());
1178 q.setMaxResults(1);
1179
1180 Double valor = ((Double) q.uniqueResult());
1181
1182 if (valor != null)
1183 return valor;
1184 else
1185 return null;
1186
1187 } catch (HibernateException e) {
1188 this.logger.error(e);
1189 throw new ECARException(e);
1190 }
1191
1192 }
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203 public Map getQtdRealizadaExercicioPorMes(ExercicioExe exercicio, ItemEstrtIndResulIettr indResul) throws ECARException {
1204 try {
1205
1206
1207
1208 Map mapaMeses = new HashMap();
1209
1210 List meses = new ExercicioDao(request).getMesesDentroDoExercicio(exercicio);
1211
1212 Iterator itMeses = meses.iterator();
1213 while (itMeses.hasNext()) {
1214 String chave = itMeses.next().toString();
1215 String mes = chave.split("-")[0];
1216 if (mes.length() == 1)
1217 mes = "0" + mes;
1218 String ano = chave.split("-")[1];
1219
1220 mapaMeses.put(chave, this.getQtdRealizadaMesAno(indResul, Long.valueOf(mes), Long.valueOf(ano)));
1221
1222 }
1223
1224 return mapaMeses;
1225 } catch (HibernateException e) {
1226 this.logger.error(e);
1227 throw new ECARException(e);
1228 }
1229
1230 }
1231
1232 public AcompRealFisicoArf criaNovoAcompRealFisico(AcompReferenciaItemAri acompReferenciaItem, ItemEstruturaIett itemMonitorado, ItemEstrtIndResulIettr indicador) throws ECARException {
1233
1234
1235 AcompRealFisicoArf acompRealFisico = this.buscarPorIettir(Long.valueOf(acompReferenciaItem.getAcompReferenciaAref().getMesAref()), Long.valueOf(acompReferenciaItem.getAcompReferenciaAref().getAnoAref()), indicador.getCodIettir());
1236
1237 if (acompRealFisico == null) {
1238 acompRealFisico = new AcompRealFisicoArf();
1239
1240 acompRealFisico.setItemEstrtIndResulIettr(indicador);
1241 acompRealFisico.setDataInclusaoArf(Data.getDataAtual());
1242 acompRealFisico.setItemEstruturaIett(itemMonitorado);
1243 acompRealFisico.setMesArf(Long.valueOf(acompReferenciaItem.getAcompReferenciaAref().getMesAref()));
1244 acompRealFisico.setAnoArf(Long.valueOf(acompReferenciaItem.getAcompReferenciaAref().getAnoAref()));
1245 }
1246
1247 return acompRealFisico;
1248
1249 }
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 public List getIndResulByAcompRefItemBySituacao(AcompReferenciaItemAri acompRefItem, String indSituacao, boolean noMes) throws Exception {
1271 List lista = new ArrayList();
1272
1273 StringBuilder query = new StringBuilder();
1274
1275 query.append("select acompRF from AcompRealFisicoArf as acompRF");
1276 query.append(" where acompRF.itemEstruturaIett.codIett = :codIett");
1277 query.append(" and acompRF.itemEstruturaIett.indAtivoIett = 'S'");
1278
1279 if (Dominios.CONCLUIDOS.equals(indSituacao)) {
1280
1281
1282
1283
1284
1285 query.append(" and (acompRF.situacaoSit.indConcluidoSit = :indConcluido) ");
1286 }
1287
1288
1289
1290
1291
1292 query.append(" and (acompRF.anoArf < :ano or (acompRF.anoArf = :ano and acompRF.mesArf <= :mes))");
1293
1294 query.append(" order by acompRF.itemEstrtIndResulIettr.nomeIettir asc, acompRF.anoArf desc, acompRF.mesArf desc ");
1295
1296 Query q = this.session.createQuery(query.toString());
1297
1298 q.setLong("codIett", acompRefItem.getItemEstruturaIett().getCodIett().longValue());
1299
1300 if (Dominios.CONCLUIDOS.equals(indSituacao)) {
1301 q.setString("indConcluido", "S");
1302 }
1303
1304 q.setLong("ano", Long.valueOf(acompRefItem.getAcompReferenciaAref().getAnoAref()).longValue());
1305 q.setLong("mes", Long.valueOf(acompRefItem.getAcompReferenciaAref().getMesAref()).longValue());
1306
1307 lista = q.list();
1308
1309 if (lista == null) {
1310 return new ArrayList();
1311 }
1312
1313
1314
1315
1316
1317
1318 String codIettr = "";
1319 for (Iterator it = lista.iterator(); it.hasNext();) {
1320 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
1321
1322 if (codIettr.equals(arf.getItemEstrtIndResulIettr().getCodIettir().toString())) {
1323 it.remove();
1324 }
1325 else {
1326 codIettr = arf.getItemEstrtIndResulIettr().getCodIettir().toString();
1327 }
1328 }
1329
1330
1331
1332 if (Dominios.NAO_CONCLUIDOS.equalsIgnoreCase(indSituacao)) {
1333
1334
1335
1336
1337
1338
1339 for (Iterator it = lista.iterator(); it.hasNext();) {
1340 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
1341
1342
1343 if (arf.getSituacaoSit() != null && "S".equals(arf.getSituacaoSit().getIndConcluidoSit())) {
1344 int mesArf = arf.getMesArf().intValue();
1345 int anoArf = arf.getAnoArf().intValue();
1346 int mesAref = Integer.valueOf(acompRefItem.getAcompReferenciaAref().getMesAref()).intValue();
1347 int anoAref = Integer.valueOf(acompRefItem.getAcompReferenciaAref().getAnoAref()).intValue();
1348
1349
1350 if (!(mesAref == mesArf && anoAref == anoArf)) {
1351
1352 it.remove();
1353 }
1354 }
1355
1356 }
1357
1358 }
1359
1360 ConfiguracaoCfg configuracao = new ConfiguracaoDao(request).getConfiguracao();
1361
1362 if (configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrMetasFisicas() != null) {
1363
1364 Set sisAtributosSatbs = acompRefItem.getAcompReferenciaAref().getTipoAcompanhamentoTa().getSisAtributoSatbs();
1365 List listAtributosConfig = new ArrayList();
1366 if (sisAtributosSatbs != null) {
1367 Iterator itSatbs = sisAtributosSatbs.iterator();
1368 while (itSatbs.hasNext()) {
1369 SisAtributoSatb sisAtributoSatb = (SisAtributoSatb) itSatbs.next();
1370 if (sisAtributoSatb.getSisGrupoAtributoSga().equals(configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrMetasFisicas())) {
1371 listAtributosConfig.add(sisAtributoSatb);
1372 }
1373 }
1374 }
1375
1376
1377
1378 if (!listAtributosConfig.isEmpty()) {
1379
1380 Iterator it = lista.iterator();
1381 while (it.hasNext()) {
1382 AcompRealFisicoArf arf = (AcompRealFisicoArf) it.next();
1383
1384 if (arf.getItemEstrtIndResulIettr().getSisAtributoSatb() != null && !listAtributosConfig.contains(arf.getItemEstrtIndResulIettr().getSisAtributoSatb())) {
1385 it.remove();
1386 }
1387 }
1388 }
1389
1390
1391
1392 Collections.sort(lista, new Comparator() {
1393 public int compare(Object arg0, Object arg1) {
1394
1395 AcompRealFisicoArf arf1 = (AcompRealFisicoArf) arg0;
1396 AcompRealFisicoArf arf2 = (AcompRealFisicoArf) arg1;
1397
1398 if (arf1.getItemEstrtIndResulIettr().getSisAtributoSatb() == null && arf2.getItemEstrtIndResulIettr().getSisAtributoSatb() == null) {
1399 return 0;
1400 }
1401 else if (arf1.getItemEstrtIndResulIettr().getSisAtributoSatb() != null && arf2.getItemEstrtIndResulIettr().getSisAtributoSatb() == null) {
1402 return 1;
1403 }
1404 else if (arf1.getItemEstrtIndResulIettr().getSisAtributoSatb() == null && arf2.getItemEstrtIndResulIettr().getSisAtributoSatb() != null) {
1405 return -1;
1406 }
1407
1408 return arf1.getItemEstrtIndResulIettr().getSisAtributoSatb().getDescricaoSatb().compareTo(arf2.getItemEstrtIndResulIettr().getSisAtributoSatb().getDescricaoSatb());
1409 }
1410 });
1411
1412 }
1413
1414 return lista;
1415 }
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427 public boolean habilitarEdicaoArf(AcompRealFisicoArf arf, AcompReferenciaAref mesReferencia, String indSituacao) {
1428
1429
1430
1431 if (Dominios.NAO_CONCLUIDOS.equals(indSituacao)) {
1432
1433
1434
1435
1436 int mesArf = arf.getMesArf().intValue();
1437 int anoArf = arf.getAnoArf().intValue();
1438 int mesAref = Integer.valueOf(mesReferencia.getMesAref()).intValue();
1439 int anoAref = Integer.valueOf(mesReferencia.getAnoAref()).intValue();
1440
1441 return (mesArf == mesAref && anoArf == anoAref);
1442 }
1443 else if (Dominios.CONCLUIDOS.equals(indSituacao)) {
1444
1445
1446
1447
1448 int mesArf = arf.getMesArf().intValue();
1449 int anoArf = arf.getAnoArf().intValue();
1450 int mesAref = Integer.valueOf(mesReferencia.getMesAref()).intValue();
1451 int anoAref = Integer.valueOf(mesReferencia.getAnoAref()).intValue();
1452
1453 return (mesArf == mesAref && anoArf == anoAref);
1454 }
1455 else {
1456
1457
1458
1459
1460
1461
1462 int mesArf = arf.getMesArf().intValue();
1463 int anoArf = arf.getAnoArf().intValue();
1464 int mesAref = Integer.valueOf(mesReferencia.getMesAref()).intValue();
1465 int anoAref = Integer.valueOf(mesReferencia.getAnoAref()).intValue();
1466
1467 if (arf.getSituacaoSit() != null && "S".equals(arf.getSituacaoSit().getIndConcluidoSit()) && (anoArf < anoAref || (anoArf == anoAref && mesArf < mesAref))) {
1468 return false;
1469 }
1470 else {
1471 return true;
1472 }
1473 }
1474 }
1475
1476 public void incAcompRealFisicoArf(HttpServletRequest request) throws ECARException {
1477 Transaction tx = null;
1478
1479 try {
1480 super.inicializarLogBean();
1481 tx = session.beginTransaction();
1482
1483 AcompReferenciaItemAri acompRefItem = (AcompReferenciaItemAri) buscar(AcompReferenciaItemAri.class, Long.valueOf(Pagina.getParamStr(request, "codAri")));
1484 ItemEstrtIndResulIettr indResul = (ItemEstrtIndResulIettr) buscar(ItemEstrtIndResulIettr.class, Long.valueOf(Pagina.getParamStr(request, "codNovoIndicador")));
1485
1486 AcompRealFisicoArf acompRealFisico = this.criaNovoAcompRealFisico(acompRefItem, acompRefItem.getItemEstruturaIett(), indResul);
1487
1488 session.save(acompRealFisico);
1489
1490 tx.commit();
1491
1492 if (super.logBean != null) {
1493 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
1494 super.logBean.setObj(acompRealFisico);
1495 super.logBean.setOperacao("INC");
1496 super.loggerAuditoria.info(logBean.toString());
1497 }
1498 } catch (HibernateException e) {
1499 if (tx != null)
1500 try {
1501 tx.rollback();
1502 } catch (HibernateException r) {
1503 this.logger.error(r);
1504 throw new ECARException("erro.hibernateException");
1505 }
1506 this.logger.error(e);
1507 throw new ECARException("erro.hibernateException");
1508 }
1509
1510 }
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521 public int getQtdRegistrosRealizadoPeriodo(ItemEstrtIndResulIettr iettr, ItemEstruturaIett iett, Long mes, Long ano) throws ECARException {
1522 try {
1523 int resultado = 0;
1524
1525 StringBuilder baseQuery = new StringBuilder("select arf.qtdRealizadaArf from AcompRealFisicoArf as arf").append(" where arf.itemEstrtIndResulIettr.codIettir = ?").append(" and arf.itemEstruturaIett.codIett = ?").append(" and arf.itemEstruturaIett.indAtivoIett = 'S'").append(" and arf.qtdRealizadaArf > 0 ").append(" and (arf.anoArf < ?").append(" or (arf.anoArf = ?").append(" and arf.mesArf <= ?)").append(" )");
1526
1527 Query q = session.createQuery(baseQuery.toString());
1528 q.setLong(0, iettr.getCodIettir().longValue());
1529 q.setLong(1, iett.getCodIett().longValue());
1530 q.setLong(2, ano.longValue());
1531 q.setLong(3, ano.longValue());
1532 q.setLong(4, mes.longValue());
1533
1534 List retorno = q.list();
1535 if (!retorno.isEmpty()) {
1536 resultado = retorno.size();
1537 }
1538
1539 return resultado;
1540
1541 } catch (HibernateException e) {
1542 this.logger.error(e);
1543 throw new ECARException(e);
1544 }
1545
1546 }
1547
1548
1549
1550
1551
1552
1553
1554
1555 public boolean verificarPermissaoExclusao(AcompRealFisicoArf arf, AcompReferenciaItemAri ari) throws ECARException {
1556 try {
1557 StringBuilder query = new StringBuilder("select ARI from AcompReferenciaItemAri as ARI").append(" where ARI.itemEstruturaIett.codIett = :codIett").append(" and ARI.itemEstruturaIett.indAtivoIett = 'S'").append(" and ARI.acompReferenciaAref.mesAref = :mesAref").append(" and ARI.acompReferenciaAref.anoAref = :anoAref").append(" and ARI.codAri <> :codAri");
1558
1559 Query q = this.getSession().createQuery(query.toString());
1560
1561 q.setLong("codIett", arf.getItemEstruturaIett().getCodIett().longValue());
1562 q.setString("mesAref", arf.getMesArf().toString());
1563 q.setString("anoAref", arf.getAnoArf().toString());
1564 q.setLong("codAri", ari.getCodAri().longValue());
1565
1566 List listaARI = q.list();
1567
1568 if (listaARI != null) {
1569 if (listaARI.size() > 0) {
1570
1571 return false;
1572 }
1573 else {
1574 return true;
1575 }
1576 }
1577 return true;
1578 } catch (Exception e) {
1579 this.logger.error(e);
1580 throw new ECARException(e);
1581 }
1582 }
1583
1584
1585
1586
1587
1588
1589
1590 public AcompRealFisicoArf getArfMaisRecenteItem(ItemEstruturaIett iett) throws ECARException {
1591 try {
1592 StringBuilder query = new StringBuilder("select ARF from AcompRealFisicoArf as ARF").append(" where ARF.itemEstruturaIett.codIett = ?").append(" and ARF.itemEstruturaIett.indAtivoIett = 'S'").append(" order by ARF.anoArf desc, ARF.mesArf desc");
1593
1594 Query q = this.getSession().createQuery(query.toString());
1595 q.setLong(0, iett.getCodIett().longValue());
1596 q.setMaxResults(1);
1597
1598 return (AcompRealFisicoArf) q.uniqueResult();
1599 } catch (HibernateException e) {
1600 this.logger.error(e);
1601 throw new ECARException(e);
1602 }
1603 }
1604
1605
1606
1607
1608
1609
1610
1611 public AcompRealFisicoArf getArfMaisAntigoItem(ItemEstruturaIett iett) throws ECARException {
1612 try {
1613 StringBuilder query = new StringBuilder("select ARF from AcompRealFisicoArf as ARF").append(" where ARF.itemEstruturaIett.codIett = ?").append(" and ARF.itemEstruturaIett.indAtivoIett = 'S'").append(" order by ARF.anoArf, ARF.mesArf");
1614
1615 Query q = this.getSession().createQuery(query.toString());
1616 q.setLong(0, iett.getCodIett().longValue());
1617 q.setMaxResults(1);
1618
1619 return (AcompRealFisicoArf) q.uniqueResult();
1620 } catch (HibernateException e) {
1621 this.logger.error(e);
1622 throw new ECARException(e);
1623 }
1624 }
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643 public List getArfsByIettAndTipoAcomp(ItemEstruturaIett item, TipoAcompanhamentoTa tipoAcomp, ConfiguracaoCfg configuracao, List filtroNiveis, Long mesArf, Long anoArf) throws ECARException {
1644
1645 List retorno = new ArrayList();
1646 if (item != null && tipoAcomp != null) {
1647 ItemEstruturaDao itemEstruturaDao = new ItemEstruturaDao(this.request);
1648
1649
1650
1651 String indMonitoramento = "";
1652
1653 if ("S".equals(tipoAcomp.getIndMonitoramentoTa()) && "N".equals(tipoAcomp.getIndNaoMonitoramentoTa())) {
1654 indMonitoramento = "S";
1655 }
1656 else if ("N".equals(tipoAcomp.getIndMonitoramentoTa()) && "S".equals(tipoAcomp.getIndNaoMonitoramentoTa())) {
1657 indMonitoramento = "N";
1658 }
1659
1660 List niveis = new ArrayList();
1661 List grupoMetaFisica = new ArrayList();
1662
1663 if (tipoAcomp.getSisAtributoSatbs() != null && !tipoAcomp.getSisAtributoSatbs().isEmpty()) {
1664 Iterator itSatb = tipoAcomp.getSisAtributoSatbs().iterator();
1665 while (itSatb.hasNext()) {
1666 SisAtributoSatb satb = (SisAtributoSatb) itSatb.next();
1667
1668
1669
1670 if (configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrNvPlan() != null && configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrNvPlan().equals(satb.getSisGrupoAtributoSga())) {
1671 niveis.add(satb.getCodSatb());
1672 }
1673
1674
1675
1676
1677 if (configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrMetasFisicas() != null && configuracao.getSisGrupoAtributoSgaByCodSgaGrAtrMetasFisicas().equals(satb.getSisGrupoAtributoSga())) {
1678 grupoMetaFisica.add(satb.getCodSatb());
1679 }
1680 }
1681
1682 }
1683
1684
1685 List itens = new ArrayList();
1686 itens.add(item);
1687
1688 if (filtroNiveis != null && !filtroNiveis.isEmpty())
1689 itens.addAll(itemEstruturaDao.getDescendentesPorNivelPlanejamento(item, filtroNiveis));
1690 else
1691 itens.addAll(itemEstruturaDao.getDescendentes(item, true));
1692
1693 if (itens != null && !itens.isEmpty()) {
1694
1695 Iterator it = itens.iterator();
1696 while (it.hasNext()) {
1697
1698 ItemEstruturaIett iett = (ItemEstruturaIett) it.next();
1699
1700 StringBuilder select = new StringBuilder();
1701 select.append("select arf from AcompRealFisicoArf arf");
1702 select.append(" where arf.itemEstruturaIett.codIett = :codIett");
1703 select.append(" and arf.itemEstruturaIett.indAtivoIett = 'S'");
1704
1705 if (mesArf != null) {
1706 select.append(" and arf.mesArf = :mesArf");
1707 }
1708 if (anoArf != null) {
1709 select.append(" and arf.anoArf = :anoArf");
1710 }
1711 if (!"".equals(indMonitoramento)) {
1712 select.append(" and arf.itemEstruturaIett.indMonitoramentoIett = :indMonitoramento");
1713 }
1714 if (!niveis.isEmpty()) {
1715 select.append(" and arf.itemEstruturaIett.itemEstruturaNivelIettns.codSatb in (:niveis)");
1716 }
1717 if (!grupoMetaFisica.isEmpty()) {
1718 select.append(" and arf.itemEstrtIndResulIettr.sisAtributoSatb.codSatb in (:grupoMetaFisica)");
1719 }
1720
1721 Query q = this.session.createQuery(select.toString());
1722
1723 q.setLong("codIett", iett.getCodIett().longValue());
1724
1725 if (mesArf != null) {
1726 q.setLong("mesArf", mesArf.longValue());
1727 }
1728 if (anoArf != null) {
1729 q.setLong("anoArf", anoArf.longValue());
1730 }
1731 if (!"".equals(indMonitoramento)) {
1732 q.setString("indMonitoramento", indMonitoramento);
1733 }
1734 if (!niveis.isEmpty()) {
1735 q.setParameterList("niveis", niveis);
1736 }
1737
1738 if (!grupoMetaFisica.isEmpty()) {
1739 q.setParameterList("grupoMetaFisica", grupoMetaFisica);
1740 }
1741
1742 List arfs = q.list();
1743 if ((item.equals(iett)) || (arfs != null && !arfs.isEmpty())) {
1744 IettArfBean iaBean = new IettArfBean();
1745 iaBean.setItem(iett);
1746 iaBean.setArfs(arfs);
1747
1748 retorno.add(iaBean);
1749 }
1750 }
1751 }
1752 }
1753 return retorno;
1754 }
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766 public List getArfsPosteriores(AcompRealFisicoArf arf) throws ECARException {
1767 List retorno = new ArrayList();
1768 try {
1769 StringBuilder s = new StringBuilder();
1770
1771 s.append("select arf from AcompRealFisicoArf arf");
1772 s.append(" where arf.itemEstruturaIett.codIett = :item");
1773 s.append(" and arf.itemEstruturaIett.indAtivoIett = 'S'");
1774 s.append(" and arf.itemEstrtIndResulIettr.codIettir = :indicador");
1775 s.append(" and (arf.anoArf > :ano or (arf.anoArf = :ano and arf.mesArf > :mes))");
1776
1777 Query q = this.session.createQuery(s.toString());
1778
1779 q.setLong("item", arf.getItemEstruturaIett().getCodIett().longValue());
1780 q.setLong("indicador", arf.getItemEstrtIndResulIettr().getCodIettir().longValue());
1781 q.setLong("ano", arf.getAnoArf().longValue());
1782 q.setLong("mes", arf.getMesArf().longValue());
1783
1784 List arfs = q.list();
1785
1786 retorno = (arfs != null && !arfs.isEmpty()) ? arfs : new ArrayList();
1787 } catch (HibernateException e) {
1788 this.logger.error(e);
1789 throw new ECARException(e);
1790 }
1791
1792 return retorno;
1793 }
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806 public AcompRealFisicoArf getArfAnterior(ItemEstrtIndResulIettr indicador, long mes, long ano) throws ECARException {
1807 try {
1808 StringBuilder s = new StringBuilder();
1809
1810 s.append("select arf from AcompRealFisicoArf arf");
1811 s.append(" where arf.itemEstruturaIett.codIett = :item");
1812 s.append(" and arf.itemEstruturaIett.indAtivoIett = 'S'");
1813 s.append(" and arf.itemEstrtIndResulIettr.codIettir = :indicador");
1814 s.append(" and (arf.anoArf < :ano or (arf.anoArf = :ano and arf.mesArf < :mes))");
1815 s.append(" order by arf.anoArf desc, arf.mesArf desc");
1816
1817 Query q = this.session.createQuery(s.toString());
1818
1819 q.setLong("item", indicador.getItemEstruturaIett().getCodIett().longValue());
1820 q.setLong("indicador", indicador.getCodIettir().longValue());
1821 q.setLong("ano", ano);
1822 q.setLong("mes", mes);
1823
1824 q.setMaxResults(1);
1825
1826 Object o = q.uniqueResult();
1827
1828 return (o != null) ? (AcompRealFisicoArf) o : null;
1829 } catch (HibernateException e) {
1830 this.logger.error(e);
1831 throw new ECARException(e);
1832 }
1833 }
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845 public double getQtdIndicadorGrupoRealizadaExercicio(ItemEstrtIndResulIettr indicador, ExercicioExe exercicio, long anoRef, long mesRef) throws ECARException {
1846 try {
1847 StringBuilder s = new StringBuilder();
1848
1849 s.append("select sum(ARF.qtdRealizadaArf) from AcompRealFisicoArf as ARF");
1850 s.append(" where ARF.itemEstruturaIett.codIett = :item");
1851 s.append(" and ARF.itemEstruturaIett.indAtivoIett = 'S'");
1852 s.append(" and ARF.itemEstrtIndResulIettr.codIettir = :indicador");
1853 s.append(" and ARF.anoArf = :anoArf");
1854
1855
1856 if (Long.valueOf(exercicio.getDescricaoExe()).equals(anoRef)) {
1857 s.append(" and ARF.mesArf <= :mesFinal");
1858 }
1859
1860 Query q = this.session.createQuery(s.toString());
1861
1862 q.setString("anoArf", exercicio.getDescricaoExe());
1863 q.setLong("item", indicador.getItemEstruturaIett().getCodIett().longValue());
1864 q.setLong("indicador", indicador.getCodIettir().longValue());
1865
1866 if (Long.valueOf(exercicio.getDescricaoExe()).equals(anoRef)) {
1867 q.setLong("mesFinal", mesRef);
1868 }
1869
1870 q.setMaxResults(1);
1871
1872 Double retorno = (Double) q.uniqueResult();
1873
1874 if (retorno != null)
1875 return retorno.doubleValue();
1876
1877 return 0;
1878 } catch (HibernateException e) {
1879 this.logger.error(e);
1880 throw new ECARException(e);
1881 }
1882 }
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897 public double getQtdIndicadorGrupoRealizadaExercicioNaoAcumulavel(ItemEstrtIndResulIettr indicador, ExercicioExe exercicio, long anoRef, long mesRef) throws ECARException {
1898 try {
1899
1900 StringBuilder s = new StringBuilder();
1901
1902 s.append("select ARF.qtdRealizadaArf from AcompRealFisicoArf as ARF");
1903 s.append(" where ARF.itemEstruturaIett.codIett = :item");
1904 s.append(" and ARF.itemEstruturaIett.indAtivoIett = 'S'");
1905 s.append(" and ARF.itemEstrtIndResulIettr.codIettir = :indicador");
1906 s.append(" and ARF.anoArf = :anoArf");
1907
1908
1909 if (Long.valueOf(exercicio.getDescricaoExe()).equals(anoRef)) {
1910 s.append(" and ARF.mesArf <= :mesFinal");
1911 }
1912
1913 s.append(" order by ARF.anoArf desc, ARF.mesArf desc");
1914
1915 Query q = this.session.createQuery(s.toString());
1916
1917 q.setLong("item", indicador.getItemEstruturaIett().getCodIett().longValue());
1918 q.setLong("indicador", indicador.getCodIettir().longValue());
1919 q.setString("anoArf", exercicio.getDescricaoExe());
1920 if (Long.valueOf(exercicio.getDescricaoExe()).equals(anoRef)) {
1921 q.setLong("mesFinal", mesRef);
1922 }
1923
1924 List listaIndicadores = q.list();
1925
1926
1927
1928
1929
1930 Double valor = this.getSomaValoresArfs(indicador, listaIndicadores);
1931
1932 if (valor != null)
1933 return valor.doubleValue();
1934 else
1935 return 0;
1936 } catch (HibernateException e) {
1937 this.logger.error(e);
1938 throw new ECARException(e);
1939 }
1940
1941 }
1942
1943 }