use of org.apache.ofbiz.entity.GenericEntityException 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.GenericEntityException 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();
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class ProductUtilServices method discVirtualsWithDiscVariants.
/**
* First expire all ProductAssocs for all disc variants, then disc all virtuals that have all expired variant ProductAssocs
*/
public static Map<String, Object> discVirtualsWithDiscVariants(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 conditionOne = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("isVariant", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp)), EntityOperator.AND);
try (EntityListIterator eliOne = EntityQuery.use(delegator).from("Product").where(conditionOne).queryIterator()) {
GenericValue productOne = null;
int numSoFarOne = 0;
while ((productOne = eliOne.next()) != null) {
String virtualProductId = ProductWorker.getVariantVirtualId(productOne);
GenericValue virtualProduct = EntityQuery.use(delegator).from("Product").where("productId", virtualProductId).queryOne();
if (virtualProduct == null) {
continue;
}
List<GenericValue> passocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", virtualProductId, "productIdTo", productOne.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
if (passocList.size() > 0) {
for (GenericValue passoc : passocList) {
passoc.set("thruDate", nowTimestamp);
passoc.store();
}
numSoFarOne++;
if (numSoFarOne % 500 == 0) {
Debug.logInfo("Expired variant ProductAssocs for " + numSoFarOne + " sales discontinued variant products.", module);
}
}
}
// get all non-discontinued virtuals, see if all variant ProductAssocs are expired, if discontinue
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("isVirtual", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_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) {
List<GenericValue> passocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", product.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
if (passocList.size() == 0) {
product.set("salesDiscontinuationDate", nowTimestamp);
delegator.store(product);
numSoFar++;
if (numSoFar % 500 == 0) {
Debug.logInfo("Sales discontinued " + numSoFar + " virtual products that have no valid variants.", module);
}
}
}
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_discVirtualsWithDiscVariants", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_discVirtualsWithDiscVariants", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class VariantEvents method quickAddChosenVariant.
/**
* Creates variant products from a virtual product and a combination of selectable features
*@param request The HTTPRequest object for the current request
*@param response The HTTPResponse object for the current request
*@return String specifying the exit status of this event
*/
public static String quickAddChosenVariant(HttpServletRequest request, HttpServletResponse response) {
String errMsg = "";
Delegator delegator = (Delegator) request.getAttribute("delegator");
String productId = request.getParameter("productId");
String variantProductId = request.getParameter("variantProductId");
String featureTypeSizeStr = request.getParameter("featureTypeSize");
if (UtilValidate.isEmpty(productId)) {
errMsg = UtilProperties.getMessage(resource, "variantevents.productId_required_but_missing", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
if (UtilValidate.isEmpty(variantProductId)) {
errMsg = UtilProperties.getMessage(resource, "variantevents.variantProductId_required_but_missing_enter_an_id", UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
int featureTypeSize = 0;
try {
featureTypeSize = Integer.parseInt(featureTypeSizeStr);
} catch (NumberFormatException e) {
Map<String, String> messageMap = UtilMisc.toMap("featureTypeSizeStr", featureTypeSizeStr);
errMsg = UtilProperties.getMessage(resource, "variantevents.featureTypeSize_not_number", messageMap, UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
try {
boolean beganTransacton = TransactionUtil.begin();
try {
// read the product, duplicate it with the given id
GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
if (product == null) {
Map<String, String> messageMap = UtilMisc.toMap("productId", productId);
errMsg = UtilProperties.getMessage(resource, "variantevents.product_not_found_with_ID", messageMap, UtilHttp.getLocale(request));
TransactionUtil.rollback(beganTransacton, errMsg, null);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
// check if product exists
GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
if (variantProduct == null) {
// if product does not exist
variantProduct = GenericValue.create(product);
variantProduct.set("productId", variantProductId);
variantProduct.set("isVirtual", "N");
variantProduct.set("isVariant", "Y");
variantProduct.set("primaryProductCategoryId", null);
// create new
variantProduct.create();
} else {
// if product does exist
variantProduct.set("isVirtual", "N");
variantProduct.set("isVariant", "Y");
variantProduct.set("primaryProductCategoryId", null);
// update entry
variantProduct.store();
}
// add an association from productId to variantProductId of the PRODUCT_VARIANT
GenericValue productAssoc = delegator.makeValue("ProductAssoc", UtilMisc.toMap("productId", productId, "productIdTo", variantProductId, "productAssocTypeId", "PRODUCT_VARIANT", "fromDate", UtilDateTime.nowTimestamp()));
productAssoc.create();
// add the selected standard features to the new product given the productFeatureIds
for (int i = 0; i < featureTypeSize; i++) {
String productFeatureId = request.getParameter("feature_" + i);
if (productFeatureId == null) {
Map<String, String> messageMap = UtilMisc.toMap("i", Integer.toString(i));
errMsg = UtilProperties.getMessage(resource, "variantevents.productFeatureId_for_feature_type_number_not_found", messageMap, UtilHttp.getLocale(request));
TransactionUtil.rollback(beganTransacton, errMsg, null);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
GenericValue productFeature = EntityQuery.use(delegator).from("ProductFeature").where("productFeatureId", productFeatureId).queryOne();
GenericValue productFeatureAppl = delegator.makeValue("ProductFeatureAppl", UtilMisc.toMap("productId", variantProductId, "productFeatureId", productFeatureId, "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", UtilDateTime.nowTimestamp()));
// set the default seq num if it's there...
if (productFeature != null) {
productFeatureAppl.set("sequenceNum", productFeature.get("defaultSequenceNum"));
}
productFeatureAppl.create();
}
TransactionUtil.commit(beganTransacton);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resource, "variantevents.entity_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request));
TransactionUtil.rollback(beganTransacton, errMsg, null);
Debug.logError(e, "Entity error creating quick add variant data", module);
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
} catch (GenericTransactionException e) {
Debug.logError(e, "Transaction error creating quick add variant data", module);
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resource, "variantevents.transaction_error_quick_add_variant_data", messageMap, UtilHttp.getLocale(request));
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
Map<String, String> messageMap = UtilMisc.toMap("variantProductId", variantProductId);
String sucMsg = UtilProperties.getMessage(resource, "variantevents.successfully_created_variant_product_with_id", messageMap, UtilHttp.getLocale(request));
request.setAttribute("_EVENT_MESSAGE_", sucMsg);
return "success";
}
use of org.apache.ofbiz.entity.GenericEntityException in project ofbiz-framework by apache.
the class ProductStoreEvents method getChildProductStoreGroupTree.
// Please note : the structure of map in this function is according to the JSON data map of the jsTree
@SuppressWarnings("unchecked")
public static String getChildProductStoreGroupTree(HttpServletRequest request, HttpServletResponse response) {
Delegator delegator = (Delegator) request.getAttribute("delegator");
String parentGroupId = request.getParameter("parentGroupId");
String onclickFunction = request.getParameter("onclickFunction");
List productStoreGroupList = new LinkedList();
List<GenericValue> children;
List<String> sortList = org.apache.ofbiz.base.util.UtilMisc.toList("sequenceNum");
try {
GenericValue productStoreGroup = EntityQuery.use(delegator).from("ProductStoreGroup").where("productStoreGroupId", parentGroupId).cache(true).queryOne();
if (productStoreGroup != null) {
children = EntityQuery.use(delegator).from("ProductStoreGroupRollupAndChild").where("parentGroupId", parentGroupId).cache(true).filterByDate().queryList();
if (UtilValidate.isNotEmpty(children)) {
for (GenericValue child : children) {
String productStoreGroupId = child.getString("productStoreGroupId");
Map josonMap = new HashMap();
List<GenericValue> childList = null;
// Get the child list of chosen category
childList = EntityQuery.use(delegator).from("ProductStoreGroupRollupAndChild").where("parentGroupId", productStoreGroupId).cache(true).filterByDate().queryList();
if (UtilValidate.isNotEmpty(childList)) {
josonMap.put("state", "closed");
}
Map dataMap = new HashMap();
Map dataAttrMap = new HashMap();
dataAttrMap.put("onClick", onclickFunction + "('" + productStoreGroupId + "')");
String hrefStr = "EditProductStoreGroupAndAssoc";
dataAttrMap.put("href", hrefStr);
dataMap.put("attr", dataAttrMap);
dataMap.put("title", child.get("productStoreGroupName") + " [" + child.get("productStoreGroupId") + "]");
josonMap.put("data", dataMap);
Map attrMap = new HashMap();
attrMap.put("parentGroupId", productStoreGroupId);
josonMap.put("attr", attrMap);
josonMap.put("sequenceNum", child.get("sequenceNum"));
josonMap.put("title", child.get("productStoreGroupName"));
productStoreGroupList.add(josonMap);
}
List<Map<Object, Object>> sortedProductStoreGroupList = UtilMisc.sortMaps(productStoreGroupList, sortList);
request.setAttribute("storeGroupTree", sortedProductStoreGroupList);
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return "error";
}
return "success";
}
Aggregations