1
2
3
4 package ecar.dao;
5
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Date;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import javax.servlet.http.HttpServletRequest;
14
15 import org.apache.commons.fileupload.FileItem;
16 import org.hibernate.HibernateException;
17 import org.hibernate.Transaction;
18
19 import comum.database.Dao;
20 import comum.util.Data;
21 import comum.util.FileUpload;
22
23 import ecar.exception.ECARException;
24 import ecar.pojo.Aba;
25 import ecar.pojo.AcompReferenciaItemAri;
26 import ecar.pojo.AcompRelatorioArel;
27 import ecar.pojo.ConfiguracaoCfg;
28 import ecar.pojo.HistoricoIettupH;
29 import ecar.pojo.HistoricoMaster;
30 import ecar.pojo.HistoricoMotivo;
31 import ecar.pojo.ItemEstrUplCategIettuc;
32 import ecar.pojo.ItemEstrutUploadIettup;
33 import ecar.pojo.ItemEstruturaIett;
34 import ecar.pojo.UploadTipoArquivoUta;
35 import ecar.pojo.UsuarioUsu;
36
37
38
39
40 public class ItemEstruturaUploadDao extends Dao {
41
42 public ItemEstruturaUploadDao(HttpServletRequest request) {
43 super();
44 this.request = request;
45 }
46
47
48
49
50
51
52
53 public Collection getAtivos(ItemEstrUplCategIettuc categoria) throws ECARException {
54 try {
55 return this.getSession().createFilter(categoria.getItemEstrutUploadIettups(), "where this.indAtivoIettups = 'S'").list();
56 } catch (HibernateException e) {
57 this.logger.error(e);
58 throw new ECARException(e);
59 }
60 }
61
62
63
64
65
66
67
68
69 public void setItemEstruturaUpload(List campos, ItemEstrutUploadIettup upload, String pathRaiz, String pathRelativo) throws ECARException {
70 try {
71 ItemEstruturaIett itemEstrutura = (ItemEstruturaIett) this.buscar(ItemEstruturaIett.class, Long.valueOf(FileUpload.verificaValorCampo(campos, "codIett")));
72 upload.setItemEstruturaIett(itemEstrutura);
73
74 if (!"".equals(FileUpload.verificaValorCampo(campos, "cod")))
75 upload.setCodIettup(Long.valueOf(FileUpload.verificaValorCampo(campos, "cod")));
76
77 upload.setDescricaoIettup(FileUpload.verificaValorCampo(campos, "descricaoIettup"));
78
79 upload.setIndAtivoIettup("S");
80
81 if (!"".equals(FileUpload.verificaValorCampo(campos, "codIettuc")))
82 upload.setItemEstrUplCategIettuc((ItemEstrUplCategIettuc) this.buscar(ItemEstrUplCategIettuc.class, Long.valueOf(FileUpload.verificaValorCampo(campos, "codIettuc"))));
83 upload.getItemEstrUplCategIettuc().setIndAtivoIettuc("S");
84
85 if (!"".equals(FileUpload.verificaValorCampo(campos, "uploadTipoArquivoUta")))
86 upload.setUploadTipoArquivoUta((UploadTipoArquivoUta) this.buscar(UploadTipoArquivoUta.class, Long.valueOf(FileUpload.verificaValorCampo(campos, "uploadTipoArquivoUta"))));
87
88 if (!"".equals(FileUpload.verificaValorCampo(campos, "usuarioUsu")))
89 upload.setUsuarioUsu((UsuarioUsu) this.buscar(UsuarioUsu.class, Long.valueOf(FileUpload.verificaValorCampo(campos, "usuarioUsu"))));
90
91
92 Iterator it = campos.iterator();
93 String formato = "ddMMyyyyHHmmssSSS";
94 while (it.hasNext()) {
95 FileItem fileItem = (FileItem) it.next();
96 if (!fileItem.isFormField() && !"".equals(fileItem.getName())) {
97
98 if (upload.getArquivoIettup() != null)
99 FileUpload.apagarArquivo(upload.getArquivoIettup());
100 SimpleDateFormat formatter = new SimpleDateFormat(formato);
101 String dataGravacao = formatter.format(new Date());
102 upload.setArquivoIettup(FileUpload.getPathFisico("", pathRelativo, dataGravacao + " - " + FileUpload.getNomeArquivo(fileItem)));
103 upload.setNomeOriginalIettup(FileUpload.getNomeArquivo(fileItem));
104 upload.setTamanhoIettup(Long.valueOf(fileItem.getSize()));
105 FileUpload.salvarNoDisco(fileItem, FileUpload.getPathFisico(pathRaiz, pathRelativo, dataGravacao + " - " + FileUpload.getNomeArquivo(fileItem)));
106 }
107 }
108
109 } catch (Exception e) {
110 this.logger.error(e);
111 throw new ECARException("erro.exception");
112 }
113
114 }
115
116
117
118
119
120
121
122
123 public void setItemEstruturaUpload(List campos, ItemEstrutUploadIettup upload, String pathRaiz, String pathRelativo, AcompRelatorioArel acompRelatorioArel, ItemEstrUplCategIettuc itemEstrUplCategIettuc) throws ECARException {
124 upload.setAcompRelatorioArel(acompRelatorioArel);
125 upload.setItemEstrUplCategIettuc(itemEstrUplCategIettuc);
126 this.setItemEstruturaUpload(campos, upload, pathRaiz, pathRelativo);
127 }
128
129
130
131
132
133
134
135 public void excluir(String[] codigosParaExcluir, String pathRaiz, UsuarioUsu usuario) throws ECARException {
136
137
138
139
140
141
142
143
144
145 ItemEstruturaUploadCategoriaDao categoriaDao = new ItemEstruturaUploadCategoriaDao(request);
146
147 ItemEstrUplCategIettuc categoriaPesquisa = new ItemEstrUplCategIettuc();
148 categoriaPesquisa.setIndAtivoIettuc("S");
149
150 for (int i = 0; i < codigosParaExcluir.length; i++) {
151
152 ItemEstrutUploadIettup upload = (ItemEstrutUploadIettup) this.buscar(ItemEstrutUploadIettup.class, Long.valueOf(codigosParaExcluir[i]));
153 ItemEstrUplCategIettuc categoria = upload.getItemEstrUplCategIettuc();
154
155 ItemEstrutUploadIettup uploadPEsquisa = new ItemEstrutUploadIettup();
156 uploadPEsquisa.setItemEstrUplCategIettuc(categoria);
157 uploadPEsquisa.setIndAtivoIettup("S");
158
159 List itensAtivosCategoria = pesquisar(uploadPEsquisa, new String[] { "codIettup", "asc" });
160
161
162
163 boolean excluirCategoria = itensAtivosCategoria.size() > 1 ? false : true;
164
165 ConfiguracaoDao dao = new ConfiguracaoDao(request);
166 ConfiguracaoCfg config = dao.getConfiguracao();
167
168 HistoricoMaster historicoMaster = new HistoricoMaster();
169
170 if ("S".equals(config.getIndGerarHistoricoCfg())) {
171
172 historicoMaster.setDataHoraHistorico(new Date());
173 historicoMaster.setUsuManutencao(upload.getUsuarioUsuManutencao() == null ? usuario : upload.getUsuarioUsuManutencao());
174 historicoMaster.setCodReferenciaGeral(upload.getItemEstruturaIett().getCodIett());
175 historicoMaster.setHistoricoMotivo((HistoricoMotivo) super.buscar(HistoricoMotivo.class, Long.valueOf(26)));
176 session.save(historicoMaster);
177
178 HistoricoIettupH iettuph = new HistoricoIettupH();
179 ItemEstrutUploadIettup iettup = (ItemEstrutUploadIettup) super.buscar(ItemEstrutUploadIettup.class, upload.getCodIettup());
180
181 iettuph.setArquivoIettup(iettup.getArquivoIettup());
182 iettuph.setCodIettup(iettup.getCodIettup());
183 iettuph.setCodIettupH(iettup.getCodIettup());
184 iettuph.setDataInclusaoIettup(iettup.getDataInclusaoIettup());
185 iettuph.setDescricaoIettup(iettup.getDescricaoIettup());
186 iettuph.setHistoricoMaster(historicoMaster);
187 iettuph.setIndAtivoIettup(iettup.getIndAtivoIettup());
188 iettuph.setItemEstrUplCategIettuc(iettup.getItemEstrUplCategIettuc());
189 iettuph.setItemEstruturaIett(iettup.getItemEstruturaIett());
190
191 iettuph.setNomeOriginalIettup(iettup.getNomeOriginalIettup());
192 iettuph.setTamanhoIettup(iettup.getTamanhoIettup());
193 iettuph.setUploadTipoArquivoUta(iettup.getUploadTipoArquivoUta());
194 iettuph.setUsuarioUsu(iettup.getUsuarioUsu());
195 iettuph.setUsuManutencao((iettup.getUsuarioUsuManutencao() == null ? usuario : iettup.getUsuarioUsuManutencao()));
196
197 session.save(iettuph);
198 }
199
200 this.excluir(upload, pathRaiz, usuario);
201
202
203
204
205 if (excluirCategoria) {
206 categoriaDao.excluir(categoria, pathRaiz);
207 }
208
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 }
245
246
247
248
249
250
251 public void excluir(ItemEstrutUploadIettup upload, String pathRaiz, UsuarioUsu usuario) throws ECARException {
252 try {
253
254 upload.setUsuarioUsuManutencao(usuario);
255 upload.setIndAtivoIettup("N");
256 upload.setIndExclusaoPosHistorico(Boolean.TRUE);
257
258 if (upload.getArquivoIettup() != null) {
259 if (FileUpload.apagarArquivo(FileUpload.getPathFisico(pathRaiz, upload.getArquivoIettup(), ""))) {
260 super.alterar(upload);
261 }
262 else {
263 throw new ECARException("erro.excluirArquivo");
264 }
265 }
266 else
267 super.alterar(upload);
268 } catch (Exception e) {
269 this.logger.error(e);
270 throw new ECARException("erro.exception");
271 }
272 }
273
274
275
276
277
278
279 public void salvar(ItemEstrutUploadIettup upload, UsuarioUsu usuario) throws ECARException {
280 upload.setUsuarioUsu(usuario);
281 upload.setDataInclusaoIettup(Data.getDataAtual());
282 super.salvar(upload);
283 }
284
285 public void salvar(ItemEstrutUploadIettup upload, UsuarioUsu usuario, List filhos) throws ECARException {
286 upload.setUsuarioUsu(usuario);
287 upload.setDataInclusaoIettup(Data.getDataAtual());
288 super.salvar(upload, filhos);
289 }
290
291 public void alterar(ItemEstrutUploadIettup upload, UsuarioUsu usuario) throws ECARException {
292 upload.setUsuarioUsuManutencao(usuario);
293
294 ConfiguracaoDao dao = new ConfiguracaoDao(request);
295 ConfiguracaoCfg config = dao.getConfiguracao();
296
297 HistoricoMaster historicoMaster = new HistoricoMaster();
298
299 if ("S".equals(config.getIndGerarHistoricoCfg())) {
300
301 historicoMaster.setDataHoraHistorico(new Date());
302 historicoMaster.setUsuManutencao(upload.getUsuarioUsuManutencao());
303 historicoMaster.setCodReferenciaGeral(upload.getItemEstruturaIett().getCodIett());
304 historicoMaster.setHistoricoMotivo((HistoricoMotivo) super.buscar(HistoricoMotivo.class, Long.valueOf(25)));
305 session.save(historicoMaster);
306
307 HistoricoIettupH iettuph = new HistoricoIettupH();
308 ItemEstrutUploadIettup iettup = (ItemEstrutUploadIettup) super.buscar(ItemEstrutUploadIettup.class, upload.getCodIettup());
309
310 iettuph.setArquivoIettup(iettup.getArquivoIettup());
311 iettuph.setCodIettup(iettup.getCodIettup());
312 iettuph.setCodIettupH(iettup.getCodIettup());
313 iettuph.setDataInclusaoIettup(iettup.getDataInclusaoIettup());
314 iettuph.setDescricaoIettup(iettup.getDescricaoIettup());
315 iettuph.setHistoricoMaster(historicoMaster);
316 iettuph.setIndAtivoIettup(iettup.getIndAtivoIettup());
317 iettuph.setItemEstrUplCategIettuc(iettup.getItemEstrUplCategIettuc());
318 iettuph.setItemEstruturaIett(iettup.getItemEstruturaIett());
319
320 iettuph.setNomeOriginalIettup(iettup.getNomeOriginalIettup());
321 iettuph.setTamanhoIettup(iettup.getTamanhoIettup());
322 iettuph.setUploadTipoArquivoUta(iettup.getUploadTipoArquivoUta());
323 iettuph.setUsuarioUsu(iettup.getUsuarioUsu());
324 iettuph.setUsuManutencao(iettup.getUsuarioUsuManutencao());
325
326 session.save(iettuph);
327 }
328
329 super.alterar(upload);
330 }
331 }