📄 productsearch.java
字号:
if (andKeywordFixedSet.size() > 0) { // add up the relevancyWeight fields from all keyword member entities for a total to sort by Iterator keywordIter = andKeywordFixedSet.iterator(); while (keywordIter.hasNext()) { String keyword = (String) keywordIter.next(); // make index based values and increment String entityAlias = "PK" + index; String prefix = "pk" + index; index++; dynamicViewEntity.addMemberEntity(entityAlias, "ProductKeyword"); dynamicViewEntity.addAlias(entityAlias, prefix + "Keyword", "keyword", null, null, null, null); dynamicViewEntity.addViewLink("PROD", entityAlias, Boolean.FALSE, ModelKeyMap.makeKeyMapList("productId")); entityConditionList.add(new EntityExpr(prefix + "Keyword", EntityOperator.LIKE, keyword)); //don't add an alias for this, will be part of a complex alias: dynamicViewEntity.addAlias(entityAlias, prefix + "RelevancyWeight", "relevancyWeight", null, null, null, null); relevancyComplexAlias.addComplexAliasMember(new ComplexAliasField(entityAlias, "relevancyWeight")); } //TODO: find out why Oracle and other dbs don't like the query resulting from this and fix: productIdGroupBy = true; if (!doingBothAndOr) { dynamicViewEntity.addAlias(null, "totalRelevancy", null, null, null, null, null, relevancyComplexAlias); } } if (keywordFixedOrSetAndList.size() > 0) { Iterator keywordFixedOrSetAndIter = keywordFixedOrSetAndList.iterator(); while (keywordFixedOrSetAndIter.hasNext()) { Set keywordFixedOrSet = (Set) keywordFixedOrSetAndIter.next(); // make index based values and increment String entityAlias = "PK" + index; String prefix = "pk" + index; index++; dynamicViewEntity.addMemberEntity(entityAlias, "ProductKeyword"); dynamicViewEntity.addAlias(entityAlias, prefix + "Keyword", "keyword", null, null, null, null); dynamicViewEntity.addViewLink("PROD", entityAlias, Boolean.FALSE, ModelKeyMap.makeKeyMapList("productId")); List keywordOrList = new LinkedList(); Iterator keywordIter = keywordFixedOrSet.iterator(); while (keywordIter.hasNext()) { String keyword = (String) keywordIter.next(); keywordOrList.add(new EntityExpr(prefix + "Keyword", EntityOperator.LIKE, keyword)); } entityConditionList.add(new EntityConditionList(keywordOrList, EntityOperator.OR)); productIdGroupBy = true; if (doingBothAndOr) { relevancyComplexAlias.addComplexAliasMember(new ComplexAliasField(entityAlias, "relevancyWeight", "sum")); } else { dynamicViewEntity.addAlias(entityAlias, "totalRelevancy", "relevancyWeight", null, null, null, "sum"); } } } if (doingBothAndOr) { dynamicViewEntity.addAlias(null, "totalRelevancy", null, null, null, null, null, relevancyComplexAlias); } } public EntityListIterator doQuery(GenericDelegator delegator) { // handle the now assembled or and and keyword fixed lists this.finishKeywordConstraints(); if (resultSortOrder != null) { resultSortOrder.setSortOrder(this); } dynamicViewEntity.addAlias("PROD", "productId", null, null, null, new Boolean(productIdGroupBy), null); EntityCondition whereCondition = new EntityConditionList(entityConditionList, EntityOperator.AND); EntityFindOptions efo = new EntityFindOptions(); efo.setDistinct(true); efo.setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE); EntityListIterator eli = null; try { eli = delegator.findListIteratorByCondition(dynamicViewEntity, whereCondition, null, fieldsToSelect, orderByList, efo); } catch (GenericEntityException e) { Debug.logError(e, "Error in product search", module); return null; } return eli; } public ArrayList makeProductIdList(EntityListIterator eli) { ArrayList productIds = new ArrayList(maxResults == null ? 100 : maxResults.intValue()); if (eli == null) { Debug.logWarning("The eli is null, returning zero results", module); return productIds; } try { boolean hasResults = false; Object initialResult = null; /* this method has been replaced by the following to address issue with SAP DB and possibly other DBs if (resultOffset != null) { Debug.logInfo("Before relative, current index=" + eli.currentIndex(), module); hasResults = eli.relative(resultOffset.intValue()); } else { initialResult = eli.next(); if (initialResult != null) { hasResults = true; } } */ initialResult = eli.next(); if (initialResult != null) { hasResults = true; } if (resultOffset != null && resultOffset.intValue() > 1) { if (Debug.infoOn()) Debug.logInfo("Before relative, current index=" + eli.currentIndex(), module); hasResults = eli.relative(resultOffset.intValue() - 1); initialResult = null; } // get the first as the current one GenericValue searchResult = null; if (hasResults) { if (initialResult != null) { searchResult = (GenericValue) initialResult; } else { searchResult = eli.currentGenericValue(); } } if (searchResult == null) { // nothing to get... int failTotal = 0; if (this.resultOffset != null) { failTotal = this.resultOffset.intValue() - 1; } this.totalResults = new Integer(failTotal); return productIds; } // init numRetreived to one since we have already grabbed the initial one int numRetreived = 1; int duplicatesFound = 0; Set productIdSet = new HashSet(); productIds.add(searchResult.getString("productId")); productIdSet.add(searchResult.getString("productId")); while (((searchResult = (GenericValue) eli.next()) != null) && (maxResults == null || numRetreived < maxResults.intValue())) { String productId = searchResult.getString("productId"); if (!productIdSet.contains(productId)) { productIds.add(productId); productIdSet.add(productId); numRetreived++; } else { duplicatesFound++; } /* StringBuffer lineMsg = new StringBuffer("Got search result line: "); Iterator fieldsToSelectIter = fieldsToSelect.iterator(); while (fieldsToSelectIter.hasNext()) { String fieldName = (String) fieldsToSelectIter.next(); lineMsg.append(fieldName); lineMsg.append("="); lineMsg.append(searchResult.get(fieldName)); if (fieldsToSelectIter.hasNext()) { lineMsg.append(", "); } } Debug.logInfo(lineMsg.toString(), module); */ } if (searchResult != null) { // we weren't at the end, so go to the end and get the index //Debug.logInfo("Getting totalResults from ending index - before last() currentIndex=" + eli.currentIndex(), module); if (eli.last()) { this.totalResults = new Integer(eli.currentIndex()); //Debug.logInfo("Getting totalResults from ending index - after last() currentIndex=" + eli.currentIndex(), module); } } if (this.totalResults == null || this.totalResults.intValue() == 0) { int total = numRetreived; if (this.resultOffset != null) { total += (this.resultOffset.intValue() - 1); } this.totalResults = new Integer(total); } Debug.logInfo("Got search values, numRetreived=" + numRetreived + ", totalResults=" + totalResults + ", maxResults=" + maxResults + ", resultOffset=" + resultOffset + ", duplicatesFound(in the current results)=" + duplicatesFound, module); } catch (GenericEntityException e) { Debug.logError(e, "Error getting results from the product search query", module); } return productIds; } public void saveSearchResultInfo(Long numResults, Double secondsTotal) { // uses entities: ProductSearchResult and ProductSearchConstraint try { // make sure this is in a transaction boolean beganTransaction = TransactionUtil.begin(); try { GenericValue productSearchResult = delegator.makeValue("ProductSearchResult", null); String productSearchResultId = delegator.getNextSeqId("ProductSearchResult"); productSearchResult.set("productSearchResultId", productSearchResultId); productSearchResult.set("visitId", this.visitId); if (this.resultSortOrder != null) { productSearchResult.set("orderByName", this.resultSortOrder.getOrderName()); productSearchResult.set("isAscending", this.resultSortOrder.isAscending() ? "Y" : "N"); } productSearchResult.set("numResults", numResults); productSearchResult.set("secondsTotal", secondsTotal); productSearchResult.set("searchDate", nowTimestamp); productSearchResult.create(); Iterator productSearchConstraintIter = productSearchConstraintList.iterator(); int seqId = 1; while (productSearchConstraintIter.hasNext()) { GenericValue productSearchConstraint = (GenericValue) productSearchConstraintIter.next(); productSearchConstraint.set("productSearchResultId", productSearchResultId); productSearchConstraint.set("constraintSeqId", Integer.toString(seqId)); productSearchConstraint.create(); seqId++; } TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e1) { String errMsg = "Error saving product search result info/stats"; Debug.logError(e1, errMsg, module); TransactionUtil.rollback(beganTransaction, errMsg, e1); } } catch (GenericTransactionException e) { Debug.logError(e, "Error saving product search result info/stats", module); } } } // ====================================================================== // Search Constraint Classes // ====================================================================== public static abstract class ProductSearchConstraint implements java.io.Serializable { public ProductSearchConstraint() { } public abstract void addConstraint(ProductSearchContext productSearchContext); /** pretty print for log messages and even UI stuff */ public abstract String prettyPrintConstraint(GenericDelegator delegator, boolean detailed); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -