Search in sources :

Example 1 with ResultSortOrder

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

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

Example 3 with ResultSortOrder

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

the class ProductSearchSession method getProductSearchResult.

@SuppressWarnings("unchecked")
public static Map<String, Object> getProductSearchResult(HttpServletRequest request, Delegator delegator, String prodCatalogId) {
    // ========== Create View Indexes
    int viewIndex = 0;
    int viewSize = 20;
    int highIndex = 0;
    int lowIndex = 0;
    int listSize = 0;
    String paging = "Y";
    int previousViewSize = 20;
    Map<String, Object> requestParams = UtilHttp.getCombinedMap(request);
    List<String> keywordTypeIds = new LinkedList<>();
    if (requestParams.get("keywordTypeId") instanceof String) {
        keywordTypeIds.add((String) requestParams.get("keywordTypeId"));
    } else if (requestParams.get("keywordTypeId") instanceof List) {
        keywordTypeIds = (List<String>) requestParams.get("keywordTypeId");
    }
    String statusId = (String) requestParams.get("statusId");
    HttpSession session = request.getSession();
    ProductSearchOptions productSearchOptions = getProductSearchOptions(session);
    String addOnTopProdCategoryId = productSearchOptions.getTopProductCategoryId();
    Integer viewIndexInteger = productSearchOptions.getViewIndex();
    if (viewIndexInteger != null) {
        viewIndex = viewIndexInteger.intValue();
    }
    Integer viewSizeInteger = productSearchOptions.getViewSize();
    if (viewSizeInteger != null) {
        viewSize = viewSizeInteger.intValue();
    }
    Integer previousViewSizeInteger = productSearchOptions.getPreviousViewSize();
    if (previousViewSizeInteger != null) {
        previousViewSize = previousViewSizeInteger.intValue();
    }
    String pag = productSearchOptions.getPaging();
    paging = pag;
    lowIndex = viewIndex * viewSize;
    highIndex = (viewIndex + 1) * viewSize;
    // ========== Do the actual search
    List<String> productIds = new LinkedList<>();
    String visitId = VisitHandler.getVisitId(session);
    List<ProductSearchConstraint> productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
    String noConditionFind = (String) requestParams.get("noConditionFind");
    if (UtilValidate.isEmpty(noConditionFind)) {
        noConditionFind = EntityUtilProperties.getPropertyValue("widget", "widget.defaultNoConditionFind", delegator);
    }
    // if noConditionFind to Y then find without conditions otherwise search according to constraints.
    if ("Y".equals(noConditionFind) || UtilValidate.isNotEmpty(productSearchConstraintList)) {
        // if the search options have changed since the last search, put at the beginning of the options history list
        checkSaveSearchOptionsHistory(session);
        int addOnTopTotalListSize = 0;
        int addOnTopListSize = 0;
        List<GenericValue> addOnTopProductCategoryMembers;
        if (UtilValidate.isNotEmpty(addOnTopProdCategoryId)) {
            // always include the members of the addOnTopProdCategoryId
            Timestamp now = UtilDateTime.nowTimestamp();
            List<EntityCondition> addOnTopProdCondList = new LinkedList<>();
            addOnTopProdCondList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN, now)));
            addOnTopProdCondList.add(EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN, now));
            addOnTopProdCondList.add(EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, addOnTopProdCategoryId));
            EntityQuery eq = EntityQuery.use(delegator).select(UtilMisc.toSet("productId", "sequenceNum")).from("ProductCategoryMember").where(addOnTopProdCondList).orderBy("sequenceNum").cursorScrollInsensitive().distinct().maxRows(highIndex);
            try (EntityListIterator pli = eq.queryIterator()) {
                addOnTopProductCategoryMembers = pli.getPartialList(lowIndex, viewSize);
                addOnTopListSize = addOnTopProductCategoryMembers.size();
                for (GenericValue alwaysAddProductCategoryMember : addOnTopProductCategoryMembers) {
                    productIds.add(alwaysAddProductCategoryMember.getString("productId"));
                }
                addOnTopTotalListSize = pli.getResultsSizeAfterPartialList();
                listSize = listSize + addOnTopTotalListSize;
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
        // setup resultOffset and maxResults, noting that resultOffset is 1 based, not zero based as these numbers
        int resultOffsetInt = lowIndex - addOnTopTotalListSize + 1;
        if (resultOffsetInt < 1) {
            resultOffsetInt = 1;
        }
        int maxResultsInt = viewSize - addOnTopListSize;
        Integer resultOffset = Integer.valueOf(resultOffsetInt);
        Integer maxResults = Integer.valueOf(maxResultsInt);
        ResultSortOrder resultSortOrder = ProductSearchOptions.getResultSortOrder(request);
        ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
        if (UtilValidate.isNotEmpty(productSearchConstraintList)) {
            productSearchContext.addProductSearchConstraints(productSearchConstraintList);
        }
        productSearchContext.setResultSortOrder(resultSortOrder);
        productSearchContext.setResultOffset(resultOffset);
        productSearchContext.setMaxResults(maxResults);
        if (UtilValidate.isNotEmpty(keywordTypeIds)) {
            productSearchContext.keywordTypeIds = keywordTypeIds;
        } else {
            productSearchContext.keywordTypeIds = UtilMisc.toList("KWT_KEYWORD");
        }
        if (UtilValidate.isNotEmpty(statusId)) {
            productSearchContext.statusId = statusId;
        }
        List<String> foundProductIds = productSearchContext.doSearch();
        if (maxResultsInt > 0) {
            productIds.addAll(foundProductIds);
        }
        Integer totalResults = productSearchContext.getTotalResults();
        if (totalResults != null) {
            listSize = listSize + totalResults.intValue();
        }
    }
    if (listSize < highIndex) {
        highIndex = listSize;
    }
    // ========== Setup other display info
    List<String> searchConstraintStrings = searchGetConstraintStrings(false, session, delegator);
    String searchSortOrderString = searchGetSortOrderString(false, request);
    // ========== populate the result Map
    Map<String, Object> result = new HashMap<>();
    result.put("productIds", productIds);
    result.put("viewIndex", Integer.valueOf(viewIndex));
    result.put("viewSize", Integer.valueOf(viewSize));
    result.put("listSize", Integer.valueOf(listSize));
    result.put("lowIndex", Integer.valueOf(lowIndex));
    result.put("highIndex", Integer.valueOf(highIndex));
    result.put("paging", paging);
    result.put("previousViewSize", previousViewSize);
    result.put("searchConstraintStrings", searchConstraintStrings);
    result.put("searchSortOrderString", searchSortOrderString);
    result.put("noConditionFind", noConditionFind);
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ResultSortOrder(org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp) 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) LinkedList(java.util.LinkedList) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ProductSearchContext(org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 4 with ResultSortOrder

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

the class ProductSearchSession method searchGetSortOrderString.

public static String searchGetSortOrderString(boolean detailed, HttpServletRequest request) {
    Locale locale = UtilHttp.getLocale(request);
    ResultSortOrder resultSortOrder = ProductSearchOptions.getResultSortOrder(request);
    return resultSortOrder.prettyPrintSortOrder(detailed, locale);
}
Also used : Locale(java.util.Locale) ResultSortOrder(org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder)

Example 5 with ResultSortOrder

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

the class ProductSearchEvents method getProductSearchResults.

private static EntityListIterator getProductSearchResults(HttpServletRequest request) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String visitId = VisitHandler.getVisitId(session);
    List<ProductSearch.ProductSearchConstraint> productSearchConstraintList = ProductSearchSession.ProductSearchOptions.getConstraintList(session);
    // if no constraints, don't do a search...
    if (UtilValidate.isNotEmpty(productSearchConstraintList)) {
        ResultSortOrder resultSortOrder = ProductSearchSession.ProductSearchOptions.getResultSortOrder(request);
        ProductSearchSession.checkSaveSearchOptionsHistory(session);
        ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
        productSearchContext.addProductSearchConstraints(productSearchConstraintList);
        productSearchContext.setResultSortOrder(resultSortOrder);
        return productSearchContext.doQuery(delegator);
    }
    return null;
}
Also used : ResultSortOrder(org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder) Delegator(org.apache.ofbiz.entity.Delegator) HttpSession(javax.servlet.http.HttpSession) ProductSearchContext(org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext)

Aggregations

ResultSortOrder (org.apache.ofbiz.product.product.ProductSearch.ResultSortOrder)5 ProductSearchConstraint (org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)3 ArrayList (java.util.ArrayList)2 HttpSession (javax.servlet.http.HttpSession)2 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 ProductSearchContext (org.apache.ofbiz.product.product.ProductSearch.ProductSearchContext)2 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Locale (java.util.Locale)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)1 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)1 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)1 SortKeywordRelevancy (org.apache.ofbiz.product.product.ProductSearch.SortKeywordRelevancy)1