use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method updateCartItem.
@Override
public ShoppingCartData updateCartItem(final Long itemID, final String cartId, final long newQuantity, final MerchantStore store, final Language language) throws Exception {
if (newQuantity < 1) {
throw new CartModificationException("Quantity must not be less than one");
}
if (StringUtils.isNotBlank(cartId)) {
ShoppingCart cartModel = getCartModel(cartId, store);
if (cartModel != null) {
com.salesmanager.core.model.shoppingcart.ShoppingCartItem entryToUpdate = getEntryToUpdate(itemID.longValue(), cartModel);
if (entryToUpdate == null) {
throw new CartModificationException("Unknown entry number.");
}
entryToUpdate.getProduct();
LOG.info("Updating cart entry quantity to" + newQuantity);
entryToUpdate.setQuantity((int) newQuantity);
List<ProductAttribute> productAttributes = new ArrayList<ProductAttribute>();
productAttributes.addAll(entryToUpdate.getProduct().getAttributes());
final FinalPrice finalPrice = productPriceUtils.getFinalProductPrice(entryToUpdate.getProduct(), productAttributes);
entryToUpdate.setItemPrice(finalPrice.getFinalPrice());
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);
}
}
return null;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method removeShoppingCartItem.
@Override
@Nullable
public ReadableShoppingCart removeShoppingCartItem(String cartCode, Long productId, MerchantStore merchant, Language language, boolean returnCart) throws Exception {
Validate.notNull(cartCode, "Shopping cart code must not be null");
Validate.notNull(productId, "product id must not be null");
Validate.notNull(merchant, "MerchantStore must not be null");
// get cart
ShoppingCart cart = getCartModel(cartCode, merchant);
if (cart == null) {
throw new ResourceNotFoundException("Cart code [ " + cartCode + " ] not found");
}
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
com.salesmanager.core.model.shoppingcart.ShoppingCartItem itemToDelete = null;
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem shoppingCartItem : cart.getLineItems()) {
if (shoppingCartItem.getProduct().getId().longValue() == productId.longValue()) {
// get cart item
itemToDelete = getEntryToUpdate(shoppingCartItem.getId(), cart);
// break;
} else {
items.add(shoppingCartItem);
}
}
// delete item
if (itemToDelete != null) {
shoppingCartService.deleteShoppingCartItem(itemToDelete.getId());
}
// remaining items
if (items.size() > 0) {
cart.setLineItems(items);
} else {
cart.getLineItems().clear();
}
// update cart with remaining items
shoppingCartService.saveOrUpdate(cart);
if (items.size() > 0 & returnCart) {
return this.getByCode(cartCode, merchant, language);
}
return null;
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method modifyCart.
@Override
public ReadableShoppingCart modifyCart(String cartCode, String promo, MerchantStore store, Language language) throws Exception {
ShoppingCart cart = shoppingCartService.getByCode(cartCode, store);
cart.setPromoCode(promo);
cart.setPromoAdded(new Date());
shoppingCartService.save(cart);
return readableShoppingCartMapper.convert(cart, store, language);
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method addToCart.
@Override
public ReadableShoppingCart addToCart(PersistableShoppingCartItem item, MerchantStore store, Language language) {
Validate.notNull(item, "PersistableShoppingCartItem cannot be null");
// if cart does not exist create a new one
ShoppingCart cartModel = new ShoppingCart();
cartModel.setMerchantStore(store);
cartModel.setShoppingCartCode(uniqueShoppingCartCode());
if (!StringUtils.isBlank(item.getPromoCode())) {
cartModel.setPromoCode(item.getPromoCode());
cartModel.setPromoAdded(new Date());
}
try {
return readableShoppingCart(cartModel, item, store, language);
} catch (Exception e) {
if (e instanceof ResourceNotFoundException) {
throw (ResourceNotFoundException) e;
} else {
throw new ServiceRuntimeException(e);
}
}
}
use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartModelPopulator method populate.
@Override
public ShoppingCart populate(ShoppingCartData shoppingCart, ShoppingCart cartMdel, final MerchantStore store, Language language) {
// if id >0 get the original from the database, override products
try {
if (shoppingCart.getId() > 0 && StringUtils.isNotBlank(shoppingCart.getCode())) {
cartMdel = shoppingCartService.getByCode(shoppingCart.getCode(), store);
if (cartMdel == null) {
cartMdel = new ShoppingCart();
cartMdel.setShoppingCartCode(shoppingCart.getCode());
cartMdel.setMerchantStore(store);
if (customer != null) {
cartMdel.setCustomerId(customer.getId());
}
shoppingCartService.create(cartMdel);
}
} else {
cartMdel.setShoppingCartCode(shoppingCart.getCode());
cartMdel.setMerchantStore(store);
if (customer != null) {
cartMdel.setCustomerId(customer.getId());
}
shoppingCartService.create(cartMdel);
}
List<ShoppingCartItem> items = shoppingCart.getShoppingCartItems();
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> newItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
if (items != null && items.size() > 0) {
for (ShoppingCartItem item : items) {
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> cartItems = cartMdel.getLineItems();
if (cartItems != null && cartItems.size() > 0) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem dbItem : cartItems) {
if (dbItem.getId().longValue() == item.getId()) {
dbItem.setQuantity(item.getQuantity());
// compare attributes
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> attributes = dbItem.getAttributes();
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem> newAttributes = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem>();
List<ShoppingCartAttribute> cartAttributes = item.getShoppingCartAttributes();
if (!CollectionUtils.isEmpty(cartAttributes)) {
for (ShoppingCartAttribute attribute : cartAttributes) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem dbAttribute : attributes) {
if (dbAttribute.getId().longValue() == attribute.getId()) {
newAttributes.add(dbAttribute);
}
}
}
dbItem.setAttributes(newAttributes);
} else {
dbItem.removeAllAttributes();
}
newItems.add(dbItem);
}
}
} else {
// create new item
com.salesmanager.core.model.shoppingcart.ShoppingCartItem cartItem = createCartItem(cartMdel, item, store);
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> lineItems = cartMdel.getLineItems();
if (lineItems == null) {
lineItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
cartMdel.setLineItems(lineItems);
}
lineItems.add(cartItem);
shoppingCartService.update(cartMdel);
}
}
// end for
}
// end if
} catch (ServiceException se) {
LOG.error("Error while converting cart data to cart model.." + se);
throw new ConversionException("Unable to create cart model", se);
} catch (Exception ex) {
LOG.error("Error while converting cart data to cart model.." + ex);
throw new ConversionException("Unable to create cart model", ex);
}
return cartMdel;
}
Aggregations