001: /*
002: * $Id: BOMServices.java,v 1.2 2004/02/17 11:27:39 jacopo Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.manufacturing.bom;
026:
027: import java.sql.Timestamp;
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Locale;
033: import java.util.Map;
034: import java.util.Date;
035:
036: import org.ofbiz.base.util.Debug;
037: import org.ofbiz.base.util.UtilDateTime;
038: import org.ofbiz.base.util.UtilMisc;
039: import org.ofbiz.base.util.UtilValidate;
040: import org.ofbiz.entity.GenericDelegator;
041: import org.ofbiz.entity.GenericEntityException;
042: import org.ofbiz.entity.GenericValue;
043: import org.ofbiz.security.Security;
044: import org.ofbiz.service.DispatchContext;
045: import org.ofbiz.service.GenericServiceException;
046: import org.ofbiz.service.LocalDispatcher;
047: import org.ofbiz.service.ServiceUtil;
048:
049: /** Bills of Materials' services implementation.
050: * These services are useful when dealing with product's
051: * bills of materials.
052: * @author <a href="mailto:tiz@sastau.it">Jacopo Cappellato</a>
053: */
054: public class BOMServices {
055:
056: public static final String module = BOMServices.class.getName();
057:
058: /** Returns the product's low level code (llc) i.e. the maximum depth
059: * in which the productId can be found in any of the
060: * bills of materials of bomType type.
061: * @param dctx
062: * @param context
063: * @return
064: */
065: public static Map getMaxDepth(DispatchContext dctx, Map context) {
066:
067: Map result = new HashMap();
068: Security security = dctx.getSecurity();
069: GenericDelegator delegator = dctx.getDelegator();
070: LocalDispatcher dispatcher = dctx.getDispatcher();
071: String productId = (String) context.get("productId");
072: String fromDateStr = (String) context.get("fromDate");
073: String bomType = (String) context.get("bomType");
074:
075: Date fromDate = null;
076: if (UtilValidate.isNotEmpty(fromDateStr)) {
077: try {
078: fromDate = Timestamp.valueOf(fromDateStr);
079: } catch (Exception e) {
080: }
081: }
082: if (fromDate == null) {
083: fromDate = new Date();
084: }
085:
086: Integer depth = null;
087: try {
088: depth = new Integer(BOMHelper.getMaxDepth(productId,
089: bomType, fromDate, delegator));
090: } catch (GenericEntityException gee) {
091: return ServiceUtil
092: .returnError("Error running max depth algorithm: "
093: + gee.getMessage());
094: }
095:
096: result.put("depth", depth);
097:
098: return result;
099: }
100:
101: /** Returns the ProductAssoc generic value for a duplicate productIdKey
102: * ancestor if present, null otherwise.
103: * Useful to avoid loops when adding new assocs (components)
104: * to a bill of materials.
105: * @param dctx
106: * @param context
107: * @return
108: */
109: public static Map searchDuplicatedAncestor(DispatchContext dctx,
110: Map context) {
111:
112: Map result = new HashMap();
113: Security security = dctx.getSecurity();
114: GenericDelegator delegator = dctx.getDelegator();
115: LocalDispatcher dispatcher = dctx.getDispatcher();
116: String productId = (String) context.get("productId");
117: String productIdKey = (String) context.get("productIdKey");
118: String fromDateStr = (String) context.get("fromDate");
119: String bomType = (String) context.get("bomType");
120:
121: Date fromDate = null;
122: if (UtilValidate.isNotEmpty(fromDateStr)) {
123: try {
124: fromDate = Timestamp.valueOf(fromDateStr);
125: } catch (Exception e) {
126: }
127: }
128: if (fromDate == null) {
129: fromDate = new Date();
130: }
131:
132: GenericValue duplicatedProductAssoc = null;
133: try {
134: duplicatedProductAssoc = BOMHelper
135: .searchDuplicatedAncestor(productId, productIdKey,
136: bomType, fromDate, delegator);
137: } catch (GenericEntityException gee) {
138: return ServiceUtil
139: .returnError("Error running duplicated ancestor search: "
140: + gee.getMessage());
141: }
142:
143: result.put("duplicatedProductAssoc", duplicatedProductAssoc);
144:
145: return result;
146: }
147:
148: /** It reads the product's bill of materials,
149: * if necessary configure it, and it returns
150: * an object (see {@link ItemConfigurationTree}
151: * and {@link ItemConfigurationNode}) that represents a
152: * configured bill of material tree.
153: * Useful for tree traversal (breakdown, explosion, implosion).
154: * @param dctx
155: * @param context
156: * @return
157: */
158: public static Map getItemConfigurationTree(DispatchContext dctx,
159: Map context) {
160:
161: Map result = new HashMap();
162: Security security = dctx.getSecurity();
163: GenericDelegator delegator = dctx.getDelegator();
164: LocalDispatcher dispatcher = dctx.getDispatcher();
165: String productId = (String) context.get("productId");
166: String fromDateStr = (String) context.get("fromDate");
167: String bomType = (String) context.get("bomType");
168: Boolean explosion = (Boolean) context.get("explosion");
169: if (explosion == null) {
170: explosion = new Boolean(true);
171: }
172:
173: Date fromDate = null;
174: if (UtilValidate.isNotEmpty(fromDateStr)) {
175: try {
176: fromDate = Timestamp.valueOf(fromDateStr);
177: } catch (Exception e) {
178: }
179: }
180: if (fromDate == null) {
181: fromDate = new Date();
182: }
183:
184: ItemConfigurationTree tree = null;
185: try {
186: tree = new ItemConfigurationTree(productId, bomType,
187: fromDate, explosion.booleanValue(), delegator);
188: } catch (GenericEntityException gee) {
189: return ServiceUtil
190: .returnError("Error creating bill of materials tree: "
191: + gee.getMessage());
192: }
193:
194: result.put("tree", tree);
195:
196: return result;
197: }
198:
199: }
|