@Override @Transactional( value = "blTransactionManager", rollbackFor = {AddToCartException.class}) public Order addItemWithPriceOverrides( Long orderId, OrderItemRequestDTO orderItemRequestDTO, boolean priceOrder) throws AddToCartException { Order order = findOrderById(orderId); if (automaticallyMergeLikeItems) { OrderItem item = findMatchingItem(order, orderItemRequestDTO); if (item != 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 { CartOperationRequest cartOpRequest = new CartOperationRequest(findOrderById(orderId), orderItemRequestDTO, priceOrder); CartOperationContext context = (CartOperationContext) addItemWorkflow.doActivities(cartOpRequest); return context.getSeedData().getOrder(); } catch (WorkflowException e) { throw new AddToCartException("Could not add to cart", getCartOperationExceptionRootCause(e)); } }
@Override @Transactional("blTransactionManager") public Order addItemFromNamedOrder( Order namedOrder, OrderItem item, int quantity, boolean priceOrder) throws RemoveFromCartException, AddToCartException, UpdateCartException { // Validate that the quantity requested makes sense if (quantity < 1 || quantity > item.getQuantity()) { throw new IllegalArgumentException("Cannot move 0 or less quantity"); } else if (quantity == item.getQuantity()) { return addItemFromNamedOrder(namedOrder, item, priceOrder); } Order cartOrder = orderDao.readCartForCustomer(namedOrder.getCustomer()); if (cartOrder == null) { cartOrder = createNewCartForCustomer(namedOrder.getCustomer()); } if (moveNamedOrderItems) { // Update the old item to its new quantity only if we're moving items OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO(); orderItemRequestDTO.setOrderItemId(item.getId()); orderItemRequestDTO.setQuantity(item.getQuantity() - quantity); updateItemQuantity(namedOrder.getId(), orderItemRequestDTO, false); } OrderItemRequestDTO orderItemRequest = orderItemService.buildOrderItemRequestDTOFromOrderItem(item); orderItemRequest.setQuantity(quantity); cartOrder = addItem(cartOrder.getId(), orderItemRequest, priceOrder); return cartOrder; }
@Override @Transactional( value = "blTransactionManager", rollbackFor = {RemoveFromCartException.class}) public Order removeItem(Long orderId, Long orderItemId, boolean priceOrder) throws RemoveFromCartException { try { OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO(); orderItemRequestDTO.setOrderItemId(orderItemId); CartOperationRequest cartOpRequest = new CartOperationRequest(findOrderById(orderId), orderItemRequestDTO, priceOrder); CartOperationContext context = (CartOperationContext) removeItemWorkflow.doActivities(cartOpRequest); return context.getSeedData().getOrder(); } catch (WorkflowException e) { throw new RemoveFromCartException( "Could not remove from cart", getCartOperationExceptionRootCause(e)); } }