Search in sources :

Example 21 with Product

use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.

the class AdminProductTranslationExtensionHandler method applyTransformation.

/**
 * If we are trying to translate a field on Product that starts with "defaultSku.", we really want to associate the
 * translation with Sku, its associated id, and the property name without "defaultSku."
 */
@Override
public ExtensionResultStatusType applyTransformation(TranslationForm form) {
    String defaultSkuPrefix = "defaultSku.";
    String unencodedPropertyName = JSCompatibilityHelper.unencode(form.getPropertyName());
    if (form.getCeilingEntity().equals(Product.class.getName()) && unencodedPropertyName.startsWith(defaultSkuPrefix)) {
        Product p = catalogService.findProductById(Long.parseLong(form.getEntityId()));
        form.setCeilingEntity(Sku.class.getName());
        form.setEntityId(String.valueOf(p.getDefaultSku().getId()));
        form.setPropertyName(unencodedPropertyName.substring(defaultSkuPrefix.length()));
    }
    return ExtensionResultStatusType.HANDLED;
}
Also used : Product(org.broadleafcommerce.core.catalog.domain.Product) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Example 22 with Product

use of org.broadleafcommerce.core.catalog.domain.Product 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 23 with Product

use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafSearchController method search.

public String search(Model model, HttpServletRequest request, HttpServletResponse response, String query) throws ServletException, IOException, ServiceException {
    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);
        return "redirect:" + newUrl;
    } else {
        // Else, if we received a GET to the category URL (either the user performed a search or we redirected
        // from the POST method, we can actually process the results
        SearchRedirect handler = searchRedirectService.findSearchRedirectBySearchTerm(query);
        if (handler != null) {
            String contextPath = request.getContextPath();
            String url = UrlUtil.fixRedirectUrl(contextPath, handler.getUrl());
            response.sendRedirect(url);
            return null;
        }
        if (StringUtils.isNotEmpty(query)) {
            SearchCriteria searchCriteria = facetService.buildSearchCriteria(request);
            if (StringUtils.isEmpty(searchCriteria.getQuery())) {
                // if our query is empty or null, we want to redirect.
                return "redirect:/";
            }
            SearchResult result = getSearchService().findSearchResults(searchCriteria);
            facetService.setActiveFacetResults(result.getFacets(), request);
            model.addAttribute(PRODUCTS_ATTRIBUTE_NAME, result.getProducts());
            model.addAttribute(SKUS_ATTRIBUTE_NAME, result.getSkus());
            model.addAttribute(FACETS_ATTRIBUTE_NAME, result.getFacets());
            model.addAttribute(PRODUCT_SEARCH_RESULT_ATTRIBUTE_NAME, result);
            model.addAttribute(ORIGINAL_QUERY_ATTRIBUTE_NAME, query);
            if (result.getProducts() != null) {
                model.addAttribute(ALL_PRODUCTS_ATTRIBUTE_NAME, new HashSet<Product>(result.getProducts()));
            }
            if (result.getSkus() != null) {
                model.addAttribute(ALL_SKUS_ATTRIBUTE_NAME, new HashSet<Sku>(result.getSkus()));
            }
        }
    }
    updateQueryRequestAttribute(query);
    return getSearchView();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SearchRedirect(org.broadleafcommerce.core.search.redirect.domain.SearchRedirect) Product(org.broadleafcommerce.core.catalog.domain.Product) SearchResult(org.broadleafcommerce.core.search.domain.SearchResult) SearchCriteria(org.broadleafcommerce.core.search.domain.SearchCriteria) Entry(java.util.Map.Entry) Sku(org.broadleafcommerce.core.catalog.domain.Sku) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with Product

use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCartController method configure.

/**
 * Takes a product id and builds out a dependant order item tree.  If it determines the order
 * item is safe to add, it will proceed to calling the "add" method.
 *
 * If the method was invoked via an AJAX call, it will render the "ajax/configure" template.
 * Otherwise, it will perform a 302 redirect to "/cart/configure"
 *
 * In the case that an "add" happened it will render either the "ajax/cart" or perform a 302
 * redirect to "/cart"
 *
 * @param request
 * @param response
 * @param model
 * @param productId
 * @throws IOException
 * @throws AddToCartException
 * @throws PricingException
 */
public String configure(HttpServletRequest request, HttpServletResponse response, Model model, Long productId) throws IOException, AddToCartException, PricingException, Exception {
    Product product = catalogService.findProductById(productId);
    ConfigurableOrderItemRequest itemRequest = orderItemService.createConfigurableOrderItemRequestFromProduct(product);
    orderItemService.modifyOrderItemRequest(itemRequest);
    // If this item request is safe to add, go ahead and add it.
    if (isSafeToAdd(itemRequest)) {
        return add(request, response, model, itemRequest);
    }
    model.addAttribute("baseItem", itemRequest);
    model.addAttribute("isUpdateRequest", Boolean.TRUE);
    model.addAttribute(ALL_PRODUCTS_ATTRIBUTE_NAME, orderItemService.findAllProductsInRequest(itemRequest));
    return isAjaxRequest(request) ? getConfigureView() : getConfigurePageRedirect();
}
Also used : Product(org.broadleafcommerce.core.catalog.domain.Product) ConfigurableOrderItemRequest(org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest)

Example 25 with Product

use of org.broadleafcommerce.core.catalog.domain.Product in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCartController method isSafeToAdd.

protected boolean isSafeToAdd(ConfigurableOrderItemRequest itemRequest) {
    if (!automaticallyAddCompleteItems) {
        return false;
    }
    boolean canSafelyAdd = true;
    for (OrderItemRequestDTO child : itemRequest.getChildOrderItems()) {
        ConfigurableOrderItemRequest configurableRequest = (ConfigurableOrderItemRequest) child;
        Product childProduct = configurableRequest.getProduct();
        if (childProduct == null) {
            return false;
        }
        int minQty = configurableRequest.getMinQuantity();
        if (minQty == 0 || childProduct.getProductOptionXrefs().size() > 0) {
            return false;
        }
        canSafelyAdd = isSafeToAdd(configurableRequest);
    }
    return canSafelyAdd;
}
Also used : OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) Product(org.broadleafcommerce.core.catalog.domain.Product) ConfigurableOrderItemRequest(org.broadleafcommerce.core.order.service.call.ConfigurableOrderItemRequest)

Aggregations

Product (org.broadleafcommerce.core.catalog.domain.Product)77 Sku (org.broadleafcommerce.core.catalog.domain.Sku)34 ArrayList (java.util.ArrayList)23 Category (org.broadleafcommerce.core.catalog.domain.Category)18 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)17 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)12 Order (org.broadleafcommerce.core.order.domain.Order)12 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.testng.annotations.Test)11 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)10 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)9 Money (org.broadleafcommerce.common.money.Money)9 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)9 CategoryProductXref (org.broadleafcommerce.core.catalog.domain.CategoryProductXref)8 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)7 CategoryImpl (org.broadleafcommerce.core.catalog.domain.CategoryImpl)7 CategoryProductXrefImpl (org.broadleafcommerce.core.catalog.domain.CategoryProductXrefImpl)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 RelatedProduct (org.broadleafcommerce.core.catalog.domain.RelatedProduct)6 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)6