diff --git a/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioArquivoBGMController.java b/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioArquivoBGMController.java index c0de4619d..55c779271 100644 --- a/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioArquivoBGMController.java +++ b/src/java/com/rjconsultores/ventaboletos/web/gui/controladores/relatorios/RelatorioArquivoBGMController.java @@ -1,17 +1,8 @@ package com.rjconsultores.ventaboletos.web.gui.controladores.relatorios; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; import java.sql.Connection; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; import javax.activation.MimetypesFileTypeMap; import javax.sql.DataSource; @@ -27,9 +18,7 @@ import org.zkoss.zk.ui.event.Event; import org.zkoss.zul.Datebox; import org.zkoss.zul.Filedownload; -import com.rjconsultores.integracaoreceitadespesa.Arquivo; -import com.rjconsultores.integracaoreceitadespesa.dao.Totalbus; -import com.rjconsultores.integracaoreceitadespesa.entidades.DespesaReceita; +import com.rjconsultores.integracaoreceitadespesa.BGMApplication; import com.rjconsultores.ventaboletos.entidad.Empresa; import com.rjconsultores.ventaboletos.entidad.PuntoVenta; import com.rjconsultores.ventaboletos.web.utilerias.MyComboboxEmpresa; @@ -42,8 +31,7 @@ public class RelatorioArquivoBGMController extends MyGenericForwardComposer { private static final long serialVersionUID = 1L; private static Logger log = Logger.getLogger(RelatorioArquivoBGMController.class); - private static final int TAMANHO_BUFFER = 4096; // 4kb - + private MyComboboxEmpresa cmbEmpresa; private MyComboboxPuntoVenta cmbPuntoVenta; private Datebox datInicial; @@ -65,6 +53,13 @@ public class RelatorioArquivoBGMController extends MyGenericForwardComposer { Messagebox.OK, Messagebox.INFORMATION); return; } + + if (cmbEmpresa.getSelectedItem() == null){ + Messagebox.show(Labels.getLabel("relatorioArquivoBGMController.msg.erro.empresa"), + Labels.getLabel("relatorioArquivoBGMController.window.title"), + Messagebox.OK, Messagebox.INFORMATION); + return; + } executarRelatorio(); } @@ -72,47 +67,15 @@ public class RelatorioArquivoBGMController extends MyGenericForwardComposer { * @throws Exception * */ - private void executarRelatorio() throws Exception { - excluirArquivosZip(); - List files = new ArrayList(); - Calendar cal = Calendar.getInstance(); - // Seta primeiro dia da iteração - cal.setTime(datInicial.getValue()); - Calendar calFinal = Calendar.getInstance(); - calFinal.setTime(datFinal.getValue()); + private void executarRelatorio() throws Exception { + Integer puntoVentaId = ((PuntoVenta)cmbPuntoVenta.getSelectedItem().getValue()).getPuntoventaId(); + Integer empresaId = ((Empresa)cmbEmpresa.getSelectedItem().getValue()).getEmpresaId(); + Connection con = dataSource.getConnection(); + String fileZip = BGMApplication.getInstance().executaExportacao(datInicial.getValue(), datFinal.getValue(), + System.getProperty("jboss.server.log.dir") , empresaId, puntoVentaId, con); - Calendar cf = Calendar.getInstance(); - cf.setTime(datFinal.getValue()); - cf.add(Calendar.DATE, 1); - // seta dia e mês finais para limite da iteração - int diaFinal = calFinal.get(Calendar.DAY_OF_MONTH); - int mesFinal = calFinal.get(Calendar.MONTH); - Connection con = null; try{ - con = dataSource.getConnection(); - - while (cal.before(cf)) { - String fileName = System.getProperty("jboss.server.log.dir") + File.separator + "BGM_" + cmbEmpresa.getSelectedItem() == null ? "" : ((Empresa)cmbEmpresa.getSelectedItem().getValue()).getEmpresaId() + "-" + cal.get(Calendar.YEAR) + "" + DespesaReceita.lpad(Integer.toString(cal.get(Calendar.MONTH) + 1), "0", 2) + "" + DespesaReceita.lpad(Integer.toString(cal.get(Calendar.DAY_OF_MONTH)), "0", 2) + ".txt"; - File file = new File(fileName); - - Totalbus totalbus = new Totalbus(con); - - log.debug("gerando arquivo..."); - Arquivo.GravaArquivo(file.getAbsolutePath(), totalbus.getDespesasReceitas( - cmbPuntoVenta.getSelectedItem() == null ? null : ((PuntoVenta)cmbPuntoVenta.getSelectedItem().getValue()).getPuntoventaId(), - cmbEmpresa.getSelectedItem() == null ? null : ((Empresa)cmbEmpresa.getSelectedItem().getValue()).getEmpresaId(), - cal.getTime())); - // adiciona um dia para iteração - cal.add(Calendar.DAY_OF_MONTH, 1); - files.add(file); - } - - - String fileZip = System.getProperty("jboss.server.log.dir") + File.separator + "ArquivosBGM.zip"; - log.debug("nome arquivo: " + fileZip); - FileInputStream inputStream; - - compactarArquivos(files, fileZip); + FileInputStream inputStream; File filez = new File(fileZip); log.info(filez.exists()); if (filez.exists()) { @@ -127,81 +90,7 @@ public class RelatorioArquivoBGMController extends MyGenericForwardComposer { } } - public static void compactarArquivo(String arqSaida,String arqEntrada) throws IOException{ - int cont; - byte[] dados = new byte[TAMANHO_BUFFER]; - BufferedInputStream origem = null; - FileInputStream streamDeEntrada = null; - FileOutputStream destino = null; - ZipOutputStream saida = null; - ZipEntry entry = null; - try { - destino = new FileOutputStream(new File(arqSaida)); - saida = new ZipOutputStream(new BufferedOutputStream(destino)); - File file = new File(arqEntrada); - streamDeEntrada = new FileInputStream(file); - origem = new BufferedInputStream(streamDeEntrada, TAMANHO_BUFFER); - entry = new ZipEntry(file.getName()); - saida.putNextEntry(entry); - while((cont = origem.read(dados, 0, TAMANHO_BUFFER)) != -1) { - saida.write(dados, 0, cont); - } - origem.close(); - saida.close(); - } catch(IOException e) { - log.error(e.getMessage()); - throw new IOException(e.getMessage()); - } - } - private void compactarArquivos(List arquivosEOuPastas, String arquivoDeSaida){ - System.out.println(arquivoDeSaida); - log.debug(arquivoDeSaida); - - // Create a buffer for reading the files - byte[] buf = new byte[1024]; - - try { - // Create the ZIP file - ZipOutputStream out = new ZipOutputStream(new FileOutputStream(arquivoDeSaida)); - - // Compress the files - for (File f : arquivosEOuPastas) { - FileInputStream in = new FileInputStream(f.getPath()); - - // Add ZIP entry to output stream. - out.putNextEntry(new ZipEntry(f.getName())); - System.out.println(f.getPath()); - // Transfer bytes from the file to the ZIP file - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - - // Complete the entry - out.closeEntry(); - in.close(); - f.delete(); - } - - // Complete the ZIP file - out.close(); - } catch (IOException e) { - log.error(e.getMessage(),e); - e.printStackTrace(); - } - } - - private void excluirArquivosZip(){ - File pasta = new File(System.getProperty("jboss.server.log.dir")); - File[] arquivos = pasta.listFiles(); - - for(File arquivo : arquivos) { - if(arquivo.getName().endsWith("zip")) { - arquivo.delete(); - } - } - } public MyComboboxEmpresa getCmbEmpresa() { return cmbEmpresa; diff --git a/web/WEB-INF/i3-label_es_MX.label b/web/WEB-INF/i3-label_es_MX.label index 6fbc99f3e..e7cb0d7d6 100644 --- a/web/WEB-INF/i3-label_es_MX.label +++ b/web/WEB-INF/i3-label_es_MX.label @@ -6337,6 +6337,7 @@ relatorioArquivoBGMController.lbDataFin.value = Data Final relatorioArquivoBGMController.lbEmpresa.value = Empresa relatorioArquivoBGMController.lbPuntoVenta.value = Agência relatorioArquivoBGMController.msg.erro.puntoventa = O Campo Agência é obrigatório! +relatorioArquivoBGMController.msg.erro.empresa = O Campo Empresa é obrigatório! # Recebimento Malote recebimentoMaloteController.window.title = Recebimento Malote diff --git a/web/WEB-INF/i3-label_pt_BR.label b/web/WEB-INF/i3-label_pt_BR.label index 9bcfaec56..7fe77ace0 100644 --- a/web/WEB-INF/i3-label_pt_BR.label +++ b/web/WEB-INF/i3-label_pt_BR.label @@ -6486,6 +6486,7 @@ relatorioArquivoBGMController.lbDataFin.value = Data Final relatorioArquivoBGMController.lbEmpresa.value = Empresa relatorioArquivoBGMController.lbPuntoVenta.value = Agência relatorioArquivoBGMController.msg.erro.puntoventa = O Campo Agência é obrigatório! +relatorioArquivoBGMController.msg.erro.empresa = O Campo Empresa é obrigatório! # Recebimento Malote recebimentoMaloteController.window.title = Recebimento Malote