Search in sources :

Example 1 with ProductSearchConstraint

use of org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint in project ofbiz-framework by apache.

the class ProductSearchSession method makeSearchParametersString.

public static String makeSearchParametersString(ProductSearchOptions productSearchOptions) {
    StringBuilder searchParamString = new StringBuilder();
    List<ProductSearchConstraint> constraintList = productSearchOptions.getConstraintList();
    if (UtilValidate.isEmpty(constraintList)) {
        constraintList = new ArrayList<>();
    }
    int categoriesCount = 0;
    int featuresCount = 0;
    int featureCategoriesCount = 0;
    int featureGroupsCount = 0;
    int keywordsCount = 0;
    boolean isNotFirst = false;
    for (ProductSearchConstraint psc : constraintList) {
        if (psc instanceof ProductSearch.CategoryConstraint) {
            ProductSearch.CategoryConstraint cc = (ProductSearch.CategoryConstraint) psc;
            categoriesCount++;
            if (isNotFirst) {
                searchParamString.append("&amp;");
            } else {
                isNotFirst = true;
            }
            searchParamString.append("S_CAT");
            searchParamString.append(categoriesCount);
            searchParamString.append("=");
            searchParamString.append(cc.productCategoryId);
            searchParamString.append("&amp;S_CSB");
            searchParamString.append(categoriesCount);
            searchParamString.append("=");
            searchParamString.append(cc.includeSubCategories ? "Y" : "N");
            if (cc.exclude != null) {
                searchParamString.append("&amp;S_CEX");
                searchParamString.append(categoriesCount);
                searchParamString.append("=");
                searchParamString.append(cc.exclude.booleanValue() ? "Y" : "N");
            }
        } else if (psc instanceof ProductSearch.FeatureConstraint) {
            ProductSearch.FeatureConstraint fc = (ProductSearch.FeatureConstraint) psc;
            featuresCount++;
            if (isNotFirst) {
                searchParamString.append("&amp;");
            } else {
                isNotFirst = true;
            }
            searchParamString.append("S_PFI");
            searchParamString.append(featuresCount);
            searchParamString.append("=");
            searchParamString.append(fc.productFeatureId);
            if (fc.exclude != null) {
                searchParamString.append("&amp;S_PFX");
                searchParamString.append(featuresCount);
                searchParamString.append("=");
                searchParamString.append(fc.exclude.booleanValue() ? "Y" : "N");
            }
        } else if (psc instanceof ProductSearch.FeatureCategoryConstraint) {
            ProductSearch.FeatureCategoryConstraint pfcc = (ProductSearch.FeatureCategoryConstraint) psc;
            featureCategoriesCount++;
            if (isNotFirst) {
                searchParamString.append("&amp;");
            } else {
                isNotFirst = true;
            }
            searchParamString.append("S_FCI");
            searchParamString.append(featureCategoriesCount);
            searchParamString.append("=");
            searchParamString.append(pfcc.productFeatureCategoryId);
            if (pfcc.exclude != null) {
                searchParamString.append("&amp;S_FCX");
                searchParamString.append(featureCategoriesCount);
                searchParamString.append("=");
                searchParamString.append(pfcc.exclude.booleanValue() ? "Y" : "N");
            }
        } else if (psc instanceof ProductSearch.FeatureGroupConstraint) {
            ProductSearch.FeatureGroupConstraint pfgc = (ProductSearch.FeatureGroupConstraint) psc;
            featureGroupsCount++;
            if (isNotFirst) {
                searchParamString.append("&amp;");
            } else {
                isNotFirst = true;
            }
            searchParamString.append("S_FGI");
            searchParamString.append(featureGroupsCount);
            searchParamString.append("=");
            searchParamString.append(pfgc.productFeatureGroupId);
            if (pfgc.exclude != null) {
                searchParamString.append("&amp;S_FGX");
                searchParamString.append(featureGroupsCount);
                searchParamString.append("=");
                searchParamString.append(pfgc.exclude.booleanValue() ? "Y" : "N");
            }
        } else if (psc instanceof ProductSearch.KeywordConstraint) {
            ProductSearch.KeywordConstraint kc = (ProductSearch.KeywordConstraint) psc;
            keywordsCount++;
            if (isNotFirst) {
                searchParamString.append("&amp;");
            } else {
                isNotFirst = true;
            }
            searchParamString.append("SEARCH_STRING");
            searchParamString.append(keywordsCount);
            searchParamString.append("=");
            searchParamString.append(UtilHttp.encodeBlanks(kc.keywordsString));
            searchParamString.append("&amp;SEARCH_OPERATOR");
            searchParamString.append(keywordsCount);
            searchParamString.append("=");
            searchParamString.append(kc.isAnd ? "AND" : "OR");
            searchParamString.append("&amp;SEARCH_ANYPRESUF");
            searchParamString.append(keywordsCount);
            searchParamString.append("=");
            searchParamString.append(kc.anyPrefix | kc.anySuffix ? "Y" : "N");
        } else if (psc instanceof ProductSearch.ListPriceRangeConstraint) {
            ProductSearch.ListPriceRangeConstraint lprc = (ProductSearch.ListPriceRangeConstraint) psc;
            if (lprc.lowPrice != null || lprc.highPrice != null) {
                if (isNotFirst) {
                    searchParamString.append("&amp;");
                } else {
                    isNotFirst = true;
                }
                searchParamString.append("S_LPR");
                searchParamString.append("=");
                if (lprc.lowPrice != null) {
                    searchParamString.append(lprc.lowPrice);
                }
                searchParamString.append("_");
                if (lprc.highPrice != null) {
                    searchParamString.append(lprc.highPrice);
                }
            }
        } else if (psc instanceof ProductSearch.SupplierConstraint) {
            ProductSearch.SupplierConstraint suppc = (ProductSearch.SupplierConstraint) psc;
            if (suppc.supplierPartyId != null) {
                if (isNotFirst) {
                    searchParamString.append("&amp;");
                } else {
                    isNotFirst = true;
                }
                searchParamString.append("S_SUP");
                searchParamString.append("=");
                searchParamString.append(suppc.supplierPartyId);
            }
        }
    }
    String topProductCategoryId = productSearchOptions.getTopProductCategoryId();
    if (topProductCategoryId != null) {
        searchParamString.append("&amp;S_TPC");
        searchParamString.append("=");
        searchParamString.append(topProductCategoryId);
    }
    ResultSortOrder resultSortOrder = productSearchOptions.getResultSortOrder();
    if (resultSortOrder instanceof ProductSearch.SortKeywordRelevancy) {
        searchParamString.append("&amp;S_O=SKR");
    } else if (resultSortOrder instanceof ProductSearch.SortProductField) {
        ProductSearch.SortProductField spf = (ProductSearch.SortProductField) resultSortOrder;
        searchParamString.append("&amp;S_O=SPF:");
        searchParamString.append(spf.fieldName);
    } else if (resultSortOrder instanceof ProductSearch.SortProductPrice) {
        ProductSearch.SortProductPrice spp = (ProductSearch.SortProductPrice) resultSortOrder;
        searchParamString.append("&amp;S_O=SPP:");
        searchParamString.append(spp.productPriceTypeId);
    } else if (resultSortOrder instanceof ProductSearch.SortProductFeature) {
        ProductSearch.SortProductFeature spf = (ProductSearch.SortProductFeature) resultSortOrder;
        searchParamString.append("&amp;S_O=SPFT:");
        searchParamString.append(spf.productFeatureTypeId);
    }
    searchParamString.append("&amp;S_A=");
    searchParamString.append(resultSortOrder.isAscending() ? "Y" : "N");
    return searchParamString.toString();
}
Also used : FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) ResultSortOrder(org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) SortKeywordRelevancy(org.apache.ofbiz.product.product.ProductSearch.SortKeywordRelevancy) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 2 with ProductSearchConstraint

use of org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint in project ofbiz-framework by apache.

the class ProductSearchSession method getCategoryCostraintIndex.

public static int getCategoryCostraintIndex(HttpSession session) {
    int index = 0;
    List<ProductSearchConstraint> productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
    for (ProductSearchConstraint constraint : productSearchConstraintList) {
        if (constraint instanceof CategoryConstraint) {
            index++;
        }
    }
    return index;
}
Also used : CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 3 with ProductSearchConstraint

use of org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint in project ofbiz-framework by apache.

the class ProductSearchSession method processSearchParameters.

public static void processSearchParameters(Map<String, Object> parameters, HttpServletRequest request) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Boolean alreadyRun = (Boolean) request.getAttribute("processSearchParametersAlreadyRun");
    if (Boolean.TRUE.equals(alreadyRun)) {
        return;
    }
    request.setAttribute("processSearchParametersAlreadyRun", Boolean.TRUE);
    HttpSession session = request.getSession();
    boolean constraintsChanged = false;
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    // clear search? by default yes, but if the clearSearch parameter is N then don't
    String clearSearchString = (String) parameters.get("clearSearch");
    if (!"N".equals(clearSearchString)) {
        searchClear(session);
        constraintsChanged = true;
    } else {
        String removeConstraint = (String) parameters.get("removeConstraint");
        if (UtilValidate.isNotEmpty(removeConstraint)) {
            try {
                searchRemoveConstraint(Integer.parseInt(removeConstraint), session);
                constraintsChanged = true;
            } catch (Exception e) {
                Debug.logError(e, "Error removing constraint [" + removeConstraint + "]", module);
            }
        }
    }
    String prioritizeCategoryId = null;
    if (UtilValidate.isNotEmpty(parameters.get("PRIORITIZE_CATEGORY_ID"))) {
        prioritizeCategoryId = (String) parameters.get("PRIORITIZE_CATEGORY_ID");
    } else if (UtilValidate.isNotEmpty(parameters.get("S_TPC"))) {
        prioritizeCategoryId = (String) parameters.get("S_TPC");
    }
    if (UtilValidate.isNotEmpty(prioritizeCategoryId)) {
        ProductSearchOptions.setTopProductCategoryId(prioritizeCategoryId, session);
        constraintsChanged = true;
    }
    // if there is another category, add a constraint for it
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_CATEGORY_ID"))) {
        String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID");
        String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES");
        String searchCategoryExc = (String) parameters.get("SEARCH_CATEGORY_EXC");
        Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
        searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories), exclude), session);
        constraintsChanged = true;
    }
    for (int catNum = 1; catNum < 10; catNum++) {
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_CATEGORY_ID" + catNum))) {
            String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID" + catNum);
            String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES" + catNum);
            String searchCategoryExc = (String) parameters.get("SEARCH_CATEGORY_EXC" + catNum);
            Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
            searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories), exclude), session);
            constraintsChanged = true;
        }
    }
    // a shorter variation for categories
    for (int catNum = 1; catNum < 10; catNum++) {
        if (UtilValidate.isNotEmpty(parameters.get("S_CAT" + catNum))) {
            String searchCategoryId = (String) parameters.get("S_CAT" + catNum);
            String searchSubCategories = (String) parameters.get("S_CSB" + catNum);
            String searchCategoryExc = (String) parameters.get("S_CEX" + catNum);
            Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
            searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories), exclude), session);
            constraintsChanged = true;
        }
    }
    // if there is any category selected try to use catalog and add a constraint for it
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_CATALOG_ID"))) {
        String searchCatalogId = (String) parameters.get("SEARCH_CATALOG_ID");
        if (searchCatalogId != null && !searchCatalogId.equalsIgnoreCase("")) {
            String topCategory = CatalogWorker.getCatalogTopCategoryId(request, searchCatalogId);
            if (UtilValidate.isEmpty(topCategory)) {
                topCategory = CatalogWorker.getCatalogTopEbayCategoryId(request, searchCatalogId);
            }
            List<GenericValue> categories = CategoryWorker.getRelatedCategoriesRet(request, "topLevelList", topCategory, true, false, true);
            searchAddConstraint(new ProductSearch.CatalogConstraint(searchCatalogId, categories), session);
            constraintsChanged = true;
        }
    }
    // if keywords were specified, add a constraint for them
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_STRING"))) {
        String keywordString = (String) parameters.get("SEARCH_STRING");
        String searchOperator = (String) parameters.get("SEARCH_OPERATOR");
        // defaults to true/Y, ie anything but N is true/Y
        boolean anyPrefixSuffix = !"N".equals(parameters.get("SEARCH_ANYPRESUF"));
        searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
        constraintsChanged = true;
    }
    // if productName were specified, add a constraint for them
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_PRODUCT_NAME"))) {
        String productName = (String) parameters.get("SEARCH_PRODUCT_NAME");
        searchAddConstraint(new ProductSearch.ProductFieldConstraint(productName, "productName"), session);
        constraintsChanged = true;
    }
    // if internalName were specified, add a constraint for them
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_INTERNAL_PROD_NAME"))) {
        String internalName = (String) parameters.get("SEARCH_INTERNAL_PROD_NAME");
        searchAddConstraint(new ProductSearch.ProductFieldConstraint(internalName, "internalName"), session);
        constraintsChanged = true;
    }
    for (int kwNum = 1; kwNum < 10; kwNum++) {
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_STRING" + kwNum))) {
            String keywordString = (String) parameters.get("SEARCH_STRING" + kwNum);
            String searchOperator = (String) parameters.get("SEARCH_OPERATOR" + kwNum);
            // defaults to true/Y, ie anything but N is true/Y
            boolean anyPrefixSuffix = !"N".equals(parameters.get("SEARCH_ANYPRESUF" + kwNum));
            searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
            constraintsChanged = true;
        }
    }
    for (Entry<String, Object> entry : parameters.entrySet()) {
        String parameterName = entry.getKey();
        if (parameterName.startsWith("SEARCH_FEAT") && !parameterName.startsWith("SEARCH_FEAT_EXC")) {
            String productFeatureId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureId)) {
                String paramNameExt = parameterName.substring("SEARCH_FEAT".length());
                String searchCategoryExc = (String) parameters.get("SEARCH_FEAT_EXC" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
                // Debug.logInfo("parameterName=" + parameterName + ", paramNameExt=" + paramNameExt + ", searchCategoryExc=" + searchCategoryExc + ", exclude=" + exclude, module);
                searchAddConstraint(new ProductSearch.FeatureConstraint(productFeatureId, exclude), session);
                constraintsChanged = true;
            }
        }
        // a shorter feature variation
        if (parameterName.startsWith("S_PFI")) {
            String productFeatureId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureId)) {
                String paramNameExt = parameterName.substring("S_PFI".length());
                String searchCategoryExc = (String) parameters.get("S_PFX" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchCategoryExc));
                searchAddConstraint(new ProductSearch.FeatureConstraint(productFeatureId, exclude), session);
                constraintsChanged = true;
            }
        }
        // if product features category were selected add a constraint for each
        if (parameterName.startsWith("SEARCH_PROD_FEAT_CAT") && !parameterName.startsWith("SEARCH_PROD_FEAT_CAT_EXC")) {
            String productFeatureCategoryId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureCategoryId)) {
                String paramNameExt = parameterName.substring("SEARCH_PROD_FEAT_CAT".length());
                String searchProdFeatureCategoryExc = (String) parameters.get("SEARCH_PROD_FEAT_CAT_EXC" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureCategoryExc));
                searchAddConstraint(new ProductSearch.FeatureCategoryConstraint(productFeatureCategoryId, exclude), session);
                constraintsChanged = true;
            }
        }
        // a shorter variation for feature category
        if (parameterName.startsWith("S_FCI")) {
            String productFeatureCategoryId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureCategoryId)) {
                String paramNameExt = parameterName.substring("S_FCI".length());
                String searchProdFeatureCategoryExc = (String) parameters.get("S_FCX" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureCategoryExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureCategoryExc));
                searchAddConstraint(new ProductSearch.FeatureCategoryConstraint(productFeatureCategoryId, exclude), session);
                constraintsChanged = true;
            }
        }
        // if product features group were selected add a constraint for each
        if (parameterName.startsWith("SEARCH_PROD_FEAT_GRP") && !parameterName.startsWith("SEARCH_PROD_FEAT_GRP_EXC")) {
            String productFeatureGroupId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureGroupId)) {
                String paramNameExt = parameterName.substring("SEARCH_PROD_FEAT_GRP".length());
                String searchProdFeatureGroupExc = (String) parameters.get("SEARCH_PROD_FEAT_GRP_EXC" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureGroupExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureGroupExc));
                searchAddConstraint(new ProductSearch.FeatureGroupConstraint(productFeatureGroupId, exclude), session);
                constraintsChanged = true;
            }
        }
        // a shorter variation for feature group
        if (parameterName.startsWith("S_FGI")) {
            String productFeatureGroupId = (String) parameters.get(parameterName);
            if (UtilValidate.isNotEmpty(productFeatureGroupId)) {
                String paramNameExt = parameterName.substring("S_FGI".length());
                String searchProdFeatureGroupExc = (String) parameters.get("S_FGX" + paramNameExt);
                Boolean exclude = UtilValidate.isEmpty(searchProdFeatureGroupExc) ? null : Boolean.valueOf(!"N".equals(searchProdFeatureGroupExc));
                searchAddConstraint(new ProductSearch.FeatureGroupConstraint(productFeatureGroupId, exclude), session);
                constraintsChanged = true;
            }
        }
    }
    // if features were selected add a constraint for each
    Map<String, String> featureIdByType = ParametricSearch.makeFeatureIdByTypeMap(parameters);
    if (featureIdByType.size() > 0) {
        constraintsChanged = true;
        searchAddFeatureIdConstraints(featureIdByType.values(), null, request);
    }
    // add a supplier to the search
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_SUPPLIER_ID")) || UtilValidate.isNotEmpty(parameters.get("S_SUP"))) {
        String supplierPartyId = (String) parameters.get("SEARCH_SUPPLIER_ID");
        if (UtilValidate.isEmpty(supplierPartyId)) {
            supplierPartyId = (String) parameters.get("S_SUP");
        }
        searchAddConstraint(new ProductSearch.SupplierConstraint(supplierPartyId), session);
        constraintsChanged = true;
    }
    // add a list price range to the search
    if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_LOW")) || UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_HIGH"))) {
        BigDecimal listPriceLow = null;
        BigDecimal listPriceHigh = null;
        String listPriceCurrency = UtilHttp.getCurrencyUom(request);
        if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_LOW"))) {
            try {
                listPriceLow = new BigDecimal((String) parameters.get("LIST_PRICE_LOW"));
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing LIST_PRICE_LOW parameter [" + (String) parameters.get("LIST_PRICE_LOW") + "]: " + e.toString(), module);
            }
        }
        if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_HIGH"))) {
            try {
                listPriceHigh = new BigDecimal((String) parameters.get("LIST_PRICE_HIGH"));
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing LIST_PRICE_HIGH parameter [" + (String) parameters.get("LIST_PRICE_HIGH") + "]: " + e.toString(), module);
            }
        }
        searchAddConstraint(new ProductSearch.ListPriceRangeConstraint(listPriceLow, listPriceHigh, listPriceCurrency), session);
        constraintsChanged = true;
    }
    if (UtilValidate.isNotEmpty(parameters.get("LIST_PRICE_RANGE")) || UtilValidate.isNotEmpty(parameters.get("S_LPR"))) {
        String listPriceRangeStr = (String) parameters.get("LIST_PRICE_RANGE");
        if (UtilValidate.isEmpty(listPriceRangeStr)) {
            listPriceRangeStr = (String) parameters.get("S_LPR");
        }
        int underscoreIndex = listPriceRangeStr.indexOf('_');
        String listPriceLowStr;
        String listPriceHighStr;
        if (underscoreIndex >= 0) {
            listPriceLowStr = listPriceRangeStr.substring(0, listPriceRangeStr.indexOf('_'));
            listPriceHighStr = listPriceRangeStr.substring(listPriceRangeStr.indexOf('_') + 1);
        } else {
            // no underscore: assume it is a low range with no high range, ie the ending underscore was left off
            listPriceLowStr = listPriceRangeStr;
            listPriceHighStr = null;
        }
        BigDecimal listPriceLow = null;
        BigDecimal listPriceHigh = null;
        String listPriceCurrency = UtilHttp.getCurrencyUom(request);
        if (UtilValidate.isNotEmpty(listPriceLowStr)) {
            try {
                listPriceLow = new BigDecimal(listPriceLowStr);
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing low part of LIST_PRICE_RANGE parameter [" + listPriceLowStr + "]: " + e.toString(), module);
            }
        }
        if (UtilValidate.isNotEmpty(listPriceHighStr)) {
            try {
                listPriceHigh = new BigDecimal(listPriceHighStr);
            } catch (NumberFormatException e) {
                Debug.logError("Error parsing high part of LIST_PRICE_RANGE parameter [" + listPriceHighStr + "]: " + e.toString(), module);
            }
        }
        searchAddConstraint(new ProductSearch.ListPriceRangeConstraint(listPriceLow, listPriceHigh, listPriceCurrency), session);
        constraintsChanged = true;
    }
    // check the ProductStore to see if we should add the ExcludeVariantsConstraint
    if (productStore != null && !"N".equals(productStore.getString("prodSearchExcludeVariants"))) {
        searchAddConstraint(new ProductSearch.ExcludeVariantsConstraint(), session);
    // not consider this a change for now, shouldn't change often: constraintsChanged = true;
    }
    if ("true".equalsIgnoreCase((String) parameters.get("AVAILABILITY_FILTER"))) {
        searchAddConstraint(new ProductSearch.AvailabilityDateConstraint(), session);
        constraintsChanged = true;
    }
    if (UtilValidate.isNotEmpty(parameters.get("SEARCH_GOOD_IDENTIFICATION_TYPE")) || UtilValidate.isNotEmpty(parameters.get("SEARCH_GOOD_IDENTIFICATION_VALUE"))) {
        String include = (String) parameters.get("SEARCH_GOOD_IDENTIFICATION_INCL");
        if (UtilValidate.isEmpty(include)) {
            include = "Y";
        }
        Boolean inc = Boolean.TRUE;
        if ("N".equalsIgnoreCase(include)) {
            inc = Boolean.FALSE;
        }
        searchAddConstraint(new ProductSearch.GoodIdentificationConstraint((String) parameters.get("SEARCH_GOOD_IDENTIFICATION_TYPE"), (String) parameters.get("SEARCH_GOOD_IDENTIFICATION_VALUE"), inc), session);
        constraintsChanged = true;
    }
    String prodCatalogId = CatalogWorker.getCurrentCatalogId(request);
    String viewProductCategoryId = CatalogWorker.getCatalogViewAllowCategoryId(delegator, prodCatalogId);
    if (UtilValidate.isNotEmpty(viewProductCategoryId)) {
        ProductSearchConstraint viewAllowConstraint = new CategoryConstraint(viewProductCategoryId, true, null);
        searchAddConstraint(viewAllowConstraint, session);
    // not consider this a change for now, shouldn't change often: constraintsChanged = true;
    }
    // set the sort order
    String sortOrder = (String) parameters.get("sortOrder");
    if (UtilValidate.isEmpty(sortOrder)) {
        sortOrder = (String) parameters.get("S_O");
    }
    String sortAscending = (String) parameters.get("sortAscending");
    if (UtilValidate.isEmpty(sortAscending)) {
        sortAscending = (String) parameters.get("S_A");
    }
    boolean ascending = !"N".equals(sortAscending);
    if (sortOrder != null) {
        if ("SortKeywordRelevancy".equals(sortOrder) || "SKR".equals(sortOrder)) {
            searchSetSortOrder(new ProductSearch.SortKeywordRelevancy(), session);
        } else if (sortOrder.startsWith("SortProductField:")) {
            String fieldName = sortOrder.substring("SortProductField:".length());
            searchSetSortOrder(new ProductSearch.SortProductField(fieldName, ascending), session);
        } else if (sortOrder.startsWith("SPF:")) {
            String fieldName = sortOrder.substring("SPF:".length());
            searchSetSortOrder(new ProductSearch.SortProductField(fieldName, ascending), session);
        } else if (sortOrder.startsWith("SortProductPrice:")) {
            String priceTypeId = sortOrder.substring("SortProductPrice:".length());
            searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
        } else if (sortOrder.startsWith("SPP:")) {
            String priceTypeId = sortOrder.substring("SPP:".length());
            searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
        } else if (sortOrder.startsWith("SortProductFeature:")) {
            String featureId = sortOrder.substring("SortProductFeature:".length());
            searchSetSortOrder(new ProductSearch.SortProductFeature(featureId, ascending), session);
        } else if (sortOrder.startsWith("SPFT:")) {
            String priceTypeId = sortOrder.substring("SPFT:".length());
            searchSetSortOrder(new ProductSearch.SortProductPrice(priceTypeId, ascending), session);
        }
    }
    ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
    if (constraintsChanged) {
        // query changed, clear out the VIEW_INDEX & VIEW_SIZE
        productSearchOptions.clearViewInfo();
    }
    productSearchOptions.setViewIndex((String) parameters.get("VIEW_INDEX"));
    productSearchOptions.setViewSize((String) parameters.get("VIEW_SIZE"));
    productSearchOptions.setPaging((String) parameters.get("PAGING"));
}
Also used : KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) GenericValue(org.apache.ofbiz.entity.GenericValue) HttpSession(javax.servlet.http.HttpSession) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint) FeatureConstraint(org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) SortKeywordRelevancy(org.apache.ofbiz.product.product.ProductSearch.SortKeywordRelevancy) CategoryConstraint(org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 4 with ProductSearchConstraint

use of org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint 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)

Example 5 with ProductSearchConstraint

use of org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint in project ofbiz-framework by apache.

the class ProductSearchSession method searchDo.

public static ArrayList<String> searchDo(HttpSession session, Delegator delegator, String prodCatalogId) {
    String visitId = VisitHandler.getVisitId(session);
    ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
    List<ProductSearchConstraint> productSearchConstraintList = productSearchOptions.getConstraintList();
    if (UtilValidate.isEmpty(productSearchConstraintList)) {
        // no constraints, don't do a search...
        return new ArrayList<>();
    }
    ResultSortOrder resultSortOrder = productSearchOptions.getResultSortOrder();
    // if the search options have changed since the last search, put at the beginning of the options history list
    checkSaveSearchOptionsHistory(session);
    return ProductSearch.searchProducts(productSearchConstraintList, resultSortOrder, delegator, visitId);
}
Also used : ResultSortOrder(org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder) ArrayList(java.util.ArrayList) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Aggregations

ProductSearchConstraint (org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)9 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)6 GenericValue (org.apache.ofbiz.entity.GenericValue)6 KeywordConstraint (org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint)5 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)4 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)4 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)4 CategoryConstraint (org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint)4 FeatureConstraint (org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint)4 ProductSearchContext (org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext)4 LinkedList (java.util.LinkedList)3 HttpSession (javax.servlet.http.HttpSession)3 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)3 ResultSortOrder (org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Delegator (org.apache.ofbiz.entity.Delegator)2 SortKeywordRelevancy (org.apache.ofbiz.product.product.ProductSearch.SortKeywordRelevancy)2