⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcartbean.java

📁 用JBuilder+Jboos+mysql实现的EJB项目
💻 JAVA
字号:
package com.jdon.estore.cart;

import javax.ejb.*;

import java.util.*;

import com.jdon.estore.model.Item;
import com.jdon.estore.model.CartItem;
import com.jdon.estore.model.ShoppingCart;

import com.jdon.estore.catalog.CatalogEJBLocal;
import com.jdon.estore.catalog.CatalogEJBLocalHome;
import com.jdon.controller.model.PageIterator;

import com.jdon.servicelocator.ejb.ServiceLocator;
import com.jdon.controller.events.EventModel;

import org.apache.log4j.Logger;
import com.jdon.estore.JNDINames;

public class ShoppingCartBean implements SessionBean {

  private final static Logger logger = Logger.getLogger(ShoppingCartBean.class);

  SessionContext sessionContext;
  private Map cart;
  private CatalogEJBLocalHome catalogEJBHome;

  public void ejbCreate() throws CreateException {
    cart = new HashMap();
    try {
      ServiceLocator sl = new ServiceLocator();
      catalogEJBHome = (CatalogEJBLocalHome) sl.getLocalHome(JNDINames.
          CATALOG_EJBHOME);

    } catch (Exception ex) {
      logger.error("create error:" + ex);
      throw new CreateException();
    }
  }

  public void addCartItem(EventModel em) throws Exception {
    CartItem citem = (CartItem) em.getModel();
    try {
      CatalogEJBLocal catalogEJB = catalogEJBHome.create();
      Item item = catalogEJB.getItem(citem.getItemId());
      if (item == null)
        throw new Exception("no found this item id=" + citem.getItemId());

      logger.debug(" qty =" + citem.getQty());
      if (citem.getQty() < 1)
        citem.setQty(1); //因为是加入购物车,如果前台没有提供数量,缺省为1
      setCartItem(citem, item, citem.getQty());
      cart.put(citem.getItemId(), citem);
    } catch (Exception ex) {
      logger.error(ex);
      throw new Exception();
    }
  }

  private void setCartItem(CartItem citem, Item item, int qty) {
    citem.setItemId(item.getItemId());
    citem.setProductId(item.getProductId());
    citem.setName(item.getName());
    citem.setQty(qty);
    citem.setListprice(item.getListprice());
  }

  /**
   * 更新时,整个购物车都要进行更新,而不是一个cartItem更新
   * @param em
   * @throws java.lang.Exception
   */
  public void updateCart(EventModel em) throws Exception {
    ShoppingCart shoppingCart = (ShoppingCart) em.getModel();
    try {
      String[] itemIds = shoppingCart.getItemId();
      int[] qtys = shoppingCart.getQty();
      int length = itemIds.length;
      for (int i = 0; i < length; i++) {
        logger.debug(" itemid = "+ itemIds[i]+" qty=" + qtys[i]);
        updateCartItem(itemIds[i], qtys[i]);
      }
    } catch (Exception ex) {
      logger.error(ex);
      throw new Exception();
    }
  }

  private void updateCartItem(String itemId, int qty) throws Exception {
    try {
      CatalogEJBLocal catalogEJB = catalogEJBHome.create();
      if (qty > 0) {
        Item item = catalogEJB.getItem(itemId);
        if (item == null)
          throw new Exception("no found this item id=" + itemId);
        CartItem citem = getCartItem(item, qty);
        cart.put(itemId, citem);
      } else {
        cart.remove(itemId);
      }
    } catch (Exception ex) {
      logger.error(ex);
      throw new Exception();
    }
  }

  private CartItem getCartItem(Item item, int qty) {
    return new CartItem(item.getItemId(),
                        item.getProductId(),
                        item.getName(),
                        qty,
                        item.getListprice());
  }

  public void deleteCartItem(EventModel em) {
    CartItem citem = (CartItem) em.getModel();
    cart.remove(citem.getItemId());
  }

  public CartItem getCartItem(String itemId) {
    return (CartItem) cart.get(itemId);
  }

  public ShoppingCart getShoppingCart() {
    ShoppingCart shoppingCart = new ShoppingCart();
    shoppingCart.setItems(getCartItems());
    shoppingCart.setTotalCost(getStringifiedValue(getSubTotal()));
    return shoppingCart;

  }

  private Collection getCartItems() {
    ArrayList items = new ArrayList(cart.size());
    Iterator it = cart.keySet().iterator();
    while (it.hasNext()) {
      String itemId = (String) it.next();
      CartItem citem = (CartItem) cart.get(itemId);
      items.add(citem);
    }
    return items;
  }

  public Integer getCount() {
    return new Integer(cart.size());
  }

  private float getSubTotal() {
    Collection citems = getCartItems();
    float ret = 0.0f;
    for (Iterator it = citems.iterator(); it.hasNext(); ) {
      CartItem i = (CartItem) it.next();
      ret += (i.getListprice() * i.getQty());
    }
    return ret;
  }

  private  String getStringifiedValue(float value) {
          String subval = "0.00";
          if (value > 0.0) {
               subval = Float.toString(value);
               int decimal_len = subval.length() - (subval.lastIndexOf('.') + 1);
          if(decimal_len > 1)
                   subval = subval.substring(0, subval.lastIndexOf('.') + 3);
          else
                   subval += "0";
          }
          return subval;
     }


  public void empty() {
    cart.clear();
  }

  public void ejbRemove() {
  /**@todo Complete this method*/
  }

  public void ejbActivate() {
  /**@todo Complete this method*/
  }

  public void ejbPassivate() {
  /**@todo Complete this method*/
  }

  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -