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
for (String keyword: andKeywordFixedSet) {
// 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);
// keyword type filter
if (UtilValidate.isNotEmpty(keywordTypeIds)) {
dynamicViewEntity.addAlias(entityAlias, "keywordTypeId");
}
// keyword status filter
if (UtilValidate.isNotEmpty(statusId)) {
dynamicViewEntity.addAlias(entityAlias, "statusId");
}
dynamicViewEntity.addViewLink("PROD", entityAlias, Boolean.FALSE, ModelKeyMap.makeKeyMapList("productId"));
entityConditionList.add(EntityCondition.makeCondition(prefix + "Keyword", EntityOperator.LIKE, keyword));
// keyword type filter
if (UtilValidate.isNotEmpty(keywordTypeIds)) {
List<EntityCondition> keywordTypeCons = FastList.newInstance();
for (String keywordTypeId : keywordTypeIds) {
keywordTypeCons.add(EntityCondition.makeCondition("keywordTypeId", EntityOperator.EQUALS, keywordTypeId));
}
entityConditionList.add(EntityCondition.makeCondition(keywordTypeCons, EntityOperator.OR));
}
// keyword status filter
if (UtilValidate.isNotEmpty(statusId)) {
entityConditionList.add(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, statusId));
}
//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) {
for (Set<String> keywordFixedOrSet: keywordFixedOrSetAndList) {
// 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<EntityCondition> keywordOrList = FastList.newInstance();
for (String keyword: keywordFixedOrSet) {
keywordOrList.add(EntityCondition.makeCondition(prefix + "Keyword", EntityOperator.LIKE, keyword));
}
entityConditionList.add(EntityCondition.makeCondition(keywordOrList, EntityOperator.OR));
if (doingBothAndOr) {
relevancyComplexAlias.addComplexAliasMember(new ComplexAliasField(entityAlias, "relevancyWeight", null, "sum"));
} else {
dynamicViewEntity.addAlias(entityAlias, "totalRelevancy", "relevancyWeight", null, null, null, "sum");
}
}
}