EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(
EntityCondition.makeCondition("productId", EntityOperator.EQUALS, productId),
EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp),
EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))
), EntityOperator.AND);
EntityListIterator productFeatureAndApplEli = delegator.find("ProductFeatureAndAppl", condition, null, null, null, null);
GenericValue productFeatureAndAppl = null;
while ((productFeatureAndAppl = productFeatureAndApplEli.next()) != null) {
String productFeatureId = productFeatureAndAppl.getString("productFeatureId");
String productFeatureTypeId = productFeatureAndAppl.getString("productFeatureTypeId");
if (UtilValidate.isNotEmpty(productFeatureTypeIdsToInclude) && !productFeatureTypeIdsToInclude.contains(productFeatureTypeId)) {
continue;
}
if (productFeatureTypeIdsToExclude != null && productFeatureTypeIdsToExclude.contains(productFeatureTypeId)) {
continue;
}
Set<String> productFeatureIdSet = productFeatureIdByTypeIdSetMap.get(productFeatureTypeId);
if (productFeatureIdSet == null) {
productFeatureIdSet = FastSet.newInstance();
productFeatureIdByTypeIdSetMap.put(productFeatureTypeId, productFeatureIdSet);
}
productFeatureIdSet.add(productFeatureId);
}
productFeatureAndApplEli.close();
}
for (Map.Entry<String, Set<String>> entry: productFeatureIdByTypeIdSetMap.entrySet()) {
String productFeatureTypeId = entry.getKey();
Set<String> productFeatureIdSet = entry.getValue();
String productFeatureGroupId = productCategoryId + "_" + productFeatureTypeId;
if (productFeatureGroupId.length() > 20) {
Debug.logWarning("Manufactured productFeatureGroupId was greater than 20 characters, means that we had some long productCategoryId and/or productFeatureTypeId values, at the category part should be unique since it is first, so if the feature type isn't unique it just means more than one type of feature will go into the category...", module);
productFeatureGroupId = productFeatureGroupId.substring(0, 20);
}
GenericValue productFeatureGroup = delegator.findByPrimaryKey("ProductFeatureGroup", UtilMisc.toMap("productFeatureGroupId", productFeatureGroupId));
if (productFeatureGroup == null) {
// auto-create the group
String description = "Feature Group for type [" + productFeatureTypeId + "] features in category [" + productCategoryId + "]";
productFeatureGroup = delegator.makeValue("ProductFeatureGroup", UtilMisc.toMap("productFeatureGroupId", productFeatureGroupId, "description", description));
productFeatureGroup.create();
GenericValue productFeatureCatGrpAppl = delegator.makeValue("ProductFeatureCatGrpAppl", UtilMisc.toMap("productFeatureGroupId", productFeatureGroupId, "productCategoryId", productCategoryId, "fromDate", nowTimestamp));
productFeatureCatGrpAppl.create();
}
// now put all of the features in the group, if there is not already a valid feature placement there...
for (String productFeatureId: productFeatureIdSet) {
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(
EntityCondition.makeCondition("productFeatureId", EntityOperator.EQUALS, productFeatureId),
EntityCondition.makeCondition("productFeatureGroupId", EntityOperator.EQUALS, productFeatureGroupId),
EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp),
EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))
), EntityOperator.AND);
if (delegator.findCountByCondition("ProductFeatureGroupAppl", condition, null, null) == 0) {
// if no valid ones, create one
GenericValue productFeatureGroupAppl = delegator.makeValue("ProductFeatureGroupAppl", UtilMisc.toMap("productFeatureGroupId", productFeatureGroupId, "productFeatureId", productFeatureId, "fromDate", nowTimestamp));
productFeatureGroupAppl.create();
}
}
}
// now get all feature groups associated with sub-categories and associate them with this category
for (GenericValue productCategoryRollup: subCategoryList) {
String subProductCategoryId = productCategoryRollup.getString("productCategoryId");
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(
EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, subProductCategoryId),
EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp),
EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))
), EntityOperator.AND);
EntityListIterator productFeatureCatGrpApplEli = delegator.find("ProductFeatureCatGrpAppl", condition, null, null, null, null);
GenericValue productFeatureCatGrpAppl = null;
while ((productFeatureCatGrpAppl = productFeatureCatGrpApplEli.next()) != null) {
String productFeatureGroupId = productFeatureCatGrpAppl.getString("productFeatureGroupId");
EntityCondition checkCondition = EntityCondition.makeCondition(UtilMisc.toList(
EntityCondition.makeCondition("productCategoryId", EntityOperator.EQUALS, productCategoryId),
EntityCondition.makeCondition("productFeatureGroupId", EntityOperator.EQUALS, productFeatureGroupId),
EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp),
EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))
), EntityOperator.AND);
if (delegator.findCountByCondition("ProductFeatureCatGrpAppl", checkCondition, null, null) == 0) {
// if no valid ones, create one
GenericValue productFeatureGroupAppl = delegator.makeValue("ProductFeatureCatGrpAppl", UtilMisc.toMap("productFeatureGroupId", productFeatureGroupId, "productCategoryId", productCategoryId, "fromDate", nowTimestamp));
productFeatureGroupAppl.create();
}
}
productFeatureCatGrpApplEli.close();
}
}