1 package ecar.servlet.relatorio.PPA_funcao;
2
3 import java.math.BigDecimal;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.TreeSet;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.apache.log4j.Logger;
12
13 import ecar.dao.ExercicioDao;
14 import ecar.dao.ItemEstruturaDao;
15 import ecar.dao.ItemEstruturaPrevisaoDao;
16 import ecar.exception.ECARException;
17 import ecar.pojo.AreaAre;
18 import ecar.pojo.ExercicioExe;
19 import ecar.pojo.ItemEstruturaIett;
20 import ecar.pojo.SubAreaSare;
21 import ecar.servlet.relatorio.PPA_funcao.bean.FuncaoAreaAreBean;
22 import ecar.servlet.relatorio.PPA_funcao.bean.FuncaoSubAreaSareBean;
23 import ecar.servlet.relatorio.PPA_funcao.bean.comparator.FuncaoComparatorAreaAre;
24 import ecar.servlet.relatorio.PPA_funcao.bean.comparator.FuncaoComparatorSubAreaSare;
25
26
27
28
29
30
31
32
33 public class RelatorioPPAFuncaoService {
34
35
36
37
38 private Logger logger = Logger.getLogger(this.getClass());
39
40
41
42
43 private HttpServletRequest request;
44
45
46
47
48
49 private TreeSet<FuncaoAreaAreBean> dados;
50
51 private static final Long codEstruturaAcao = Long.valueOf(20);
52
53
54
55
56
57
58
59
60 private List<ExercicioExe> listaExercicios = new ArrayList<ExercicioExe>();
61
62
63 private final Long CONSTANTE_PERIODO_EXERCICIO = Long.valueOf(2);
64
65
66 private BigDecimal totalGeral1;
67 private BigDecimal totalGeral2;
68 private BigDecimal totalGeral3;
69 private BigDecimal totalGeral4;
70 private BigDecimal totalGeral;
71
72 public BigDecimal getTotalGeral() {
73 return totalGeral;
74 }
75
76 public BigDecimal getTotalGeral1() {
77 return totalGeral1;
78 }
79
80 public BigDecimal getTotalGeral2() {
81 return totalGeral2;
82 }
83
84 public BigDecimal getTotalGeral3() {
85 return totalGeral3;
86 }
87
88 public BigDecimal getTotalGeral4() {
89 return totalGeral4;
90 }
91
92
93
94
95
96
97 private RelatorioPPAFuncaoService(){
98 dados = new TreeSet<FuncaoAreaAreBean>( new FuncaoComparatorAreaAre() );
99 }
100
101
102
103
104
105
106 public static RelatorioPPAFuncaoService getInstance( HttpServletRequest paramRequest ){
107 RelatorioPPAFuncaoService objeto = new RelatorioPPAFuncaoService();
108 objeto.request = paramRequest;
109 return objeto;
110 }
111
112
113
114
115
116 public TreeSet<FuncaoAreaAreBean> generatePPA(){
117 loadExerciciosValidos();
118 getDados();
119
120 calcularTotalAreaARE();
121 return dados;
122 }
123
124
125
126
127
128
129
130
131 private void getDados(){
132
133 try {
134 final BigDecimal ZERO = new BigDecimal(0);
135
136 final ItemEstruturaDao itemDao = new ItemEstruturaDao(request);
137 List itens = itemDao.getItensByEstrutura( codEstruturaAcao );
138 logger.info( "Quantidade de itens:: " + itens.size() );
139 TreeSet<FuncaoAreaAreBean> colecao = new TreeSet<FuncaoAreaAreBean>( new FuncaoComparatorAreaAre() );
140
141 for (Iterator iter = itens.iterator(); iter.hasNext();) {
142 ItemEstruturaIett itemAcao = (ItemEstruturaIett) iter.next();
143
144 AreaAre areaAre = itemAcao.getAreaAre();
145 SubAreaSare subAreaSare = itemAcao.getSubAreaSare();
146
147 FuncaoAreaAreBean tmpAreBean = new FuncaoAreaAreBean();
148 tmpAreBean.setAreaAre( areaAre );
149 tmpAreBean.setCodigoIdentAre( areaAre.getCodigoIdentAre() );
150 tmpAreBean.setNomeAre( areaAre.getNomeAre() );
151
152 FuncaoSubAreaSareBean tmpSareBean = new FuncaoSubAreaSareBean();
153 tmpSareBean.setSubAreaSare(subAreaSare);
154 tmpSareBean.setCodigoIdentSare( subAreaSare.getCodigoIdentSare() );
155 tmpSareBean.setNomeSare( subAreaSare.getNomeSare() );
156
157 TreeSet<FuncaoSubAreaSareBean> tmpSubAreaSet = new TreeSet<FuncaoSubAreaSareBean>( new FuncaoComparatorSubAreaSare() );
158 tmpSubAreaSet.add(tmpSareBean);
159 tmpAreBean.setSubAreaList( tmpSubAreaSet );
160
161 ArrayList<Long> codIettList = new ArrayList<Long>();
162 codIettList.add( itemAcao.getCodIett() );
163 tmpAreBean.setCodIettList( codIettList );
164
165 BigDecimal[] totais = getPrevisao(itemAcao);
166
167 tmpAreBean.setValor1( totais[0]==null?ZERO:totais[0] );
168 tmpAreBean.setValor2( totais[1]==null?ZERO:totais[1] );
169 tmpAreBean.setValor3( totais[2]==null?ZERO:totais[2] );
170 tmpAreBean.setValor4( totais[3]==null?ZERO:totais[3] );
171 BigDecimal total = new BigDecimal(0D);
172 total = total.add( tmpAreBean.getValor1() );
173 total = total.add( tmpAreBean.getValor2() );
174 total = total.add( tmpAreBean.getValor3() );
175 total = total.add( tmpAreBean.getValor4() );
176 tmpAreBean.setTotal( total );
177
178 tmpSareBean.setValor1( totais[0]==null?ZERO:totais[0] );
179 tmpSareBean.setValor2( totais[1]==null?ZERO:totais[1] );
180 tmpSareBean.setValor3( totais[2]==null?ZERO:totais[2] );
181 tmpSareBean.setValor4( totais[3]==null?ZERO:totais[3] );
182 BigDecimal totalSare = new BigDecimal(0D);
183 totalSare = totalSare.add( tmpSareBean.getValor1() );
184 totalSare = totalSare.add( tmpSareBean.getValor2() );
185 totalSare = totalSare.add( tmpSareBean.getValor3() );
186 totalSare = totalSare.add( tmpSareBean.getValor4() );
187 tmpSareBean.setTotal( totalSare );
188
189 boolean jaExisteAre = colecao.contains( tmpAreBean );
190 if ( jaExisteAre ){
191 for (Iterator iterator = colecao.iterator(); iterator
192 .hasNext();) {
193 FuncaoAreaAreBean itemAreaAre = (FuncaoAreaAreBean) iterator.next();
194
195 if ( tmpAreBean.equals( itemAreaAre ) ){
196 itemAreaAre.setValor1( itemAreaAre.getValor1().add( tmpAreBean.getValor1() ) );
197 itemAreaAre.setValor2( itemAreaAre.getValor2().add( tmpAreBean.getValor2() ) );
198 itemAreaAre.setValor3( itemAreaAre.getValor3().add( tmpAreBean.getValor3() ) );
199 itemAreaAre.setValor4( itemAreaAre.getValor4().add( tmpAreBean.getValor4() ) );
200 itemAreaAre.setTotal( itemAreaAre.getTotal().add( tmpAreBean.getTotal() ) );
201
202 TreeSet<FuncaoSubAreaSareBean> itemSubAreaSet = itemAreaAre.getSubAreaList();
203 boolean naoExisteSare = (!itemSubAreaSet.contains( tmpSareBean ));
204 if(naoExisteSare){
205 itemSubAreaSet.add( tmpSareBean );
206 }
207
208 ArrayList<Long> itemCodIettList = itemAreaAre.getCodIettList();
209 itemCodIettList.addAll( tmpAreBean.getCodIettList() );
210
211 }
212
213 }
214
215 }else{
216 boolean adicionou = colecao.add( tmpAreBean );
217 if ( !adicionou ){
218 logger.error( "Nao foi possivel adicionar bean " + tmpAreBean.toString() );
219 }
220 }
221
222 }
223
224 this.dados = colecao;
225
226
227 } catch (Exception e) {
228 logger.error("Nao foi possivel carregar dados", e);
229 }
230 }
231
232
233
234
235
236
237
238 private BigDecimal[] getPrevisao(final ItemEstruturaIett itemEstrut ) throws ECARException{
239
240 final ItemEstruturaPrevisaoDao prevDao = new ItemEstruturaPrevisaoDao(request);
241
242 ArrayList<BigDecimal> previsao = new ArrayList<BigDecimal>();
243 BigDecimal previsaoItem = null;
244 for (Iterator iterExer = listaExercicios.iterator(); iterExer.hasNext();) {
245 ExercicioExe elementoExerc = (ExercicioExe) iterExer.next();
246 previsaoItem = prevDao.previsaoItemAcao( itemEstrut.getCodIett(), elementoExerc.getCodExe() ) ;
247 previsao.add(previsaoItem);
248 }
249 return previsao.toArray(new BigDecimal[previsao.size()]);
250
251 }
252
253
254
255
256
257 private void loadExerciciosValidos(){
258
259 try {
260
261 final ExercicioDao exercicioDao = new ExercicioDao(request);
262
263
264 listaExercicios = exercicioDao.getExercicioByPeriodicidade( CONSTANTE_PERIODO_EXERCICIO );
265
266 } catch (ECARException e) {
267 logger.error("Nao foi possivel carregar Exercicio", e);
268 } catch (Exception e) {
269 logger.error("Nao foi possivel carregar Exercicio", e);
270 }
271
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289 private void calcularTotalAreaARE(){
290
291 BigDecimal localTotalGeral1 = new BigDecimal(0);
292 BigDecimal localTotalGeral2 = new BigDecimal(0);
293 BigDecimal localTotalGeral3 = new BigDecimal(0);
294 BigDecimal localTotalGeral4 = new BigDecimal(0);
295 BigDecimal localTotalGeral = new BigDecimal(0);
296
297 for (Iterator iter = dados.iterator(); iter.hasNext();) {
298 FuncaoAreaAreBean tmp = (FuncaoAreaAreBean) iter.next();
299
300 BigDecimal vlr1 = new BigDecimal(0);
301 BigDecimal vlr2 = new BigDecimal(0);
302 BigDecimal vlr3 = new BigDecimal(0);
303 BigDecimal vlr4 = new BigDecimal(0);
304 BigDecimal total = new BigDecimal(0);
305
306 int cont = 0;
307 for (Iterator iter2 = tmp.getSubAreaList().iterator(); iter2.hasNext();) {
308 FuncaoSubAreaSareBean tmp2 = (FuncaoSubAreaSareBean) iter2.next();
309
310 vlr1 = vlr1.add( tmp2.getValor1() );
311 vlr2 = vlr2.add( tmp2.getValor2() );
312 vlr3 = vlr3.add( tmp2.getValor3() );
313 vlr4 = vlr4.add( tmp2.getValor4() );
314 total = total.add( tmp2.getTotal() );
315 tmp2.setIndice(new Integer(cont));
316 cont++;
317 }
318
319 tmp.setValor1( vlr1 );
320 tmp.setValor2( vlr2 );
321 tmp.setValor3( vlr3 );
322 tmp.setValor4( vlr4 );
323 tmp.setTotal( total );
324
325 localTotalGeral1 = localTotalGeral1.add( vlr1 );
326 localTotalGeral2 = localTotalGeral2.add( vlr2 );
327 localTotalGeral3 = localTotalGeral3.add( vlr3 );
328 localTotalGeral4 = localTotalGeral4.add( vlr4 );
329 localTotalGeral = localTotalGeral.add( total );
330
331 }
332
333 this.totalGeral1 = localTotalGeral1;
334 this.totalGeral2 = localTotalGeral2;
335 this.totalGeral3 = localTotalGeral3;
336 this.totalGeral4 = localTotalGeral4;
337 this.totalGeral = localTotalGeral;
338 }
339
340
341 }