Search in sources :

Example 1 with SearchRedirect

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

the class SearchRedirectDaoImpl method buildFindSearchRedirectBySearchTermCriteria.

private CriteriaQuery<SearchRedirect> buildFindSearchRedirectBySearchTermCriteria(String searchTerm) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<SearchRedirect> criteria = builder.createQuery(SearchRedirect.class);
    Root<SearchRedirectImpl> redirect = criteria.from(SearchRedirectImpl.class);
    List<Predicate> restrictions = new ArrayList<>();
    restrictions.add(builder.equal(builder.upper(redirect.<String>get("searchTerm")), searchTerm.toUpperCase()));
    // Add the active start/end date restrictions
    Date currentDate = getCurrentDateAfterFactoringInDateResolution();
    if (isNullActiveStartDateActive) {
        restrictions.add(builder.or(builder.isNull(redirect.get("activeStartDate")), builder.lessThanOrEqualTo(redirect.get("activeStartDate").as(Date.class), currentDate)));
    } else {
        restrictions.add(builder.and(builder.isNotNull(redirect.get("activeStartDate")), builder.lessThanOrEqualTo(redirect.get("activeStartDate").as(Date.class), currentDate)));
    }
    restrictions.add(builder.or(builder.isNull(redirect.get("activeEndDate")), builder.greaterThan(redirect.get("activeEndDate").as(Date.class), currentDate)));
    // Add the restrictions to the criteria query
    criteria.select(redirect);
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    return criteria.orderBy(builder.asc(redirect.get("searchPriority")));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) SearchRedirect(org.broadleafcommerce.core.search.redirect.domain.SearchRedirect) ArrayList(java.util.ArrayList) SearchRedirectImpl(org.broadleafcommerce.core.search.redirect.domain.SearchRedirectImpl) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate)

Example 2 with SearchRedirect

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

the class SearchRedirectDaoImpl method findSearchRedirectBySearchTerm.

@Override
public SearchRedirect findSearchRedirectBySearchTerm(String searchTerm) {
    Query query = em.createQuery(buildFindSearchRedirectBySearchTermCriteria(searchTerm));
    query.setMaxResults(1);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    List<SearchRedirect> results = query.getResultList();
    if (results != null && !results.isEmpty()) {
        return results.get(0);
    } else {
        return null;
    }
}
Also used : CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) SearchRedirect(org.broadleafcommerce.core.search.redirect.domain.SearchRedirect)

Example 3 with SearchRedirect

use of org.broadleafcommerce.core.search.redirect.domain.SearchRedirect 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)

Aggregations

SearchRedirect (org.broadleafcommerce.core.search.redirect.domain.SearchRedirect)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Query (javax.persistence.Query)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)1 Predicate (javax.persistence.criteria.Predicate)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 SearchCriteria (org.broadleafcommerce.core.search.domain.SearchCriteria)1 SearchResult (org.broadleafcommerce.core.search.domain.SearchResult)1 SearchRedirectImpl (org.broadleafcommerce.core.search.redirect.domain.SearchRedirectImpl)1