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.components; import java.io.IOException; import java.io.Serializable; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import org.springframework.context.annotation.Scope; import com.matel.components.authentication.CustomerProfile; import com.matel.data.ShoppingCartData; import com.matel.data.ShoppingCartItemData; import com.matel.domain.FinishedProduct; import com.matel.domain.Product; import com.matel.domain.UserProfile; import com.matel.service.MatelProductService; import com.matel.service.ShoppingCartService; import com.matel.utils.Component; import com.matel.utils.MatelValidator; import com.matel.utils.SpringComponent; /** * * @author badri */ @org.springframework.stereotype.Component(ShoppingCartModel.COMP_NAME) @Scope(SpringComponent.SCOPE_SESSION) public class ShoppingCartModel implements Serializable { private static final long serialVersionUID = 1L; public static final String COMP_NAME = "components_shoppingCartModel"; MatelProductService matelProductInterface = (MatelProductService) SpringComponent .getInstance("service_impl_matelProductServiceImpl"); ShoppingCartService cartService = (ShoppingCartService) SpringComponent .getInstance("service_impl_shoppingCartServiceImpl"); private ShoppingCartData shoppingCart; private String state; private String selectedShipping; private String countryCode; private String postalCode; private boolean itemAdded; private CardInformation cardInfo; private BillingInformation billingInfo; private BillingInformation shippingInfo; private SpecialInstructions specialInfo; private boolean copyBilllingToShipping; private String purchaserName; private String address; private String suite; private String city; private String phoneNumber1; private String phoneNumber2; private String phoneNumber3; private String fax; private String email; private String instructions; private String cardHolderName; private String cardType; private String creditCard; private String expMonth; private String expYear; private String ccvCode; private Boolean hasErrors = Boolean.FALSE; public ShoppingCartData getShoppingCart() { return shoppingCart; } /** * * @param subCategory * @return */ public String addCartItem(ProductSubCategory subCategory) { FacesContext ctx = FacesContext.getCurrentInstance(); if (shoppingCart == null) { shoppingCart = new ShoppingCartData(); shoppingCart.setCartItems(new ArrayList<ShoppingCartItemData>()); } boolean valid = MatelValidator.validShopingItem(ctx, subCategory); if (!valid) { return null; } else { boolean itemFound = false; for (ShoppingCartItemData shoppingCartItem : shoppingCart.getCartItems()) { if (subCategory.getProductId() != null && shoppingCartItem.getProductId() != null) { if (subCategory.getProductId().intValue() == shoppingCartItem.getProductId().intValue()) { if (!isFinishProductSame(subCategory, shoppingCartItem)) { continue; } shoppingCartItem .setQuantity(shoppingCartItem.getQuantity() + subCategory.getItemQuantity()); //shoppingCart.setTotal(shoppingCart.getTotal().add(shoppingCartItem.getProductPrice())); itemFound = true; shoppingCart.setSubTotal(shoppingCart.getSubTotal().add(shoppingCartItem.getProductPrice() .multiply(new BigDecimal(shoppingCartItem.getQuantity())))); break; } } } if (!itemFound) { ShoppingCartItemData prepareShoppingCartItem = prepareShoppingCartItem(subCategory); if (null != prepareShoppingCartItem) { shoppingCart.getCartItems().add(prepareShoppingCartItem); } } CustomerProfile custProfile = CustomerProfile.getInstance(); if (custProfile.isLoggedIn()) { custProfile.getUserProfile().getQboUserProfile(); shoppingCart.setCustomer(custProfile.getUserProfile()); } cartService.saveShoppingCart(shoppingCart); itemAdded = true; } return null; } /** * * @param subCategory * @return */ private ShoppingCartItemData prepareShoppingCartItem(ProductSubCategory subCategory) { ShoppingCartItemData cartItem = new ShoppingCartItemData(); Product productById = matelProductInterface.getProductById(subCategory.getProductId()); if (subCategory.getFinishedId() != null) { FinishedProduct finishedProduct = matelProductInterface.getFinishedProduct(productById.getProductId(), subCategory.getFinishedId()); cartItem.setFinishedProductId(finishedProduct.getFinishlookup().getId()); cartItem.setFinishedProductName(finishedProduct.getFinishlookup().getName()); productById.setPrice(finishedProduct.getRate()); productById.setProductName(productById.getProductName()); cartItem.setFinishedProductId(finishedProduct.getFinishlookup().getId()); } cartItem.setName(productById.getProductName()); cartItem.setProductId(productById.getProductId()); cartItem.setProduct(productById); cartItem.setQuantity(subCategory.getItemQuantity()); cartItem.setProductPrice(productById.getPrice()); shoppingCart.setSubTotal(shoppingCart.getSubTotal() .add(cartItem.getProductPrice().multiply(new BigDecimal(cartItem.getQuantity())))); if (productById.getPrice().compareTo(BigDecimal.ZERO) == 0) { return null; } return cartItem; } /** * * @param cartItem */ public void removeCartItem(ShoppingCartItemData cartItem) { for (ShoppingCartItemData shoppingCartItem : shoppingCart.getCartItems()) { if (shoppingCartItem.equals(cartItem)) { shoppingCart.setSubTotal(shoppingCart.getSubTotal() .subtract(cartItem.getProductPrice().multiply(new BigDecimal(cartItem.getQuantity())))); shoppingCart.getCartItems().remove(shoppingCartItem); cartService.removeCartItem(shoppingCart, shoppingCartItem); break; } } if (shoppingCart.getCartItems().isEmpty()) { shoppingCart.setSubTotal(BigDecimal.ZERO); } } public void updateSalesTaxData() { if (null != state) { cartService.prepareTaxData(state, shoppingCart); } } public void updateShippingMethod() { if (null != selectedShipping) { cartService.prepareShippingData(selectedShipping, shoppingCart); } } /** * * @return */ public List<ShoppingCartItemData> getShoppingCartItems() { if (shoppingCart == null) { shoppingCart = new ShoppingCartData(); shoppingCart.setCartItems(new ArrayList<ShoppingCartItemData>()); } return shoppingCart.getCartItems(); } /** * * @return */ public static ShoppingCartModel getInstance() { return (ShoppingCartModel) Component.getInstance(COMP_NAME); } public Float getTotal() { if (shoppingCart.getCartItems() == null || shoppingCart.getCartItems().isEmpty()) { return 0f; } Float total = 0f; // Sum up the quantities for (ShoppingCartItemData cartItem : shoppingCart.getCartItems()) { total += (cartItem.getProductPrice().multiply(new BigDecimal(cartItem.getQuantity()))).floatValue(); } return total; } public void onItemQuantityChange(ValueChangeEvent e) { System.out.println(e.getNewValue()); } public void resetCart() { shoppingCart = new ShoppingCartData(); shoppingCart.setCartItems(new ArrayList<ShoppingCartItemData>()); } private Boolean isFinishProductSame(ProductSubCategory subCategory, ShoppingCartItemData shoppingCartItem) { if (null != subCategory.getFinishedId()) { if (subCategory.getFinishedId().intValue() == shoppingCartItem.getFinishedProductId().intValue()) { return true; } } return false; } public String getState() { return state; } public void setState(String state) { this.state = state; } public void getCart() { FacesContext ctx = FacesContext.getCurrentInstance(); ExternalContext ec = ctx.getExternalContext(); try { ec.redirect(ec.getRequestContextPath() + "/views/placeOrder.jsf"); } catch (IOException ioe) { ioe.printStackTrace(); } } public void proceedToCheckOut() { FacesContext ctx = FacesContext.getCurrentInstance(); ExternalContext ec = ctx.getExternalContext(); CustomerProfile custProfile = CustomerProfile.getInstance(); try { if (custProfile.isLoggedIn()) { ec.redirect(ec.getRequestContextPath() + "/views/placeOrder.jsf"); } else { ec.redirect(ec.getRequestContextPath() + "/views/login.jsf"); } } catch (IOException ioe) { ioe.printStackTrace(); } } public void updateQuantity() { System.out.println(" update quantity ......"); } public void updateQuantity(ShoppingCartItemData cartItemData) { cartService.updateItemQuantity(shoppingCart, cartItemData); } public void loadUserCart(UserProfile profile) { if (null == shoppingCart) { this.resetCart(); } // this.resetCart(); cartService.populateUserCartData(profile, shoppingCart); } public String getSelectedShipping() { return selectedShipping; } public void setSelectedShipping(String selectedShipping) { this.selectedShipping = selectedShipping; } public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getPostalCode() { return postalCode; } public void setPostalCode(String postalCode) { this.postalCode = postalCode; } public boolean isItemAdded() { return itemAdded; } public void setItemAdded(boolean itemAdded) { this.itemAdded = itemAdded; } public CardInformation getCardInfo() { if (cardInfo == null) { cardInfo = new CardInformation(); } return cardInfo; } public void setCardInfo(CardInformation cardInfo) { this.cardInfo = cardInfo; } public BillingInformation getBillingInfo() { if (billingInfo == null) { billingInfo = new BillingInformation(); } return billingInfo; } public void setBillingInfo(BillingInformation billingInfo) { this.billingInfo = billingInfo; } public BillingInformation getShippingInfo() { if (shippingInfo == null) { shippingInfo = new BillingInformation(); } return shippingInfo; } public void setShippingInfo(BillingInformation shippingInfo) { this.shippingInfo = shippingInfo; } public SpecialInstructions getSpecialInfo() { if (specialInfo == null) { specialInfo = new SpecialInstructions(); } return specialInfo; } public void setSpecialInfo(SpecialInstructions specialInfo) { this.specialInfo = specialInfo; } public boolean isCopyBilllingToShipping() { return copyBilllingToShipping; } public void setCopyBilllingToShipping(boolean copyBilllingToShipping) { this.copyBilllingToShipping = copyBilllingToShipping; } public String getPurchaserName() { return purchaserName; } public void setPurchaserName(String purchaserName) { this.purchaserName = purchaserName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSuite() { return suite; } public void setSuite(String suite) { this.suite = suite; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPhoneNumber1() { return phoneNumber1; } public void setPhoneNumber1(String phoneNumber1) { this.phoneNumber1 = phoneNumber1; } public String getPhoneNumber2() { return phoneNumber2; } public void setPhoneNumber2(String phoneNumber2) { this.phoneNumber2 = phoneNumber2; } public String getPhoneNumber3() { return phoneNumber3; } public void setPhoneNumber3(String phoneNumber3) { this.phoneNumber3 = phoneNumber3; } public String getFax() { return fax; } public void setFax(String fax) { this.fax = fax; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getInstructions() { return instructions; } public void setInstructions(String instructions) { this.instructions = instructions; } public Boolean getHasErrors() { return hasErrors; } public void setHasErrors(Boolean hasErrors) { this.hasErrors = hasErrors; } public String getCardHolderName() { return cardHolderName; } public void setCardHolderName(String cardHolderName) { this.cardHolderName = cardHolderName; } public String getCardType() { return cardType; } public void setCardType(String cardType) { this.cardType = cardType; } public String getCreditCard() { return creditCard; } public void setCreditCard(String creditCard) { this.creditCard = creditCard; } public String getExpMonth() { return expMonth; } public void setExpMonth(String expMonth) { this.expMonth = expMonth; } public String getExpYear() { return expYear; } public void setExpYear(String expYear) { this.expYear = expYear; } public String getCcvCode() { return ccvCode; } public void setCcvCode(String ccvCode) { this.ccvCode = ccvCode; } }