Search in sources :

Example 1 with SearchCriteria

use of org.broadleafcommerce.core.search.domain.SearchCriteria 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 2 with SearchCriteria

use of org.broadleafcommerce.core.search.domain.SearchCriteria 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 3 with SearchCriteria

use of org.broadleafcommerce.core.search.domain.SearchCriteria in project BroadleafCommerce by BroadleafCommerce.

the class SearchFacetDTOServiceImpl method buildSearchCriteria.

@Override
public SearchCriteria buildSearchCriteria(HttpServletRequest request) {
    SearchCriteria searchCriteria = createSearchCriteria();
    searchCriteria.setPageSize(getDefaultPageSize());
    Map<String, String[]> facets = new HashMap<>();
    for (Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
        String key = entry.getKey();
        if (Objects.equals(key, SearchCriteria.SORT_STRING)) {
            searchCriteria.setSortQuery(StringUtils.join(entry.getValue(), ","));
        } else if (Objects.equals(key, SearchCriteria.PAGE_NUMBER)) {
            searchCriteria.setPage(Integer.parseInt(entry.getValue()[0]));
        } else if (Objects.equals(key, SearchCriteria.PAGE_SIZE_STRING)) {
            int requestedPageSize = Integer.parseInt(entry.getValue()[0]);
            int maxPageSize = getMaxPageSize();
            searchCriteria.setPageSize(Math.min(requestedPageSize, maxPageSize));
        } else if (Objects.equals(key, SearchCriteria.QUERY_STRING)) {
            String query = request.getParameter(SearchCriteria.QUERY_STRING);
            try {
                if (StringUtils.isNotEmpty(query)) {
                    query = exploitProtectionService.cleanString(StringUtils.trim(query));
                }
            } catch (ServiceException e) {
                query = null;
            }
            searchCriteria.setQuery(query);
        } else if (Objects.equals(key, SearchCriteria.REQUEST_HANDLER)) {
            String requestHandler = entry.getValue()[0];
            if (!requestHandler.startsWith("/")) {
                requestHandler = "/" + requestHandler;
            }
            searchCriteria.setRequestHandler(requestHandler);
        } else {
            facets.put(key, entry.getValue());
        }
    }
    searchCriteria.setFilterCriteria(facets);
    searchCriteria.setCategory((Category) request.getAttribute(CategoryHandlerMapping.CURRENT_CATEGORY_ATTRIBUTE_NAME));
    return searchCriteria;
}
Also used : ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) SearchCriteria(org.broadleafcommerce.core.search.domain.SearchCriteria)

Example 4 with SearchCriteria

use of org.broadleafcommerce.core.search.domain.SearchCriteria in project BroadleafCommerce by BroadleafCommerce.

the class MvelToSearchCriteriaConversionServiceImpl method convert.

@Override
public SearchCriteria convert(String mvelRule) {
    SearchCriteria criteria = new SearchCriteria();
    criteria.setPageSize(Integer.MAX_VALUE);
    if (isCustomFieldTargetingRule(mvelRule)) {
        String customFieldQuery = buildCustomFieldQuery(mvelRule);
        criteria.setQuery(customFieldQuery);
    } else if (isCategoryTargetingRule(mvelRule)) {
        Long categoryId = getCategoryId(mvelRule);
        Category category = catalogService.findCategoryById(categoryId);
        criteria.setCategory(category);
    } else {
        throw new UnsupportedOperationException("The provided MVEL rule format is not currently supported " + "for MVEL to Solr Search Criteria conversion.");
    }
    return criteria;
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) SearchCriteria(org.broadleafcommerce.core.search.domain.SearchCriteria)

Aggregations

SearchCriteria (org.broadleafcommerce.core.search.domain.SearchCriteria)4 HashMap (java.util.HashMap)2 Entry (java.util.Map.Entry)2 Category (org.broadleafcommerce.core.catalog.domain.Category)2 Product (org.broadleafcommerce.core.catalog.domain.Product)2 Sku (org.broadleafcommerce.core.catalog.domain.Sku)2 SearchResult (org.broadleafcommerce.core.search.domain.SearchResult)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)1 SearchRedirect (org.broadleafcommerce.core.search.redirect.domain.SearchRedirect)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1