KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > manufacturing > bom > BOMHelper


1 /*
2  *
3  * Copyright 2001-2006 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy of
7  * the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  */

17
18 package org.ofbiz.manufacturing.bom;
19
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.ArrayList JavaDoc;
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 /** Helper class containing static method useful when dealing
36  * with product's bills of materials.
37  * These methods are also available as services (see {@link BOMServices}).
38  * @author <a HREF="mailto:tiz@sastau.it">Jacopo Cappellato</a>
39  */

40 public class BOMHelper {
41     
42     public static final String JavaDoc module = BOMHelper.class.getName();
43     
44     /** Creates a new instance of BOMHelper */
45     public BOMHelper() {
46     }
47     
48     /** Returns the product's low level code (llc) i.e. the maximum depth
49      * in which the productId can be found in any of the
50      * bills of materials of bomType type.
51      * @return The low level code for the productId. (0 = root, 1 = first level, etc...)
52      * @param productId The product id
53      * @param bomType The bill of materials type (e.g. manufacturing, engineering,...)
54      * @param delegator Validity date (if null, today is used).
55      * @param inDate The delegator
56      * @throws GenericEntityException If a db problem occurs.
57      */

58     /*
59      * It's implemented as a recursive method that performs the following tasks:
60      * 1.given a product id, it selects all the product's parents
61      * 2.if no parents are found, the method returns zero (llc = 0, this is a root element)
62      * 3.for every parent the method is called recursively, the llc returned is incremented by one and the max of these number is saved as maxDepth
63      * 4.the maxDepth value is returned
64      */

65     public static int getMaxDepth(String JavaDoc productId, String JavaDoc bomType, Date JavaDoc inDate, GenericDelegator delegator) throws GenericEntityException {
66         // If the date is null, set it to today.
67
if (inDate == null) inDate = new Date JavaDoc();
68         int maxDepth = 0;
69         List JavaDoc productNodesList = delegator.findByAndCache("ProductAssoc",
70                                          UtilMisc.toMap("productIdTo", productId,
71                                          "productAssocTypeId", bomType));
72         productNodesList = EntityUtil.filterByDate(productNodesList, inDate);
73         GenericValue oneNode = null;
74         Iterator JavaDoc 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     /** Returns the ProductAssoc generic value for a duplicate productIdKey
90      * ancestor if present, null otherwise.
91      * Useful to avoid loops when adding new assocs (components)
92      * to a bill of materials.
93      * @param productId The product to which we want to add a new child.
94      * @param productIdKey The new component we want to add to the existing bom.
95      * @param bomType The bill of materials type (e.g. manufacturing, engineering).
96      * @param inDate Validity date (if null, today is used).
97      *
98      * @param delegator The delegator used
99      * @throws GenericEntityException If a db problem occurs
100      * @return the ProductAssoc generic value for a duplicate productIdKey
101      * ancestor if present, null otherwise.
102      */

103     public static GenericValue searchDuplicatedAncestor(String JavaDoc productId, String JavaDoc productIdKey, String JavaDoc bomType, Date JavaDoc 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 JavaDoc productId, String JavaDoc productIdKey, ArrayList JavaDoc productIdKeys, String JavaDoc bomType, Date JavaDoc inDate, GenericDelegator delegator, LocalDispatcher dispatcher, GenericValue userLogin) throws GenericEntityException {
108         // If the date is null, set it to today.
109
if (inDate == null) inDate = new Date JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc)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 JavaDoc createProductionRunsForShipment(javax.servlet.http.HttpServletRequest JavaDoc request, javax.servlet.http.HttpServletResponse JavaDoc response) {
138         String JavaDoc 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 JavaDoc shipmentId = request.getParameter("shipmentId");
145
146         try {
147         List JavaDoc shipmentPlans = delegator.findByAnd("OrderShipment", UtilMisc.toMap("shipmentId", shipmentId));
148         Iterator JavaDoc shipmentPlansIt = shipmentPlans.iterator();
149         while (shipmentPlansIt.hasNext()) {
150             GenericValue shipmentPlan = (GenericValue)shipmentPlansIt.next();
151             GenericValue orderItem = shipmentPlan.getRelatedOne("OrderItem");
152     
153             List JavaDoc 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 JavaDoc 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 JavaDoc e) {
161             // if there is an exception for either, the other probably wont work
162
Debug.logWarning(e, module);
163         }
164
165         return "success";
166     }
167
168 }
169
Popular Tags