📄 productutilservices.java
字号:
} else {
productAssoc.store();
}
}
String variantProductId = productAssoc.getString("productIdTo");
// Product
GenericValue variantProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", variantProductId));
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("Test mode - returning error to get a rollback");
}
} catch (GenericEntityException e) {
Map messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resource,"productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
protected static void duplicateRelated(GenericValue product, String title, String relatedEntityName, String productIdField, String variantProductId, Timestamp nowTimestamp, boolean removeOld, GenericDelegator delegator, boolean test) throws GenericEntityException {
List relatedList = EntityUtil.filterByDate(product.getRelated(title + relatedEntityName), nowTimestamp);
Iterator relatedIter = relatedList.iterator();
while (relatedIter.hasNext()) {
GenericValue relatedValue = (GenericValue) relatedIter.next();
GenericValue newRelatedValue = (GenericValue) relatedValue.clone();
newRelatedValue.set(productIdField, variantProductId);
// create a new one? see if one already exists with different from/thru dates
ModelEntity modelEntity = relatedValue.getModelEntity();
if (modelEntity.isField("fromDate")) {
GenericPK findValue = newRelatedValue.getPrimaryKey();
// can't just set to null, need to remove the value so it isn't a constraint in the query
//findValue.set("fromDate", null);
findValue.remove("fromDate");
List existingValueList = EntityUtil.filterByDate(delegator.findByAnd(relatedEntityName, findValue), nowTimestamp);
if (existingValueList.size() > 0) {
if (test) {
Debug.logInfo("Found " + existingValueList.size() + " existing values for related entity name: " + relatedEntityName + ", not copying, findValue is: " + findValue, module);
}
continue;
}
newRelatedValue.set("fromDate", nowTimestamp);
}
if (delegator.findCountByAnd(relatedEntityName, newRelatedValue.getPrimaryKey()) == 0) {
if (test) {
Debug.logInfo("Test mode, would create: " + newRelatedValue, module);
} else {
newRelatedValue.create();
}
}
}
if (removeOld) {
if (test) {
Debug.logInfo("Test mode, would remove related " + title + relatedEntityName + " with dummy key: " + product.getRelatedDummyPK(title + relatedEntityName), module);
} else {
product.removeRelated(title + relatedEntityName);
}
}
}
/** reset all product image names with a certain pattern, ex: /images/products/${sizeStr}/${productId}.jpg
* NOTE: only works on fields of Product right now
*/
public static Map setAllProductImageNames(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
String pattern = (String) context.get("pattern");
Locale locale = (Locale) context.get("locale");
String errMsg = null;
try {
EntityListIterator eli = delegator.findListIteratorByCondition("Product", null, null, null);
GenericValue product = null;
int numSoFar = 0;
while ((product = (GenericValue) eli.next()) != null) {
String productId = (String) product.get("productId");
Map smallMap = UtilMisc.toMap("sizeStr", "small", "productId", productId);
Map mediumMap = UtilMisc.toMap("sizeStr", "medium", "productId", productId);
Map largeMap = UtilMisc.toMap("sizeStr", "large", "productId", productId);
Map detailMap = UtilMisc.toMap("sizeStr", "detail", "productId", productId);
if ("Y".equals(product.getString("isVirtual"))) {
// find the first variant, use it's ID for the names...
List productAssocList = EntityUtil.filterByDate(delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT")), nowTimestamp);
if (productAssocList.size() > 0) {
GenericValue productAssoc = EntityUtil.getFirst(productAssocList);
smallMap.put("productId", productAssoc.get("productIdTo"));
mediumMap.put("productId", productAssoc.get("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);
}
}
eli.close();
Debug.logInfo("Completed - Image URLs set for " + numSoFar + " products.", module);
} catch (GenericEntityException e) {
Map messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resource,"productutilservices.entity_error_running_setAllProductImageNames", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
public static Map clearAllVirtualProductImageNames(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
try {
EntityListIterator eli = delegator.findListIteratorByCondition("Product", new EntityExpr("isVirtual", EntityOperator.EQUALS, "Y"), null, null);
GenericValue product = null;
int numSoFar = 0;
while ((product = (GenericValue) eli.next()) != null) {
product.set("smallImageUrl", null);
product.set("mediumImageUrl", null);
product.set("largeImageUrl", null);
product.set("detailImageUrl", null);
product.store();
numSoFar++;
if (numSoFar % 500 == 0) {
Debug.logInfo("Image URLs cleared for " + numSoFar + " products.", module);
}
}
eli.close();
Debug.logInfo("Completed - Image URLs set for " + numSoFar + " products.", module);
} catch (GenericEntityException e) {
Map messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resource,"productutilservices.entity_error_running_clearAllVirtualProductImageNames", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -