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

本文介绍如何通过继承OrderItemAddCmdImpl来创建自定义的MyOrderItemAddCmdImpl类,该类实现了OrderItemAddCmd接口,并重写了performExecute方法以限制购物车中的商品数量不超过五个。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值