268 lines
10 KiB
Java
268 lines
10 KiB
Java
package com.rjconsultores.integracaoreceitadespesa;
|
|
|
|
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.nio.channels.FileChannel;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Properties;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.log4j.Logger;
|
|
|
|
import com.rjconsultores.integracaoreceitadespesa.dao.Totalbus;
|
|
|
|
public class BGMApplication {
|
|
private static BGMApplication instance = null;
|
|
|
|
private static final Logger log = Logger.getLogger(BGMApplication.class);
|
|
|
|
private static final int TAMANHO_BUFFER = 4096; // 4kb
|
|
|
|
private boolean isReceitasDespesasComporte;
|
|
|
|
private boolean isCodReceitaFixoBgm;
|
|
|
|
private boolean isReceitasDespesasOuroPrata;
|
|
|
|
private BGMApplication(){
|
|
|
|
}
|
|
|
|
public Connection getConnection(){
|
|
try {
|
|
Properties props = BGMApplication.getInstance().getApplicationProperties();
|
|
|
|
String DRIVER = "oracle.jdbc.driver.OracleDriver";
|
|
|
|
Class.forName(DRIVER);
|
|
Connection conn = DriverManager.getConnection(
|
|
props.getProperty("url"),
|
|
props.getProperty("username").trim(),
|
|
props.getProperty("password").trim());
|
|
|
|
return conn;
|
|
|
|
} catch (Exception e){
|
|
log.error("", e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static BGMApplication getInstance(){
|
|
if (instance == null){
|
|
instance = new BGMApplication();
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public Properties getApplicationProperties(){
|
|
Properties props = new Properties();
|
|
try {
|
|
props.load(new FileInputStream("db.properties"));
|
|
} catch (IOException e) {
|
|
log.error("", e);
|
|
}
|
|
return props;
|
|
}
|
|
|
|
public String executaExportacao(Date dataInicio, Date dataFinal, String diretorio, Integer empresaId, Integer puntoVentaId, Connection con,String pathGravacaoExternaArquivos, boolean incluiTipoPagamentoTurismoBGM, boolean isReceitasDespesasComporte,boolean isCodReceitaFixoBgm, boolean isReceitasDespesasOuroPrata, boolean layoutNovo) throws IllegalArgumentException, IntegracaoReceitaDespesaException{
|
|
this.isCodReceitaFixoBgm = isCodReceitaFixoBgm;
|
|
this.isReceitasDespesasOuroPrata = isReceitasDespesasOuroPrata;
|
|
return executaExportacao(dataInicio, dataFinal, diretorio, empresaId, puntoVentaId, con, pathGravacaoExternaArquivos, incluiTipoPagamentoTurismoBGM, layoutNovo);
|
|
}
|
|
|
|
public String executaExportacao(Date dataInicio, Date dataFinal, String diretorio, Integer empresaId, Integer puntoVentaId, Connection con,String pathGravacaoExternaArquivos, boolean incluiTipoPagamentoTurismoBGM, boolean isReceitasDespesasComporte,boolean isCodReceitaFixoBgm, boolean isReceitasDespesasOuroPrata) throws IllegalArgumentException, IntegracaoReceitaDespesaException{
|
|
this.isCodReceitaFixoBgm = isCodReceitaFixoBgm;
|
|
this.isReceitasDespesasOuroPrata = isReceitasDespesasOuroPrata;
|
|
return executaExportacao(dataInicio, dataFinal, diretorio, empresaId, puntoVentaId, con, pathGravacaoExternaArquivos, incluiTipoPagamentoTurismoBGM, false);
|
|
}
|
|
|
|
public String executaExportacao(Date dataInicio, Date dataFinal, String diretorio, Integer empresaId, Integer puntoVentaId, Connection con,String pathGravacaoExternaArquivos, boolean incluiTipoPagamentoTurismoBGM, boolean isReceitasDespesasComporte, boolean isReceitasDespesasOuroPrata) throws IllegalArgumentException, IntegracaoReceitaDespesaException{
|
|
this.isReceitasDespesasComporte = isReceitasDespesasComporte;
|
|
return executaExportacao(dataInicio, dataFinal, diretorio, empresaId, puntoVentaId, con, pathGravacaoExternaArquivos, incluiTipoPagamentoTurismoBGM, false);
|
|
}
|
|
|
|
public String executaExportacao(Date dataInicio, Date dataFinal, String diretorio, Integer empresaId, Integer puntoVentaId, Connection con,String pathGravacaoExternaArquivos, boolean incluiTipoPagamentoTurismoBGM, boolean layoutNovo) throws IllegalArgumentException, IntegracaoReceitaDespesaException{
|
|
if (empresaId == null){
|
|
throw new IllegalArgumentException("o parâmetro empesaId é obrigatório!");
|
|
}
|
|
if (puntoVentaId == null){
|
|
throw new IllegalArgumentException("o parâmetro puntoVentaId é obrigatório!");
|
|
}
|
|
excluirArquivosZip(diretorio);
|
|
Totalbus totalbus = new Totalbus(con, isReceitasDespesasComporte, isCodReceitaFixoBgm, isReceitasDespesasOuroPrata, layoutNovo );
|
|
if (totalbus.isConstanteBloqueioMenorQueData(empresaId, dataFinal)){
|
|
totalbus.updateDataBloqueio(empresaId, dataFinal);
|
|
}
|
|
List<File> files = new ArrayList<File>();
|
|
Calendar cal = Calendar.getInstance();
|
|
// Seta primeiro dia da iteração
|
|
cal.setTime(dataInicio);
|
|
Calendar calFinal = Calendar.getInstance();
|
|
calFinal.setTime(dataFinal);
|
|
|
|
Calendar cf = Calendar.getInstance();
|
|
cf.setTime(dataFinal);
|
|
cf.add(Calendar.DATE, 1);
|
|
try{
|
|
|
|
while (cal.before(cf)) {
|
|
String fileName = diretorio + File.separator + "BGM_" + empresaId + "-" + cal.get(Calendar.YEAR)
|
|
+ "" + StringUtils.leftPad(Integer.toString(cal.get(Calendar.MONTH) + 1), 2, "0") + ""
|
|
+ StringUtils.leftPad(Integer.toString(cal.get(Calendar.DAY_OF_MONTH)), 2, "0") + ".txt";
|
|
File file = new File(fileName);
|
|
|
|
log.debug("gerando arquivo...");
|
|
Arquivo.GravaArquivo(file.getAbsolutePath(), totalbus.getDespesasReceitas(puntoVentaId, empresaId, cal.getTime(), incluiTipoPagamentoTurismoBGM, layoutNovo));
|
|
|
|
this.copiarArquivoExternamente(file, pathGravacaoExternaArquivos);
|
|
|
|
// adiciona um dia para iteração
|
|
cal.add(Calendar.DAY_OF_MONTH, 1);
|
|
files.add(file);
|
|
}
|
|
|
|
String fileZip = diretorio + File.separator + "ArquivosBGM.zip";
|
|
log.debug("nome arquivo: " + fileZip);
|
|
|
|
compactarArquivos(files, fileZip);
|
|
return fileZip;
|
|
} catch (IntegracaoReceitaDespesaException e){
|
|
throw e;
|
|
} catch (Exception e) {
|
|
log.error("", e);
|
|
return "";
|
|
} finally{
|
|
try {
|
|
con.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void copiarArquivoExternamente(File arquivoBGM,String pathGravacaoExternaArquivos){
|
|
if (pathGravacaoExternaArquivos == null){
|
|
log.info("Path externo para gravação de arquivos Não configurado");
|
|
|
|
return;
|
|
}
|
|
|
|
if (!pathGravacaoExternaArquivos.endsWith("/")){
|
|
pathGravacaoExternaArquivos = pathGravacaoExternaArquivos +"/";
|
|
}
|
|
|
|
File destino = new File(pathGravacaoExternaArquivos + arquivoBGM.getName());
|
|
|
|
try {
|
|
this.copyFileUsingChannel(arquivoBGM, destino);
|
|
} catch (IOException e) {
|
|
log.error(String.format("Erro ao gravar o arquivo %s no diretorio %s", arquivoBGM.getName(),pathGravacaoExternaArquivos) ,e);
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("resource")
|
|
private void copyFileUsingChannel(File source, File dest) throws IOException {
|
|
|
|
FileChannel sourceChannel = null;
|
|
FileChannel destChannel = null;
|
|
try {
|
|
sourceChannel = new FileInputStream(source).getChannel();
|
|
destChannel = new FileOutputStream(dest).getChannel();
|
|
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
|
|
}finally{
|
|
sourceChannel.close();
|
|
destChannel.close();
|
|
}
|
|
}
|
|
|
|
private void excluirArquivosZip(String diretorio){
|
|
File pasta = new File(diretorio);
|
|
File[] arquivos = pasta.listFiles();
|
|
|
|
for(File arquivo : arquivos) {
|
|
if(arquivo.getName().endsWith("zip")) {
|
|
arquivo.delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
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<File> 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();
|
|
}
|
|
}
|
|
|
|
}
|