protected OrderItemService orderItemService;
@Override
public ProcessContext<CartOperationRequest> execute(ProcessContext<CartOperationRequest> context) throws Exception {
CartOperationRequest request = context.getSeedData();
OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
// Quantity was not specified or was equal to zero. We will not throw an exception,
// but we will preven the workflow from continuing to execute
if (orderItemRequestDTO.getQuantity() == null || orderItemRequestDTO.getQuantity() == 0) {
context.stopProcess();
return context;
}
// Throw an exception if the user tried to add a negative quantity of something
if (orderItemRequestDTO.getQuantity() < 0) {
throw new IllegalArgumentException("Quantity cannot be negative");
}
// Throw an exception if the user did not specify an order to add the item to
if (request.getOrder() == null) {
throw new IllegalArgumentException("Order is required when adding item to order");
}
// Validate that if the user specified a productId, it is a legitimate productId
Product product = null;
if (orderItemRequestDTO.getProductId() != null) {
product = catalogService.findProductById(orderItemRequestDTO.getProductId());
if (product == null) {
throw new IllegalArgumentException("Product was specified but no matching product was found for productId " + orderItemRequestDTO.getProductId());
}
}
Sku sku = determineSku(product, orderItemRequestDTO.getSkuId(), orderItemRequestDTO.getItemAttributes(), (ActivityMessages) context);
// If we couldn't find a sku, then we're unable to add to cart.
if (sku == null && !(orderItemRequestDTO instanceof NonDiscreteOrderItemRequestDTO)) {
StringBuilder sb = new StringBuilder();
for (Entry<String, String> entry : orderItemRequestDTO.getItemAttributes().entrySet()) {
sb.append(entry.toString());
}
throw new IllegalArgumentException("Could not find SKU for :" +
" productId: " + (product == null ? "null" : product.getId()) +
" skuId: " + orderItemRequestDTO.getSkuId() +
" attributes: " + sb.toString());
} else if (sku == null) {
NonDiscreteOrderItemRequestDTO ndr = (NonDiscreteOrderItemRequestDTO) orderItemRequestDTO;
if (StringUtils.isBlank(ndr.getItemName())) {
throw new IllegalArgumentException("Item name is required for non discrete order item add requests");
}
if (ndr.getOverrideRetailPrice() == null && ndr.getOverrideSalePrice() == null) {
throw new IllegalArgumentException("At least one override price is required for non discrete order item add requests");
}
} else if (!sku.isActive()) {
throw new IllegalArgumentException("The requested skuId of " + sku.getId() + " is no longer active");
} else {
// We know definitively which sku we're going to add, so we can set this
// value with certainty
request.getItemRequest().setSkuId(sku.getId());
}
if (!(orderItemRequestDTO instanceof NonDiscreteOrderItemRequestDTO) &&
request.getOrder().getCurrency() != null &&
sku.getCurrency() != null &&
!request.getOrder().getCurrency().equals(sku.getCurrency())) {
throw new IllegalArgumentException("Cannot have items with differing currencies in one cart");
}
// If the user has specified a parent order item to attach this to, it must exist in this cart
if (orderItemRequestDTO.getParentOrderItemId() != null) {
OrderItem parent = orderItemService.readOrderItemById(orderItemRequestDTO.getParentOrderItemId());
if (parent == null) {
throw new IllegalArgumentException("Could not find parent order item by the given id");
}
}