Search in sources :

Example 1 with KeywordConstraint

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";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HttpSession(javax.servlet.http.HttpSession) IOException(java.io.IOException) Delegator(org.apache.ofbiz.entity.Delegator) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ServletContext(javax.servlet.ServletContext) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) HashSet(java.util.HashSet) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Aggregations

IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ServletContext (javax.servlet.ServletContext)1 HttpSession (javax.servlet.http.HttpSession)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 KeywordConstraint (org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint)1 ProductSearchConstraint (org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)1 RequestHandler (org.apache.ofbiz.webapp.control.RequestHandler)1