Think objects when creating Java mappings

 

Most of the XI consultants come from SAP (ABAP) background and face many problems when they need to implement Java mapping for the source to target transformation. This weblog attempts to give a guideline on approach to be taken when developing mapping programs.

Assuming we have a IDoc to flat file scenario, let us first have a look at the source IDoc structure.

Source IDoc XML structure

Graphical reprensentation for quick understanding

graphical

Starting from the lowermost level of T08 as a composite structure i.e. made up of other simple properties, think of T08 as a class having all string properties with names same as that of the tag names of IDoc XML. So a typical T08 class structure would be,

------------------------------------------------------------------------------------------------------------------------------------

public class T08{
    private String TRANSACTION_TYPE;
    private String METER_POINT_REFERENCE;
    private String DELIVERY_POINT_ALIAS;
    private String PO_BOX;
    private String SUB_BUILDING_NAME;
    private String BUILDING_NAME;
    private String BUILDING_NUMBER;
    private String DEPENDENT_STREET;
    private String PRINCIPAL_STREET;
    private String DOUBLE_DEPENDENT_LOCALITY;
    private String DEPENDANT_LOCALITY;
    private String POST_TOWN;
    private String COUNTY;
    private String POSTCODE_OUTCODE;
    private String POSTCODE_INCODE;
    private String PAF_INDICATOR;
}

------------------------------------------------------------------------------------------------------------------------------------

The fastest way to create this class could be going to the segment editor in SAPGUI, copy all the property names and create String variables with all these names. Next, assuming that you are using NWDS as your development tool, create the getter / setter methods for all these properties. Matter of a right click only!!! Right click anywhere inside the class body, select Source --> Generate getter / setter --> Select All.Click Ok.

Now that we are done with T08 class, lets move on to its parent i.e. A00. Create the A00 class on the similar lines of T08, key point to note is, A00 should have a class member of type T08 .

 

------------------------------------------------------------------------------------------------------------------------------------

public class A00{
    private String TRANSACTION_TYPE;
    private String ORGANISATION_ID;
    private String FILE_TYPE;
    private String CREATION_DATE;
    private String CREATION_TIME;
    private String GENERATION_NUMBER;
    private String INT_FILE_TYPE;
    private String RECEIVER_ID;
    private T08 t08; //reference to the T08 structure
}

 

------------------------------------------------------------------------------------------------------------------------------------

Generate the getter / setter methods for this class too.

We are now ready with the basic data structure in which the data would be stored at runtime and following steps with more or less modification could be taken as the basis for the actual program logic

1.Based on the occurrence of a particular sub-structure the data type of the class member should be decided. E.g. in our case, T08 was having only one occurrence so we declared A00 with a class member T08. If T08 had multiple occurrences, then declare a Vector / Arraylist of T08 as a class member for A00.
e.g. private T08 t08; //reference to the T08 structure i.e. for single T08 occurrence.
private Vector t08List ; //reference to a dynamically growing Vector of T08 structures.

2.If multiple source structures appear in the input to the mapping program , simply have a class level member ArrayList or Vector which would contain various A00 structures.
private Vector idocList; // list of idocs

3.Build the child object completely before adding / setting it to the parent object. So , in our case, we have to make sure that we set all the possible properties of T08 before adding it to A00 structure.

4.The sequence of building all the hierarchical objects should be from the lowermost composite structure to the top level composite structure. So a typical code based on this approach would look like ,

 

------------------------------------------------------------------------------------------------------------------------------------

a00.setTRANSACTION_TYPE(value);
....
....
t08.setPAF_INDICATOR(value);
....
....
t08.setBUILDING_NUMBER(value);
....
....
a00.setT08(t08);
....
....
idocList.add(a00);

 

------------------------------------------------------------------------------------------------------------------------------------

5.So once you are done with populating the complete data structure , imagine how handy it is to refer any value within the data structure using the getXXX methods.

Advantages of this approach:

1.You can write more readable, modular, and maintainable code.

2.Use of objects along with Java’s predefined data structures, collections helps you reduce programming effort, increases program speed and quality as these predefined structures are very well designed and well tested. Performance & efficiency overheads as well as chances of runtime errors associated with usage of Arrays can be safely avoided.

3.toString method of the composite structure classes can be overridden to provide any specific output requirements. E.g. if FILE_TYPE attribute of A00 should appear with double quotes in the final output, the logic could be written in toString of A00 class. toString is the method which if overridden, gets called automatically say if we use a statement like
System.out.println(a00);

 

SAP XI Interview Questions

SAP Developer Network SAP Weblogs: SAP Process Integration (PI)