📄 productevents.java
字号:
Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); String variantProductId = request.getParameter("productId0"); boolean applyToAll = (request.getParameter("applyToAll") != null); try { boolean beganTransaction = TransactionUtil.begin(); try { // check for variantProductId - this will mean that we have multiple ship info to update if (variantProductId == null) { // only single product to update String productId = request.getParameter("productId"); GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); product.set("lastModifiedDate", nowTimestamp); product.setString("lastModifiedByUserLogin", userLogin.getString("userLoginId")); try { product.set("productHeight", UtilParse.parseDoubleForEntity(request.getParameter("productHeight"))); product.set("productWidth", UtilParse.parseDoubleForEntity(request.getParameter("productWidth"))); product.set("productDepth", UtilParse.parseDoubleForEntity(request.getParameter("productDepth"))); product.set("weight", UtilParse.parseDoubleForEntity(request.getParameter("weight"))); // default unit settings for shipping parameters product.set("heightUomId", "LEN_in"); product.set("widthUomId", "LEN_in"); product.set("depthUomId", "LEN_in"); product.set("weightUomId", "WT_oz"); Double floz = UtilParse.parseDoubleForEntity(request.getParameter("~floz")); Double ml = UtilParse.parseDoubleForEntity(request.getParameter("~ml")); Double ntwt = UtilParse.parseDoubleForEntity(request.getParameter("~ntwt")); Double grams = UtilParse.parseDoubleForEntity(request.getParameter("~grams")); List currentProductFeatureAndAppls = EntityUtil.filterByDate(delegator.findByAnd("ProductFeatureAndAppl", UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "STANDARD_FEATURE")), true); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ozUS", "AMOUNT", floz); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ml", "AMOUNT", ml); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_g", "AMOUNT", grams); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_oz", "AMOUNT", ntwt); product.store(); } catch (NumberFormatException nfe) { String errMsg = "Shipping Dimensions and Weights must be numbers."; request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logError(nfe, errMsg, module); return "error"; } } else { // multiple products, so use a numeric suffix to get them all int prodIdx = 0; int attribIdx = 0; String productId = variantProductId; do { GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); try { product.set("productHeight", UtilParse.parseDoubleForEntity(request.getParameter("productHeight" + attribIdx))); product.set("productWidth", UtilParse.parseDoubleForEntity(request.getParameter("productWidth" + attribIdx))); product.set("productDepth", UtilParse.parseDoubleForEntity(request.getParameter("productDepth" + attribIdx))); product.set("weight", UtilParse.parseDoubleForEntity(request.getParameter("weight" + attribIdx))); Double floz = UtilParse.parseDoubleForEntity(request.getParameter("~floz" + attribIdx)); Double ml = UtilParse.parseDoubleForEntity(request.getParameter("~ml" + attribIdx)); Double ntwt = UtilParse.parseDoubleForEntity(request.getParameter("~ntwt" + attribIdx)); Double grams = UtilParse.parseDoubleForEntity(request.getParameter("~grams" + attribIdx)); List currentProductFeatureAndAppls = EntityUtil.filterByDate(delegator.findByAnd("ProductFeatureAndAppl", UtilMisc.toMap("productId", productId, "productFeatureApplTypeId", "STANDARD_FEATURE")), true); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ozUS", "AMOUNT", floz); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "VLIQ_ml", "AMOUNT", ml); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_g", "AMOUNT", grams); setOrCreateProdFeature(delegator, productId, currentProductFeatureAndAppls, "WT_oz", "AMOUNT", ntwt); product.store(); } catch (NumberFormatException nfe) { String errMsg = "Shipping Dimensions and Weights must be numbers."; request.setAttribute("_ERROR_MESSAGE_", errMsg); Debug.logError(nfe, errMsg, module); return "error"; } prodIdx++; if (!applyToAll) { attribIdx = prodIdx; } productId = request.getParameter("productId" + prodIdx); } while (productId != null); } TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { String errMsg = "Error updating quick admin shipping settings: " + e.toString(); Debug.logError(e, errMsg, module); request.setAttribute("_ERROR_MESSAGE_", errMsg); TransactionUtil.rollback(beganTransaction, errMsg, e); return "error"; } } catch (GenericTransactionException gte) { String errMsg = "Error updating quick admin shipping settings: " + gte.toString(); Debug.logError(gte, errMsg, module); request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } return "success"; } /** * find a specific feature in a given list, then update it or create it if it doesn't exist. * @param delegator * @param productId * @param existingFeatures * @param uomId * @param productFeatureTypeId * @param numberSpecified * @return * @throws GenericEntityException */ private static void setOrCreateProdFeature(GenericDelegator delegator, String productId, List currentProductFeatureAndAppls, String uomId, String productFeatureTypeId, Double numberSpecified) throws GenericEntityException { GenericValue productFeatureType = delegator.findByPrimaryKey("ProductFeatureType", UtilMisc.toMap("productFeatureTypeId", productFeatureTypeId)); GenericValue uom = delegator.findByPrimaryKey("Uom", UtilMisc.toMap("uomId", uomId)); Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); // filter list of features to the one we'll be editing List typeUomProductFeatureAndApplList = EntityUtil.filterByAnd(currentProductFeatureAndAppls, UtilMisc.toMap("productFeatureTypeId", productFeatureTypeId, "uomId", uomId)); // go through each; need to remove? do it now Iterator typeUomProductFeatureAndApplIter = typeUomProductFeatureAndApplList.iterator(); boolean foundOneEqual = false; while (typeUomProductFeatureAndApplIter.hasNext()) { GenericValue typeUomProductFeatureAndAppl = (GenericValue) typeUomProductFeatureAndApplIter.next(); if ((numberSpecified != null) && (numberSpecified.equals(typeUomProductFeatureAndAppl.getDouble("numberSpecified")))) { foundOneEqual = true; } else { // remove the PFA... GenericValue productFeatureAppl = delegator.makeValidValue("ProductFeatureAppl", typeUomProductFeatureAndAppl); productFeatureAppl.remove(); } } // NOTE: if numberSpecified is null then foundOneEqual will always be false, so need to check both if (numberSpecified != null && !foundOneEqual) { String productFeatureId = null; List existingProductFeatureList = delegator.findByAnd("ProductFeature", UtilMisc.toMap("productFeatureTypeId", productFeatureTypeId, "numberSpecified", numberSpecified, "uomId", uomId)); if (existingProductFeatureList.size() > 0) { GenericValue existingProductFeature = (GenericValue) existingProductFeatureList.get(0); productFeatureId = existingProductFeature.getString("productFeatureId"); } else { // doesn't exist, so create it productFeatureId = delegator.getNextSeqId("ProductFeature").toString(); GenericValue prodFeature = delegator.makeValue("ProductFeature", UtilMisc.toMap("productFeatureId", productFeatureId, "productFeatureTypeId", productFeatureTypeId)); if (uomId != null) { prodFeature.set("uomId", uomId); } prodFeature.set("numberSpecified", numberSpecified); prodFeature.set("description", numberSpecified.toString() + (uom == null ? "" : (" " + uom.getString("description")))); // if there is a productFeatureCategory with the same id as the productFeatureType, use that category. // otherwise, use a default category from the configuration if (delegator.findByPrimaryKey("ProductFeatureCategory", UtilMisc.toMap("productFeatureCategoryId", productFeatureTypeId)) == null) { GenericValue productFeatureCategory = delegator.makeValue("ProductFeatureCategory", null); productFeatureCategory.set("productFeatureCategoryId", productFeatureTypeId); productFeatureCategory.set("description", productFeatureType.get("description")); productFeatureCategory.create(); } prodFeature.set("productFeatureCategoryId", productFeatureTypeId); prodFeature.create(); } delegator.create("ProductFeatureAppl", UtilMisc.toMap("productId", productId, "productFeatureId", productFeatureId, "productFeatureApplTypeId", "STANDARD_FEATURE", "fromDate", nowTimestamp)); } } public static String updateProductQuickAdminSelFeat(HttpServletRequest request, HttpServletResponse response) { GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); //GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); String productId = request.getParameter("productId"); String variantProductId = request.getParameter("productId0"); String useImagesProdId = request.getParameter("useImages"); String productFeatureTypeId = request.getParameter("productFeatureTypeId"); if (UtilValidate.isEmpty(productFeatureTypeId)) { String errMsg = "Error: please select a ProductFeature Type to add or update variant features."; request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } try { boolean beganTransaction = TransactionUtil.begin(); try { GenericValue productFeatureType = delegator.findByPrimaryKey("ProductFeatureType", UtilMisc.toMap("productFeatureTypeId", productFeatureTypeId)); if (productFeatureType == null) { String errMsg = "Error: the ProductFeature Type specified was not valid and one is require to add or update variant features."; request.setAttribute("_ERROR_MESSAGE_", errMsg); return "error"; } // check for variantProductId - this will mean that we have multiple variants to update if (variantProductId != null) { // multiple products, so use a numeric suffix to get them all int attribIdx = 0; GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); do { GenericValue variantProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", variantProductId)); String description = request.getParameter("description" + attribIdx); // blank means null, which means delete the feature application if ((description != null) && (description.trim().length() < 1)) { description = null; } Set variantDescRemoveToRemoveOnVirtual = new HashSet(); checkUpdateFeatureApplByDescription(variantProductId, variantProduct, description, productFeatureTypeId, productFeatureType, "STANDARD_FEATURE", nowTimestamp, delegator, null, variantDescRemoveToRemoveOnVirtual); checkUpdateFeatureApplByDescription(productId, product, description, productFeatureTypeId, productFeatureType, "SELECTABLE_FEATURE", nowTimestamp, delegator, variantDescRemoveToRemoveOnVirtual, null); // update image urls if ((useImagesProdId != null) && (useImagesProdId.equals(variantProductId))) { product.set("smallImageUrl", variantProduct.getString("smallImageUrl")); product.set("mediumImageUrl", variantProduct.getString("mediumImageUrl")); product.set("largeImageUrl", null); product.set("detailImageUrl", null); product.store(); } attribIdx++; variantProductId = request.getParameter("productId" + attribIdx); } while (variantProductId != null); } TransactionUtil.commit(beganTransaction); } catch (GenericEntityException e) { String errMsg = "Error updating quick admin selectable feature settings: " + e.toString(); Debug.logError(e, errMsg, module); request.setAttribute("_ERROR_MESSAGE_", errMsg); TransactionUtil.rollback(beganTransaction, errMsg, e); return "error";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -