Search in sources :

Example 1 with CategoryConstraint

use of org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint 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 2 with CategoryConstraint

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

Aggregations

CategoryConstraint (org.apache.ofbiz.product.product.ProductSearch.CategoryConstraint)2 FeatureConstraint (org.apache.ofbiz.product.product.ProductSearch.FeatureConstraint)2 KeywordConstraint (org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint)2 ProductSearchConstraint (org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)2 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)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 SortKeywordRelevancy (org.apache.ofbiz.product.product.ProductSearch.SortKeywordRelevancy)1