fixes bug #7384
git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/Integracion/IntegracaoReceitaDespesa/trunk/IntegracaoReceitaDespesa@55698 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
6b4c9fdf43
commit
8f9b14c2de
|
@ -0,0 +1,193 @@
|
|||
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.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
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.log4j.Logger;
|
||||
|
||||
import com.rjconsultores.integracaoreceitadespesa.dao.Totalbus;
|
||||
import com.rjconsultores.integracaoreceitadespesa.entidades.DespesaReceita;
|
||||
|
||||
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 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) throws IllegalArgumentException{
|
||||
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);
|
||||
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) + "" + 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);
|
||||
|
||||
log.debug("gerando arquivo...");
|
||||
Arquivo.GravaArquivo(file.getAbsolutePath(), totalbus.getDespesasReceitas(puntoVentaId, empresaId, cal.getTime()));
|
||||
// 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 (Exception e) {
|
||||
log.error("", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -407,6 +407,57 @@ public class Totalbus {
|
|||
|
||||
}
|
||||
|
||||
public Boolean isConstanteBloqueioMenorQueData(Integer empresaId, Date data){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Select valorconstante from constante where nombconstante = 'DATA_LIMITE_EDICAO_DEPOSITO_" + empresaId + "'");
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
Boolean result = null;
|
||||
try{
|
||||
pstmt = getConnection().prepareStatement(sb.toString());
|
||||
rs = pstmt.executeQuery();
|
||||
if (rs.next()){
|
||||
Date rsDate = new SimpleDateFormat("dd/MM/yyyy").parse(rs.getString(1));
|
||||
result = !rsDate.after(data);
|
||||
} else { // se não existir a constante, retorno true para que ela seja criada na função updateDataBloqueio
|
||||
result = true;
|
||||
}
|
||||
} catch (Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
try { rs.close(); } catch (Exception ignore) { log.error(ignore.getMessage(), ignore); }
|
||||
try { pstmt.close(); } catch (Exception ignore) { log.error(ignore.getMessage(), ignore); }
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public void updateDataBloqueio(Integer empresaId, Date data){
|
||||
StringBuilder sb = new StringBuilder();
|
||||
PreparedStatement pstmt = null;
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
|
||||
SimpleDateFormat sdfConstante = new SimpleDateFormat("yyyy/MM/dd");
|
||||
try{
|
||||
if (!getConnection().
|
||||
prepareStatement("Select valorconstante from constante where nombconstante = 'DATA_LIMITE_EDICAO_DEPOSITO_" + empresaId + "'").
|
||||
executeQuery().next()){
|
||||
sb.append("insert into constante values (constante_seq.nextval, 'DATA_LIMITE_EDICAO_DEPOSITO_" + empresaId + "', 'DATA_LIMITE_EDICAO_DEPOSITO_" + empresaId + "',1,"+
|
||||
"'" + sdf.format(data) + "', 1, 1, " + sdfConstante.format(new Date()) +", 1)");
|
||||
pstmt = getConnection().prepareStatement(sb.toString());
|
||||
} else {
|
||||
sb.append("update constante set valorconstante = :valor where nombconstante = 'DATA_LIMITE_EDICAO_DEPOSITO_" + empresaId + "'");
|
||||
pstmt = getConnection().prepareStatement(sb.toString());
|
||||
pstmt.setString(1, sdf.format(data));
|
||||
}
|
||||
|
||||
pstmt.executeUpdate();
|
||||
} catch (Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
} finally {
|
||||
try { pstmt.close(); } catch (Exception ignore) { log.error(ignore.getMessage(), ignore); }
|
||||
}
|
||||
}
|
||||
|
||||
private Date getData(){
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(2015, 10, 13);
|
||||
|
|
Loading…
Reference in New Issue