📄 bomservices.java
字号:
/* * * Copyright 2001-2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */package org.ofbiz.manufacturing.bom;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.util.EntityUtil;import org.ofbiz.order.order.OrderReadHelper;import org.ofbiz.security.Security;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;/** Bills of Materials' services implementation. * These services are useful when dealing with product's * bills of materials. * @author <a href="mailto:tiz@sastau.it">Jacopo Cappellato</a> */public class BOMServices { public static final String module = BOMServices.class.getName(); public static final String resource = "ManufacturingUiLabels"; /** Returns the product's low level code (llc) i.e. the maximum depth * in which the productId can be found in any of the * bills of materials of bomType type. * If the bomType input field is not passed then the depth is searched for all the bom types and the lowest depth is returned. * @param dctx * @param context * @return */ public static Map getMaxDepth(DispatchContext dctx, Map context) { Map result = new HashMap(); Security security = dctx.getSecurity(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String productId = (String) context.get("productId"); String fromDateStr = (String) context.get("fromDate"); String bomType = (String) context.get("bomType"); Date fromDate = null; if (UtilValidate.isNotEmpty(fromDateStr)) { try { fromDate = Timestamp.valueOf(fromDateStr); } catch (Exception e) { } } if (fromDate == null) { fromDate = new Date(); } List bomTypes = new ArrayList(); if (bomType == null) { try { List bomTypesValues = delegator.findByAnd("ProductAssocType", UtilMisc.toMap("parentTypeId", "PRODUCT_COMPONENT")); Iterator bomTypesValuesIt = bomTypesValues.iterator(); while (bomTypesValuesIt.hasNext()) { bomTypes.add(((GenericValue)bomTypesValuesIt.next()).getString("productAssocTypeId")); } } catch(GenericEntityException gee) { return ServiceUtil.returnError("Error running max depth algorithm: " + gee.getMessage()); } } else { bomTypes.add(bomType); } int depth = 0; int maxDepth = 0; Iterator bomTypesIt = bomTypes.iterator(); try { while (bomTypesIt.hasNext()) { String oneBomType = (String)bomTypesIt.next(); depth = BOMHelper.getMaxDepth(productId, oneBomType, fromDate, delegator); if (depth > maxDepth) { maxDepth = depth; } } } catch(GenericEntityException gee) { return ServiceUtil.returnError("Error running max depth algorithm: " + gee.getMessage()); } result.put("depth", new Integer(maxDepth)); return result; } /** Updates the product's low level code (llc) * Given a product id, computes and updates the product's low level code (field billOfMaterialLevel in Product entity). * It also updates the llc of all the product's descendants. * For the llc only the manufacturing bom ("MANUF_COMPONENT") is considered. * @param dctx * @param context * @return */ public static Map updateLowLevelCode(DispatchContext dctx, Map context) { Map result = new HashMap(); Security security = dctx.getSecurity(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); String productId = (String) context.get("productIdTo"); Boolean alsoComponents = (Boolean) context.get("alsoComponents"); if (alsoComponents == null) { alsoComponents = new Boolean(true); } Boolean alsoVariants = (Boolean) context.get("alsoVariants"); if (alsoVariants == null) { alsoVariants = new Boolean(true); } Integer llc = null; try { GenericValue product = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", productId)); Map depthResult = dispatcher.runSync("getMaxDepth", UtilMisc.toMap("productId", productId, "bomType", "MANUF_COMPONENT")); llc = (Integer)depthResult.get("depth"); // If the product is a variant of a virtual, then the billOfMaterialLevel cannot be // lower than the billOfMaterialLevel of the virtual product. List virtualProducts = delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productIdTo", productId, "productAssocTypeId", "PRODUCT_VARIANT")); if (virtualProducts != null) { int virtualMaxDepth = 0; Iterator virtualProductsIt = virtualProducts.iterator(); while (virtualProductsIt.hasNext()) { int virtualDepth = 0; GenericValue oneVirtualProductAssoc = (GenericValue)virtualProductsIt.next(); GenericValue virtualProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", oneVirtualProductAssoc.getString("productId"))); if (virtualProduct.get("billOfMaterialLevel") != null) { virtualDepth = virtualProduct.getLong("billOfMaterialLevel").intValue(); } else { virtualDepth = 0; } if (virtualDepth > virtualMaxDepth) { virtualMaxDepth = virtualDepth; } } if (virtualMaxDepth > llc.intValue()) { llc = new Integer(virtualMaxDepth); } } product.set("billOfMaterialLevel", llc); product.store(); if (alsoComponents.booleanValue()) { Map treeResult = dispatcher.runSync("getBOMTree", UtilMisc.toMap("productId", productId, "bomType", "MANUF_COMPONENT")); BOMTree tree = (BOMTree)treeResult.get("tree"); ArrayList products = new ArrayList(); tree.print(products, llc.intValue()); for (int i = 0; i < products.size(); i++) { BOMNode oneNode = (BOMNode)products.get(i); GenericValue oneProduct = oneNode.getProduct(); int lev = 0; if (oneProduct.get("billOfMaterialLevel") != null) { lev = oneProduct.getLong("billOfMaterialLevel").intValue(); } if (lev < oneNode.getDepth()) { oneProduct.set("billOfMaterialLevel", new Integer(oneNode.getDepth())); oneProduct.store(); } } } if (alsoVariants.booleanValue()) { List variantProducts = delegator.findByAnd("ProductAssoc", UtilMisc.toMap("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT")); if (variantProducts != null) { Iterator variantProductsIt = variantProducts.iterator(); while (variantProductsIt.hasNext()) { GenericValue oneVariantProductAssoc = (GenericValue)variantProductsIt.next(); GenericValue variantProduct = delegator.findByPrimaryKey("Product", UtilMisc.toMap("productId", oneVariantProductAssoc.getString("productId"))); variantProduct.set("billOfMaterialLevel", llc); variantProduct.store(); } } } } catch (Exception e) { return ServiceUtil.returnError("Error running updateLowLevelCode: " + e.getMessage()); } result.put("lowLevelCode", llc); return result; } /** Updates the product's low level code (llc) for all the products in the Product entity. * For the llc only the manufacturing bom ("MANUF_COMPONENT") is considered. * @param dctx * @param context * @return */ public static Map initLowLevelCode(DispatchContext dctx, Map context) { Map result = new HashMap(); Security security = dctx.getSecurity(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); try { List products = delegator.findAll("Product", UtilMisc.toList("isVirtual DESC")); Iterator productsIt = products.iterator(); Long zero = new Long(0); List allProducts = new ArrayList(); while (productsIt.hasNext()) { GenericValue product = (GenericValue)productsIt.next(); product.set("billOfMaterialLevel", zero); allProducts.add(product); } delegator.storeAll(allProducts); Debug.logInfo("Low Level Code set to 0 for all the products", module); productsIt = products.iterator(); while (productsIt.hasNext()) { GenericValue product = (GenericValue)productsIt.next(); try { Map depthResult = dispatcher.runSync("updateLowLevelCode", UtilMisc.toMap("productIdTo", product.getString("productId"), "alsoComponents", Boolean.valueOf(false), "alsoVariants", Boolean.valueOf(false))); Debug.logInfo("Product [" + product.getString("productId") + "] Low Level Code [" + depthResult.get("lowLevelCode") + "]", module); } catch(Exception exc) { Debug.logWarning(exc.getMessage(), module); } } // FIXME: also all the variants llc should be updated? } catch (Exception e) { return ServiceUtil.returnError("Error running initLowLevelCode: " + e.getMessage()); } return result; } /** Returns the ProductAssoc generic value for a duplicate productIdKey * ancestor if present, null otherwise. * Useful to avoid loops when adding new assocs (components) * to a bill of materials. * @param dctx * @param context * @return */ public static Map searchDuplicatedAncestor(DispatchContext dctx, Map context) { Map result = new HashMap(); Security security = dctx.getSecurity(); GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue)context.get("userLogin"); String productId = (String) context.get("productId"); String productIdKey = (String) context.get("productIdTo"); Timestamp fromDate = (Timestamp) context.get("fromDate"); String bomType = (String) context.get("productAssocTypeId"); if (fromDate == null) { fromDate = Timestamp.valueOf((new Date()).toString()); } GenericValue duplicatedProductAssoc = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -