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")));
}
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;
}
}
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();
}
Aggregations