boolean doingBothAndOr = (keywordFixedOrSetAndList.size() > 1) || (keywordFixedOrSetAndList.size() > 0 && andKeywordFixedSet.size() > 0);
Debug.logInfo("Finished initial setup of keywords, doingBothAndOr=" + doingBothAndOr + ", andKeywordFixedSet=" + andKeywordFixedSet + "\n keywordFixedOrSetAndList=" + keywordFixedOrSetAndList, module);
ComplexAlias relevancyComplexAlias = new ComplexAlias("+");
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(EntityCondition.makeCondition(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);
//needed when doingBothAndOr or will get an SQL error
if (doingBothAndOr) {
dynamicViewEntity.addAlias(entityAlias, prefix + "RelevancyWeight", "relevancyWeight", null, null, Boolean.TRUE, null);
}
relevancyComplexAlias.addComplexAliasMember(new ComplexAliasField(entityAlias, "relevancyWeight", null, null));
}
//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 = FastList.newInstance();
Iterator keywordIter = keywordFixedOrSet.iterator();
while (keywordIter.hasNext()) {
String keyword = (String) keywordIter.next();
keywordOrList.add(EntityCondition.makeCondition(prefix + "Keyword", EntityOperator.LIKE, keyword));
}
entityConditionList.add(EntityCondition.makeCondition(keywordOrList, EntityOperator.OR));
productIdGroupBy = true;
if (doingBothAndOr) {
relevancyComplexAlias.addComplexAliasMember(new ComplexAliasField(entityAlias, "relevancyWeight", null, "sum"));
} else {
dynamicViewEntity.addAlias(entityAlias, "totalRelevancy", "relevancyWeight", null, null, null, "sum");
}
}
}