Wednesday, July 25, 2012

BeanFace Example


1. BeanFaceGeneratorExample.java - Auto Generate BeanFace based on Legacy Message Formats such as Delimited, Fixed, CSV

In com.wwlee.beanface.examples package in BeanFaceExample, you will see 5 java files.
Delete all except BeanFaceGeneratorExample.java file.

Let's assume the message have four columns as the below
 - EmployeeName,DepartmentName,CompanyName,CountryCode
And it has four different formats with Fixed, Delimited(','), CSV format with ',', Character Delimiter('\t').
After running BeanFaceGeneratorExample, you will see four different types of java files, respectively. 

Let's only focus BeanFaceSampleDelimited.java file. The other java files are the exactly same for the function.
1)public class BeanFaceSampleDelimited extends BeanFaceImpl
Each one should extends BeanFaceImpl to have common methods such as unmarshalFromString(), unmarshalFromBytes(), marshalToString(), marshalToBytes(), getBeanFaceJAXB(), etc.
2)Java Bean - All of set() and get() methods are automatically generated based on Input Column Names.
3)Auto generated XML messages through getBeanFaceJAXB() method.

Let's see the main method and run it.

 public static void main(String[] args) throws Exception {
        String inputMessage = "EmployeeName,DepartmentName,CompanyName,CountryCode";
        BeanFaceSampleDelimited beanFace = new BeanFaceSampleDelimited();
        beanFace.unmarshalFromString(inputMessage);
        System.out.println("Before Changing getEmployeeName : " + beanFace.getEmployeeName());
        beanFace.setEmployeeName("ABC");
        System.out.println("After  Changing getEmployeeName : " + beanFace.getEmployeeName());
        System.out.println(beanFace.marshalToString());    
        /* JAXB Unmarshal and Marshal */
        BeanFaceJAXB beanFaceJAXB = beanFace.getBeanFaceJAXB();
        String xml = beanFaceJAXB.marshalToString(beanFace);
        System.out.println(xml);
        beanFaceJAXB.unmarshalFromString(beanFace, xml);
}
Result]
Before Changing getEmployeeName : EmployeeName
After  Changing getEmployeeName : ABC
ABC,DepartmentName,CompanyName,CountryCode
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beanFaceSampleDelimited>
    <employeeName>ABC</employeeName>
    <departmentName>DepartmentName</departmentName>
    <companyName>CompanyName</companyName>
    <countryCode>CountryCode</countryCode>
</beanFaceSampleDelimited>

2. com.wwlee.beanface.examples.transformation.FixedToDelimtedTransformation.java
   - This example will show you how to transform fixed message format to Delimited format. 
   - After converting it to delimited format, you can also automatically generate XML messages by through BeanFaceJAXB object. Here is the snippet code for this same and its result.

public String transformByJAXBXML(String inputMessage) throws BeanFaceException{
        inputBeanFace.unmarshalFromString(inputMessage);
        int inputColumnNumbers = inputBeanFace.getInputColumnNumbers();
        for(int i=0; i < inputColumnNumbers; i++){
            /*
           outputBeanFace.setEmployeeName(inputBeanFace.getEmployeeName());
           outputBeanFace.setDepartmentName(inputBeanFace.getDepartmentName());
           outputBeanFace.setCompanyName(inputBeanFace.getCompanyName());
           outputBeanFace.setCountryCode(inputBeanFace.getCountryCode());
           */
            outputBeanFace.set(i, inputBeanFace.get(i));
        }
        BeanFaceJAXB beanFaceJAXB = outputBeanFace.getBeanFaceJAXB();
        return beanFaceJAXB.marshalToString(outputBeanFace);
}

Result]<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<beanFaceSampleDelimited>
    <employeeName>111111</employeeName>
    <departmentName>22</departmentName>
    <companyName>3333333333</companyName>
    <countryCode>44444</countryCode>
</beanFaceSampleDelimited>

Enjoy the above two samples and we will move to SOAP message through BeanFace in the next blog.

To fully use BeanFace and its Web Service, you need to download the followings.
1. BeanFace.jar - BeanFace Core API
2. BeanFaceExample.zip - Include all of the example source codes including Web Service Clients
3. BeanFaceWebService.zip - Include all of the sample java source for Web Service Server sides.  

No comments:

Post a Comment