419 lines
11 KiB
Java
419 lines
11 KiB
Java
package com.rjconsultores.ventaboletos.layouts;
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.jrimum.texgit.FlatFile;
|
|
import org.jrimum.texgit.Record;
|
|
import org.jrimum.texgit.Texgit;
|
|
import org.jrimum.utilix.Collections;
|
|
import org.jrimum.utilix.Objects;
|
|
import org.jrimum.utilix.text.Strings;
|
|
|
|
import com.rjconsultores.ventaboletos.ArquivoRemessa;
|
|
import com.rjconsultores.ventaboletos.ArquivoRemessaItemInteface;
|
|
import com.rjconsultores.ventaboletos.ArquivoRetorno;
|
|
import com.rjconsultores.ventaboletos.ArquivoRetornoItem;
|
|
import com.rjconsultores.ventaboletos.blocos.CabecalhoRemessa;
|
|
import com.rjconsultores.ventaboletos.blocos.CabecalhoRetorno;
|
|
import com.rjconsultores.ventaboletos.blocos.DadosBoleto;
|
|
import com.rjconsultores.ventaboletos.blocos.DetalheObrigatorio;
|
|
import com.rjconsultores.ventaboletos.blocos.DetalheRetorno;
|
|
import com.rjconsultores.ventaboletos.blocos.RateioDeCredito;
|
|
import com.rjconsultores.ventaboletos.blocos.RodapeRemessa;
|
|
import com.rjconsultores.ventaboletos.blocos.RodapeRetorno;
|
|
import com.rjconsultores.ventaboletos.enuns.BancoLayout;
|
|
|
|
/**
|
|
* <p>
|
|
* Implementação base para classes utilizadoras do <a href="http://www.jrimum.org/texgit"> JRimum-Texgit </a>.
|
|
* </p>
|
|
*
|
|
* @author <a href="http://gilmatryx.googlepages.com/">Gilmar P.S.L.</a>
|
|
*/
|
|
public abstract class AbstractFlatFile{
|
|
|
|
//protected static Logger log = LoggerFactory.getLogger(AbstractFlatFile.class);
|
|
|
|
/**
|
|
* Nome do arquivo xml de configuração.
|
|
*/
|
|
protected BancoLayout cfgFile;
|
|
|
|
/**
|
|
* FlatFile Texgit.
|
|
*/
|
|
private FlatFile<Record> flatFile;
|
|
|
|
/**
|
|
* Linhas do Arquivo
|
|
*/
|
|
private List<String> lines = new ArrayList<String>();
|
|
|
|
/**
|
|
* Construtor para inicializção com layout xml.
|
|
*
|
|
* @param cfgFile
|
|
* - nome do arquivo layout em xml.
|
|
*/
|
|
protected AbstractFlatFile(BancoLayout cfgFile) {
|
|
|
|
init(cfgFile);
|
|
}
|
|
|
|
/**
|
|
* Inicializador e validador do nome do e layout do arquivo.
|
|
*
|
|
* @param cfgFile
|
|
*/
|
|
protected final void init(BancoLayout cfgFile) {
|
|
|
|
//Strings.checkNotBlank(cfgFile.getPath(), "Arquivo invalido!");
|
|
|
|
this.cfgFile = cfgFile;
|
|
|
|
configure();
|
|
}
|
|
|
|
/**
|
|
* Retorna o Texgit flatfile da instância.
|
|
*
|
|
* @return the flatFile
|
|
*/
|
|
protected final FlatFile<Record> getFlatFile() {
|
|
|
|
return flatFile;
|
|
}
|
|
|
|
/**
|
|
* Configura o flat file a partir do nome do arquivo layout xml da instância
|
|
* procurando no classpath.
|
|
*/
|
|
private void configure() {
|
|
|
|
InputStream in = null;
|
|
|
|
try {
|
|
|
|
//in = ClassLoaders.getResourceAsStream(cfgFile.getPath(), this.getClass());
|
|
in = getClass().getResourceAsStream(cfgFile.getPath());
|
|
|
|
File config = File.createTempFile(cfgFile.getTemp(), null);
|
|
|
|
copyInputStreamToFile(in, config);
|
|
|
|
//FileUtils.copyInputStreamToFile(in, config);
|
|
|
|
flatFile = Texgit.createFlatFile(config);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}finally {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
Objects.checkNotNull(flatFile, "NAO FOI POSSIVEL INICIALIZAR A LIB TEXGIT!");
|
|
}
|
|
|
|
public void copyInputStreamToFile(InputStream initialStream, File targetFile) throws IOException {
|
|
|
|
OutputStream outStream = new FileOutputStream(targetFile);
|
|
|
|
byte[] buffer = new byte[8 * 1024];
|
|
int bytesRead;
|
|
while ((bytesRead = initialStream.read(buffer)) != -1) {
|
|
outStream.write(buffer, 0, bytesRead);
|
|
}
|
|
|
|
IOUtils.closeQuietly(initialStream);
|
|
IOUtils.closeQuietly(outStream);
|
|
|
|
// Reader reader = new BufferedReader(new InputStreamReader(initialStream));
|
|
// Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "Cp1252"));
|
|
//
|
|
// char[] buffer = new char[8 * 1024];
|
|
// int bytesRead;
|
|
// while ((bytesRead = reader.read(buffer)) != -1){
|
|
// writer.write(buffer, 0, bytesRead);
|
|
// }
|
|
//
|
|
// IOUtils.closeQuietly(initialStream);
|
|
// IOUtils.closeQuietly(reader);
|
|
// IOUtils.closeQuietly(writer);
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Lê um arquivo do layout da instancia.
|
|
* </p>
|
|
*
|
|
* @param lines
|
|
* linhas do arquivo
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public <FF extends AbstractFlatFile> FF read(final List<String> lines) {
|
|
|
|
Collections.checkNotEmpty(lines, "Linhas ausentes!");
|
|
|
|
try {
|
|
|
|
getFlatFile().read(lines);
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return (FF) this;
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Lê um arquivo do layout da instancia com enconding UTF-8.
|
|
* </p>
|
|
*
|
|
* @param file
|
|
* arquivo texto
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public <FF extends AbstractFlatFile> FF read(final File file) {
|
|
|
|
Objects.checkNotNull(file, "Arquivo TXT a ser importado nulo!");
|
|
|
|
try {
|
|
|
|
getFlatFile().read(FileUtils.readLines(file));
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return (FF) this;
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Lê um arquivo do layout da instancia.
|
|
* </p>
|
|
*
|
|
* @param file
|
|
* - Arquivo texto
|
|
* @param encoding
|
|
* - Econding em que o arquivo será lido
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public <FF extends AbstractFlatFile> FF read(final File file, String encoding) {
|
|
|
|
Objects.checkNotNull(file, "Arquivo TXT a ser importado nulo!");
|
|
Strings.checkNotBlank(encoding, "Encoding inválido!");
|
|
|
|
try {
|
|
|
|
getFlatFile().read(FileUtils.readLines(file, encoding));
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return (FF) this;
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Escreve um arquivo do layout da instancia com enconding UTF-8.
|
|
* </p>
|
|
*
|
|
* @return arquivo texto
|
|
* @throws IOException
|
|
*/
|
|
public File write() throws IOException {
|
|
|
|
Objects.checkNotNull(getFlatFile(), "Arquivo TXT a ser importado nulo!");
|
|
|
|
File f = File.createTempFile(this.getClass().getName() + ""
|
|
+ new Date().getTime(), "_jnfmtmp.txt");
|
|
|
|
FileUtils.writeLines(f, getFlatFile().write());
|
|
|
|
return f;
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Escreve um arquivo do layout da instancia.
|
|
* </p>
|
|
* @param encoding - Econding em que o arquivo será escrito
|
|
* @return arquivo texto
|
|
* @throws IOException
|
|
*/
|
|
public File write(String encoding) throws IOException {
|
|
|
|
if (getFlatFile() != null) {
|
|
|
|
File f = File.createTempFile(this.getClass().getName() + ""
|
|
+ new Date().getTime(), "_jnfmtmp.txt");
|
|
|
|
FileUtils.writeLines(f, getFlatFile().write(), encoding);
|
|
|
|
return f;
|
|
|
|
} else{
|
|
|
|
throw new IllegalArgumentException(new NullPointerException(
|
|
"Arquivo TXT a ser importado nulo!"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* <p>
|
|
* Escreve um arquivo do layout da instancia.
|
|
* </p>
|
|
* @param arquivoRemessaFisico
|
|
* @throws IOException
|
|
*/
|
|
protected void write(File arquivoRemessaFisico) throws IOException{
|
|
|
|
if(arquivoRemessaFisico == null){
|
|
throw new IllegalArgumentException(new NullPointerException(
|
|
"Arquivo TXT a ser importado nulo!"));
|
|
}
|
|
|
|
FileUtils.writeLines(arquivoRemessaFisico, getFlatFile().write(), "\r\n");
|
|
}
|
|
|
|
protected void writeLines(File arquivoRemessaFisico) throws IOException{
|
|
|
|
if(arquivoRemessaFisico == null){
|
|
throw new IllegalArgumentException(new NullPointerException(
|
|
"Arquivo TXT a ser importado nulo!"));
|
|
}
|
|
|
|
FileUtils.writeLines(arquivoRemessaFisico, "Cp1252", this.lines, "\r\n");
|
|
//FileUtils.writeLines(file, encoding, lines, lineEnding);
|
|
}
|
|
|
|
protected void writeLines() throws IOException{
|
|
this.lines.addAll(getFlatFile().write());
|
|
|
|
flatFile = null;
|
|
configure();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param arquivoRemessaFisico
|
|
* @param arquivoRemessa
|
|
* @throws FileNotFoundException
|
|
* @throws IOException
|
|
*/
|
|
public List<String> gerarRemessa(File arquivoRemessaFisico, ArquivoRemessa arquivoRemessa) throws FileNotFoundException, IOException{
|
|
|
|
//log.info("Gerando Remessa: " + cfgFile.name());
|
|
//log.info("INICIO - Bloco de Header...");
|
|
|
|
if(arquivoRemessa != null && arquivoRemessa.getItens() != null) {
|
|
for(ArquivoRemessaItemInteface arquivoRemessaItem : arquivoRemessa.getItens()){
|
|
createHeader(arquivoRemessaItem.getCabecalhoRemessa());
|
|
//log.info("FIM - Bloco de Header...");
|
|
|
|
//log.info("INICIO - Bloco de Titulos...");
|
|
for (DetalheObrigatorio titulo : arquivoRemessaItem.getTitulos()) {
|
|
createTransacaoTitulos(titulo);
|
|
}
|
|
//log.info("FIM - Bloco de Titulos...");
|
|
|
|
//log.info("INICIO - Bloco de Trailler...");
|
|
createTrailler(arquivoRemessaItem.getRodapeRemessa());
|
|
//log.info("FIM - Bloco de Trailler...");
|
|
|
|
//log.info("INICIO - Gerando arquivo...");
|
|
writeLines();
|
|
//log.info("FIM - Gerando arquivo...");
|
|
}
|
|
|
|
}
|
|
writeLines(arquivoRemessaFisico);
|
|
|
|
return this.lines;
|
|
}
|
|
|
|
|
|
public ArquivoRetorno lerRetorno(File arquivoRetornoFisico) throws FileNotFoundException, IOException{
|
|
|
|
//log.info("Gerando Retorno: " + cfgFile.name());
|
|
//log.info("INICIO - Bloco de Header...");
|
|
|
|
read(arquivoRetornoFisico);
|
|
|
|
return loadInfo();
|
|
}
|
|
|
|
private ArquivoRetorno loadInfo() {
|
|
|
|
ArquivoRetorno arquivoRetorno = new ArquivoRetorno();
|
|
|
|
Record header = getFlatFile().getRecord("Header");
|
|
|
|
ArquivoRetornoItem arquivoRetornoItem = new ArquivoRetornoItem();
|
|
|
|
arquivoRetornoItem.setCabecalhoRetorno(readHeader(header));
|
|
|
|
Collection<Record> registrosDeTransacoes = getFlatFile().getRecords(
|
|
"TransacaoTitulo");
|
|
|
|
List<DetalheRetorno> titulos = readTransacaoTitulos(registrosDeTransacoes);
|
|
|
|
for(DetalheRetorno titulo : titulos){
|
|
arquivoRetornoItem.addTitulo(titulo);
|
|
}
|
|
|
|
|
|
Record trailler = getFlatFile().getRecord("Trailler");
|
|
|
|
arquivoRetornoItem.setRodapeRetorno(readTrailler(trailler));
|
|
|
|
arquivoRetorno.addItem(arquivoRetornoItem);
|
|
|
|
return arquivoRetorno;
|
|
}
|
|
|
|
|
|
protected abstract void createHeader(CabecalhoRemessa cabecalhoRemessaParam);
|
|
|
|
protected abstract void createTransacaoTitulos(DetalheObrigatorio tituloParam);
|
|
|
|
protected abstract Record createDadosBoletos(DadosBoleto boleto);
|
|
|
|
protected abstract Record createRateioDeCredito(RateioDeCredito rateioDeCredito);
|
|
|
|
protected abstract void createTrailler(RodapeRemessa trailler);
|
|
|
|
protected abstract CabecalhoRetorno readHeader(Record header);
|
|
|
|
protected abstract List<DetalheRetorno> readTransacaoTitulos(Collection<Record> registrosDeTransacoes);
|
|
|
|
protected abstract RodapeRetorno readTrailler(Record trailler);
|
|
}
|