Search in sources :

Example 21 with Sku

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

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

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

the class BroadleafSkuController method handleRequest.

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ModelAndView model = new ModelAndView();
    Sku sku = (Sku) request.getAttribute(SkuHandlerMapping.CURRENT_SKU_ATTRIBUTE_NAME);
    assert (sku != null);
    model.addObject(MODEL_ATTRIBUTE_NAME, sku);
    Set<Sku> allSkusSet = new HashSet<Sku>();
    allSkusSet.add(sku);
    model.addObject(ALL_SKUS_ATTRIBUTE_NAME, new HashSet<Sku>(allSkusSet));
    addDeepLink(model, deepLinkService, sku);
    if (StringUtils.isNotEmpty(sku.getDisplayTemplate())) {
        model.setViewName(sku.getDisplayTemplate());
    } else {
        model.setViewName(getDefaultSkuView());
    }
    return model;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) Sku(org.broadleafcommerce.core.catalog.domain.Sku) HashSet(java.util.HashSet)

Example 24 with Sku

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

the class ProductLinkedDataGeneratorImpl method addSkus.

protected void addSkus(final HttpServletRequest request, final Product product, final JSONObject productData, final String url) throws JSONException {
    final JSONArray offers = new JSONArray();
    final String currency = product.getRetailPrice().getCurrency().getCurrencyCode();
    BigDecimal highPrice = BigDecimal.ZERO;
    BigDecimal lowPrice = null;
    for (final Sku sku : product.getAllSellableSkus()) {
        final JSONObject offer = new JSONObject();
        offer.put("@type", "Offer");
        offer.put("sku", sku.getId());
        offer.put("priceCurrency", currency);
        offer.put("availability", determineAvailability(sku));
        offer.put("url", url);
        offer.put("category", product.getCategory().getName());
        if (sku.getActiveEndDate() != null) {
            offer.put("priceValidUntil", ISO_8601_FORMAT.format(sku.getActiveEndDate()));
        }
        final Money price = sku.getPriceData().getPrice();
        offer.put("price", price.getAmount());
        if (price.greaterThan(highPrice)) {
            highPrice = price.getAmount();
        }
        if (lowPrice == null || price.lessThan(lowPrice)) {
            lowPrice = price.getAmount();
        }
        extensionManager.getProxy().addSkuData(request, product, offer);
        offers.put(offer);
    }
    // use aggregateOffer to handle multiple sellable SKUs for a single product
    if (offers.length() > 1) {
        final JSONObject aggregateOffer = new JSONObject();
        aggregateOffer.put("@type", "AggregateOffer");
        aggregateOffer.put("highPrice", highPrice.doubleValue());
        aggregateOffer.put("lowPrice", lowPrice.doubleValue());
        aggregateOffer.put("priceCurrency", currency);
        aggregateOffer.put("offerCount", offers.length());
        aggregateOffer.put("offers", offers);
        extensionManager.getProxy().addAggregateSkuData(request, product, aggregateOffer);
        productData.put("offers", aggregateOffer);
    } else {
        productData.put("offers", offers);
    }
}
Also used : Money(org.broadleafcommerce.common.money.Money) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) Sku(org.broadleafcommerce.core.catalog.domain.Sku) BigDecimal(java.math.BigDecimal)

Example 25 with Sku

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

the class SkuDaoImpl method readSkusByIds.

@Override
public List<Sku> readSkusByIds(List<Long> skuIds) {
    if (skuIds == null || skuIds.size() == 0) {
        return null;
    }
    if (skuIds.size() > 100) {
        logger.warn("Not recommended to use the readSkusByIds method for long lists of skuIds, since " + "Hibernate is required to transform the distinct results. The list of requested" + "sku ids was (" + skuIds.size() + ") in length.");
    }
    // Set up the criteria query that specifies we want to return Products
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Sku> criteria = builder.createQuery(Sku.class);
    Root<SkuImpl> sku = criteria.from(SkuImpl.class);
    criteria.select(sku);
    // We only want results that match the sku IDs
    criteria.where(sku.get("id").as(Long.class).in(sandBoxHelper.mergeCloneIds(SkuImpl.class, skuIds.toArray(new Long[skuIds.size()]))));
    TypedQuery<Sku> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) Sku(org.broadleafcommerce.core.catalog.domain.Sku)

Aggregations

Sku (org.broadleafcommerce.core.catalog.domain.Sku)80 Product (org.broadleafcommerce.core.catalog.domain.Product)34 ArrayList (java.util.ArrayList)27 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)22 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)18 Money (org.broadleafcommerce.common.money.Money)16 ProductImpl (org.broadleafcommerce.core.catalog.domain.ProductImpl)16 Order (org.broadleafcommerce.core.order.domain.Order)15 Category (org.broadleafcommerce.core.catalog.domain.Category)13 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)12 Test (org.testng.annotations.Test)12 DiscreteOrderItemImpl (org.broadleafcommerce.core.order.domain.DiscreteOrderItemImpl)10 OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)10 Transactional (org.springframework.transaction.annotation.Transactional)10 ProductBundle (org.broadleafcommerce.core.catalog.domain.ProductBundle)9 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)9 HashMap (java.util.HashMap)8 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)8 Entity (org.broadleafcommerce.openadmin.dto.Entity)8