1
2
3
4 package ecar.dao;
5
6 import java.util.ArrayList;
7 import java.util.Iterator;
8 import java.util.List;
9
10 import javax.servlet.http.HttpServletRequest;
11
12 import org.hibernate.HibernateException;
13 import org.hibernate.Query;
14 import org.hibernate.Transaction;
15
16 import comum.database.Dao;
17 import comum.util.Data;
18
19 import ecar.exception.ECARException;
20 import ecar.pojo.AcompRealFisicoArf;
21 import ecar.pojo.AcompRealFisicoLocalArfl;
22 import ecar.pojo.LocalItemLit;
23
24
25
26
27 public class AcompRealFisicoLocalDao extends Dao {
28
29
30
31 public AcompRealFisicoLocalDao(HttpServletRequest request) {
32 super();
33 this.request = request;
34 }
35
36
37
38
39
40
41
42 public AcompRealFisicoLocalArfl buscar(Long codArfl) throws ECARException {
43 return (AcompRealFisicoLocalArfl) super.buscar(AcompRealFisicoLocalArfl.class, codArfl);
44 }
45
46
47
48
49
50
51
52 public void excluir(List listaCodArfl, Long codArf) throws ECARException {
53 Transaction tx = null;
54
55 try {
56 if (listaCodArfl == null || listaCodArfl.isEmpty()) {
57 return;
58 }
59 ArrayList objetos = new ArrayList();
60 super.inicializarLogBean();
61
62 AcompRealFisicoDao acompRealFisicoDao = new AcompRealFisicoDao(request);
63
64 tx = session.beginTransaction();
65
66 AcompRealFisicoArf arf = (AcompRealFisicoArf) acompRealFisicoDao.buscar(codArf);
67
68 double qtdeRealizadaArf = 0;
69
70 if (arf.getQtdRealizadaArf() != null) {
71 qtdeRealizadaArf = arf.getQtdRealizadaArf().doubleValue();
72 }
73
74 for (Iterator it = listaCodArfl.iterator(); it.hasNext();) {
75 AcompRealFisicoLocalArfl arfl = (AcompRealFisicoLocalArfl) this.buscar(Long.valueOf(it.next().toString()));
76
77 qtdeRealizadaArf = qtdeRealizadaArf - arfl.getQuantidadeArfl().doubleValue();
78 arf.setQtdRealizadaArf(Double.valueOf(qtdeRealizadaArf));
79
80 session.delete(arfl);
81 objetos.add(arfl);
82
83 session.update(arf);
84 objetos.add(arf);
85 }
86
87 tx.commit();
88
89 if (super.logBean != null) {
90 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
91 super.logBean.setOperacao("ALT_EXC");
92
93 for (Iterator itObj = objetos.iterator(); itObj.hasNext();) {
94 super.logBean.setObj(itObj.next());
95 super.loggerAuditoria.info(logBean.toString());
96 }
97 }
98 } catch (HibernateException e) {
99 if (tx != null)
100 try {
101 tx.rollback();
102 } catch (HibernateException r) {
103 this.logger.error(r);
104 throw new ECARException("erro.hibernateException");
105 }
106 this.logger.error(e);
107 throw new ECARException("erro.hibernateException");
108 }
109 }
110
111
112
113
114
115
116
117 public void salvar(AcompRealFisicoLocalArfl arfl) throws ECARException {
118 Transaction tx = null;
119
120 try {
121 ArrayList objetos = new ArrayList();
122 super.inicializarLogBean();
123
124 AcompRealFisicoDao acompRealFisicoDao = new AcompRealFisicoDao(request);
125 tx = session.beginTransaction();
126 AcompRealFisicoArf arf = (AcompRealFisicoArf) acompRealFisicoDao.buscar(arfl.getAcompRealFisicoArf().getCodArf());
127 double qtdeRealizadaArf = 0;
128 if (arf.getQtdRealizadaArf() != null) {
129 qtdeRealizadaArf = arf.getQtdRealizadaArf().doubleValue();
130 }
131 arf.setQtdRealizadaArf(new Double(qtdeRealizadaArf + arfl.getQuantidadeArfl().doubleValue()));
132 session.save(arfl);
133 objetos.add(arfl);
134 session.update(arf);
135 objetos.add(arf);
136 tx.commit();
137
138 if (super.logBean != null) {
139 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
140 super.logBean.setOperacao("INC_ALT");
141
142 for (Iterator itObj = objetos.iterator(); itObj.hasNext();) {
143 super.logBean.setObj(itObj.next());
144 super.loggerAuditoria.info(logBean.toString());
145 }
146 }
147 } catch (HibernateException e) {
148 if (tx != null)
149 try {
150 tx.rollback();
151 } catch (HibernateException r) {
152 this.logger.error(r);
153 throw new ECARException("erro.hibernateException");
154 }
155 this.logger.error(e);
156 throw new ECARException("erro.hibernateException");
157 }
158 }
159
160
161
162
163
164
165
166 public void salvar(List listaArfl, AcompRealFisicoArf arf) throws ECARException {
167 Transaction tx = null;
168
169 try {
170 if (listaArfl == null || listaArfl.isEmpty()) {
171 return;
172 }
173 ArrayList objetos = new ArrayList();
174 super.inicializarLogBean();
175
176 AcompRealFisicoDao acompRealFisicoDao = new AcompRealFisicoDao(request);
177
178
179 tx = session.beginTransaction();
180
181
182
183 double qtdeRealizadaArf = 0;
184
185
186 for (Iterator it = listaArfl.iterator(); it.hasNext();) {
187 AcompRealFisicoLocalArfl arfl = (AcompRealFisicoLocalArfl) it.next();
188 qtdeRealizadaArf = qtdeRealizadaArf + arfl.getQuantidadeArfl().doubleValue();
189
190 session.save(arfl);
191 objetos.add(arfl);
192 }
193
194
195 arf.setQtdRealizadaArf(Double.valueOf(qtdeRealizadaArf));
196 session.update(arf);
197 objetos.add(arf);
198
199 if (super.logBean != null) {
200 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
201 super.logBean.setOperacao("ALT_EXC");
202
203 for (Iterator itObj = objetos.iterator(); itObj.hasNext();) {
204 super.logBean.setObj(itObj.next());
205 super.loggerAuditoria.info(logBean.toString());
206 }
207 }
208
209
210 tx.commit();
211
212 } catch (HibernateException e) {
213 if (tx != null)
214 try {
215 tx.rollback();
216 } catch (HibernateException r) {
217 this.logger.error(r);
218 throw new ECARException("erro.hibernateException");
219 }
220 this.logger.error(e);
221 throw new ECARException("erro.hibernateException");
222 }
223 }
224
225
226
227
228
229
230
231
232
233
234 public List getAcompRealFisicoLocalByArf(AcompRealFisicoArf arf) {
235 List retorno = new ArrayList();
236 Query q = this.session.createQuery("select arfl from AcompRealFisicoLocalArfl arfl where arfl.acompRealFisicoArf.codArf = :codArf");
237 q.setLong("codArf", arf.getCodArf().longValue());
238 retorno = q.list();
239 return retorno != null ? retorno : new ArrayList();
240 }
241
242
243
244
245
246
247
248
249
250
251 public AcompRealFisicoLocalArfl getAcompRealFisicoLocalByLocal(AcompRealFisicoArf arf, LocalItemLit lit) {
252
253 try {
254
255 StringBuilder sb = new StringBuilder();
256 sb.append("from AcompRealFisicoLocalArfl bean");
257 sb.append(" where ");
258 sb.append(" bean.acompRealFisicoArf.codArf = :codArf ");
259 sb.append(" and bean.localItemLit.codLit = :codLitl ");
260
261 Query query = session.createQuery(sb.toString());
262
263 query.setLong("codArf", arf.getCodArf().longValue());
264 query.setLong("codLitl", lit.getCodLit().longValue());
265
266 Object retorno = query.uniqueResult();
267
268 if (retorno != null && retorno instanceof AcompRealFisicoLocalArfl) {
269 return (AcompRealFisicoLocalArfl) retorno;
270 }
271
272 } catch (HibernateException e) {
273 e.printStackTrace(System.out);
274 this.logger.error(e);
275 AcompRealFisicoLocalArfl arfl = new AcompRealFisicoLocalArfl();
276 arfl.setLocalItemLit(lit);
277 arfl.setQuantidadeArfl(0.0);
278 arfl.setAcompRealFisicoArf(arf);
279
280 return arfl;
281 }
282
283 AcompRealFisicoLocalArfl arfl = new AcompRealFisicoLocalArfl();
284 arfl.setLocalItemLit(lit);
285 arfl.setQuantidadeArfl(0.0);
286 arfl.setAcompRealFisicoArf(arf);
287
288 return arfl;
289
290 }
291 }