edismooks

From java Object prepare the edi data


I am newbie in edi data. I am using smooks api to read the edi data and able to parse it into java object. I want to convert java object to edi data for that i am not getting much information. Here is the example i am trying to read from edi file and creating the java object -

customOrder.edi - COR*130*PINGPONG02*You got it to work*1230
---------------
POJO -
------
    public class CustomOrder implements Serializable{
    private int number;
    private String sender;
    private String message;
    private int price;
    // setter and getter
    }
custom-order-mapping.xml -
-------------------------
    <?xml version="1.0" encoding="UTF-8"?><medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.3.xsd">
    <medi:description name="DVD Order" version="1.0" />

    <medi:delimiters segment="&#10;" field="*" component="^" sub-component="~" />

    <medi:segments xmltag="CustomOrder">
        <medi:segment segcode="COR" xmltag="co">
            <medi:field xmltag="number" />
            <medi:field xmltag="sender" />
            <medi:field xmltag="message" />
            <medi:field xmltag="price" />
        </medi:segment>
    </medi:segments>
</medi:edimap>

smooks-config.xml -
------------------
<?xml version="1.0"?>
<smooks-resource-list
    xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.1.xsd"
    xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"
    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

    <edi:reader mappingModel="/example/custom-order-mapping.xml" />


    <jb:bean beanId="customer" class="example.model.CustomOrder" createOnElement="co">
        <!-- Customer bindings... -->
        <jb:value property="number" data="#/number" decoder="Integer"/>
        <jb:value property="sender" data="#/sender" decoder="String"/>
        <jb:value property="message" data="#/message" decoder="String"/>
        <jb:value property="price" data="#/price" decoder="Integer"/>
    </jb:bean>
</smooks-resource-list>

 Main method -
--------------
     Main smooksMain = new Main();
        ExecutionContext executionContext = smooksMain.smooks.createExecutionContext();
        org.milyn.payload.JavaResult result = smooksMain.runSmooksTransform(executionContext);
        CustomOrder custOrder = (CustomOrder) result.getBean("customer");
        // Need to get to edi data from java object custOrder
       // Please help me - this part of code

I want to prepare edi data from java object. If any other api/framework apart from Smooks which will do the same, it will be fine for me.please let me know, Thanks.


Solution

  • I searched about it and get to know from smooks forum that to prepare edi data from java object, we have to use Edifact Java Compiler(EJC). Above example is to prepare java object from edi data. Pojo class have to implement EDIWritable and override the write method.Here is the changed Pojo class -

    public class CustomOrder implements Serializable, EDIWritable{
    private int number;
    private IntegerDecoder numberDecoder;
    private String sender;
    private String message;
    private int price;
    private IntegerDecoder priceDecoder;
    public CustomOrder() {
        numberDecoder = new IntegerDecoder();
        priceDecoder = new IntegerDecoder();
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public void setSender(String sender) {
        this.sender = sender;
    }
    public String getSender() {
        return sender;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public  void write(Writer writer, Delimiters delimiters) throws IOException {
        // TODO Auto-generated method stub
         Writer nodeWriter = writer;
    
            if(number != 0) {
                nodeWriter.write(delimiters.escape(numberDecoder.encode(number)));
            }
            nodeWriter.write(delimiters.getField());
    
            if(sender != null) {
                nodeWriter.write(delimiters.escape(sender.toString()));
            }
            nodeWriter.write(delimiters.getField());
    
            if(message != null) {
                nodeWriter.write(delimiters.escape(message.toString()));
            }
            nodeWriter.write(delimiters.getField());
            if(price != 0) {
                nodeWriter.write(delimiters.escape(priceDecoder.encode(price)));
            }
            //nodeWriter.write(delimiters.getField());
    
            writer.write(delimiters.getSegmentDelimiter());
            writer.flush();
    
    }
    }
    

    Next, we have to prepare the Factory of the pojo class -

    CustomOrderFactory

    public class CustomOrderFactory {
     private Smooks smooks;
     private Delimiters delimiters;
     public static CustomOrderFactory getInstance() throws IOException, SAXException {
            return new CustomOrderFactory();
        }
        public void addConfigurations(InputStream resourceConfigStream) throws SAXException, IOException {
            smooks.addConfigurations(resourceConfigStream);
        }
        public void toEDI(CustomOrder instance, Writer writer) throws IOException {
            instance.write(writer, delimiters);
        }
    
        private CustomOrderFactory() throws IOException, SAXException {
            smooks = new Smooks(CustomOrderFactory.class.getResourceAsStream("smooks-config.xml"));
            System.out.println("smooks is prepared");
            try {   
                Edimap edimap = EDIConfigDigester.digestConfig(CustomOrderFactory.class.getResourceAsStream("custom-order-mapping.xml"));
                System.out.println("ediMap is prepared");
                delimiters = edimap.getDelimiters();
                System.out.println("delimeter is prepared");
            } catch(EDIConfigurationException e) {
                IOException ioException = new IOException("Exception reading EDI Mapping model.");
                ioException.initCause(e);
                throw ioException;
            }
        }
    }
    

    Once CustomOrder object is ready, as shown above in Main class. We have to use this object to convert to edi data format. Here is the complete Main class -

    Main class
    ----------
    Main smooksMain = new Main();
    ExecutionContext executionContext = smooksMain.smooks.createExecutionContext();
    org.milyn.payload.JavaResult result = smooksMain.runSmooksTransform(executionContext);
    CustomOrder custOrder = (CustomOrder) result.getBean("customer");
    // Prepare edi data from java object custOrder
    CustomOrderFactory customOrderFactory = CustomOrderFactory.getInstance();
    OutputStream os = new FileOutputStream("createdEDIFile.edi");
    customOrderFactory.toEDI(custOrder, new OutputStreamWriter(os));    
    System.out.println("Edi file is created from java object");
    

    Thats it. Hope it would help. Thanks.