Tutorials > Program model > Modify an existing controller command
Create MyOrderItemAddCmdImpl
You will create the new MyOrderItemAddCmdImpl class that implements the interface defined in OrderItemAddCmd. The MyOrderItemAddCmdImpl class contains the code that ensures a customer's shopping cart contains five or fewer items.
Procedure
- In WebSphere Commerce Developer, in the Enterprise Explorer view, navigate to Other Projects > WebSphereCommerceServerExtensionsLogic > src.
- If the com.ibm.commerce.sample.commands package does not already exist in the project, create the package:
- From the src folder's pop-up menu select New > Package.
- In the Name field, enter com.ibm.commerce.sample.commands.
- Click Finish.
- Select the com.ibm.commerce.sample.commands package and from its pop-up menu select New > Class.
- In the New Java Class wizard:
The source code for the MyOrderItemAddCmdImpl class displays.
- In the Name field, enter MyOrderItemAddCmdImpl.
- Click the Browse button to the right of the Superclass field.
- Type OrderItemAddCmdImpl and click OK.
- Click Add, then enter OrderItemAddCmd and click OK.
- Click Finish.
- Add business logic and exception handling
The business logic and exception handling determines if there are five or more items in the customer's shopping cart before allowing the customer to add more items to the shopping cart.
To add business logic and exception handling:
- In the Outline view, select the MyOrderItemAddCmdImpl class. The following code displays:
public class MyOrderItemAddCmdImpl extends OrderItemAddCmdImpl {
- Add a new performExecute method to this class. This method contains the logic to check the number of items in the shopping cart. If the number of items already in the shopping cart is less than five, the method calls the performExecute method of the superclass (OrderItemAddCmdImpl) as normal. If the number of items in the shopping cart is five or more, an exception is thrown and the user cannot add more items to the cart. Note: With this restriction, the user should never be able to have more than five items in the shopping cart. However, it is good programming practice to account for any items more than five rather than coding for when there are exactly five items in the cart in case of any unforeseen circumstances.
To add this method, copy the following source code into the class, ensuring that it is included before the last closing brace (}) that denotes the end of the class:
public void performExecute() throws ECException { // Get a list of order ids String[] orderIds = getOrderId(); // Check to make sure that an id exists at all // if order ID exists then get number of items in the order // else if no order ID exists then execute normal code if (orderIds != null && orderIds.length > 0) { // An exception should be thrown when trying to add a sixth item // to the shopping cart. This code runs before an item is added and // throws an exception if there are 5 or more items in the cart. if (itemsInOrder(orderIds[0]) >= 5) { throw new ECApplicationException( MyNewMessages._ERR_TOO_MANY_ITEMS, this.getClass().getName(), "performExecute"); } //else perform normal flow } super.performExecute(); } //get number of items in the order protected int itemsInOrder(String orderId) throws ECException { try { OrderAccessBean order = new OrderAccessBean(); order.setInitKey_orderId(orderId); order.refreshCopyHelper(); return order.getOrderItems().length; } catch (javax.ejb.FinderException e) { throw new ECSystemException( ECMessage._ERR_FINDER_EXCEPTION, this.getClass().getName(), "itemsInOrder"); } catch (javax.naming.NamingException e) { throw new ECSystemException( ECMessage._ERR_NAMING_EXCEPTION, this.getClass().getName(), "itemsInOrder"); } catch (java.rmi.RemoteException e) { throw new ECSystemException( ECMessage._ERR_REMOTE_EXCEPTION, this.getClass().getName(), "itemsInOrder"); } catch (javax.ejb.CreateException e) { throw new ECSystemException( ECMessage._ERR_CREATE_EXCEPTION, this.getClass().getName(), "itemsInOrder"); } }
Tip: After pasting the new code into the class, select Source > Format to format the code for ease of reading.
- From the Source menu, select Organize Imports to add the required import statements to the class.
- Save the work.
WebSphere Commerce provides facilities for logging. The purpose of logging messages in the WebSphere Commerce Server is to record unexpected errors or abnormal conditions within the WebSphere Commerce application. IBM recommends that you use the WebSphere Application Server recommended tracing. See the Working with tracetopic in the WebSphere Application Server Information Center for more information.