Search in sources :

Example 6 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class MultiTenantCopyContext method checkCloneStatus.

protected boolean checkCloneStatus(Object instance) {
    boolean shouldClone = true;
    ExtensionResultHolder<Boolean> shouldCloneHolder = new ExtensionResultHolder<Boolean>();
    if (extensionManager != null) {
        ExtensionResultStatusType status = extensionManager.getProxy().shouldClone(this, instance, shouldCloneHolder);
        if (ExtensionResultStatusType.NOT_HANDLED != status) {
            shouldClone = shouldCloneHolder.getResult();
        }
    }
    return shouldClone;
}
Also used : ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 7 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class OfferServiceUtilitiesImpl method updatePriceDetail.

@Override
public void updatePriceDetail(OrderItemPriceDetail itemDetail, PromotableOrderItemPriceDetail promotableDetail) {
    Map<Long, OrderItemPriceDetailAdjustment> itemAdjustmentMap = buildItemDetailAdjustmentMap(itemDetail);
    if (itemDetail.getQuantity() != promotableDetail.getQuantity()) {
        itemDetail.setQuantity(promotableDetail.getQuantity());
    }
    if (promotableDetail.isAdjustmentsFinalized()) {
        itemDetail.setUseSalePrice(promotableDetail.useSaleAdjustments());
    }
    for (PromotableOrderItemPriceDetailAdjustment adjustment : promotableDetail.getCandidateItemAdjustments()) {
        OrderItemPriceDetailAdjustment itemAdjustment = itemAdjustmentMap.remove(adjustment.getOfferId());
        if (itemAdjustment != null) {
            // Update existing adjustment
            if (!itemAdjustment.getValue().equals(adjustment.getAdjustmentValue())) {
                updateItemAdjustment(itemAdjustment, adjustment);
            }
        } else {
            // Add a new adjustment
            final OrderItemPriceDetailAdjustment newItemAdjustment;
            ExtensionResultHolder resultHolder = new ExtensionResultHolder();
            if (extensionManager != null) {
                extensionManager.createOrderItemPriceDetailAdjustment(resultHolder, itemDetail);
            }
            if (resultHolder != null && resultHolder.getContextMap().containsKey("OrderItemPriceDetailAdjustment")) {
                newItemAdjustment = (OrderItemPriceDetailAdjustment) resultHolder.getContextMap().get("OrderItemPriceDetailAdjustment");
            } else {
                newItemAdjustment = offerDao.createOrderItemPriceDetailAdjustment();
            }
            newItemAdjustment.init(itemDetail, adjustment.getOffer(), null);
            updateItemAdjustment(newItemAdjustment, adjustment);
            itemDetail.getOrderItemPriceDetailAdjustments().add(newItemAdjustment);
        }
    }
    if (itemAdjustmentMap.size() > 0) {
        // Remove adjustments that were on the order item but no longer needed.
        List<Long> adjustmentIdsToRemove = new ArrayList<Long>();
        for (OrderItemPriceDetailAdjustment adjustmentToRemove : itemAdjustmentMap.values()) {
            adjustmentIdsToRemove.add(adjustmentToRemove.getOffer().getId());
        }
        Iterator<OrderItemPriceDetailAdjustment> iterator = itemDetail.getOrderItemPriceDetailAdjustments().iterator();
        while (iterator.hasNext()) {
            OrderItemPriceDetailAdjustment adj = iterator.next();
            if (adjustmentIdsToRemove.contains(adj.getOffer().getId())) {
                iterator.remove();
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PromotableOrderItemPriceDetailAdjustment(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetailAdjustment) OrderItemPriceDetailAdjustment(org.broadleafcommerce.core.offer.domain.OrderItemPriceDetailAdjustment) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder) PromotableOrderItemPriceDetailAdjustment(org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItemPriceDetailAdjustment)

Example 8 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCategoryController method handleRequest.

@Override
@SuppressWarnings("unchecked")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ModelAndView model = new ModelAndView();
    if (request.getParameterMap().containsKey("facetField")) {
        // If we receive a facetField parameter, we need to convert the field to the
        // product search criteria expected format. This is used in multi-facet selection. We
        // will send a redirect to the appropriate URL to maintain canonical URLs
        String fieldName = request.getParameter("facetField");
        List<String> activeFieldFilters = new ArrayList<String>();
        Map<String, String[]> parameters = new HashMap<String, String[]>(request.getParameterMap());
        for (Iterator<Entry<String, String[]>> iter = parameters.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry<String, String[]> entry = iter.next();
            String key = entry.getKey();
            if (key.startsWith(fieldName + "-")) {
                activeFieldFilters.add(key.substring(key.indexOf('-') + 1));
                iter.remove();
            }
        }
        parameters.remove(SearchCriteria.PAGE_NUMBER);
        parameters.put(fieldName, activeFieldFilters.toArray(new String[activeFieldFilters.size()]));
        parameters.remove("facetField");
        String newUrl = ProcessorUtils.getUrl(request.getRequestURL().toString(), parameters);
        model.setViewName("redirect:" + newUrl);
    } else {
        // Else, if we received a GET to the category URL (either the user clicked this link or we redirected
        // from the POST method, we can actually process the results
        Category category = (Category) request.getAttribute(CategoryHandlerMapping.CURRENT_CATEGORY_ATTRIBUTE_NAME);
        assert (category != null);
        SearchCriteria searchCriteria = facetService.buildSearchCriteria(request);
        SearchResult result = getSearchService().findSearchResults(searchCriteria);
        facetService.setActiveFacetResults(result.getFacets(), request);
        model.addObject(CATEGORY_ATTRIBUTE_NAME, category);
        model.addObject(PRODUCTS_ATTRIBUTE_NAME, result.getProducts());
        model.addObject(SKUS_ATTRIBUTE_NAME, result.getSkus());
        model.addObject(FACETS_ATTRIBUTE_NAME, result.getFacets());
        model.addObject(PRODUCT_SEARCH_RESULT_ATTRIBUTE_NAME, result);
        if (request.getParameterMap().containsKey("q")) {
            model.addObject(ORIGINAL_QUERY_ATTRIBUTE_NAME, request.getParameter("q"));
        }
        model.addObject("BLC_PAGE_TYPE", "category");
        if (result.getProducts() != null) {
            model.addObject(ALL_PRODUCTS_ATTRIBUTE_NAME, new HashSet<Product>(result.getProducts()));
        }
        if (result.getSkus() != null) {
            model.addObject(ALL_SKUS_ATTRIBUTE_NAME, new HashSet<Sku>(result.getSkus()));
        }
        addDeepLink(model, deepLinkService, category);
        String templatePath = null;
        // Use the categories custom template if available
        if (StringUtils.isNotBlank(category.getDisplayTemplate())) {
            templatePath = category.getDisplayTemplate();
        } else {
            // Otherwise, use the controller default.
            templatePath = getDefaultCategoryView();
        }
        // Allow extension managers to override.
        ExtensionResultHolder<String> erh = new ExtensionResultHolder<String>();
        ExtensionResultStatusType extResult = templateOverrideManager.getProxy().getOverrideTemplate(erh, category);
        if (extResult != ExtensionResultStatusType.NOT_HANDLED) {
            templatePath = erh.getResult();
        }
        model.setViewName(templatePath);
    }
    return model;
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) ModelAndView(org.springframework.web.servlet.ModelAndView) Product(org.broadleafcommerce.core.catalog.domain.Product) SearchResult(org.broadleafcommerce.core.search.domain.SearchResult) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) SearchCriteria(org.broadleafcommerce.core.search.domain.SearchCriteria) Entry(java.util.Map.Entry) Sku(org.broadleafcommerce.core.catalog.domain.Sku) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 9 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class CartStateRequestProcessor method process.

@Override
public void process(WebRequest request) {
    Customer customer = CustomerState.getCustomer();
    if (customer == null) {
        LOG.info("No customer was found on the current request, no cart will be added to the current request. Ensure that the" + " blCustomerStateFilter occurs prior to the blCartStateFilter");
        return;
    }
    ExtensionResultHolder<Order> erh = new ExtensionResultHolder<Order>();
    extensionManager.getProxy().lookupOrCreateCart(request, customer, erh);
    Order cart;
    if (erh.getResult() != null) {
        cart = orderService.findCartForCustomerWithEnhancements(customer, erh.getResult());
    } else {
        cart = getOverrideCart(request);
        if (cart == null && mergeCartNeeded(customer, request)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Merge cart required, calling mergeCart " + customer.getId());
            }
            cart = mergeCart(customer, request);
        } else if (cart == null) {
            cart = orderService.findCartForCustomerWithEnhancements(customer);
        }
        if (cart == null) {
            cart = orderService.getNullOrder();
        } else {
            updateCartService.updateAndValidateCart(cart);
        }
    }
    updateCartRequestAttributes(request, cart);
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Customer(org.broadleafcommerce.profile.core.domain.Customer) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 10 with ExtensionResultHolder

use of org.broadleafcommerce.common.extension.ExtensionResultHolder in project BroadleafCommerce by BroadleafCommerce.

the class EntityDuplicatorImpl method copy.

@Override
public <T> T copy(Class<T> entityClass, Long id, Map<String, String> copyHints, EntityDuplicateModifier... modifiers) {
    genericEntityService.flush();
    genericEntityService.clear();
    Object entity = genericEntityService.readGenericEntity(entityClass, id);
    if (!(entity instanceof MultiTenantCloneable)) {
        IllegalArgumentException e = new IllegalArgumentException("Copying is only supported for classes implementing MultiTenantCloneable");
        LOG.error(String.format("Unable to duplicate entity %s:%s", entityClass.getName(), id), e);
        throw e;
    }
    boolean isValid = validate(entity);
    T dup;
    if (isValid) {
        try {
            Site currentSite = BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite();
            MultiTenantCopyContext context = new MultiTenantCopyContext(null, null, currentSite, currentSite, genericEntityService, mtCopierExtensionManager);
            if (extensionManager != null) {
                ExtensionResultHolder<MultiTenantCopyContext> contextResponse = new ExtensionResultHolder<MultiTenantCopyContext>();
                extensionManager.setupDuplicate(entity, contextResponse);
                if (contextResponse.getResult() != null) {
                    context = contextResponse.getResult();
                }
            }
            dup = performCopy(context, (MultiTenantCloneable<T>) entity, copyHints, modifiers);
        } catch (Exception e) {
            LOG.error(String.format("Unable to duplicate entity %s:%s", entityClass.getName(), id), e);
            throw ExceptionHelper.refineException(e);
        } finally {
            if (extensionManager != null) {
                extensionManager.tearDownDuplicate();
            }
        }
    } else {
        LOG.error(String.format("Entity not valid for duplication - %s:%s", entityClass.getName(), id));
        throw new IllegalArgumentException(String.format("Entity not valid for duplication - %s:%s", entityClass.getName(), id));
    }
    return dup;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) MultiTenantCloneable(org.broadleafcommerce.common.copy.MultiTenantCloneable) MultiTenantCopyContext(org.broadleafcommerce.common.copy.MultiTenantCopyContext) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)56 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)35 ArrayList (java.util.ArrayList)11 Product (org.broadleafcommerce.core.catalog.domain.Product)7 Sku (org.broadleafcommerce.core.catalog.domain.Sku)6 PersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException)6 ParentEntityPersistenceException (org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException)5 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)5 List (java.util.List)4 Set (java.util.Set)4 File (java.io.File)3 Serializable (java.io.Serializable)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Query (javax.persistence.Query)3 TypedQuery (javax.persistence.TypedQuery)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)3 Site (org.broadleafcommerce.common.site.domain.Site)3 Category (org.broadleafcommerce.core.catalog.domain.Category)3