Popular Posts

How to extend an SOI service with UserData ?

 Order SOI Service is taken up here to explain.

Steps to extend are as follows:

  • Create three new classes.
  • ExtOrderFacadeClient.java, extends the client API library(OrderFacadeClient) to a value for the field1 column for Orders table
  • ExtExtendOrderItemProcessCmdImpl.java, extends ExtendOrderItemProcessCmdImpl and persists the field1 value passed in the ORDERS.FIELD1 column
  • ExtComposeOrderDetailsCmdImpl.java, extends ComposeOrderDetailsCmdImpl and retrieves the field1 value from the ORDERS.FIELD1 column

  • Open ExtOrderFacadeClient.java and replace the contents with the following code:
package com.custom.order.commands.order.commands;

import java.util.Iterator;
import java.util.Map;

import javax.security.auth.callback.CallbackHandler;

import com.ibm.commerce.foundation.common.datatypes.BusinessContextType;
import com.ibm.commerce.foundation.common.datatypes.CommerceFoundationFactory;
import com.ibm.commerce.foundation.common.datatypes.UserDataType;
import com.ibm.commerce.order.facade.client.OrderException;
import com.ibm.commerce.order.facade.client.OrderFacadeClient;
import com.ibm.commerce.order.facade.datatypes.OrderType;


/**
 * ExtOrderFacadeClient will extend OrderFacadeClient.  This customization will
 * extend the Change noun to include new personalization attributes for 
 * the Order noun. 
 */
public class ExtOrderFacadeClient extends OrderFacadeClient{
   
    private static final String CLASSNAME = ExtOrderFacadeClient.class.getName();

    private static final java.util.logging.Logger LOGGER = com.ibm.commerce.foundation.common.util.logging.LoggingHelper.getLogger(ExtOrderFacadeClient.class);
   
    public ExtOrderFacadeClient(){
        super();
    }
   
    public ExtOrderFacadeClient(BusinessContextType businessContext, CallbackHandler callbackHandler) {
        super(businessContext, callbackHandler);
    }
   
    @Override
    protected OrderType buildOrder(Map parameters, String actionName) throws OrderException {
      final String strMethodName = "buildOrderItem"; 
      LOGGER.entering(CLASSNAME, strMethodName);
     
      OrderType  orderType = super.buildOrder(parameters, actionName);
      String field1 = "";
      Iterator keys = parameters.keySet().iterator();
      while (keys.hasNext()) {
          String key = (String) keys.next();
          if (key.startsWith("field1")) {
            field1 =  ((String[])parameters.get(key))[0];
          }
      }
      UserDataType userData = orderType.getUserData();
     
      if (userData == null)
      {
          userData = CommerceFoundationFactory.eINSTANCE.createUserDataType();
      }
      orderType.setUserData(userData);           
      Map userDataFields  = userData.getUserDataField();
    
      userDataFields.put("field1", field1);       
      return orderType;
    }
}



  • ExtendOrderItemProcessCmdImpl is the component command that is required to be changed to handle the new information inside the extended UserData. The new command takes the new personalization parameters out of the response properties and persists them to the database.
  •  Open MyExtendedOrderItemProcessCmdImpl.java and replace the contents with the following code:

package com.custom.order.commands.order.commands;

import java.util.Enumeration;

import org.apache.commons.lang.StringUtils;

import com.ibm.commerce.datatype.TypedProperty;
import com.ibm.commerce.exception.ECException;
import com.ibm.commerce.order.objects.OrderAccessBean;
import com.ibm.commerce.orderitems.commands.ExtendOrderItemProcessCmdImpl;

 /** * When the message is received, the request properties will be processed by
 the command * ExtendOrderItemProcessCmdImpl, so we extend this command and put
 our new logic into the * performExecute() method.  This logic will write the
 new personalization data into the ORDERS.FIELD1 */

public class ExtExtendOrderItemProcessCmdImpl extends ExtendOrderItemProcessCmdImpl{

    private static final String CLASSNAME = ExtExtendOrderItemProcessCmdImpl.class.getName();

    private static final java.util.logging.Logger LOGGER = com.ibm.commerce.foundation.common.util.logging.LoggingHelper.getLogger(ExtExtendOrderItemProcessCmdImpl.class);
   
    private TypedProperty myRequestProperties;   
   
    public ExtExtendOrderItemProcessCmdImpl(){
        super();
    }
   
    public void setRequestProperties(TypedProperty aRequestproperties) throws ECException{       
        myRequestProperties = aRequestproperties;   
        super.setRequestProperties(aRequestproperties);
    }

    public void performExecute() throws ECException{       
       
        final String METHODNAME = "performExecute";
       
        LOGGER.entering(CLASSNAME, METHODNAME);
       
        super.performExecute();   
               
        if(myRequestProperties!=null){       
            Enumeration keys = myRequestProperties.keys();       
            String field1 = "", orderId = "";
            while (keys.hasMoreElements()) {   
                String keyName = (String)keys.nextElement();       
                if (keyName.startsWith("field1")) {   
                  field1 = (String)myRequestProperties.get(keyName);  
                }
                if (keyName.startsWith("resolvedOrderIds")) { 
                  orderId = ((String[])myRequestProperties.get(keyName))[0];
                }
            }  
            try {
              if(!StringUtils.isEmpty(field1)) {
                OrderAccessBean orderAccessBean = new OrderAccessBean();
                orderAccessBean.setInitKey_orderId(orderId);
                orderAccessBean.refreshCopyHelper();
                orderAccessBean.setField1(field1);
                orderAccessBean.commitCopyHelper();
              }
            } catch(Exception exception) {
                System.out.println(exception);
            }
        }
        LOGGER.exiting(CLASSNAME, METHODNAME);
    }   
}


  • ExtComposeOrderDetailsCmdImpl.java returns ORDERS.FIELD1
  • Open ExtComposeOrderDetailsCmdImpl.java and replace the contents with the following code:
package com.custom.order.commands.order.commands;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.ibm.commerce.foundation.common.datatypes.CommerceFoundationFactory;
import com.ibm.commerce.foundation.common.datatypes.UserDataType;
import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
import com.ibm.commerce.order.facade.datatypes.OrderType;
import com.ibm.commerce.order.facade.datatypes.ShowOrderType;
import com.ibm.commerce.order.facade.server.commands.ComposeOrderDetailsCmdImpl;
import com.ibm.commerce.order.objects.OrderAccessBean;
import com.ibm.websphere.command.CommandException;

public class ExtComposeOrderDetailsCmdImpl extends ComposeOrderDetailsCmdImpl {
   
    private static final String CLASSNAME = "com.custom.order.commands.order.commands.XComposeOrderDetailsCmdImpl";
      
    private static final Logger LOGGER = LoggingHelper.getLogger(CLASSNAME);
   
    @Override
    protected ShowOrderType composeOrderDetail(List avOrders) throws CommandException {
      ShowOrderType showOrderType  = super.composeOrderDetail(avOrders);
      List ordNouns =showOrderType.getDataArea().getOrder();
      try {
          for(Object ordNoun :ordNouns)
          {
              OrderAccessBean orderAccessBean = new OrderAccessBean();
              OrderType orderType = (OrderType)ordNoun;                    
              orderAccessBean.setInitKey_orderId(orderType.getOrderIdentifier().getUniqueID());
              orderAccessBean.refreshCopyHelper();                  
              UserDataType userData = orderType.getUserData();
            
              if(orderAccessBean != null) {                                     
                  if(userData == null) {
                      userData = CommerceFoundationFactory.eINSTANCE.createUserDataType();
                  }                 
                  orderType.setUserData(userData);
                  Map  userDataFields  = userData.getUserDataField();                
                  userDataFields.put("field1",orderAccessBean.getField1());
                   }
             
          }
      } catch(Exception exception) {
          System.out.println(exception);
      }
      return showOrderType;
    }
}


  • Register the new commands
INSERT INTO CMDREG(STOREENT_ID, INTERFACENAME, CLASSNAME, TARGET)
VALUES (0,'com.ibm.commerce.orderitems.commands.ExtendOrderItemProcessCmd',
'com.ibm.commerce.custom.order.commands.ExtExtendOrderItemProcessCmdImpl','Local');
   
UPDATE CMDREG SET CLASSNAME ='com.ibm.commerce.custom.order.commands.ExtComposeOrderDetailsCmdImpl' WHERE INTERFACENAME='com.ibm.commerce.order.facade.server.commands.ComposeOrderCmd+IBM_Details';


  • Register the new Client API
    •     In WebSphere Commerce Developer, open the Enterprise Explorer view , and expand Stores > WebContent > WEB-INF .
    •     Open struts-config-ext.xml.
    •     Copy the following text into the bottom of the file, before the </struts-config> tag:
    <plug-in className="com.ibm.commerce.struts.ComponentPlugIn">
            <set-property property="componentId" value="order"/>
            <set-property property="clientFacadeClassName" value="com.ibm.commerce.custom.order.commands.ExtOrderFacadeClient"/>
    </plug-in>

    •     Save and close the file.
Hit the following URL and observe that the ORDERS.FIELD1 value is 5. Without this customization, field1 will be null.

http://localhost/webapp/wcs/stores/servlet/OrderChangeServiceItemAdd?field1=5&doInventory=N&catEntryId=12345&calculationUsage=-1%2C-3&calculateOrder=1&orderId=.&langId=-20&storeId=11151&URL=ShopCartDisplayView&quantity=1

For more details please refer to the below link in infocenter.

http://www.ibm.com/support/knowledgecenter/SSZLC2_7.0.0/com.ibm.commerce.component-services.doc/tutorial/twvuserdatatutorial.htm


1 comment: