1 package ecar.servlet.relatorio.PPA_Orgao;
2
3 import java.math.BigDecimal;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Set;
9 import java.util.TreeSet;
10
11 import javax.servlet.http.HttpServletRequest;
12
13 import org.apache.log4j.Logger;
14
15 import ecar.dao.ExercicioDao;
16 import ecar.dao.ItemEstruturaDao;
17 import ecar.dao.ItemEstruturaPrevisaoDao;
18 import ecar.dao.OrgaoDao;
19 import ecar.dao.PoderDao;
20 import ecar.dao.UnidadeOrcamentariaDao;
21 import ecar.exception.ECARException;
22 import ecar.pojo.ExercicioExe;
23 import ecar.pojo.ItemEstruturaIett;
24 import ecar.pojo.OrgaoOrg;
25 import ecar.pojo.PoderPod;
26 import ecar.servlet.relatorio.PPA_Util.CalcularTotalVisitor;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class RelatorioPPAOrgaoService {
42
43
44
45
46 private Logger logger = Logger.getLogger(this.getClass());
47
48
49
50
51 private HttpServletRequest request;
52
53
54 private final Long CONSTANTE_PERIODO_EXERCICIO = Long.valueOf(2);
55
56
57 private final Character CONSTANTE_IND_ATIVO = new Character('S');
58
59
60
61
62 private ArrayList<PPA_OrgaoBean> dados;
63
64
65
66
67 private final CalcularTotalVisitor calcularTotal = new CalcularTotalVisitor();
68
69
70
71
72 private List<ExercicioExe> listaExercicios = new ArrayList<ExercicioExe>();
73
74 private final int CONSTANTE_PODER = 1;
75 private final int CONSTANTE_ORGAO = 2;
76
77
78 private final Long CONSTANTE_MINISTERIO_PUBLICO = Long.valueOf( 3L );
79
80 private final Long ORGAO_ADMIN_SEPL = Long.valueOf( 103L );
81 private final Long ORGAO_ADMIN_SEFA = Long.valueOf( 104L );
82 private final Long CONSTANTE_PODER_LEGISLATIVO = Long.valueOf( 1L );
83
84 private BigDecimal totalGeral1 = new BigDecimal(0);
85 private BigDecimal totalGeral2 = new BigDecimal(0);
86 private BigDecimal totalGeral3 = new BigDecimal(0);
87 private BigDecimal totalGeral4 = new BigDecimal(0);
88
89
90
91
92
93 private RelatorioPPAOrgaoService(){
94 dados = new ArrayList<PPA_OrgaoBean>();
95 }
96
97
98
99
100
101
102 public static RelatorioPPAOrgaoService getInstance( HttpServletRequest paramRequest ){
103 RelatorioPPAOrgaoService objeto = new RelatorioPPAOrgaoService();
104 objeto.request = paramRequest;
105 return objeto;
106 }
107
108
109
110
111
112 public ArrayList<PPA_OrgaoBean> generatePPA(){
113 getDados();
114 gerarContador();
115 return dados;
116 }
117
118 public BigDecimal getTotalGeral1(){
119 return totalGeral1;
120 }
121
122 public BigDecimal getTotalGeral2(){
123 return totalGeral2;
124 }
125
126 public BigDecimal getTotalGeral3(){
127 return totalGeral3;
128 }
129
130 public BigDecimal getTotalGeral4(){
131 return totalGeral4;
132 }
133
134 public BigDecimal getTotalGeral(){
135 return new BigDecimal(0).add(totalGeral1).add(totalGeral2).add(totalGeral3).add(totalGeral4);
136 }
137
138 private ArrayList<OrgaoOrg> fixarOrgaos(Set<OrgaoOrg> list){
139
140 OrgaoOrg org_SEPL = null;
141 OrgaoOrg org_SEFA = null;
142
143
144 final Long ORGAO_SECR_FAZENDA = Long.valueOf( 13L );
145
146
147 final Long ORGAO_SECR_PLAN = Long.valueOf( 1L );
148
149 ArrayList<OrgaoOrg> novaLista = new ArrayList<OrgaoOrg>();
150
151
152 for (Iterator iter = list.iterator(); iter.hasNext();) {
153 OrgaoOrg org = (OrgaoOrg) iter.next();
154
155 if ( org.getCodOrg().equals( ORGAO_ADMIN_SEPL )){
156 org_SEPL = org;
157 iter.remove();
158 }
159
160 if ( org.getCodOrg().equals( ORGAO_ADMIN_SEFA )){
161 org_SEFA = org;
162 iter.remove();
163 }
164
165
166
167 }
168
169 for (Iterator iter2 = list.iterator(); iter2.hasNext();) {
170 OrgaoOrg tmp = (OrgaoOrg) iter2.next();
171
172 if ( tmp.getCodOrg().equals( ORGAO_SECR_FAZENDA ) ){
173 novaLista.add(tmp);
174 novaLista.add(org_SEFA);
175 }
176
177 if ( tmp.getCodOrg().equals( ORGAO_SECR_PLAN ) ){
178 novaLista.add(tmp);
179 novaLista.add(org_SEPL);
180 }
181
182
183 if ( tmp.getCodOrg().equals( ORGAO_SECR_FAZENDA ) ){
184 }else if ( tmp.getCodOrg().equals( ORGAO_SECR_PLAN ) ){
185 }else{
186 novaLista.add(tmp);
187
188 }
189
190 }
191
192 return novaLista;
193
194 }
195
196
197
198
199
200
201 private void loadExerciciosValidos(){
202
203 try {
204
205 final ExercicioDao exercicioDao = new ExercicioDao(request);
206
207
208 listaExercicios = exercicioDao.getExercicioByPeriodicidade( CONSTANTE_PERIODO_EXERCICIO );
209
210 } catch (ECARException e) {
211 logger.error("Nao foi possivel carregar Exercicio", e);
212 } catch (Exception e) {
213 logger.error("Nao foi possivel carregar Exercicio", e);
214 }
215
216 }
217
218 private void gerarContador(){
219
220 int cont = 0;
221 for (Iterator iter = dados.iterator(); iter.hasNext();) {
222 PPA_OrgaoBean elemento = (PPA_OrgaoBean) iter.next();
223 elemento.setIndice(cont);
224 cont++;
225 }
226
227 }
228
229
230
231
232
233
234 private Set reordenarOrgao(Collection list){
235 TreeSet<OrgaoOrg> novalista = new TreeSet<OrgaoOrg>( new OrgaoComparatorNome() );
236 for (Iterator iter = list.iterator(); iter.hasNext();) {
237 OrgaoOrg org = (OrgaoOrg) iter.next();
238
239 if ( CONSTANTE_IND_ATIVO.toString().equalsIgnoreCase( org.getIndAtivoOrg() ) ){
240 novalista.add(org);
241 }
242
243 }
244 return novalista;
245 }
246
247
248
249
250
251
252
253
254 private void getDados(){
255
256 final PoderDao poderDao = new PoderDao(request);
257 final ItemEstruturaDao itemEstruturaDao = new ItemEstruturaDao(request);
258 final ItemEstruturaPrevisaoDao prevDao = new ItemEstruturaPrevisaoDao(request);
259 final OrgaoDao orgaoDao = new OrgaoDao(request);
260
261
262 try{
263 ArrayList<PoderPod> listPoder = poderDao.getPoderesByPeriodicidade(CONSTANTE_PERIODO_EXERCICIO, CONSTANTE_IND_ATIVO);
264
265
266 loadExerciciosValidos();
267
268 for (Iterator iter = listPoder.iterator(); iter.hasNext();) {
269 PoderPod poder = (PoderPod) iter.next();
270
271 PPA_OrgaoBean orgaoBeanPod = new PPA_OrgaoBean();
272 orgaoBeanPod.setNome( poder.getNomePod().toUpperCase() );
273 orgaoBeanPod.setFlag(CONSTANTE_PODER);
274 dados.add(orgaoBeanPod);
275
276 ArrayList<OrgaoOrg> listOrgao = fixarOrgaos( reordenarOrgao(orgaoDao.getOrgaoByPeriodicidade(CONSTANTE_PERIODO_EXERCICIO, poder.getCodPod(), CONSTANTE_IND_ATIVO)));
277
278 BigDecimal totalOrgaoAno1 = new BigDecimal(0);
279 BigDecimal totalOrgaoAno2 = new BigDecimal(0);
280 BigDecimal totalOrgaoAno3 = new BigDecimal(0);
281 BigDecimal totalOrgaoAno4 = new BigDecimal(0);
282 for (Iterator iterator = listOrgao.iterator(); iterator
283 .hasNext();) {
284 OrgaoOrg orgao = (OrgaoOrg) iterator.next();
285
286 PPA_OrgaoBean orgaoBeanOrg = new PPA_OrgaoBean();
287 orgaoBeanOrg.setNome( orgao.getDescricaoOrg() );
288 orgaoBeanOrg.setFlag(CONSTANTE_ORGAO);
289
290 Set itens = orgao.getItemEstruturaIettsByCodOrgaoResponsavel1Iett();
291
292 for (Iterator itItem = itens.iterator(); itItem.hasNext();) {
293 ItemEstruturaIett itemOrgao = (ItemEstruturaIett) itItem.next();
294
295 if ( CONSTANTE_IND_ATIVO.toString().equalsIgnoreCase( itemOrgao.getIndAtivoIett() ) ){
296
297 if ( itemOrgao.getNivelIett().equals( Integer.valueOf(3) )){
298
299 BigDecimal[] previsto = getPrevisao(itemOrgao);
300 totalizarBean( orgaoBeanOrg, previsto );
301 }
302 }
303
304
305 }
306
307 dados.add(orgaoBeanOrg);
308 totalOrgaoAno1 = totalOrgaoAno1.add( orgaoBeanOrg.getValor1() );
309 totalOrgaoAno2 = totalOrgaoAno2.add( orgaoBeanOrg.getValor2() );
310 totalOrgaoAno3 = totalOrgaoAno3.add( orgaoBeanOrg.getValor3() );
311 totalOrgaoAno4 = totalOrgaoAno4.add( orgaoBeanOrg.getValor4() );
312
313
314 }
315 orgaoBeanPod.setValor1( totalOrgaoAno1 );
316 orgaoBeanPod.setValor2( totalOrgaoAno2 );
317 orgaoBeanPod.setValor3( totalOrgaoAno3 );
318 orgaoBeanPod.setValor4( totalOrgaoAno4 );
319 calcularTotal.visit(orgaoBeanPod);
320
321 totalGeral1 = totalGeral1.add( orgaoBeanPod.getValor1() );
322 totalGeral2 = totalGeral2.add( orgaoBeanPod.getValor2() );
323 totalGeral3 = totalGeral3.add( orgaoBeanPod.getValor3() );
324 totalGeral4 = totalGeral4.add( orgaoBeanPod.getValor4() );
325
326
327 }
328
329
330 }catch (Exception e) {
331 logger.error("Nao foi possivel carregar dados do relatorio", e);
332 listError();
333 }
334
335
336 }
337
338 private void totalizarBean(PPA_OrgaoBean orgaoBean, BigDecimal[] valores){
339
340 final BigDecimal ZERO = new BigDecimal(0);
341
342 try {
343 if( valores!=null && valores.length == 4 ){
344
345 BigDecimal vlr1 = orgaoBean.getValor1();
346 BigDecimal vlr2 = orgaoBean.getValor2();
347 BigDecimal vlr3 = orgaoBean.getValor3();
348 BigDecimal vlr4 = orgaoBean.getValor4();
349
350 orgaoBean.setValor1( vlr1.add( valores[0]==null?ZERO:valores[0] ) );
351 orgaoBean.setValor2( vlr2.add( valores[1]==null?ZERO:valores[1] ) );
352 orgaoBean.setValor3( vlr3.add( valores[2]==null?ZERO:valores[2] ) );
353 orgaoBean.setValor4( vlr4.add( valores[3]==null?ZERO:valores[3] ) );
354 calcularTotal.visit(orgaoBean);
355
356 }else{
357 orgaoBean.setValor1( ZERO );
358 orgaoBean.setValor2( ZERO );
359 orgaoBean.setValor3( ZERO );
360 orgaoBean.setValor4( ZERO );
361 orgaoBean.setTotal( ZERO );
362 }
363 } catch (Exception e) {
364 logger.error("Nao foi possivel totalizar bean: " + orgaoBean.getNome() + " Valores: " + valores , e);
365 }
366
367
368 }
369
370 private BigDecimal[] getPrevisao(final ItemEstruturaIett itemEstrut ) throws ECARException{
371
372 final ItemEstruturaPrevisaoDao prevDao = new ItemEstruturaPrevisaoDao(request);
373
374 ArrayList<BigDecimal> previsao = new ArrayList<BigDecimal>();
375 BigDecimal previsaoItem = null;
376 for (Iterator iterExer = listaExercicios.iterator(); iterExer.hasNext();) {
377 ExercicioExe elementoExerc = (ExercicioExe) iterExer.next();
378 previsaoItem = prevDao.previsaoItemAcao( itemEstrut.getCodIett(), elementoExerc.getCodExe() ) ;
379 previsao.add(previsaoItem);
380 }
381 return previsao.toArray(new BigDecimal[previsao.size()]);
382
383 }
384
385
386 private void listError(){
387 dados = new ArrayList<PPA_OrgaoBean>();
388 PPA_OrgaoBean novoBean = new PPA_OrgaoBean();
389 novoBean.setNome("Vazio");
390 dados.add(novoBean);
391 }
392
393 }