Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.matel.service.impl; import java.io.Serializable; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.matel.dao.ShoppingCartDAO; import com.matel.data.SalesTaxData; import com.matel.data.ShippingMethodData; import com.matel.data.ShoppingCartData; import com.matel.data.ShoppingCartItemData; import com.matel.domain.ShippingMethod; import com.matel.domain.ShoppingCart; import com.matel.domain.ShoppingCartItem; import com.matel.domain.States; import com.matel.domain.UserProfile; import com.matel.service.ShoppingCartService; import com.matel.utils.SpringComponent; @Service(ShoppingCartServiceImpl.SERVICE_NAME) @Scope(SpringComponent.SCOPE_REQUEST) public class ShoppingCartServiceImpl implements ShoppingCartService, Serializable { private static final long serialVersionUID = 1L; public static final String SERVICE_NAME = "service_impl_shoppingCartServiceImpl"; @Autowired private ShoppingCartDAO shoppingCartDAO; @Override @Transactional public void saveShoppingCart(ShoppingCartData cartData) { ShoppingCart shoppingCart; if (cartData.getCartId() == 0) { shoppingCart = prepareNewShoppingCartEntity(cartData); shoppingCart = shoppingCartDAO.save(shoppingCart); cartData.setCartId(shoppingCart.getShpCartId()); saveCartItems(shoppingCart, cartData); } else { shoppingCart = shoppingCartDAO.findByCartId(cartData.getCartId()); updateCartItems(shoppingCart, cartData); shoppingCart.setDateModified(new Date()); shoppingCartDAO.save(shoppingCart); } } private ShoppingCart prepareNewShoppingCartEntity(ShoppingCartData cart) { ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.setDateCreated(new Date()); shoppingCart.setDateModified(new Date()); shoppingCart.setUserId(cart.getCustomer()); return shoppingCart; } private void saveCartItems(ShoppingCart shoppingCart, ShoppingCartData cartData) { if (!cartData.getCartItems().isEmpty()) { for (ShoppingCartItemData cartItemData : cartData.getCartItems()) { ShoppingCartItem cartItem = new ShoppingCartItem(); cartItem.setProductId(cartItemData.getProduct()); cartItem.setDateCreated(new Date()); cartItem.setDateModified(new Date()); cartItem.setItemPrice(cartItemData.getProductPrice()); cartItem.setQuantity(cartItemData.getQuantity()); cartItem.setShpCartId(shoppingCart); cartItem.setFinishId(cartItemData.getFinishedProductId()); cartItem.setFinishName(cartItemData.getFinishedProductName()); shoppingCartDAO.saveCartItem(cartItem); } } } private void updateCartItems(ShoppingCart shoppingCart, ShoppingCartData cartData) { if (!cartData.getCartItems().isEmpty()) { for (ShoppingCartItemData cartItemData : cartData.getCartItems()) { ShoppingCartItem cartItem = shoppingCartDAO.findCartItem(shoppingCart.getShpCartId(), cartItemData.getProductId(), cartItemData.getFinishedProductId()); if (null == cartItem) { cartItem = new ShoppingCartItem(); cartItem.setProductId(cartItemData.getProduct()); cartItem.setDateCreated(new Date()); cartItem.setDateModified(new Date()); cartItem.setItemPrice(cartItemData.getProductPrice()); cartItem.setQuantity(cartItemData.getQuantity()); cartItem.setShpCartId(shoppingCart); cartItem.setFinishId(cartItemData.getFinishedProductId()); cartItem.setFinishName(cartItemData.getFinishedProductName()); cartItem = shoppingCartDAO.saveCartItem(cartItem); } else { cartItem.setQuantity(cartItemData.getQuantity()); cartItem = shoppingCartDAO.updateCartItem(cartItem); } } } } @Override @Transactional public void removeCartItem(ShoppingCartData shoppingCart, ShoppingCartItemData cartItemData) { ShoppingCartItem cartItem = shoppingCartDAO.findCartItem(shoppingCart.getCartId(), cartItemData.getProduct().getProductId(), cartItemData.getFinishedProductId()); if (null != cartItem) { shoppingCartDAO.removeCartItem(cartItem); } if (shoppingCart.getCartItems().isEmpty()) { shoppingCartDAO.removeShoppingCart(cartItem.getShpCartId()); } } @Override public void clearCartData(ShoppingCartData sc) { List<ShoppingCartItem> cartItems = shoppingCartDAO.findCartItemsByCartId(sc.getCartId()); shoppingCartDAO.removeAllCartItems(cartItems); ShoppingCart findByCartId = shoppingCartDAO.findByCartId(sc.getCartId()); shoppingCartDAO.removeShoppingCart(findByCartId); } @Override @Transactional public ShoppingCartData populateUserCartData(UserProfile profile, ShoppingCartData cartData) { ShoppingCart cart = shoppingCartDAO.findByUserId(profile.getUserid()); if (null != cart) { updateAvalibleItems(cart, cartData); mapCartData(cart, cartData); } else if (cartData.getCartId() != 0) { ShoppingCart findByCartId = shoppingCartDAO.findByCartId(cartData.getCartId()); findByCartId.setUserId(profile); shoppingCartDAO.save(findByCartId); cartData.setCustomer(profile); } return cartData; } private void mapCartData(ShoppingCart cart, ShoppingCartData cartData) { cartData.setCartId(cart.getShpCartId()); cartData.setCustomer(cart.getUserId()); BigDecimal totalAmount = BigDecimal.ZERO; for (ShoppingCartItem shoppingCartItem : cart.getShoppingCartItemSet()) { ShoppingCartItemData cartItemData = new ShoppingCartItemData(); cartItemData.setName(shoppingCartItem.getProductId().getProductName()); cartItemData.setProductPrice(shoppingCartItem.getItemPrice()); cartItemData.setQuantity(shoppingCartItem.getQuantity()); cartItemData.setFinishedProductId(shoppingCartItem.getFinishId()); cartItemData.setFinishedProductName(shoppingCartItem.getFinishName()); shoppingCartItem.getProductId().getQboProduct(); cartItemData.setProduct(shoppingCartItem.getProductId()); totalAmount = totalAmount .add(cartItemData.getProductPrice().multiply(new BigDecimal(cartItemData.getQuantity()))); cartData.getCartItems().add(cartItemData); } cartData.setSubTotal(totalAmount); } /** * * @param cart * @param cartData */ @Transactional private void updateAvalibleItems(ShoppingCart cart, ShoppingCartData cartData) { ShoppingCart shoppingCart = shoppingCartDAO.findByCartId(cartData.getCartId()); if (shoppingCart == null) { return; } for (ShoppingCartItem shoppingCartItem : shoppingCart.getShoppingCartItemSet()) { ShoppingCartItem findCartItem = shoppingCartDAO.findCartItem(cart.getShpCartId(), shoppingCartItem.getProductId().getProductId(), shoppingCartItem.getFinishId()); if (null != findCartItem) { findCartItem.setQuantity(findCartItem.getQuantity() + shoppingCartItem.getQuantity()); shoppingCartDAO.updateCartItem(findCartItem); shoppingCartDAO.removeCartItem(shoppingCartItem); continue; } shoppingCartItem.setShpCartId(cart); shoppingCartDAO.updateCartItem(shoppingCartItem); // shoppingCart.getShoppingCartItemSet().remove(shoppingCartItem); } shoppingCart.getShoppingCartItemSet().clear(); cartData.getCartItems().clear(); shoppingCartDAO.removeShoppingCart(shoppingCart); cart = shoppingCartDAO.findByUserId(cart.getUserId().getUserid()); } @Override @Transactional public void updateItemQuantity(ShoppingCartData shoppingCart, ShoppingCartItemData cartItemData) { ShoppingCartItem findCartItem = shoppingCartDAO.findCartItem(shoppingCart.getCartId(), cartItemData.getProduct().getProductId(), cartItemData.getFinishedProductId()); if (null != findCartItem) { Integer oldQuantity = findCartItem.getQuantity(); Integer newQuantity = cartItemData.getQuantity(); if (oldQuantity != newQuantity) { shoppingCart.setSubTotal(shoppingCart.getSubTotal() .subtract(findCartItem.getItemPrice().multiply(new BigDecimal(oldQuantity))) .add(findCartItem.getItemPrice().multiply(new BigDecimal(newQuantity)))); findCartItem.setQuantity(newQuantity); shoppingCartDAO.updateCartItem(findCartItem); } } } @Override public void prepareTaxData(String state, ShoppingCartData shoppingCart) { States findStateByStateCode = shoppingCartDAO.findStateByStateCode(state); if (null != findStateByStateCode) { if (findStateByStateCode.getStateSalesTax() != null) { SalesTaxData taxData = new SalesTaxData(); taxData.setState(findStateByStateCode); taxData.setTax(findStateByStateCode.getStateSalesTax().getTaxRate()); taxData.setTaxCharge(shoppingCart.getSubTotal().multiply(taxData.getTax()) .divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)); shoppingCart.setSalesTaxData(taxData); shoppingCart .setTotal(shoppingCart.getSubTotal().add(shoppingCart.getSalesTaxData().getTaxCharge())); } } } @Override public void prepareShippingData(String selectedShipping, ShoppingCartData shoppingCart) { ShippingMethod shippingMethod = shoppingCartDAO.findShippingMethodById(Integer.parseInt(selectedShipping)); if (null != shippingMethod) { ShippingMethodData data = new ShippingMethodData(); data.setShippingMethod(shippingMethod); data.setShippingCharges(shippingMethod.getShipCharge()); data.setShippingMethodId(shippingMethod.getShipMethodId()); shoppingCart.setShippingMethodData(data); shoppingCart.setTotal( shoppingCart.getTotal().add(shoppingCart.getShippingMethodData().getShippingCharges())); } } }