1 package ecar.dao;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Set;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.hibernate.HibernateException;
11 import org.hibernate.Query;
12 import org.hibernate.Transaction;
13
14 import comum.database.Dao;
15 import comum.util.Data;
16 import comum.util.Pagina;
17 import comum.util.Util;
18
19 import ecar.exception.ECARException;
20 import ecar.pojo.ExercicioExe;
21 import ecar.pojo.IettIndResulRevIettrr;
22 import ecar.pojo.ItemEstFisicoRevIettfr;
23 import ecar.pojo.ItemEstFisicoRevIettfrPK;
24 import ecar.pojo.ItemEstrtIndResulIettr;
25 import ecar.pojo.ItemEstruturarevisaoIettrev;
26
27 public class IettIndResulRevIettrrDAO extends Dao {
28
29
30
31 public IettIndResulRevIettrrDAO(HttpServletRequest request) {
32 super();
33 this.request = request;
34 }
35
36
37
38
39
40
41
42
43 public void setItemEstrtIndResul(HttpServletRequest request, IettIndResulRevIettrr itemEstrtIndResul) throws ECARException {
44 itemEstrtIndResul.setItemEstruturarevisaoIettrev((ItemEstruturarevisaoIettrev) this.buscar(ItemEstruturarevisaoIettrev.class, Integer.valueOf(Pagina.getParamStr(request, "codIettrev"))));
45 }
46
47
48
49
50
51
52
53 public void excluir(String[] codigosParaExcluir) throws ECARException {
54 Transaction tx = null;
55
56 try {
57 ArrayList objetos = new ArrayList();
58
59 super.inicializarLogBean();
60
61 tx = session.beginTransaction();
62
63 for (int i = 0; i < codigosParaExcluir.length; i++) {
64 IettIndResulRevIettrr itemEstrtIndResul = (IettIndResulRevIettrr) buscar(IettIndResulRevIettrr.class, Integer.valueOf(codigosParaExcluir[i]));
65 session.delete(itemEstrtIndResul);
66 objetos.add(itemEstrtIndResul);
67 }
68
69 tx.commit();
70
71 if (super.logBean != null) {
72 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
73 super.logBean.setOperacao("EXC");
74 Iterator itObj = objetos.iterator();
75
76 while (itObj.hasNext()) {
77 super.logBean.setObj(itObj.next());
78 super.loggerAuditoria.info(logBean.toString());
79 }
80 }
81 } catch (HibernateException e) {
82 if (tx != null)
83 try {
84 tx.rollback();
85 } catch (HibernateException r) {
86 this.logger.error(r);
87 throw new ECARException("erro.hibernateException");
88 }
89 this.logger.error(e);
90 throw new ECARException("erro.hibernateException");
91 }
92 }
93
94
95
96
97
98
99
100
101 public double getSomaQuantidades(IettIndResulRevIettrr itemEstrtIndResul) throws ECARException {
102 double total = 0;
103 if (itemEstrtIndResul.getItemEstFisicoRevIettfrs() != null) {
104 Iterator it = itemEstrtIndResul.getItemEstFisicoRevIettfrs().iterator();
105 while (it.hasNext()) {
106 ItemEstFisicoRevIettfr itemEstrutFisico = (ItemEstFisicoRevIettfr) it.next();
107 if ("S".equalsIgnoreCase(itemEstrutFisico.getIndAtivoIettfr()))
108 total += itemEstrutFisico.getQtdPrevistaIettfr().doubleValue();
109 }
110 }
111 return total;
112 }
113
114
115
116
117
118
119
120
121 public String getSomaQuantidadePrevista(IettIndResulRevIettrr itemEstrtIndResul) throws ECARException {
122 String retorno = "";
123 double total = 0;
124
125 ItemEstrtIndResulIettr indicador = itemEstrtIndResul.getItemEstrtIndResulIettr();
126
127 if (indicador != null) {
128 if ("S".equals(indicador.getIndAcumulavelIettr())) {
129 List exercicios = new ArrayList(itemEstrtIndResul.getItemEstFisicoRevIettfrs());
130
131 Iterator it = exercicios.iterator();
132 while (it.hasNext()) {
133 ItemEstFisicoRevIettfr exercicio = (ItemEstFisicoRevIettfr) it.next();
134 if ("S".equals(exercicio.getIndAtivoIettfr()))
135 total += exercicio.getQtdPrevistaIettfr().doubleValue();
136 }
137
138 retorno = Util.formataNumeroSemDecimal(total);
139
140 }
141 else {
142
143
144
145
146
147
148
149
150 if ("M".equals(indicador.getIndValorFinalIettr())) {
151
152 List exercicios = new ArrayList(itemEstrtIndResul.getItemEstFisicoRevIettfrs());
153
154 Iterator it = exercicios.iterator();
155 double maior = 0;
156 while (it.hasNext()) {
157 ItemEstFisicoRevIettfr exercicio = (ItemEstFisicoRevIettfr) it.next();
158 if ("S".equals(exercicio.getIndAtivoIettfr())) {
159 if (exercicio.getQtdPrevistaIettfr().doubleValue() > maior) {
160 maior = exercicio.getQtdPrevistaIettfr().doubleValue();
161 }
162 total = maior;
163 }
164 }
165
166 retorno = Util.formataNumeroSemDecimal(total);
167 }
168 else if ("U".equals(indicador.getIndValorFinalIettr())) {
169 double ultimo = 0;
170 ExercicioExe ultimoExe = getMaiorExercicioIndicador(itemEstrtIndResul);
171
172 List exercicios = new ArrayList(itemEstrtIndResul.getItemEstFisicoRevIettfrs());
173
174 Iterator it = exercicios.iterator();
175 while (it.hasNext()) {
176 ItemEstFisicoRevIettfr exercicio = (ItemEstFisicoRevIettfr) it.next();
177 if ("S".equals(exercicio.getIndAtivoIettfr())) {
178 if (exercicio.getExercicioExe().getCodExe().equals(ultimoExe.getCodExe())) {
179 ultimo = exercicio.getQtdPrevistaIettfr().doubleValue();
180 break;
181 }
182 }
183 }
184 retorno = Util.formataNumeroSemDecimal(ultimo);
185 }
186 else if ("N".equals(indicador.getIndValorFinalIettr())) {
187
188 retorno = "";
189 }
190 }
191
192 }
193
194 return retorno;
195 }
196
197
198
199
200
201
202
203
204
205
206 public double getQtdPrevistoExercicio(IettIndResulRevIettrr indResul, ExercicioExe exercicio) {
207 double quant = 0;
208
209 try {
210 ItemEstFisicoRevIettfr qtdPrevista = new ItemEstFisicoRevIettfrDAO(request).buscar(Long.valueOf(indResul.getCodIettirr().longValue()),Long.valueOf(exercicio.getCodExe()));
211 if ("S".equalsIgnoreCase(qtdPrevista.getIndAtivoIettfr()))
212 quant = qtdPrevista.getQtdPrevistaIettfr().doubleValue();
213 } catch (ECARException e) {
214 this.logger.error(e);
215
216 }
217
218 return quant;
219 }
220
221
222
223
224
225
226
227
228 public ExercicioExe getMaiorExercicioIndicador(IettIndResulRevIettrr indResul) throws ECARException {
229 try {
230 Query q = this.getSession().createQuery("select ieFisico.exercicioExe from ItemEstFisicoRevIettfr ieFisico" + " where ieFisico.iettIndResulRevIettrr.codIettirr = ? " + " order by ieFisico.exercicioExe.dataFinalExe desc");
231 q.setLong(0, indResul.getCodIettirr().longValue());
232 q.setMaxResults(1);
233 return (ExercicioExe) q.uniqueResult();
234 } catch (HibernateException e) {
235 this.logger.error(e);
236 throw new ECARException(e);
237 }
238 }
239
240
241
242
243
244
245
246 public List getExerciosCadastroPermitidos(IettIndResulRevIettrr indicativoResultado) throws ECARException {
247 List exercicios = new ExercicioDao(request).listar(ExercicioExe.class, new String[] { "descricaoExe", Dao.ORDEM_ASC });
248 Set quantidades = indicativoResultado.getItemEstFisicoRevIettfrs();
249 Iterator it = quantidades.iterator();
250 while (it.hasNext()) {
251 ItemEstFisicoRevIettfr ieFisico = (ItemEstFisicoRevIettfr) it.next();
252 ExercicioExe exercicioCadastrado = ieFisico.getExercicioExe();
253 exercicios.remove(exercicioCadastrado);
254 }
255 return exercicios;
256 }
257
258
259
260
261
262
263
264
265
266 public List getListaQuantidadePrevista(HttpServletRequest request) throws ECARException {
267 ExercicioDao exercicioDao = new ExercicioDao(request);
268 List listaExercicio = exercicioDao.getExerciciosValidos(Long.valueOf(Pagina.getParamStr(request, "codIett")));
269 Iterator itExercicio = listaExercicio.iterator();
270
271 List listaQtd = new ArrayList();
272
273 while (itExercicio.hasNext()) {
274 ExercicioExe exercicio = (ExercicioExe) itExercicio.next();
275
276 if (!"".equals(Pagina.getParamStr(request, "qtdPrevistaIettf" + exercicio.getCodExe()))) {
277 ItemEstFisicoRevIettfr itemEstrutFisico = new ItemEstFisicoRevIettfr();
278 ItemEstFisicoRevIettfrPK chave = new ItemEstFisicoRevIettfrPK();
279
280 chave.setCodExe(Long.valueOf(exercicio.getCodExe().intValue()));
281 itemEstrutFisico.setComp_id(chave);
282 itemEstrutFisico.setQtdPrevistaIettfr(Double.valueOf(Util.formataNumero(Pagina.getParamStr(request, "qtdPrevistaIettf" + exercicio.getCodExe()))));
283
284 itemEstrutFisico.setIndAtivoIettfr("S");
285 itemEstrutFisico.setDataInclusaoIettfr(Data.getDataAtual());
286
287 listaQtd.add(itemEstrutFisico);
288 }
289 }
290
291 return listaQtd;
292 }
293
294
295
296
297
298
299
300 public void salvar(IettIndResulRevIettrr itemEstrtIndResul, List listaQtd) throws ECARException {
301 inicializarLogBean();
302 Transaction tx = null;
303 try {
304 ArrayList objetosInseridos = new ArrayList();
305 tx = session.beginTransaction();
306
307
308 session.save(itemEstrtIndResul);
309 objetosInseridos.add(itemEstrtIndResul);
310
311 Iterator itQtd = listaQtd.iterator();
312 while (itQtd.hasNext()) {
313 ItemEstFisicoRevIettfr itemEstrutFisico = (ItemEstFisicoRevIettfr) itQtd.next();
314 itemEstrutFisico.getComp_id().setCodIettirr(itemEstrtIndResul.getCodIettirr());
315
316 session.save(itemEstrutFisico);
317 objetosInseridos.add(itemEstrutFisico);
318 }
319
320 tx.commit();
321
322 if (logBean != null) {
323 logBean.setCodigoTransacao(Data.getHoraAtual(false));
324 logBean.setOperacao("INC");
325 Iterator it2 = objetosInseridos.iterator();
326
327 while (it2.hasNext()) {
328 logBean.setObj(it2.next());
329 loggerAuditoria.info(logBean.toString());
330 }
331 }
332 } catch (HibernateException e) {
333 if (tx != null)
334 try {
335 tx.rollback();
336 } catch (HibernateException r) {
337 this.logger.error(r);
338 throw new ECARException("erro.hibernateException");
339 }
340 this.logger.error(e);
341 throw new ECARException("erro.hibernateException");
342 }
343 }
344
345
346
347
348
349
350
351 public void alterar(IettIndResulRevIettrr itemEstrtIndResul, List listaQtd) throws ECARException {
352 inicializarLogBean();
353 Transaction tx = null;
354 try {
355 ArrayList objetosInseridos = new ArrayList();
356 ArrayList objetosExcluidos = new ArrayList();
357 tx = session.beginTransaction();
358
359
360 List listaAux = new ArrayList();
361 listaAux.addAll(itemEstrtIndResul.getItemEstFisicoRevIettfrs());
362
363 Iterator itAux = listaAux.iterator();
364 while (itAux.hasNext()) {
365 ItemEstFisicoRevIettfr itemEstrutFisico = (ItemEstFisicoRevIettfr) itAux.next();
366 objetosExcluidos.add(itemEstrutFisico);
367 session.delete(itemEstrutFisico);
368 }
369
370 if (logBean != null) {
371 logBean.setCodigoTransacao(Data.getHoraAtual(false));
372 logBean.setOperacao("EXC");
373 Iterator it = objetosExcluidos.iterator();
374
375 while (it.hasNext()) {
376 logBean.setObj(it.next());
377 loggerAuditoria.info(logBean.toString());
378 }
379 }
380
381
382 session.update(itemEstrtIndResul);
383
384 if (logBean != null) {
385 logBean.setCodigoTransacao(Data.getHoraAtual(false));
386 logBean.setObj(itemEstrtIndResul);
387 logBean.setOperacao("ALT");
388 loggerAuditoria.info(logBean.toString());
389 }
390
391 Iterator itQtd = listaQtd.iterator();
392 while (itQtd.hasNext()) {
393 ItemEstFisicoRevIettfr itemEstrutFisico = (ItemEstFisicoRevIettfr) itQtd.next();
394 itemEstrutFisico.getComp_id().setCodIettirr(itemEstrtIndResul.getCodIettirr());
395
396 session.save(itemEstrutFisico);
397 objetosInseridos.add(itemEstrutFisico);
398 }
399
400 tx.commit();
401
402 if (logBean != null) {
403 logBean.setCodigoTransacao(Data.getHoraAtual(false));
404 logBean.setOperacao("INC");
405 Iterator it2 = objetosInseridos.iterator();
406
407 while (it2.hasNext()) {
408 logBean.setObj(it2.next());
409 loggerAuditoria.info(logBean.toString());
410 }
411 }
412 } catch (HibernateException e) {
413 if (tx != null)
414 try {
415 tx.rollback();
416 } catch (HibernateException r) {
417 this.logger.error(r);
418 throw new ECARException("erro.hibernateException");
419 }
420 this.logger.error(e);
421 throw new ECARException("erro.hibernateException");
422 }
423 }
424
425 }