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("&");
} else {
isNotFirst = true;
}
searchParamString.append("S_CAT");
searchParamString.append(categoriesCount);
searchParamString.append("=");
searchParamString.append(cc.productCategoryId);
searchParamString.append("&S_CSB");
searchParamString.append(categoriesCount);
searchParamString.append("=");
searchParamString.append(cc.includeSubCategories ? "Y" : "N");
if (cc.exclude != null) {
searchParamString.append("&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("&");
} else {
isNotFirst = true;
}
searchParamString.append("S_PFI");
searchParamString.append(featuresCount);
searchParamString.append("=");
searchParamString.append(fc.productFeatureId);
if (fc.exclude != null) {
searchParamString.append("&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("&");
} else {
isNotFirst = true;
}
searchParamString.append("S_FCI");
searchParamString.append(featureCategoriesCount);
searchParamString.append("=");
searchParamString.append(pfcc.productFeatureCategoryId);
if (pfcc.exclude != null) {
searchParamString.append("&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("&");
} else {
isNotFirst = true;
}
searchParamString.append("S_FGI");
searchParamString.append(featureGroupsCount);
searchParamString.append("=");
searchParamString.append(pfgc.productFeatureGroupId);
if (pfgc.exclude != null) {
searchParamString.append("&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("&");
} else {
isNotFirst = true;
}
searchParamString.append("SEARCH_STRING");
searchParamString.append(keywordsCount);
searchParamString.append("=");
searchParamString.append(UtilHttp.encodeBlanks(kc.keywordsString));
searchParamString.append("&SEARCH_OPERATOR");
searchParamString.append(keywordsCount);
searchParamString.append("=");
searchParamString.append(kc.isAnd ? "AND" : "OR");
searchParamString.append("&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("&");
} 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("&");
} else {
isNotFirst = true;
}
searchParamString.append("S_SUP");
searchParamString.append("=");
searchParamString.append(suppc.supplierPartyId);
}
}
}
String topProductCategoryId = productSearchOptions.getTopProductCategoryId();
if (topProductCategoryId != null) {
searchParamString.append("&S_TPC");
searchParamString.append("=");
searchParamString.append(topProductCategoryId);
}
ResultSortOrder resultSortOrder = productSearchOptions.getResultSortOrder();
if (resultSortOrder instanceof ProductSearch.SortKeywordRelevancy) {
searchParamString.append("&S_O=SKR");
} else if (resultSortOrder instanceof ProductSearch.SortProductField) {
ProductSearch.SortProductField spf = (ProductSearch.SortProductField) resultSortOrder;
searchParamString.append("&S_O=SPF:");
searchParamString.append(spf.fieldName);
} else if (resultSortOrder instanceof ProductSearch.SortProductPrice) {
ProductSearch.SortProductPrice spp = (ProductSearch.SortProductPrice) resultSortOrder;
searchParamString.append("&S_O=SPP:");
searchParamString.append(spp.productPriceTypeId);
} else if (resultSortOrder instanceof ProductSearch.SortProductFeature) {
ProductSearch.SortProductFeature spf = (ProductSearch.SortProductFeature) resultSortOrder;
searchParamString.append("&S_O=SPFT:");
searchParamString.append(spf.productFeatureTypeId);
}
searchParamString.append("&S_A=");
searchParamString.append(resultSortOrder.isAscending() ? "Y" : "N");
return searchParamString.toString();
}
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);
}
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;
}
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);
}
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;
}
Aggregations