use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ProductUtilServices method attachProductFeaturesToCategory.
public static Map<String, Object> attachProductFeaturesToCategory(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
String productCategoryId = (String) context.get("productCategoryId");
String doSubCategoriesStr = (String) context.get("doSubCategories");
Locale locale = (Locale) context.get("locale");
String errMsg = null;
// default to true
boolean doSubCategories = !"N".equals(doSubCategoriesStr);
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Set<String> productFeatureTypeIdsToExclude = new HashSet<>();
String excludeProp = EntityUtilProperties.getPropertyValue("prodsearch", "attach.feature.type.exclude", delegator);
if (UtilValidate.isNotEmpty(excludeProp)) {
List<String> typeList = StringUtil.split(excludeProp, ",");
productFeatureTypeIdsToExclude.addAll(typeList);
}
Set<String> productFeatureTypeIdsToInclude = null;
String includeProp = EntityUtilProperties.getPropertyValue("prodsearch", "attach.feature.type.include", delegator);
if (UtilValidate.isNotEmpty(includeProp)) {
List<String> typeList = StringUtil.split(includeProp, ",");
if (typeList.size() > 0) {
productFeatureTypeIdsToInclude = UtilMisc.makeSetWritable(typeList);
}
}
try {
attachProductFeaturesToCategory(productCategoryId, productFeatureTypeIdsToInclude, productFeatureTypeIdsToExclude, delegator, doSubCategories, nowTimestamp);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.error_in_attachProductFeaturesToCategory", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ProductUtilServices method removeCategoryMembersOfDiscProducts.
/**
* for all disc products, remove from category memberships
*/
public static Map<String, Object> removeCategoryMembersOfDiscProducts(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp)), EntityOperator.AND);
try (EntityListIterator eli = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) {
GenericValue product = null;
int numSoFar = 0;
while ((product = eli.next()) != null) {
String productId = product.getString("productId");
List<GenericValue> productCategoryMemberList = EntityQuery.use(delegator).from("ProductCategoryMember").where("productId", productId).queryList();
if (productCategoryMemberList.size() > 0) {
for (GenericValue productCategoryMember : productCategoryMemberList) {
// coded this way rather than a removeByAnd so it can be easily changed...
productCategoryMember.remove();
}
numSoFar++;
if (numSoFar % 500 == 0) {
Debug.logInfo("Removed category members for " + numSoFar + " sales discontinued products.", module);
}
}
}
Debug.logInfo("Completed - Removed category members for " + numSoFar + " sales discontinued products.", module);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_removeCategoryMembersOfDiscProducts", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ProductUtilServices method makeStandAloneFromSingleVariantVirtuals.
public static Map<String, Object> makeStandAloneFromSingleVariantVirtuals(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
Debug.logInfo("Starting makeStandAloneFromSingleVariantVirtuals", module);
DynamicViewEntity dve = new DynamicViewEntity();
dve.addMemberEntity("PVIRT", "Product");
dve.addMemberEntity("PVA", "ProductAssoc");
dve.addViewLink("PVIRT", "PVA", Boolean.FALSE, UtilMisc.toList(new ModelKeyMap("productId", "productId")));
dve.addAlias("PVIRT", "productId", null, null, null, Boolean.TRUE, null);
dve.addAlias("PVIRT", "salesDiscontinuationDate", null, null, null, null, null);
dve.addAlias("PVA", "productAssocTypeId", null, null, null, null, null);
dve.addAlias("PVA", "fromDate", null, null, null, null, null);
dve.addAlias("PVA", "thruDate", null, null, null, null, null);
dve.addAlias("PVA", "productIdToCount", "productIdTo", null, null, null, "count-distinct");
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp))), EntityOperator.AND);
EntityCondition havingCond = EntityCondition.makeCondition("productIdToCount", EntityOperator.EQUALS, Long.valueOf(1));
EntityQuery eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(condition).having(havingCond);
try (EntityListIterator eliOne = eq.queryIterator()) {
List<GenericValue> valueList = eliOne.getCompleteList();
Debug.logInfo("Found " + valueList.size() + " virtual products with one variant to turn into a stand alone product.", module);
int numWithOneOnly = 0;
for (GenericValue value : valueList) {
// has only one variant period, is it valid? should already be discontinued if not
String productId = value.getString("productId");
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
// verify the query; tested on a bunch, looks good
if (paList.size() != 1) {
Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
} else {
// for all virtuals with one variant move all info from virtual to variant and remove virtual, make variant as not a variant
dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.TRUE, "userLogin", userLogin));
numWithOneOnly++;
if (numWithOneOnly % 100 == 0) {
Debug.logInfo("Made " + numWithOneOnly + " virtual products with only one valid variant stand-alone products.", module);
}
}
}
EntityCondition conditionWithDates = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp)), 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);
eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(conditionWithDates).having(havingCond);
try (EntityListIterator eliMulti = eq.queryIterator()) {
List<GenericValue> valueMultiList = eliMulti.getCompleteList();
Debug.logInfo("Found " + valueMultiList.size() + " virtual products with one VALID variant to pull the variant from to make a stand alone product.", module);
int numWithOneValid = 0;
for (GenericValue value : valueMultiList) {
// has only one valid variant
String productId = value.getString("productId");
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
// verify the query; tested on a bunch, looks good
if (paList.size() != 1) {
Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
} else {
// for all virtuals with one valid variant move info from virtual to variant, put variant in categories from virtual, remove virtual from all categories but leave "family" otherwise intact, mark variant as not a variant
dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.FALSE, "userLogin", userLogin));
numWithOneValid++;
if (numWithOneValid % 100 == 0) {
Debug.logInfo("Made " + numWithOneValid + " virtual products with one valid variant stand-alone products.", module);
}
}
}
Debug.logInfo("Found virtual products with one valid variant: " + numWithOneValid + ", with one variant only: " + numWithOneOnly, module);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} catch (GenericEntityException | GenericServiceException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ProductUtilServices method setAllProductImageNames.
/**
* reset all product image names with a certain pattern, ex: /images/products/${size}/${productId}.jpg
* NOTE: only works on fields of Product right now
*/
public static Map<String, Object> setAllProductImageNames(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
String pattern = (String) context.get("pattern");
Locale locale = (Locale) context.get("locale");
String errMsg = null;
if (UtilValidate.isEmpty(pattern)) {
Map<String, Object> imageContext = new HashMap<>();
imageContext.putAll(context);
imageContext.put("tenantId", delegator.getDelegatorTenantId());
String imageFilenameFormat = EntityUtilProperties.getPropertyValue("catalog", "image.filename.format", delegator);
String imageUrlPrefix = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.url.prefix", delegator), imageContext);
imageUrlPrefix = imageUrlPrefix.endsWith("/") ? imageUrlPrefix.substring(0, imageUrlPrefix.length() - 1) : imageUrlPrefix;
pattern = imageUrlPrefix + "/" + imageFilenameFormat;
}
try (EntityListIterator eli = EntityQuery.use(delegator).from("Product").queryIterator()) {
GenericValue product = null;
int numSoFar = 0;
while ((product = eli.next()) != null) {
String productId = (String) product.get("productId");
Map<String, String> smallMap = UtilMisc.toMap("size", "small", "productId", productId);
Map<String, String> mediumMap = UtilMisc.toMap("size", "medium", "productId", productId);
Map<String, String> largeMap = UtilMisc.toMap("size", "large", "productId", productId);
Map<String, String> detailMap = UtilMisc.toMap("size", "detail", "productId", productId);
if ("Y".equals(product.getString("isVirtual"))) {
// find the first variant, use it's ID for the names...
List<GenericValue> productAssocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
if (productAssocList.size() > 0) {
GenericValue productAssoc = EntityUtil.getFirst(productAssocList);
smallMap.put("productId", productAssoc.getString("productIdTo"));
mediumMap.put("productId", productAssoc.getString("productIdTo"));
product.set("smallImageUrl", FlexibleStringExpander.expandString(pattern, smallMap));
product.set("mediumImageUrl", FlexibleStringExpander.expandString(pattern, mediumMap));
} else {
product.set("smallImageUrl", null);
product.set("mediumImageUrl", null);
}
product.set("largeImageUrl", null);
product.set("detailImageUrl", null);
} else {
product.set("smallImageUrl", FlexibleStringExpander.expandString(pattern, smallMap));
product.set("mediumImageUrl", FlexibleStringExpander.expandString(pattern, mediumMap));
product.set("largeImageUrl", FlexibleStringExpander.expandString(pattern, largeMap));
product.set("detailImageUrl", FlexibleStringExpander.expandString(pattern, detailMap));
}
product.store();
numSoFar++;
if (numSoFar % 500 == 0) {
Debug.logInfo("Image URLs set for " + numSoFar + " products.", module);
}
}
Debug.logInfo("Completed - Image URLs set for " + numSoFar + " products.", module);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_setAllProductImageNames", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.
the class ProductUtilServices method mergeVirtualWithSingleVariant.
public static Map<String, Object> mergeVirtualWithSingleVariant(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
String productId = (String) context.get("productId");
Boolean removeOldBool = (Boolean) context.get("removeOld");
boolean removeOld = removeOldBool.booleanValue();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
Boolean testBool = (Boolean) context.get("test");
boolean test = false;
if (testBool != null) {
test = testBool.booleanValue();
}
try {
GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
Debug.logInfo("Processing virtual product with one variant with ID: " + productId + " and name: " + product.getString("internalName"), module);
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
if (paList.size() > 1) {
Map<String, String> messageMap = UtilMisc.toMap("productId", productId);
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.found_more_than_one_valid_variant_for_virtual_ID", messageMap, locale);
Debug.logInfo(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
if (paList.size() == 0) {
Map<String, String> messageMap = UtilMisc.toMap("productId", productId);
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.did_not_find_any_valid_variants_for_virtual_ID", messageMap, locale);
Debug.logInfo(errMsg, module);
return ServiceUtil.returnError(errMsg);
}
GenericValue productAssoc = EntityUtil.getFirst(paList);
if (removeOld) {
// remove the productAssoc before getting down so it isn't copied over...
if (test) {
Debug.logInfo("Test mode, would remove: " + productAssoc, module);
} else {
productAssoc.remove();
}
} else {
// don't remove, just expire to avoid running again in the future
productAssoc.set("thruDate", nowTimestamp);
if (test) {
Debug.logInfo("Test mode, would store: " + productAssoc, module);
} else {
productAssoc.store();
}
}
String variantProductId = productAssoc.getString("productIdTo");
// Product
GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
Debug.logInfo("--variant has ID: " + variantProductId + " and name: " + variantProduct.getString("internalName"), module);
// start with the values from the virtual product, override from the variant...
GenericValue newVariantProduct = delegator.makeValue("Product", product);
newVariantProduct.setAllFields(variantProduct, false, "", null);
newVariantProduct.set("isVariant", "N");
if (test) {
Debug.logInfo("Test mode, would store: " + newVariantProduct, module);
} else {
newVariantProduct.store();
}
// ProductCategoryMember - always remove these to pull the virtual from any categories it might have been in
duplicateRelated(product, "", "ProductCategoryMember", "productId", variantProductId, nowTimestamp, true, delegator, test);
// ProductFeatureAppl
duplicateRelated(product, "", "ProductFeatureAppl", "productId", variantProductId, nowTimestamp, removeOld, delegator, test);
// ProductContent
duplicateRelated(product, "", "ProductContent", "productId", variantProductId, nowTimestamp, removeOld, delegator, test);
// ProductPrice
duplicateRelated(product, "", "ProductPrice", "productId", variantProductId, nowTimestamp, removeOld, delegator, test);
// GoodIdentification
duplicateRelated(product, "", "GoodIdentification", "productId", variantProductId, nowTimestamp, removeOld, delegator, test);
// ProductAttribute
duplicateRelated(product, "", "ProductAttribute", "productId", variantProductId, nowTimestamp, removeOld, delegator, test);
// ProductAssoc
duplicateRelated(product, "Main", "ProductAssoc", "productId", variantProductId, nowTimestamp, removeOld, delegator, test);
duplicateRelated(product, "Assoc", "ProductAssoc", "productIdTo", variantProductId, nowTimestamp, removeOld, delegator, test);
if (removeOld) {
if (test) {
Debug.logInfo("Test mode, would remove related ProductKeyword with dummy key: " + product.getRelatedDummyPK("ProductKeyword"), module);
Debug.logInfo("Test mode, would remove: " + product, module);
} else {
product.removeRelated("ProductKeyword");
product.remove();
}
}
if (test) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductMergeVirtualWithSingleVariant", locale));
}
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
Aggregations