Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.cotefacil.poc.eai.actions;
- import java.util.Scanner;
- import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
- import org.jboss.soa.esb.actions.ActionProcessingException;
- import org.jboss.soa.esb.helpers.ConfigTree;
- import org.jboss.soa.esb.message.Message;
- import com.cotefacil.poc.eai.types.Constants;
- public class FlatFile2EDIMessageTranslator extends AbstractActionPipelineProcessor {
- private static final String REGISTRO_FORNECEDORES_COTACAO = "F";
- private static final String REGISTRO_CABECALHO_COTACAO = "1";
- private static final String REGISTRO_CABECALHO_FILIAL = "2";
- private static final String REGISTRO_PRODUTOS = "3";
- private static final String REGISTRO_RODAPE_PRODUTOS = "4";
- private static final String REGISTRO_HEADER = "H";
- private static final String REGISTRO_TRAILLER = "T";
- public FlatFile2EDIMessageTranslator(ConfigTree configTree) {
- }
- @Override
- public Message process(Message message) throws ActionProcessingException {
- StringBuilder documentoEDI = new StringBuilder();
- Scanner scanner = new Scanner(message.getBody().get().toString());
- scanner.useDelimiter(Constants.QUEBRA_DE_LINHA);
- while (scanner.hasNext()) {
- String registro = scanner.next();
- documentoEDI.append(prepararRegistroParaEDI(registro));
- }
- message.getBody().add(documentoEDI.toString());
- return message;
- }
- private String prepararRegistroParaEDI(String registro) {
- StringBuilder bufferRegistro = new StringBuilder();
- bufferRegistro.append(registro);
- if (registro.startsWith(REGISTRO_HEADER)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(7, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(17, Constants.SEPARADOR_CAMPO);
- }
- if (registro.startsWith(REGISTRO_CABECALHO_COTACAO)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(47, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(58, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(64, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(75, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(86, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(97, Constants.SEPARADOR_CAMPO);
- }
- if (registro.startsWith(REGISTRO_CABECALHO_FILIAL)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
- }
- if (registro.startsWith(REGISTRO_PRODUTOS)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(24, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(33, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(114, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(145, Constants.SEPARADOR_CAMPO);
- }
- if (registro.startsWith(REGISTRO_RODAPE_PRODUTOS)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- }
- if (registro.startsWith(REGISTRO_FORNECEDORES_COTACAO)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
- bufferRegistro.insert(19, Constants.SEPARADOR_CAMPO);
- }
- if (registro.startsWith(REGISTRO_TRAILLER)) {
- bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
- }
- bufferRegistro.append(Constants.QUEBRA_DE_LINHA);
- return bufferRegistro.toString();
- }
- }
Add Comment
Please, Sign In to add comment