Create MyOrderItemAddCmdImpl

In this lesson, you create the 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

  1. In WebSphere Commerce Developer, in the Enterprise Explorer view, navigate to Utility Projects > WebSphereCommerceServerExtensionsLogic > src.
  2. If the com.ibm.commerce.sample.commands package does not exist in the project, create the package:
    1. Right click the src folder, select New > Package.
    2. In the Name field, enter com.ibm.commerce.sample.commands.
    3. Click Finish.
  3. Right click the com.ibm.commerce.sample.commands package and select New > Class.
  4. In the New Java Class wizard:
    1. In the Name field, enter MyOrderItemAddCmdImpl.
    2. Click the Browse button to the right of the Superclass field.
    3. Type OrderItemAddCmdImpl and click OK.
    4. Click Add, then enter OrderItemAddCmd and click OK.
    5. Click Finish.
    The source code for the MyOrderItemAddCmdImpl class displays.
  5. Adding business logic and exception handling
    The business logic and exception handling determines whether there are five or more items in the shopping cart. If there are five or more items in the cart, the customer is not allowed to add more items to the cart. To add business logic and exception handling:
    1. In the Outline view, select the MyOrderItemAddCmdImpl class. The following code displays:
      
      public class MyOrderItemAddCmdImpl extends OrderItemAddCmdImpl { 
      
    2. Add a 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 there are five or more items in the shopping cart, an exception is thrown and the user cannot add more items to the cart.
      Note: With this restriction, the user will 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. Ensure that the code 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.
    3. From the Source menu, select Organize Imports to add the required import statements to the class.
    4. Save your work.
    Note: 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. It is recommended that you use the WebSphere Application Server recommended tracing. For more information, see the Working with trace topic.