Guest User

Untitled

a guest
Apr 25th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. package com.cotefacil.poc.eai.actions;
  2.  
  3. import java.util.Scanner;
  4.  
  5. import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
  6. import org.jboss.soa.esb.actions.ActionProcessingException;
  7. import org.jboss.soa.esb.helpers.ConfigTree;
  8. import org.jboss.soa.esb.message.Message;
  9.  
  10. import com.cotefacil.poc.eai.types.Constants;
  11.  
  12. public class FlatFile2EDIMessageTranslator extends AbstractActionPipelineProcessor {
  13.  
  14. private static final String REGISTRO_FORNECEDORES_COTACAO = "F";
  15. private static final String REGISTRO_CABECALHO_COTACAO = "1";
  16. private static final String REGISTRO_CABECALHO_FILIAL = "2";
  17. private static final String REGISTRO_PRODUTOS = "3";
  18. private static final String REGISTRO_RODAPE_PRODUTOS = "4";
  19. private static final String REGISTRO_HEADER = "H";
  20. private static final String REGISTRO_TRAILLER = "T";
  21.  
  22. public FlatFile2EDIMessageTranslator(ConfigTree configTree) {
  23. }
  24.  
  25. @Override
  26. public Message process(Message message) throws ActionProcessingException {
  27. StringBuilder documentoEDI = new StringBuilder();
  28. Scanner scanner = new Scanner(message.getBody().get().toString());
  29. scanner.useDelimiter(Constants.QUEBRA_DE_LINHA);
  30. while (scanner.hasNext()) {
  31. String registro = scanner.next();
  32. documentoEDI.append(prepararRegistroParaEDI(registro));
  33. }
  34. message.getBody().add(documentoEDI.toString());
  35. return message;
  36. }
  37.  
  38. private String prepararRegistroParaEDI(String registro) {
  39. StringBuilder bufferRegistro = new StringBuilder();
  40. bufferRegistro.append(registro);
  41. if (registro.startsWith(REGISTRO_HEADER)) {
  42. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  43. bufferRegistro.insert(7, Constants.SEPARADOR_CAMPO);
  44. bufferRegistro.insert(17, Constants.SEPARADOR_CAMPO);
  45. }
  46. if (registro.startsWith(REGISTRO_CABECALHO_COTACAO)) {
  47. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  48. bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
  49. bufferRegistro.insert(47, Constants.SEPARADOR_CAMPO);
  50. bufferRegistro.insert(58, Constants.SEPARADOR_CAMPO);
  51. bufferRegistro.insert(64, Constants.SEPARADOR_CAMPO);
  52. bufferRegistro.insert(75, Constants.SEPARADOR_CAMPO);
  53. bufferRegistro.insert(86, Constants.SEPARADOR_CAMPO);
  54. bufferRegistro.insert(97, Constants.SEPARADOR_CAMPO);
  55. }
  56. if (registro.startsWith(REGISTRO_CABECALHO_FILIAL)) {
  57. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  58. bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
  59. }
  60. if (registro.startsWith(REGISTRO_PRODUTOS)) {
  61. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  62. bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
  63. bufferRegistro.insert(24, Constants.SEPARADOR_CAMPO);
  64. bufferRegistro.insert(33, Constants.SEPARADOR_CAMPO);
  65. bufferRegistro.insert(114, Constants.SEPARADOR_CAMPO);
  66. bufferRegistro.insert(145, Constants.SEPARADOR_CAMPO);
  67. }
  68. if (registro.startsWith(REGISTRO_RODAPE_PRODUTOS)) {
  69. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  70. }
  71. if (registro.startsWith(REGISTRO_FORNECEDORES_COTACAO)) {
  72. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  73. bufferRegistro.insert(16, Constants.SEPARADOR_CAMPO);
  74. bufferRegistro.insert(19, Constants.SEPARADOR_CAMPO);
  75. }
  76. if (registro.startsWith(REGISTRO_TRAILLER)) {
  77. bufferRegistro.insert(1, Constants.SEPARADOR_CAMPO);
  78. }
  79. bufferRegistro.append(Constants.QUEBRA_DE_LINHA);
  80. return bufferRegistro.toString();
  81. }
  82.  
  83. }
Add Comment
Please, Sign In to add comment