1 17 18 package org.ofbiz.manufacturing.bom; 19 20 import java.util.Date ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Iterator ; 24 import java.util.ArrayList ; 25 26 import org.ofbiz.entity.util.EntityUtil; 27 import org.ofbiz.entity.GenericDelegator; 28 import org.ofbiz.entity.GenericValue; 29 import org.ofbiz.entity.GenericEntityException; 30 import org.ofbiz.base.util.UtilMisc; 31 import org.ofbiz.service.LocalDispatcher; 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.security.Security; 34 35 40 public class BOMHelper { 41 42 public static final String module = BOMHelper.class.getName(); 43 44 45 public BOMHelper() { 46 } 47 48 58 65 public static int getMaxDepth(String productId, String bomType, Date inDate, GenericDelegator delegator) throws GenericEntityException { 66 if (inDate == null) inDate = new Date (); 68 int maxDepth = 0; 69 List productNodesList = delegator.findByAndCache("ProductAssoc", 70 UtilMisc.toMap("productIdTo", productId, 71 "productAssocTypeId", bomType)); 72 productNodesList = EntityUtil.filterByDate(productNodesList, inDate); 73 GenericValue oneNode = null; 74 Iterator nodesIterator = productNodesList.iterator(); 75 int depth = 0; 76 while (nodesIterator.hasNext()) { 77 oneNode = (GenericValue)nodesIterator.next(); 78 depth = 0; 79 depth = getMaxDepth(oneNode.getString("productId"), bomType, inDate, delegator); 80 depth++; 81 if (depth > maxDepth) { 82 maxDepth = depth; 83 } 84 } 85 86 return maxDepth; 87 } 88 89 103 public static GenericValue searchDuplicatedAncestor(String productId, String productIdKey, String bomType, Date inDate, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin) throws GenericEntityException { 104 return searchDuplicatedAncestor(productId, productIdKey, null, bomType, inDate, delegator, dispatcher, userLogin); 105 } 106 107 private static GenericValue searchDuplicatedAncestor(String productId, String productIdKey, ArrayList productIdKeys, String bomType, Date inDate, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin) throws GenericEntityException { 108 if (inDate == null) inDate = new Date (); 110 if (productIdKeys == null) { 111 BOMTree tree = new BOMTree(productIdKey, bomType, inDate, delegator, dispatcher, userLogin); 112 productIdKeys = tree.getAllProductsId(); 113 productIdKeys.add(productIdKey); 114 } 115 List productNodesList = delegator.findByAndCache("ProductAssoc", 116 UtilMisc.toMap("productIdTo", productId, 117 "productAssocTypeId", bomType)); 118 productNodesList = EntityUtil.filterByDate(productNodesList, inDate); 119 GenericValue oneNode = null; 120 GenericValue duplicatedNode = null; 121 Iterator nodesIterator = productNodesList.iterator(); 122 while (nodesIterator.hasNext()) { 123 oneNode = (GenericValue)nodesIterator.next(); 124 for (int i = 0; i < productIdKeys.size(); i++) { 125 if (oneNode.getString("productId").equals((String )productIdKeys.get(i))) { 126 return oneNode; 127 } 128 } 129 duplicatedNode = searchDuplicatedAncestor(oneNode.getString("productId"), productIdKey, productIdKeys, bomType, inDate, delegator, dispatcher, userLogin); 130 if (duplicatedNode != null) { 131 break; 132 } 133 } 134 return duplicatedNode; 135 } 136 137 public static String createProductionRunsForShipment(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) { 138 String errMsg = ""; 139 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 140 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 141 Security security = (Security) request.getAttribute("security"); 142 GenericValue userLogin = (GenericValue)request.getSession().getAttribute("userLogin"); 143 144 String shipmentId = request.getParameter("shipmentId"); 145 146 try { 147 List shipmentPlans = delegator.findByAnd("OrderShipment", UtilMisc.toMap("shipmentId", shipmentId)); 148 Iterator shipmentPlansIt = shipmentPlans.iterator(); 149 while (shipmentPlansIt.hasNext()) { 150 GenericValue shipmentPlan = (GenericValue)shipmentPlansIt.next(); 151 GenericValue orderItem = shipmentPlan.getRelatedOne("OrderItem"); 152 153 List productionRuns = delegator.findByAndCache("WorkOrderItemFulfillment", UtilMisc.toMap("orderId", shipmentPlan.getString("orderId"), "orderItemSeqId", shipmentPlan.getString("orderItemSeqId"))); 154 if (productionRuns != null && productionRuns.size() > 0) { 155 Debug.logError("Production Run for order item (" + orderItem.getString("orderId") + "/" + orderItem.getString("orderItemSeqId") + ") not created.", module); 156 continue; 157 } 158 Map result = dispatcher.runSync("createProductionRunsForOrder", UtilMisc.toMap("productId", orderItem.getString("productId"), "quantity", shipmentPlan.getDouble("quantity"), "orderId", shipmentPlan.getString("orderId"), "orderItemSeqId", shipmentPlan.getString("orderItemSeqId"), "shipmentId", shipmentId, "userLogin", userLogin)); 159 } 160 } catch (Exception e) { 161 Debug.logWarning(e, module); 163 } 164 165 return "success"; 166 } 167 168 } 169 | Popular Tags |