use of org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint in project ofbiz-framework by apache.
the class ProductSearchSession method checkDoKeywordOverride.
/**
* A ControlServlet event method used to check to see if there is an override for any of the current keywords in the search
*/
public static final String checkDoKeywordOverride(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
Delegator delegator = (Delegator) request.getAttribute("delegator");
Map<String, Object> requestParams = UtilHttp.getParameterMap(request);
// get the current productStoreId
String productStoreId = ProductStoreWorker.getProductStoreId(request);
if (productStoreId != null) {
// get a Set of all keywords in the search, if there are any...
Set<String> keywords = new HashSet<>();
List<ProductSearchConstraint> constraintList = ProductSearchOptions.getConstraintList(session);
if (constraintList != null) {
for (ProductSearchConstraint constraint : constraintList) {
if (constraint instanceof KeywordConstraint) {
KeywordConstraint keywordConstraint = (KeywordConstraint) constraint;
Set<String> keywordSet = keywordConstraint.makeFullKeywordSet(delegator);
if (keywordSet != null) {
keywords.addAll(keywordSet);
}
}
}
}
if (keywords.size() > 0) {
List<GenericValue> productStoreKeywordOvrdList = null;
try {
productStoreKeywordOvrdList = EntityQuery.use(delegator).from("ProductStoreKeywordOvrd").where("productStoreId", productStoreId).orderBy("-fromDate").cache(true).filterByDate().queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Error reading ProductStoreKeywordOvrd list, not doing keyword override", module);
}
if (UtilValidate.isNotEmpty(productStoreKeywordOvrdList)) {
for (GenericValue productStoreKeywordOvrd : productStoreKeywordOvrdList) {
String ovrdKeyword = productStoreKeywordOvrd.getString("keyword");
if (keywords.contains(ovrdKeyword)) {
String targetTypeEnumId = productStoreKeywordOvrd.getString("targetTypeEnumId");
String target = productStoreKeywordOvrd.getString("target");
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
if ("KOTT_PRODCAT".equals(targetTypeEnumId)) {
String requestName = "/category/~category_id=" + target;
target = rh.makeLink(request, response, requestName, false, false, false);
} else if ("KOTT_PRODUCT".equals(targetTypeEnumId)) {
String requestName = "/product/~product_id=" + target;
target = rh.makeLink(request, response, requestName, false, false, false);
} else if ("KOTT_OFBURL".equals(targetTypeEnumId)) {
target = rh.makeLink(request, response, target, false, false, false);
} else if ("KOTT_AURL".equals(targetTypeEnumId)) {
// do nothing, is absolute URL
} else {
Debug.logError("The targetTypeEnumId [] is not recognized, not doing keyword override", module);
// might as well see if there are any others...
continue;
}
try {
response.sendRedirect(target);
return "none";
} catch (IOException e) {
Debug.logError(e, "Could not send redirect to: " + target, module);
continue;
}
}
}
}
}
}
return "success";
}
Aggregations