use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OrderServiceImpl method removeItem.
@Override
@Transactional(value = "blTransactionManager", rollbackFor = { RemoveFromCartException.class })
public Order removeItem(Long orderId, Long orderItemId, boolean priceOrder) throws RemoveFromCartException {
preValidateCartOperation(findOrderById(orderId));
try {
OrderItem oi = orderItemService.readOrderItemById(orderItemId);
if (oi == null) {
throw new WorkflowException(new ItemNotFoundException());
}
List<Long> childrenToRemove = new ArrayList<Long>();
if (oi instanceof BundleOrderItem) {
List<DiscreteOrderItem> bundledItems = ((BundleOrderItem) oi).getDiscreteOrderItems();
for (DiscreteOrderItem doi : bundledItems) {
findAllChildrenToRemove(childrenToRemove, doi);
}
} else {
findAllChildrenToRemove(childrenToRemove, oi);
}
for (Long childToRemove : childrenToRemove) {
removeItemInternal(orderId, childToRemove, false);
}
return removeItemInternal(orderId, orderItemId, priceOrder);
} catch (WorkflowException e) {
throw new RemoveFromCartException("Could not remove from cart", getCartOperationExceptionRootCause(e));
}
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OrderServiceImpl method addItemWithPriceOverrides.
@Override
@Transactional(value = "blTransactionManager", rollbackFor = { AddToCartException.class })
public Order addItemWithPriceOverrides(Long orderId, OrderItemRequestDTO orderItemRequestDTO, boolean priceOrder) throws AddToCartException {
Order order = findOrderById(orderId);
preValidateCartOperation(order);
if (getAutomaticallyMergeLikeItems()) {
OrderItem item = findMatchingItem(order, orderItemRequestDTO);
if (item != null && item.getParentOrderItem() == null) {
orderItemRequestDTO.setQuantity(item.getQuantity() + orderItemRequestDTO.getQuantity());
orderItemRequestDTO.setOrderItemId(item.getId());
try {
return updateItemQuantity(orderId, orderItemRequestDTO, priceOrder);
} catch (RemoveFromCartException e) {
throw new AddToCartException("Unexpected error - system tried to remove item while adding to cart", e);
} catch (UpdateCartException e) {
throw new AddToCartException("Could not update quantity for matched item", e);
}
}
}
try {
// We only want to price on the last addition for performance reasons and only if the user asked for it.
int numAdditionRequests = priceOrder ? (getTotalChildOrderItems(orderItemRequestDTO)) : -1;
int currentAddition = 1;
CartOperationRequest cartOpRequest = new CartOperationRequest(findOrderById(orderId), orderItemRequestDTO, currentAddition == numAdditionRequests);
Session session = em.unwrap(Session.class);
FlushMode current = session.getFlushMode();
if (!autoFlushAddToCart) {
// Performance measure. Hibernate will sometimes perform an autoflush when performing query operations and this can
// be expensive. It is possible to avoid the autoflush if there's no concern about queries in the flow returning
// incorrect results because something has not been flushed to the database yet.
session.setFlushMode(FlushMode.MANUAL);
}
ProcessContext<CartOperationRequest> context;
try {
context = (ProcessContext<CartOperationRequest>) addItemWorkflow.doActivities(cartOpRequest);
} finally {
if (!autoFlushAddToCart) {
session.setFlushMode(current);
}
}
List<ActivityMessageDTO> orderMessages = new ArrayList<ActivityMessageDTO>();
orderMessages.addAll(((ActivityMessages) context).getActivityMessages());
// Update the orderItemRequest incase it changed during the initial add to cart workflow
orderItemRequestDTO = context.getSeedData().getItemRequest();
numAdditionRequests = priceOrder ? (getTotalChildOrderItems(orderItemRequestDTO) - 1) : -1;
addChildItems(orderItemRequestDTO, numAdditionRequests, currentAddition, context, orderMessages);
context.getSeedData().getOrder().setOrderMessages(orderMessages);
return context.getSeedData().getOrder();
} catch (WorkflowException e) {
throw new AddToCartException("Could not add to cart", getCartOperationExceptionRootCause(e));
}
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class OrderServiceImpl method findAllChildrenToRemove.
protected void findAllChildrenToRemove(List<Long> childrenToRemove, OrderItem orderItem) {
if (CollectionUtils.isNotEmpty(orderItem.getChildOrderItems())) {
for (OrderItem childOrderItem : orderItem.getChildOrderItems()) {
findAllChildrenToRemove(childrenToRemove, childOrderItem);
childrenToRemove.add(childOrderItem.getId());
}
}
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class LegacyCartServiceImpl method moveItemToCartFromNamedOrder.
public OrderItem moveItemToCartFromNamedOrder(Long customerId, String orderName, Long orderItemId, Integer quantity, boolean priceOrder) throws PricingException {
Order wishlistOrder = findNamedOrderForCustomer(orderName, customerService.createCustomerFromId(customerId));
OrderItem orderItem = orderItemService.readOrderItemById(orderItemId);
orderItem.setQuantity(quantity);
return moveItemToCartFromNamedOrder(wishlistOrder, orderItem, priceOrder);
}
use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.
the class LegacyMergeCartServiceImpl method removeExpiredGiftWrapOrderItems.
protected Order removeExpiredGiftWrapOrderItems(MergeCartResponse mergeCartResponse, Order customerCart, Map<OrderItem, OrderItem> oldNewItemMap) throws PricingException {
// clear out any Gift Wrap items that contain one or more removed wrapped items
Iterator<OrderItem> addedItems = mergeCartResponse.getAddedItems().iterator();
while (addedItems.hasNext()) {
OrderItem addedItem = addedItems.next();
if (addedItem instanceof GiftWrapOrderItem) {
GiftWrapOrderItem giftWrapOrderItem = (GiftWrapOrderItem) addedItem;
boolean removeItem = false;
for (OrderItem wrappedItem : giftWrapOrderItem.getWrappedItems()) {
if (mergeCartResponse.getRemovedItems().contains(wrappedItem)) {
removeItem = true;
break;
}
}
if (removeItem) {
for (OrderItem wrappedItem : giftWrapOrderItem.getWrappedItems()) {
wrappedItem.setGiftWrapOrderItem(null);
}
giftWrapOrderItem.getWrappedItems().clear();
for (OrderItem cartItem : customerCart.getOrderItems()) {
if (cartItem.getGiftWrapOrderItem() != null && oldNewItemMap.containsKey(cartItem.getGiftWrapOrderItem())) {
cartItem.setGiftWrapOrderItem(null);
}
}
customerCart = removeItemFromOrder(customerCart, giftWrapOrderItem, false);
addedItems.remove();
mergeCartResponse.getRemovedItems().add(giftWrapOrderItem);
}
}
}
return customerCart;
}
Aggregations