Customizing the component façade and task command to support customization of the OrderItem noun part using the UserData field
In this lesson, you learn how to extend the ComposeTransferOrderCmdImpl
to include the new engraving attributes for the outbound service.
Procedure
- Open WebSphere Commerce Developer.
- In the Enterprise Explorer view, click WebSphereCommerceServerExtensionsLogic > src > com.mycompany.commerce.customization.order.
- In the com.mycompany.commerce.customization.order
package,
create a new class called ExtComposeTransferOrderCmdImpl that extends
ComposeTransferOrderCmdImpl.
- Right-click com.mycompany.commerce.customization.order
- Select New > Class. In the name field, type ExtComposeTransferOrderCmdImpl and click Finish.
- Open ExtComposeTransferOrderCmdImpl.java and replace
the
contents with the following code:
package com.mycompany.commerce.customization.order; import java.rmi.RemoteException; import java.util.Enumeration; import java.util.Hashtable; import javax.ejb.CreateException; import javax.ejb.FinderException; import javax.naming.NamingException; import com.ibm.commerce.exception.ECException; import com.ibm.commerce.exception.ECSystemException; 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.OrderItemType; import com.ibm.commerce.order.facade.server.commands.ComposeTransferOrderCmdImpl; import com.ibm.commerce.order.objects.OrderItemAccessBean; import com.ibm.commerce.ras.ECMessage; import com.ibm.commerce.utf.objects.PAttrProdAccessBean; import com.ibm.commerce.utf.objects.PAttrValueAccessBean; import com.ibm.commerce.utf.objects.PAttributeAccessBean; public class ExtComposeTransferOrderCmdImpl extends ComposeTransferOrderCmdImpl { private static final String CLASSNAME = ExtComposeTransferOrderCmdImpl.class.getName(); private static final java.util.logging.Logger LOGGER = com.ibm.commerce.foundation.common.util.logging.LoggingHelper .getLogger(ExtComposeTransferOrderCmdImpl.class); protected OrderItemType composeOrderItem(OrderItemAccessBean aabOrderItem) throws ECException { final String METHODNAME = "ExtcomposeOrderItem"; if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isEntryExitTraceEnabled(LOGGER)){ LOGGER.entering(CLASSNAME, METHODNAME); } LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, "ExtComposeTransferOrderCmdImpl OrderItemId"); OrderItemType orderItem = super.composeOrderItem(aabOrderItem); try { // customization code here String orderItem_id=aabOrderItem.getOrderItemId(); if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isTraceEnabled(LOGGER)){ LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, "OrderItemId = " + orderItem_id); } Hashtable processedAttrs = new Hashtable(); //get all attributes for the orderitem PAttrValueAccessBean pAttValueAccessBean = new PAttrValueAccessBean(); Enumeration ee = pAttValueAccessBean.findByOrderItemId(new Long(orderItem_id)); //add all attributes that have values. while(ee.hasMoreElements()){ pAttValueAccessBean =(PAttrValueAccessBean)ee.nextElement(); String attrId=(String)(pAttValueAccessBean.getAttributeId()); if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isTraceEnabled(LOGGER)){ LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, "PAttributeId= " + attrId); } //get value for the attribute String value=(String)(pAttValueAccessBean.getPAttrValue()); if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isTraceEnabled(LOGGER)){ LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, " engraving information value is "+ value); } processedAttrs.put(attrId,""); //get name of the attribute PAttributeAccessBean pAttAccessBean = new PAttributeAccessBean(); pAttAccessBean.setInitKey_referenceNumber(Long.valueOf(attrId)); String name=pAttAccessBean.getName(); if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isTraceEnabled(LOGGER)){ LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, "engraving information name is "+ name); } UserDataType userData = orderItem.getUserData(); if (userData == null) { userData = CommerceFoundationFactory.eINSTANCE.createUserDataType(); orderItem.setUserData(userData); } userData.getUserDataField().put(name, value); } //scan pattrprod table to check the attributes that do not have the values PAttrProdAccessBean pAttrProdAB = new PAttrProdAccessBean(); Enumeration pAttrProdABs = pAttrProdAB.findByCatentryId(new Long(orderItem.getCatalogEntryIdentifier().getUniqueID())); if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isTraceEnabled(LOGGER)){ LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, "CatalogEntryID is "+orderItem.getCatalogEntryIdentifier().getUniqueID()); } while(pAttrProdABs.hasMoreElements()){ pAttrProdAB=(PAttrProdAccessBean)pAttrProdABs.nextElement(); String attrId1=(String)(pAttrProdAB.getAttributeId()); Enumeration keys=processedAttrs.keys(); boolean nameWithValue=false; while ( keys.hasMoreElements() ){ String s=keys.nextElement().toString(); if(attrId1.equalsIgnoreCase(s)){ nameWithValue=true; } } if(!nameWithValue){ PAttributeAccessBean pAttAccessBean1 = new PAttributeAccessBean(); pAttAccessBean1.setInitKey_referenceNumber(new Long(attrId1)); String name1=pAttAccessBean1.getName(); if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isTraceEnabled(LOGGER)){ LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL,CLASSNAME, METHODNAME, "engraving information name is"+ name1); } UserDataType userData = orderItem.getUserData(); if (userData == null) { userData = CommerceFoundationFactory.eINSTANCE.createUserDataType(); orderItem.setUserData(userData); } userData.getUserDataField().put(name1, ""); } } } catch (CreateException e) { throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION, CLASSNAME, METHODNAME, new Object[] { e.toString() }, e); } catch (FinderException e) { throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION, CLASSNAME, METHODNAME, new Object[] { e.toString() }, e); } catch (NamingException e) { throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION, CLASSNAME, METHODNAME, new Object[] { e.toString() }, e); } catch (RemoteException e) { throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION, CLASSNAME, METHODNAME, new Object[] { e.toString() }, e); } if (com.ibm.commerce.foundation.common.util.logging.LoggingHelper.isEntryExitTraceEnabled(LOGGER)){ LOGGER.exiting(CLASSNAME, METHODNAME); } return orderItem; } }
- After entering the new code for the Java class, right-click in the editor and select Source > Organize Imports. This ensures that the redundant imported packages are cleaned up.
-
Register your new command:
- Open
a Web browser and navigate to the following URL:
- http://localhost/webapp/wcs/admin/servlet/db.jsp
- Enter the following SQL statement
to register the new
commands:
update cmdreg set CLASSNAME = 'com.mycompany.commerce.customization.order.ExtComposeTransferOrderCmdImpl' where INTERFACENAME = 'com.ibm.commerce.order.facade.server.commands.ComposeOrderCmd+IBM_TransferOrder';
- Open
a Web browser and navigate to the following URL: