1. 创建MyOrderItemAddCmdImpl:
a. 右键 , 选择
b. 在 File name 项中输入com.ibm.commerce.sample.commands , 点击 Finish
c. 右键 , 选择
d. 在 File name 项中输入 MyOrderItemAddCmdImpl , 点击 Browse 输入 OrderItemAddCmdImpl ,点击 Ok , 点击 Add 输入 OrderItemAddCmd 点击 OK , 点击 Finish , 代码如下:
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");
}
}
}