use of org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest in project BroadleafCommerce by BroadleafCommerce.
the class OrderItemServiceImpl method buildOrderItemFromDTO.
@Override
public OrderItem buildOrderItemFromDTO(Order order, OrderItemRequestDTO orderItemRequestDTO) {
Sku sku = null;
if (orderItemRequestDTO.getSkuId() != null) {
sku = catalogService.findSkuById(orderItemRequestDTO.getSkuId());
}
Product product = null;
if (orderItemRequestDTO.getProductId() != null) {
product = catalogService.findProductById(orderItemRequestDTO.getProductId());
}
Category category = null;
if (orderItemRequestDTO.getCategoryId() != null) {
category = catalogService.findCategoryById(orderItemRequestDTO.getCategoryId());
}
if (category == null && product != null) {
category = product.getDefaultCategory();
}
OrderItem item;
if (orderItemRequestDTO instanceof NonDiscreteOrderItemRequestDTO) {
NonDiscreteOrderItemRequestDTO ndr = (NonDiscreteOrderItemRequestDTO) orderItemRequestDTO;
OrderItemRequest itemRequest = new OrderItemRequest();
itemRequest.setQuantity(ndr.getQuantity());
itemRequest.setRetailPriceOverride(ndr.getOverrideRetailPrice());
itemRequest.setSalePriceOverride(ndr.getOverrideSalePrice());
itemRequest.setItemAttributes(orderItemRequestDTO.getItemAttributes());
itemRequest.setAdditionalAttributes(orderItemRequestDTO.getAdditionalAttributes());
itemRequest.setItemName(ndr.getItemName());
itemRequest.setOrder(order);
item = createOrderItem(itemRequest);
} else if (product == null || !(product instanceof ProductBundle)) {
DiscreteOrderItemRequest itemRequest = new DiscreteOrderItemRequest();
itemRequest.setCategory(category);
itemRequest.setProduct(product);
itemRequest.setSku(sku);
itemRequest.setQuantity(orderItemRequestDTO.getQuantity());
itemRequest.setItemAttributes(orderItemRequestDTO.getItemAttributes());
itemRequest.setAdditionalAttributes(orderItemRequestDTO.getAdditionalAttributes());
itemRequest.setOrder(order);
itemRequest.setSalePriceOverride(orderItemRequestDTO.getOverrideSalePrice());
itemRequest.setRetailPriceOverride(orderItemRequestDTO.getOverrideRetailPrice());
item = createDiscreteOrderItem(itemRequest);
} else {
ProductBundleOrderItemRequest bundleItemRequest = new ProductBundleOrderItemRequest();
bundleItemRequest.setCategory(category);
bundleItemRequest.setProductBundle((ProductBundle) product);
bundleItemRequest.setSku(sku);
bundleItemRequest.setQuantity(orderItemRequestDTO.getQuantity());
bundleItemRequest.setItemAttributes(orderItemRequestDTO.getItemAttributes());
bundleItemRequest.setName(product.getName());
bundleItemRequest.setOrder(order);
bundleItemRequest.setSalePriceOverride(orderItemRequestDTO.getOverrideSalePrice());
bundleItemRequest.setRetailPriceOverride(orderItemRequestDTO.getOverrideRetailPrice());
item = createBundleOrderItem(bundleItemRequest, false);
}
if (orderItemRequestDTO.getParentOrderItemId() != null) {
OrderItem parent = readOrderItemById(orderItemRequestDTO.getParentOrderItemId());
item.setParentOrderItem(parent);
}
if (orderItemRequestDTO.getHasConfigurationError()) {
item.setHasValidationError(true);
}
if (orderItemRequestDTO instanceof ConfigurableOrderItemRequest) {
ConfigurableOrderItemRequest configRequest = (ConfigurableOrderItemRequest) orderItemRequestDTO;
if (configRequest.getHasConfigurationError()) {
item.setHasValidationError(true);
}
if (!configRequest.getDiscountsAllowed()) {
item.setDiscountingAllowed(false);
}
}
applyAdditionalOrderItemProperties(item);
return item;
}
use of org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest in project BroadleafCommerce by BroadleafCommerce.
the class ValidateAddRequestActivity method validate.
protected ProcessContext<CartOperationRequest> validate(ProcessContext<CartOperationRequest> context) {
CartOperationRequest request = context.getSeedData();
OrderItemRequestDTO orderItemRequestDTO = request.getItemRequest();
Integer orderItemQuantity = orderItemRequestDTO.getQuantity();
if (!hasQuantity(orderItemQuantity)) {
context.stopProcess();
} else if (orderItemQuantity < 0) {
throw new IllegalArgumentException("Quantity cannot be negative");
} else if (request.getOrder() == null) {
throw new IllegalArgumentException("Order is required when adding item to order");
} else {
Product product = determineProduct(orderItemRequestDTO);
Sku sku;
try {
sku = determineSku(product, orderItemRequestDTO.getSkuId(), orderItemRequestDTO.getItemAttributes(), (ActivityMessages) context);
} catch (RequiredAttributeNotProvidedException e) {
if (orderItemRequestDTO instanceof ConfigurableOrderItemRequest) {
// Mark the request as a configuration error and proceed with the add.
orderItemRequestDTO.setHasConfigurationError(Boolean.TRUE);
return context;
}
throw e;
}
addSkuToCart(sku, orderItemRequestDTO, product, request);
if (!hasSameCurrency(orderItemRequestDTO, request, sku)) {
throw new IllegalArgumentException("Cannot have items with differing currencies in one cart");
}
validateIfParentOrderItemExists(orderItemRequestDTO);
}
return context;
}
use of org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest in project BroadleafCommerce by BroadleafCommerce.
the class BroadleafProductController method handleRequest.
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView model = new ModelAndView();
Product product = (Product) request.getAttribute(ProductHandlerMapping.CURRENT_PRODUCT_ATTRIBUTE_NAME);
assert (product != null);
model.addObject(MODEL_ATTRIBUTE_NAME, product);
model.addObject(PAGE_TYPE_ATTRIBUTE_NAME, "product");
// Build the add to cart request and add it to the page
ConfigurableOrderItemRequest itemRequest = orderItemService.createConfigurableOrderItemRequestFromProduct(product);
orderItemService.modifyOrderItemRequest(itemRequest);
model.addObject(CONFIGURATION_ATTRIBUTE_NAME, itemRequest);
model.addObject(ALL_PRODUCTS_ATTRIBUTE_NAME, orderItemService.findAllProductsInRequest(itemRequest));
addDeepLink(model, deepLinkService, product);
String templatePath;
// Use the products custom template if available
if (StringUtils.isNotBlank(product.getDisplayTemplate())) {
templatePath = product.getDisplayTemplate();
} else {
// Otherwise, use the controller default.
templatePath = getDefaultProductView();
}
// Allow extension managers to override.
ExtensionResultHolder<String> erh = new ExtensionResultHolder<String>();
ExtensionResultStatusType extResult = templateOverrideManager.getProxy().getOverrideTemplate(erh, product);
if (extResult != ExtensionResultStatusType.NOT_HANDLED) {
templatePath = erh.getResult();
}
model.setViewName(templatePath);
// Check if this is request to edit an existing order item
String isEditRequest = request.getParameter("edit");
String oidParam = request.getParameter("oid");
if (StringUtils.isNotEmpty(isEditRequest) && StringUtils.isNotEmpty(oidParam)) {
Long oid = Long.parseLong(oidParam);
OrderItem orderItem = orderItemService.readOrderItemById(oid);
if (orderItemBelongsToCurrentCustomer(orderItem)) {
// Update the itemRequest to contain user's previous input
orderItemService.mergeOrderItemRequest(itemRequest, orderItem);
// Add the order item to the model
model.addObject("isUpdateRequest", true);
model.addObject("orderItem", orderItem);
}
}
return model;
}
Aggregations