1 package ecar.dao;
2
3 import java.util.ArrayList;
4 import java.util.Date;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Set;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.hibernate.HibernateException;
12 import org.hibernate.Query;
13 import org.hibernate.Transaction;
14
15 import comum.database.Dao;
16
17 import ecar.exception.ECARException;
18 import ecar.pojo.ExercicioExe;
19 import ecar.pojo.ItemEstrtIndResulIettr;
20 import ecar.pojo.ItemEstrtIndResulLocalIettirl;
21 import ecar.pojo.LocalItemLit;
22
23 public class ItemEstrtIndResultLocalIettirlDao extends Dao {
24
25 public ItemEstrtIndResultLocalIettirlDao(HttpServletRequest request) {
26 super();
27 this.request = request;
28 }
29
30
31
32
33
34
35
36
37 public void salvarByLocal(Long codIettir, Long codExe, Long codLit, Double qtdePrevista) throws ECARException {
38
39 Transaction tx = null;
40
41 try {
42
43 tx = session.beginTransaction();
44
45 ItemEstrtIndResulIettr itemEstrtIndResulIettr = (ItemEstrtIndResulIettr) this.buscar(ItemEstrtIndResulIettr.class, codIettir);
46 ExercicioExe exercicioExe = (ExercicioExe) this.buscar(ExercicioExe.class, codExe);
47 LocalItemLit localItemLit = (LocalItemLit) this.buscar(LocalItemLit.class, codLit);
48
49 ItemEstrtIndResulLocalIettirl itemEstrtIndResulLocalIettirl = new ItemEstrtIndResulLocalIettirl();
50
51 itemEstrtIndResulLocalIettirl.setDataInclusaoIettirl(new Date());
52 itemEstrtIndResulLocalIettirl.setExercicioExe(exercicioExe);
53 itemEstrtIndResulLocalIettirl.setLocalItemLit(localItemLit);
54 itemEstrtIndResulLocalIettirl.setQtdPrevistaIettirl(qtdePrevista);
55 itemEstrtIndResulLocalIettirl.setIndAtivoIettirl("S");
56 itemEstrtIndResulLocalIettirl.setItemEsrtIndResulIettr(itemEstrtIndResulIettr);
57
58 session.save(itemEstrtIndResulLocalIettirl);
59 tx.commit();
60
61 } catch (Exception e) {
62 if (tx != null) {
63 tx.rollback();
64 }
65 this.logger.error(e);
66 throw new ECARException("erro.hibernateException");
67
68 }
69
70 }
71
72
73
74
75
76
77
78
79 public ItemEstrtIndResulLocalIettirl getQtdePrevistaByLocal(Long codIettir, Long codExe, Long codLitl) {
80
81 try {
82
83 StringBuilder sb = new StringBuilder();
84 sb.append("from ItemEstrtIndResulLocalIettirl bean");
85 sb.append(" where ");
86 sb.append(" bean.itemEsrtIndResulIettr.codIettir = :codIettir ");
87 sb.append(" and bean.exercicioExe.codExe = :codExe ");
88 sb.append(" and bean.localItemLit.codLit = :codLitl ");
89 sb.append(" and bean.indAtivoIettirl = :status ");
90
91 Query query = session.createQuery(sb.toString());
92
93 query.setLong("codIettir", codIettir.longValue());
94 query.setLong("codExe", codExe.longValue());
95 query.setLong("codLitl", codLitl.longValue());
96 query.setString("status", "S");
97
98
99 List retorno = query.list();
100
101 if (retorno != null && retorno instanceof List) {
102 for (Iterator iter = retorno.iterator(); iter.hasNext();) {
103 ItemEstrtIndResulLocalIettirl element = (ItemEstrtIndResulLocalIettirl) iter.next();
104 return element;
105 }
106
107
108 }
109
110 } catch (HibernateException e) {
111 e.printStackTrace(System.out);
112 this.logger.error(e);
113 return new ItemEstrtIndResulLocalIettirl();
114 }
115 return new ItemEstrtIndResulLocalIettirl();
116 }
117
118 private Double getQtdePrevistaBySomaSubLocal(Long codIettir, Long codLitl, Long exe) {
119
120 final LocalItemDao localDao = new LocalItemDao(request);
121 ArrayList<Long> listaSubLocalPK = new ArrayList<Long>();
122
123 try {
124 LocalItemLit localChave = (LocalItemLit) localDao.buscar(LocalItemLit.class, codLitl);
125
126 Set locais = localChave.getLocalItemHierarquiaLithsByCodLitPai();
127
128 for (Iterator iter = locais.iterator(); iter.hasNext();) {
129 LocalItemLit loc = (LocalItemLit) iter.next();
130 listaSubLocalPK.add(loc.getCodLit());
131 }
132
133 StringBuilder sb = new StringBuilder();
134 sb.append("select sum(bean.qtdPrevistaIettirl) ");
135 sb.append("from ItemEstrtIndResulLocalIettirl bean");
136 sb.append(" where ");
137 sb.append(" bean.itemEsrtIndResulIettr.codIettir = :codIettir ");
138 sb.append(" and bean.exercicioExe.codExe = :exercicio ");
139 sb.append(" and bean.localItemLit.codLit in (:listaLitl) ");
140 sb.append(" and bean.indAtivoIettirl = :status ");
141
142 Query query = session.createQuery(sb.toString());
143
144 query.setLong("codIettir", codIettir.longValue());
145 query.setLong("exercicio", exe.longValue());
146 query.setParameterList("listaLitl", listaSubLocalPK);
147 query.setString("status", "S");
148
149 Object retorno = query.uniqueResult();
150
151 if (retorno != null && retorno instanceof Double) {
152 return (Double) retorno;
153 }
154 else {
155 return 0D;
156 }
157
158 } catch (Exception e) {
159 logger.error(e);
160 return 0D;
161 }
162
163 }
164
165 public List<Double> getListQtdePrevistaByLocal(Long codIettir, Long codLitl) {
166
167 try {
168 final ExercicioDao exercicioDao = new ExercicioDao(request);
169 ArrayList<Double> valores = new ArrayList<Double>();
170
171 List listaExercicios = exercicioDao.getExercicioByPeriodicidade(2L);
172
173 for (Iterator iter = listaExercicios.iterator(); iter.hasNext();) {
174 ExercicioExe exe = (ExercicioExe) iter.next();
175 Double soma = getQtdePrevistaBySomaSubLocal(codIettir, codLitl, exe.getCodExe());
176 valores.add(soma);
177 }
178
179 return valores;
180
181 } catch (Exception e) {
182 logger.error(e);
183 return null;
184
185 }
186
187 }
188
189
190
191
192
193
194
195
196 public Double getSomaQtdePrevistaByLocal(Long codIettir, ArrayList<Long> exercicio, Long codLitl) {
197
198 try {
199 StringBuilder sb = new StringBuilder();
200 sb.append("select sum(bean.qtdPrevistaIettirl) ");
201 sb.append("from ItemEstrtIndResulLocalIettirl bean");
202 sb.append(" where ");
203 sb.append(" bean.itemEsrtIndResulIettr.codIettir = :codIettir ");
204 sb.append(" and bean.exercicioExe.codExe in (:listaExercicios) ");
205 sb.append(" and bean.localItemLit.codLit = :codLitl ");
206 sb.append(" and bean.indAtivoIettirl = :status ");
207
208 Query query = session.createQuery(sb.toString());
209
210 query.setLong("codIettir", codIettir.longValue());
211 query.setParameterList("listaExercicios", exercicio);
212 query.setLong("codLitl", codLitl.longValue());
213 query.setString("status", "S");
214
215 Object retorno = query.uniqueResult();
216
217 if (retorno != null && retorno instanceof Double) {
218 return (Double) retorno;
219 }
220
221 } catch (HibernateException e) {
222 e.printStackTrace(System.out);
223 this.logger.error(e);
224 return null;
225 }
226 return new Double(0D);
227
228 }
229
230
231
232
233
234 public int deleteBycodIettir(Long codIettir) {
235
236 try {
237
238 StringBuilder sb = new StringBuilder();
239 sb.append("delete from ItemEstrtIndResulLocalIettirl ");
240 sb.append(" where ");
241 sb.append(" itemEsrtIndResulIettr.codIettir = :codIettir ");
242
243 Query query = session.createQuery(sb.toString());
244
245 query.setLong("codIettir", codIettir.longValue());
246
247 int rowcount = query.executeUpdate();
248 return rowcount;
249
250 } catch (HibernateException e) {
251 e.printStackTrace(System.out);
252 this.logger.error(e);
253 return 0;
254 }
255
256 }
257
258 }