Create waraper to process ofbiz soap service response

How to call soap service is describe in my previous post.

http://tariqliferay.blogspot.com/2015/09/how-to-create-soap-client-for-ofbiz.html

Here you get response as OMElement. Then you need to process this.
Now i show how can you make pojo/dto class from response for further need.

Think you get campaign list in response as key="campaignList" and  value list a list of campaign.

First create dto/pojo class as your requirement.
Here below a sample dto:


import java.io.Serializable;

/**
 * @author tariq
 *
 */
public class Campaign implements Serializable{

    public String campaignName = null;
    public String campaignType = null;
    public String contentId = null;
    public String createdStamp = null;
    public String createdTxStamp = null;
    public String emailSubscription = null;
    public String lastUpdatedStamp = null;
    public String lastUpdatedTxStamp = null;
    public String marketingCampaignId = null;
    public String nameSender = null;
    public String resEmailAddress = null;
    public String startDate = null;
    public String statusId = null;
    public String templateId = null;

    public Campaign() {
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((campaignName == null) ? 0 : campaignName.hashCode());
        result = prime * result
                + ((campaignType == null) ? 0 : campaignType.hashCode());
        result = prime * result
                + ((contentId == null) ? 0 : contentId.hashCode());
        result = prime * result
                + ((createdStamp == null) ? 0 : createdStamp.hashCode());
        result = prime * result
                + ((createdTxStamp == null) ? 0 : createdTxStamp.hashCode());
        result = prime
                * result
                + ((emailSubscription == null) ? 0 : emailSubscription
                        .hashCode());
        result = prime
                * result
                + ((lastUpdatedStamp == null) ? 0 : lastUpdatedStamp.hashCode());
        result = prime
                * result
                + ((lastUpdatedTxStamp == null) ? 0 : lastUpdatedTxStamp
                        .hashCode());
        result = prime
                * result
                + ((marketingCampaignId == null) ? 0 : marketingCampaignId
                        .hashCode());
        result = prime * result
                + ((nameSender == null) ? 0 : nameSender.hashCode());
        result = prime * result
                + ((resEmailAddress == null) ? 0 : resEmailAddress.hashCode());
        result = prime * result
                + ((startDate == null) ? 0 : startDate.hashCode());
        result = prime * result
                + ((statusId == null) ? 0 : statusId.hashCode());
        result = prime * result
                + ((templateId == null) ? 0 : templateId.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Campaign other = (Campaign) obj;
        if (campaignName == null) {
            if (other.campaignName != null)
                return false;
        } else if (!campaignName.equals(other.campaignName))
            return false;
        if (campaignType == null) {
            if (other.campaignType != null)
                return false;
        } else if (!campaignType.equals(other.campaignType))
            return false;
        if (contentId == null) {
            if (other.contentId != null)
                return false;
        } else if (!contentId.equals(other.contentId))
            return false;
        if (createdStamp == null) {
            if (other.createdStamp != null)
                return false;
        } else if (!createdStamp.equals(other.createdStamp))
            return false;
        if (createdTxStamp == null) {
            if (other.createdTxStamp != null)
                return false;
        } else if (!createdTxStamp.equals(other.createdTxStamp))
            return false;
        if (emailSubscription == null) {
            if (other.emailSubscription != null)
                return false;
        } else if (!emailSubscription.equals(other.emailSubscription))
            return false;
        if (lastUpdatedStamp == null) {
            if (other.lastUpdatedStamp != null)
                return false;
        } else if (!lastUpdatedStamp.equals(other.lastUpdatedStamp))
            return false;
        if (lastUpdatedTxStamp == null) {
            if (other.lastUpdatedTxStamp != null)
                return false;
        } else if (!lastUpdatedTxStamp.equals(other.lastUpdatedTxStamp))
            return false;
        if (marketingCampaignId == null) {
            if (other.marketingCampaignId != null)
                return false;
        } else if (!marketingCampaignId.equals(other.marketingCampaignId))
            return false;
        if (nameSender == null) {
            if (other.nameSender != null)
                return false;
        } else if (!nameSender.equals(other.nameSender))
            return false;
        if (resEmailAddress == null) {
            if (other.resEmailAddress != null)
                return false;
        } else if (!resEmailAddress.equals(other.resEmailAddress))
            return false;
        if (startDate == null) {
            if (other.startDate != null)
                return false;
        } else if (!startDate.equals(other.startDate))
            return false;
        if (statusId == null) {
            if (other.statusId != null)
                return false;
        } else if (!statusId.equals(other.statusId))
            return false;
        if (templateId == null) {
            if (other.templateId != null)
                return false;
        } else if (!templateId.equals(other.templateId))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Campaign [campaignName=" + campaignName + ", campaignType="
                + campaignType + ", contentId=" + contentId + ", createdStamp="
                + createdStamp + ", createdTxStamp=" + createdTxStamp
                + ", emailSubscription=" + emailSubscription
                + ", lastUpdatedStamp=" + lastUpdatedStamp
                + ", lastUpdatedTxStamp=" + lastUpdatedTxStamp
                + ", marketingCampaignId=" + marketingCampaignId
                + ", nameSender=" + nameSender + ", resEmailAddress="
                + resEmailAddress + ", startDate=" + startDate + ", statusId="
                + statusId + ", templateId=" + templateId + "]";
    }

    public Campaign(String campaignName, String campaignType, String contentId,
            String createdStamp, String createdTxStamp,
            String emailSubscription, String lastUpdatedStamp,
            String lastUpdatedTxStamp, String marketingCampaignId,
            String nameSender, String resEmailAddress, String startDate,
            String statusId, String templateId) {
        this.campaignName = campaignName;
        this.campaignType = campaignType;
        this.contentId = contentId;
        this.createdStamp = createdStamp;
        this.createdTxStamp = createdTxStamp;
        this.emailSubscription = emailSubscription;
        this.lastUpdatedStamp = lastUpdatedStamp;
        this.lastUpdatedTxStamp = lastUpdatedTxStamp;
        this.marketingCampaignId = marketingCampaignId;
        this.nameSender = nameSender;
        this.resEmailAddress = resEmailAddress;
        this.startDate = startDate;
        this.statusId = statusId;
        this.templateId = templateId;
    }

    public String getCampaignName() {
        return campaignName;
    }

    public void setCampaignName(String campaignName) {
        this.campaignName = campaignName;
    }

    public String getCampaignType() {
        return campaignType;
    }

    public void setCampaignType(String campaignType) {
        this.campaignType = campaignType;
    }

    public String getContentId() {
        return contentId;
    }

    public void setContentId(String contentId) {
        this.contentId = contentId;
    }

    public String getCreatedStamp() {
        return createdStamp;
    }

    public void setCreatedStamp(String createdStamp) {
        this.createdStamp = createdStamp;
    }

    public String getCreatedTxStamp() {
        return createdTxStamp;
    }

    public void setCreatedTxStamp(String createdTxStamp) {
        this.createdTxStamp = createdTxStamp;
    }

    public String getEmailSubscription() {
        return emailSubscription;
    }

    public void setEmailSubscription(String emailSubscription) {
        this.emailSubscription = emailSubscription;
    }

    public String getLastUpdatedStamp() {
        return lastUpdatedStamp;
    }

    public void setLastUpdatedStamp(String lastUpdatedStamp) {
        this.lastUpdatedStamp = lastUpdatedStamp;
    }

    public String getLastUpdatedTxStamp() {
        return lastUpdatedTxStamp;
    }

    public void setLastUpdatedTxStamp(String lastUpdatedTxStamp) {
        this.lastUpdatedTxStamp = lastUpdatedTxStamp;
    }

    public String getMarketingCampaignId() {
        return marketingCampaignId;
    }

    public void setMarketingCampaignId(String marketingCampaignId) {
        this.marketingCampaignId = marketingCampaignId;
    }

    public String getNameSender() {
        return nameSender;
    }

    public void setNameSender(String nameSender) {
        this.nameSender = nameSender;
    }

    public String getResEmailAddress() {
        return resEmailAddress;
    }

    public void setResEmailAddress(String resEmailAddress) {
        this.resEmailAddress = resEmailAddress;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getStatusId() {
        return statusId;
    }

    public void setStatusId(String statusId) {
        this.statusId = statusId;
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }


}

Then create a wrapper class with generic type to wrap the response to your dto/pojo.

Here below a sample wrapper class:


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;


/**
 * @author tariq
 *
 */
public class Waraper<T> {
   
    private OMElement resultRootOMElement=null;
    public static final Logger log = Logger.getLogger(Waraper.class.getName());
   
    Class<T> clazz;
   
    /**
     * @author tariq
     */
    public Waraper(Class<T> clazz) {
        this.clazz = clazz;
        // TODO Auto-generated constructor stub
    }
   
    /**
     * @author tariq
     * @param element
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public  Object getResultObject(OMElement element) throws InstantiationException, IllegalAccessException {
        //Create new instance of target class
        Object tempObject= clazz.newInstance();
        for (Iterator childiterator = element.getChildElements(); childiterator.hasNext();) {
             OMElement omelement=(OMElement)childiterator.next();
             log.info("==========:Filling all attribute or field values:=============");
                for (Iterator attributeiterator = omelement.getAllAttributes(); attributeiterator.hasNext();) {
                    OMAttribute omattribute=(OMAttribute) attributeiterator.next();
                    String attributeValue=omattribute.getAttributeValue();
                    String localName=omattribute.getLocalName();
                    setObjectProperty(localName,attributeValue,tempObject,clazz);
                }
        }
        return tempObject;
    }

    /**
     * @author tariq
     * @param element
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public  List getResultObjects(OMElement element) throws InstantiationException, IllegalAccessException {
        List results=new ArrayList<>();
        OMElement result= element.getFirstElement();
        if(result!=null){
            //Iterate through result to create target pojo object
            for (Iterator childiterator = result.getChildElements(); childiterator.hasNext();) {
                 OMElement campaign=(OMElement)childiterator.next();
                 //Create new instance of target class
                   Object tempObject= clazz.newInstance();
                   log.info("==========:Filling all attribute or field values:=============");
                    for (Iterator attributeiterator = campaign.getAllAttributes(); attributeiterator.hasNext();) {
                        OMAttribute omattribute=(OMAttribute) attributeiterator.next();
                        String attributeValue=omattribute.getAttributeValue();
                        String localName=omattribute.getLocalName();
                        setObjectProperty(localName,attributeValue,tempObject,clazz);
                    }
                    results.add(tempObject);
             }
        }else{
            log.info("==========:Result not found:=============");
        }
        return results;
    }

/**
    * @author tariq  
    * @param found
    * @param element
    * @param resultName
    * @return
    */
   public boolean setResultRootOMElement(boolean found,OMElement element,String resultName){
       OMElement ome;
       for (Iterator childiterator = element.getChildElements(); childiterator.hasNext();) {
        // get current child element
           ome = (OMElement) childiterator.next();
           if(found){
               element=ome;
               setResultRootOMElement(ome);
               log.info("==========:Result found:=============");
               break;
           }else{
            for (Iterator attributeiterator = ome.getAllAttributes(); attributeiterator.hasNext();) {
                    OMAttribute oma=(OMAttribute) attributeiterator.next();
                     String attributeValue=oma.getAttributeValue();
                     //Check result name
                     if(attributeValue.equalsIgnoreCase(resultName)){
                         found=true;
                     }
             }
            //Recursively call to find result
            found=setResultRootOMElement(found, ome, resultName);
        }
       }
       return found;
   }
   
   /**
    * @author tariq  
    * @param fieldName
    * @param attributeValue
    * @param object
    * @param className
    */
   public void setObjectProperty(String fieldName,String attributeValue,Object object,Class targetClass){
        Field field = null;
        try {
             //Check field existence
             field = targetClass.getField(fieldName.trim());
             if(field!=null){
                 field.setAccessible(true);
                 field.set(object, attributeValue);
             }
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
       }
     
       /**
        * @author tariq
        * @return
        */
    public OMElement getResultRootOMElement() {
        return resultRootOMElement;
    }
   
    /**
     * @author tariq
     * @param resultOMElement
     */
    public void setResultRootOMElement(OMElement resultRootOMElement) {
        this.resultRootOMElement = resultRootOMElement;
    }
   

   
    /**
     *
     * @return
     */
    public Class<T> getClazz() {
        return clazz;
    }
    /**
     *
     * @param clazz
     */
    public void setClazz(Class<T> clazz) {
        this.clazz = clazz;
    }

   
}

Then below i show to create wraper class instance and call wraper list or single object into your dto/pojo class.

    Waraper<Campaign> waraper=new Waraper<Campaign>(Campaign.class);
    waraper.setResultRootOMElement(false, response, "campaignList");
    List<Campaign> campaignList=waraper.getResultObjects(waraper.getResultRootOMElement());

Thanks...........


Comments

Popular posts from this blog

How to find the companyId in Liferay.

How to add portlet in liferay theme.

How to create soap client for ofbiz service or ofbiz service invocation from another application.