Create MyOrderItemAddCmdImpl

In this lesson, you create the MyOrderItemAddCmdImpl class that implements the interface that is defined within the OrderItemAddCmd class. Your new MyOrderItemAddCmdImpl class must include the code to ensure that a customer shopping cart includes five, or fewer, items.

Procedure

  1. In the Enterprise Explorer view, expand WebSphereCommerceServerExtensionsLogic > src.
  2. Within the src directory, create the com.ibm.commerce.sample.commands package.
    1. Right click the src directory and select New > Package.
    2. Enter com.ibm.commerce.sample.commands in the Name field.
    3. Click Finish.
  3. Create the MyOrderItemAddCmdImpl.java class.
    1. Right click the new com.ibm.commerce.sample.commands package and select New > Class.
    2. In the New Java Class wizard, enter MyOrderItemAddCmdImpl in the Name field.
    3. Click the Browse button to the right of the Superclass field.
    4. Type OrderItemAddCmdImpl and click OK.
    5. Click Add, then enter OrderItemAddCmd and click OK.
    6. Click Finish.
    Your new MyOrderItemAddCmdImpl.java class can resemble the following code:
    package com.ibm.commerce.sample.commands;
    
    import com.ibm.commerce.orderitems.commands.OrderItemAddCmd;
    import com.ibm.commerce.orderitems.commands.OrderItemAddCmdImpl;
    
    public class MyOrderItemAddCmdImpl extends OrderItemAddCmdImpl implements
      OrderItemAddCmd {
    }
    
  4. Add the business logic and exception handling in your new class.
    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. Within your new class, add the following code 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");
          }
        }
      

      This code add a performExecute method for checking 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.

    2. From the Source menu, select Organize Imports to add the import statements to the class.
      Your completed class can resemble the following code:
      
      package com.ibm.commerce.sample.commands;
      
      import com.ibm.commerce.exception.ECApplicationException;
      import com.ibm.commerce.exception.ECException;
      import com.ibm.commerce.exception.ECSystemException;
      import com.ibm.commerce.order.objects.OrderAccessBean;
      import com.ibm.commerce.orderitems.commands.OrderItemAddCmd;
      import com.ibm.commerce.orderitems.commands.OrderItemAddCmdImpl;
      import com.ibm.commerce.ras.ECMessage;
      import com.ibm.commerce.sample.messages.MyNewMessages;
      
      public class MyOrderItemAddCmdImpl extends OrderItemAddCmdImpl implements
        OrderItemAddCmd {
      
          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");
          }
        }
      }
      
    3. Save your work.