您的位置:首页 > 其它

WCS学习笔记(第十一天) - 创建MyOrderItemAddCmdImpl实现重写加入购物车逻辑

2013-10-15 11:21 344 查看
1. 创建MyOrderItemAddCmdImpl:

a. 右键
WebSphereCommerceServerExtensionsLogic >Java Resources >src
, 选择New >Package

b. 在 File name 项中输入com.ibm.commerce.sample.commands
, 点击
Finish


c. 右键
com.ibm.commerce.sample.commands
, 选择 New >Class

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");
}
}

}


参考: http://pic.dhe.ibm.com/infocenter/wchelp/v7r0m0/topic/com.ibm.commerce.developer.tutorial.doc/tutorial/ttd22.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: