use of com.salesmanager.shop.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartController method shoppingCart.
private String shoppingCart(final Model model, final HttpServletRequest request, final HttpServletResponse response, final Locale locale) throws Exception {
LOG.debug("Starting to calculate shopping cart...");
Language language = (Language) request.getAttribute(Constants.LANGUAGE);
// meta information
PageInformation pageInformation = new PageInformation();
pageInformation.setPageTitle(messages.getMessage("label.cart.placeorder", locale));
request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
Customer customer = getSessionAttribute(Constants.CUSTOMER, request);
/**
* there must be a cart in the session *
*/
String cartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
if (StringUtils.isBlank(cartCode)) {
// display empty cart
StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
return template.toString();
}
ShoppingCartData shoppingCart = shoppingCartFacade.getShoppingCartData(customer, store, cartCode, language);
if (shoppingCart == null) {
// display empty cart
StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
return template.toString();
}
Language lang = languageUtils.getRequestLanguage(request, response);
// Filter unavailables
List<ShoppingCartItem> unavailables = new ArrayList<ShoppingCartItem>();
List<ShoppingCartItem> availables = new ArrayList<ShoppingCartItem>();
// Take out items no more available
List<ShoppingCartItem> items = shoppingCart.getShoppingCartItems();
for (ShoppingCartItem item : items) {
String code = item.getProductCode();
Product p = productService.getByCode(code, lang);
if (!p.isAvailable()) {
unavailables.add(item);
} else {
availables.add(item);
}
}
shoppingCart.setShoppingCartItems(availables);
shoppingCart.setUnavailables(unavailables);
model.addAttribute("cart", shoppingCart);
/**
* template *
*/
StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(store.getStoreTemplate());
return template.toString();
}
use of com.salesmanager.shop.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartController method displayShoppingCart.
@RequestMapping(value = { "/shoppingCartByCode" }, method = { RequestMethod.GET })
public String displayShoppingCart(@ModelAttribute String shoppingCartCode, final Model model, HttpServletRequest request, HttpServletResponse response, final Locale locale) throws Exception {
MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
Customer customer = getSessionAttribute(Constants.CUSTOMER, request);
Language language = (Language) request.getAttribute(Constants.LANGUAGE);
if (StringUtils.isBlank(shoppingCartCode)) {
return "redirect:/shop";
}
ShoppingCartData cart = shoppingCartFacade.getShoppingCartData(customer, merchantStore, shoppingCartCode, language);
if (cart == null) {
return "redirect:/shop";
}
Language lang = languageUtils.getRequestLanguage(request, response);
// Filter unavailables
List<ShoppingCartItem> unavailables = new ArrayList<ShoppingCartItem>();
List<ShoppingCartItem> availables = new ArrayList<ShoppingCartItem>();
// Take out items no more available
List<ShoppingCartItem> items = cart.getShoppingCartItems();
for (ShoppingCartItem item : items) {
String code = item.getProductCode();
Product p = productService.getByCode(code, lang);
if (!p.isAvailable()) {
unavailables.add(item);
} else {
availables.add(item);
}
}
cart.setShoppingCartItems(availables);
cart.setUnavailables(unavailables);
// meta information
PageInformation pageInformation = new PageInformation();
pageInformation.setPageTitle(messages.getMessage("label.cart.placeorder", locale));
request.setAttribute(Constants.REQUEST_PAGE_INFORMATION, pageInformation);
request.getSession().setAttribute(Constants.SHOPPING_CART, cart.getCode());
model.addAttribute("cart", cart);
/**
* template *
*/
StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.ShoppingCart.shoppingCart).append(".").append(merchantStore.getStoreTemplate());
return template.toString();
}
use of com.salesmanager.shop.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method removeCartItem.
@Override
public ShoppingCartData removeCartItem(final Long itemID, final String cartId, final MerchantStore store, final Language language) throws Exception {
if (StringUtils.isNotBlank(cartId)) {
ShoppingCart cartModel = getCartModel(cartId, store);
if (cartModel != null) {
if (CollectionUtils.isNotEmpty(cartModel.getLineItems())) {
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> shoppingCartItemSet = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem shoppingCartItem : cartModel.getLineItems()) {
if (shoppingCartItem.getId().longValue() == itemID.longValue()) {
shoppingCartService.deleteShoppingCartItem(itemID);
} else {
shoppingCartItemSet.add(shoppingCartItem);
}
}
cartModel.setLineItems(shoppingCartItemSet);
ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
shoppingCartDataPopulator.setPricingService(pricingService);
shoppingCartDataPopulator.setimageUtils(imageUtils);
return shoppingCartDataPopulator.populate(cartModel, store, language);
}
}
}
return null;
}
use of com.salesmanager.shop.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method updateCartItems.
// TODO promoCode request parameter
@Override
public ShoppingCartData updateCartItems(Optional<String> promoCode, final List<ShoppingCartItem> shoppingCartItems, final MerchantStore store, final Language language) throws Exception {
Validate.notEmpty(shoppingCartItems, "shoppingCartItems null or empty");
ShoppingCart cartModel = null;
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
for (ShoppingCartItem item : shoppingCartItems) {
if (item.getQuantity() < 1) {
throw new CartModificationException("Quantity must not be less than one");
}
if (cartModel == null) {
cartModel = getCartModel(item.getCode(), store);
}
com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(item.getId(), cartModel);
if (entryToUpdate == null) {
throw new CartModificationException("Unknown entry number.");
}
entryToUpdate.getProduct();
LOG.info("Updating cart entry quantity to" + item.getQuantity());
entryToUpdate.setQuantity((int) item.getQuantity());
List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
cartItems.add(entryToUpdate);
}
cartModel.setPromoCode(null);
if (promoCode.isPresent()) {
cartModel.setPromoCode(promoCode.get());
cartModel.setPromoAdded(new Date());
}
cartModel.setLineItems(cartItems);
shoppingCartService.saveOrUpdate(cartModel);
LOG.info("Cart entry updated with desired quantity");
ShoppingCartDataPopulator shoppingCartDataPopulator = new ShoppingCartDataPopulator();
shoppingCartDataPopulator.setShoppingCartCalculationService(shoppingCartCalculationService);
shoppingCartDataPopulator.setPricingService(pricingService);
shoppingCartDataPopulator.setimageUtils(imageUtils);
return shoppingCartDataPopulator.populate(cartModel, store, language);
}
use of com.salesmanager.shop.model.shoppingcart.ShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method createCartItems.
// used for api
private List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> createCartItems(ShoppingCart cartModel, List<PersistableShoppingCartItem> shoppingCartItems, MerchantStore store) throws Exception {
List<Long> productIds = shoppingCartItems.stream().map(s -> Long.valueOf(s.getProduct())).collect(Collectors.toList());
List<Product> products = productService.getProductsByIds(productIds);
if (products == null || products.size() != shoppingCartItems.size()) {
LOG.warn("----------------------- Items with in id-list " + productIds + " does not exist");
throw new ResourceNotFoundException("Item with id " + productIds + " does not exist");
}
List<Product> wrongStoreProducts = products.stream().filter(p -> p.getMerchantStore().getId() != store.getId()).collect(Collectors.toList());
if (wrongStoreProducts.size() > 0) {
throw new ResourceNotFoundException("One or more of the items with id's " + wrongStoreProducts.stream().map(s -> Long.valueOf(s.getId())).collect(Collectors.toList()) + " does not belong to merchant " + store.getId());
}
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new ArrayList<>();
for (Product p : products) {
com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(p);
Optional<PersistableShoppingCartItem> oShoppingCartItem = shoppingCartItems.stream().filter(i -> i.getProduct() == p.getId()).findFirst();
if (!oShoppingCartItem.isPresent()) {
// Should never happen if not something is updated in realtime or user has item in local storage and add it long time after to cart!
LOG.warn("Missing shoppingCartItem for product " + p.getSku() + " ( " + p.getId() + " )");
continue;
}
PersistableShoppingCartItem shoppingCartItem = oShoppingCartItem.get();
item.setQuantity(shoppingCartItem.getQuantity());
item.setShoppingCart(cartModel);
/**
* Check if product is available
* Check if product quantity is 0
* Check if date available <= now
*/
if (!p.isAvailable()) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
Set<ProductAvailability> availabilities = p.getAvailabilities();
if (availabilities == null) {
throw new Exception("Item with id " + p.getId() + " is not properly configured");
}
for (ProductAvailability availability : availabilities) {
if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
}
if (!DateUtil.dateBeforeEqualsDate(p.getDateAvailable(), new Date())) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
// end qty & availablility checks
// set attributes
List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
if (!CollectionUtils.isEmpty(attributes)) {
for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
if (productAttribute != null && productAttribute.getProduct().getId().longValue() == p.getId().longValue()) {
com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
item.addAttributes(attributeItem);
}
}
}
items.add(item);
}
return items;
}
Aggregations