📄 cartserviceimp.java
字号:
/*
* Copyright 2003-2005 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.jdon.framework.samples.jpetstore.service.bo;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.jdon.controller.events.EventModel;
import com.jdon.controller.model.PageIterator;
import com.jdon.controller.service.Stateful;
import com.jdon.framework.samples.jpetstore.domain.CartItem;
import com.jdon.framework.samples.jpetstore.domain.CartItems;
import com.jdon.framework.samples.jpetstore.domain.Item;
import com.jdon.framework.samples.jpetstore.service.CartService;
import com.jdon.framework.samples.jpetstore.service.ProductManager;
/**
* @author <a href="mailto:banqiao@jdon.com">banq</a>
*
*/
public class CartServiceImp implements CartService, Stateful {
private final static Logger logger = Logger.getLogger(CartServiceImp.class);
private ProductManager productManager;
private Map itemMap = Collections.synchronizedMap(new HashMap());
private List itemList = new ArrayList();
/**
* @param productManager
*/
public CartServiceImp(ProductManager productManager) {
super();
this.productManager = productManager;
}
/* (non-Javadoc)
* @see com.jdon.framework.samples.jpetstore.service.CartService#getAccount(java.lang.String)
*/
public CartItem getCartItem(String itemId) {
return (CartItem) itemMap.get(itemId);
}
/* (non-Javadoc)
* @see com.jdon.framework.samples.jpetstore.service.CartService#insertCartItem(com.jdon.controller.events.EventModel)
*/
public void addCartItem(EventModel em) {
CartItem cartItem = (CartItem) em.getModel();
String workingItemId = cartItem.getWorkingItemId();
if (itemMap.containsKey(workingItemId)) {
incrementQuantityByItemId(workingItemId);
} else {
// isInStock is a "real-time" property that must be updated
// every time an item is added to the cart, even if other
// item details are cached.
boolean isInStock = productManager.isItemInStock(workingItemId);
Item item = productManager.getItem(workingItemId);
addItem(item, isInStock);
}
}
public void incrementQuantityByItemId(String itemId) {
CartItem cartItem = (CartItem) itemMap.get(itemId);
int qrty = cartItem.getQuantity() + 1;
cartItem.setQuantity(qrty);
}
public void addItem(Item item, boolean isInStock) {
CartItem cartItem = (CartItem) itemMap.get(item.getItemId());
if (cartItem == null) {
cartItem = new CartItem();
cartItem.setItem(item);
cartItem.setQuantity(0);
cartItem.setInStock(isInStock);
itemMap.put(item.getItemId(), cartItem);
itemList.add(cartItem);
}
incrementQuantityByItemId(item.getItemId());
}
/* (non-Javadoc)
* @see com.jdon.framework.samples.jpetstore.service.CartService#updateCartItem(com.jdon.controller.events.EventModel)
*/
public void updateCartItems(EventModel em) {
CartItems cartItems = (CartItems) em.getModel();
try {
String[] itemIds = cartItems.getItemId();
int[] qtys = cartItems.getQuantity();
int length = itemIds.length;
for (int i = 0; i < length; i++) {
updateCartItem(itemIds[i], qtys[i]);
}
} catch (Exception ex) {
logger.error(ex);
}
}
private void updateCartItem(String itemId, int qty) throws Exception {
if (qty > 0) {
CartItem citem = (CartItem) itemMap.get(itemId);
citem.setQuantity(qty);
} else {
itemMap.remove(itemId);
}
}
public void deleteCartItem(EventModel em) {
CartItem cartItem = (CartItem) em.getModel();
cartItem = (CartItem) itemMap.remove(cartItem.getWorkingItemId());
if (cartItem != null) {
itemList.remove(cartItem);
} else
em.setErrors("Attempted to remove null CartItem from Cart.");
}
/* (non-Javadoc)
* @see com.jdon.framework.samples.jpetstore.service.CartService#getCartItemIDs()
*/
public PageIterator getCartItems(int start, int count) {
int offset = itemList.size() - start;
int pageCount = (count < offset) ? count : offset;
List pageList = new ArrayList(pageCount);
for (int i = start; i < pageCount + start; i++) {
pageList.add(itemList.get(i));
}
int allCount = itemList.size();
int currentCount = start + pageCount;
return new PageIterator(allCount, pageList.toArray(new CartItem[0]), start, (currentCount < allCount) ? true : false);
}
public BigDecimal getSubTotal() {
BigDecimal subTotal = new BigDecimal("0");
Iterator items = itemList.iterator();
while (items.hasNext()) {
CartItem cartItem = (CartItem) items.next();
Item item = cartItem.getItem();
BigDecimal listPrice = item.getListPrice();
BigDecimal quantity = new BigDecimal(String.valueOf(cartItem.getQuantity()));
subTotal = subTotal.add(listPrice.multiply(quantity));
}
return subTotal;
}
public List getAllCartItems(){
return itemList;
}
public void clear(){
this.itemList.clear();
this.itemMap.clear();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -