📄 productsearchsession.java
字号:
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); } } } // if there is another category, add a constraint for it if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_CATEGORY_ID"))) { String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID"); String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES"); searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories)), session); constraintsChanged = true; } if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_CATEGORY_ID2"))) { String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID2"); String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES2"); searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories)), session); constraintsChanged = true; } if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_CATEGORY_ID3"))) { String searchCategoryId = (String) parameters.get("SEARCH_CATEGORY_ID3"); String searchSubCategories = (String) parameters.get("SEARCH_SUB_CATEGORIES3"); searchAddConstraint(new ProductSearch.CategoryConstraint(searchCategoryId, !"N".equals(searchSubCategories)), session); constraintsChanged = true; } // if keywords were specified, add a constraint for them if (UtilValidate.isNotEmpty((String) 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((String) parameters.get("SEARCH_ANYPRESUF")); searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session); constraintsChanged = true; } if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_STRING2"))) { String keywordString = (String) parameters.get("SEARCH_STRING2"); String searchOperator = (String) parameters.get("SEARCH_OPERATOR2"); // defaults to true/Y, ie anything but N is true/Y boolean anyPrefixSuffix = !"N".equals((String) parameters.get("SEARCH_ANYPRESUF2")); searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session); constraintsChanged = true; } if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_STRING3"))) { String keywordString = (String) parameters.get("SEARCH_STRING3"); String searchOperator = (String) parameters.get("SEARCH_OPERATOR3"); // defaults to true/Y, ie anything but N is true/Y boolean anyPrefixSuffix = !"N".equals((String) parameters.get("SEARCH_ANYPRESUF3")); searchAddConstraint(new ProductSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session); constraintsChanged = true; } // get independently defined features, ie not with a type parameter if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_FEAT"))) { searchAddConstraint(new ProductSearch.FeatureConstraint((String) parameters.get("SEARCH_FEAT")), session); constraintsChanged = true; } if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_FEAT2"))) { searchAddConstraint(new ProductSearch.FeatureConstraint((String) parameters.get("SEARCH_FEAT2")), session); constraintsChanged = true; } if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_FEAT3"))) { searchAddConstraint(new ProductSearch.FeatureConstraint((String) parameters.get("SEARCH_FEAT3")), session); constraintsChanged = true; } // if features were selected add a constraint for each Map featureIdByType = ParametricSearch.makeFeatureIdByTypeMap(parameters); if (featureIdByType.size() > 0) { constraintsChanged = true; searchAddFeatureIdConstraints(featureIdByType.values(), session); } // add a supplier to the search if (UtilValidate.isNotEmpty((String) parameters.get("SEARCH_SUPPLIER_ID"))) { String supplierPartyId = (String) parameters.get("SEARCH_SUPPLIER_ID"); searchAddConstraint(new ProductSearch.SupplierConstraint(supplierPartyId), session); constraintsChanged = true; } // set the sort order String sortOrder = (String) parameters.get("sortOrder"); String sortAscending = (String) parameters.get("sortAscending"); boolean ascending = !"N".equals(sortAscending); if (sortOrder != null) { if (sortOrder.equals("SortKeywordRelevancy")) { 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("SortProductPrice:")) { String priceTypeId = sortOrder.substring("SortProductPrice:".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(); } String viewIndexStr = (String) parameters.get("VIEW_INDEX"); if (UtilValidate.isNotEmpty(viewIndexStr)) { try { productSearchOptions.setViewIndex(Integer.valueOf(viewIndexStr)); } catch (Exception e) { Debug.logError(e, "Error formatting VIEW_INDEX, setting to 0", module); // we could just do nothing here, but we know something was specified so we don't want to use the previous value from the session productSearchOptions.setViewIndex(new Integer(0)); } } String viewSizeStr = (String) parameters.get("VIEW_SIZE"); if (UtilValidate.isNotEmpty(viewSizeStr)) { try { productSearchOptions.setViewSize(Integer.valueOf(viewSizeStr)); } catch (Exception e) { Debug.logError(e, "Error formatting VIEW_SIZE, setting to 20", module); productSearchOptions.setViewSize(new Integer(20)); } } } public static Map getProductSearchResult(HttpSession session, GenericDelegator delegator, String prodCatalogId) { // ========== Create View Indexes int viewIndex = 0; int viewSize = 20; int highIndex = 0; int lowIndex = 0; int listSize = 0; ProductSearchOptions productSearchOptions = getProductSearchOptions(session); Integer viewIndexInteger = productSearchOptions.getViewIndex(); if (viewIndexInteger != null) viewIndex = viewIndexInteger.intValue(); Integer viewSizeInteger = productSearchOptions.getViewSize(); if (viewSizeInteger != null) viewSize = viewSizeInteger.intValue(); lowIndex = viewIndex * viewSize; highIndex = (viewIndex + 1) * viewSize; // setup resultOffset and maxResults, noting that resultOffset is 1 based, not zero based as these numbers Integer resultOffset = new Integer(lowIndex + 1); Integer maxResults = new Integer(viewSize); // ========== Do the actual search ArrayList productIds = null; String visitId = VisitHandler.getVisitId(session); List productSearchConstraintList = ProductSearchOptions.getConstraintList(session); // if no constraints, don't do a search... if (productSearchConstraintList != null && productSearchConstraintList.size() > 0) { // if the search options have changed since the last search, put at the beginning of the options history list checkSaveSearchOptionsHistory(session); productSearchConstraintList = ensureViewAllowConstraint(productSearchConstraintList, prodCatalogId, delegator); ResultSortOrder resultSortOrder = ProductSearchOptions.getResultSortOrder(session); ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId); productSearchContext.addProductSearchConstraints(productSearchConstraintList); productSearchContext.setResultSortOrder(resultSortOrder); productSearchContext.setResultOffset(resultOffset); productSearchContext.setMaxResults(maxResults); productIds = productSearchContext.doSearch(); Integer totalResults = productSearchContext.getTotalResults(); if (totalResults != null) { listSize = totalResults.intValue(); } } if (listSize < highIndex) { highIndex = listSize; } // ========== Setup other display info List searchConstraintStrings = searchGetConstraintStrings(false, session, delegator); String searchSortOrderString = searchGetSortOrderString(false, session); // ========== populate the result Map Map result = new HashMap(); result.put("productIds", productIds); result.put("viewIndex", new Integer(viewIndex)); result.put("viewSize", new Integer(viewSize)); result.put("listSize", new Integer(listSize)); result.put("lowIndex", new Integer(lowIndex)); result.put("highIndex", new Integer(highIndex)); result.put("searchConstraintStrings", searchConstraintStrings); result.put("searchSortOrderString", searchSortOrderString); return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -