001: /*
002: * $Id: BOMEvents.java,v 1.4 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: package org.ofbiz.manufacturing.bom;
025:
026: import java.sql.Timestamp;
027: import java.util.LinkedList;
028: import java.util.List;
029:
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilMisc;
035: import org.ofbiz.base.util.UtilValidate;
036: import org.ofbiz.entity.GenericDelegator;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.entity.util.EntityListIterator;
040: import org.ofbiz.security.Security;
041:
042: /**
043: * Product's Bill of Materials Related Events
044: *
045: * @author <a href="mailto:tiz@sastau.it">Jacopo Cappellato</a>
046: *
047: */
048: public class BOMEvents {
049:
050: public static final String module = BOMEvents.class.getName();
051:
052: /** Updates ProductAssoc information for Bill of Materials
053: *@param request The HTTPRequest object for the current request
054: *@param response The HTTPResponse object for the current request
055: *@return String specifying the exit status of this event
056: */
057: public static String updateProductBom(HttpServletRequest request,
058: HttpServletResponse response) {
059: String errMsg = "";
060: GenericDelegator delegator = (GenericDelegator) request
061: .getAttribute("delegator");
062: Security security = (Security) request.getAttribute("security");
063:
064: String updateMode = request.getParameter("UPDATE_MODE");
065:
066: if (updateMode == null || updateMode.length() <= 0) {
067: request.setAttribute("_ERROR_MESSAGE_",
068: "Update Mode was not specified, but is required.");
069: Debug
070: .logWarning(
071: "[BOMEvents.updateProductBom] Update Mode was not specified, but is required",
072: module);
073: return "error";
074: }
075:
076: // check permissions before moving on...
077: if (!security.hasEntityPermission("MANUFACTURING", "_"
078: + updateMode, request.getSession())) {
079: request.setAttribute("_ERROR_MESSAGE_",
080: "You do not have sufficient permissions to "
081: + updateMode
082: + " MANUFACTURING (MANUFACTURING_"
083: + updateMode
084: + " or MANUFACTURING__ADMIN needed).");
085: return "error";
086: }
087:
088: String productId = request.getParameter("PRODUCT_ID");
089: String productIdTo = request.getParameter("PRODUCT_ID_TO");
090: String productAssocTypeId = request
091: .getParameter("PRODUCT_ASSOC_TYPE_ID");
092: String fromDateStr = request.getParameter("FROM_DATE");
093: Timestamp fromDate = null;
094:
095: try {
096: if (delegator.findByPrimaryKey("Product", UtilMisc.toMap(
097: "productId", productId)) == null)
098: errMsg += "<li>Product with id " + productId
099: + " not found.";
100: if (delegator.findByPrimaryKey("Product", UtilMisc.toMap(
101: "productId", productIdTo)) == null)
102: errMsg += "<li>Product To with id " + productIdTo
103: + " not found.";
104: } catch (GenericEntityException e) {
105: // if there is an exception for either, the other probably wont work
106: Debug.logWarning(e, module);
107: }
108:
109: if (UtilValidate.isNotEmpty(fromDateStr)) {
110: try {
111: fromDate = Timestamp.valueOf(fromDateStr);
112: } catch (Exception e) {
113: errMsg += "<li>From Date not formatted correctly.";
114: }
115: }
116: if (!UtilValidate.isNotEmpty(productId))
117: errMsg += "<li>Product ID is missing.";
118: if (!UtilValidate.isNotEmpty(productIdTo))
119: errMsg += "<li>Product ID To is missing.";
120: if (!UtilValidate.isNotEmpty(productAssocTypeId))
121: errMsg += "<li>Association Type ID is missing.";
122: // from date is only required if update mode is not CREATE
123: if (!updateMode.equals("CREATE")
124: && !UtilValidate.isNotEmpty(fromDateStr))
125: errMsg += "<li>From Date is missing.";
126: // Will the new node create loops in the bill of materials tree?
127: try {
128: // FIXME: fromDate should be provided instead of null
129: GenericValue dupAncestor = BOMHelper
130: .searchDuplicatedAncestor(productId, productIdTo,
131: productAssocTypeId, null, delegator);
132: if (dupAncestor != null) {
133: errMsg += "<li>The link could cause conflicts because of the following link: "
134: + dupAncestor.getString("productId")
135: + " --> "
136: + dupAncestor.getString("productIdTo");
137: }
138: } catch (GenericEntityException e) {
139: // if there is an exception for either, the other probably wont work
140: Debug.logWarning(e, module);
141: }
142:
143: if (errMsg.length() > 0) {
144: errMsg = "<b>The following errors occurred:</b><br><ul>"
145: + errMsg + "</ul>";
146: request.setAttribute("_ERROR_MESSAGE_", errMsg);
147: return "error";
148: }
149:
150: // clear some cache entries
151: delegator.clearCacheLine("ProductAssoc", UtilMisc.toMap(
152: "productId", productId));
153: delegator.clearCacheLine("ProductAssoc", UtilMisc.toMap(
154: "productId", productId, "productAssocTypeId",
155: productAssocTypeId));
156:
157: delegator.clearCacheLine("ProductAssoc", UtilMisc.toMap(
158: "productIdTo", productIdTo));
159: delegator.clearCacheLine("ProductAssoc", UtilMisc.toMap(
160: "productIdTo", productIdTo, "productAssocTypeId",
161: productAssocTypeId));
162:
163: delegator.clearCacheLine("ProductAssoc", UtilMisc.toMap(
164: "productAssocTypeId", productAssocTypeId));
165: delegator.clearCacheLine("ProductAssoc", UtilMisc.toMap(
166: "productId", productId, "productIdTo", productIdTo,
167: "productAssocTypeId", productAssocTypeId, "fromDate",
168: fromDate));
169:
170: GenericValue tempProductAssoc = delegator.makeValue(
171: "ProductAssoc", UtilMisc.toMap("productId", productId,
172: "productIdTo", productIdTo,
173: "productAssocTypeId", productAssocTypeId,
174: "fromDate", fromDate));
175:
176: if (updateMode.equals("DELETE")) {
177: GenericValue productAssoc = null;
178:
179: try {
180: productAssoc = delegator
181: .findByPrimaryKey(tempProductAssoc
182: .getPrimaryKey());
183: } catch (GenericEntityException e) {
184: Debug.logWarning(e.getMessage(), module);
185: productAssoc = null;
186: }
187: if (productAssoc == null) {
188: request
189: .setAttribute("_ERROR_MESSAGE_",
190: "Could not remove product association (does not exist)");
191: return "error";
192: }
193: try {
194: productAssoc.remove();
195: } catch (GenericEntityException e) {
196: request
197: .setAttribute("_ERROR_MESSAGE_",
198: "Could not remove product association (write error)");
199: Debug
200: .logWarning(
201: "[BOMEvents.updateProductBom] Could not remove product association (write error); message: "
202: + e.getMessage(), module);
203: return "error";
204: }
205: return "success";
206: }
207:
208: String thruDateStr = request.getParameter("THRU_DATE");
209: String reason = request.getParameter("REASON");
210: String instruction = request.getParameter("INSTRUCTION");
211: String quantityStr = request.getParameter("QUANTITY");
212: String scrapFactorStr = request.getParameter("SCRAP_FACTOR");
213: String routingWorkEffortId = request
214: .getParameter("WORK_EFFORT_ID");
215: String sequenceNumStr = request.getParameter("SEQUENCE_NUM");
216: Timestamp thruDate = null;
217: Double quantity = null;
218: Double scrapFactor = null;
219: Long sequenceNum = null;
220:
221: if (UtilValidate.isNotEmpty(thruDateStr)) {
222: try {
223: thruDate = Timestamp.valueOf(thruDateStr);
224: } catch (Exception e) {
225: errMsg += "<li>Thru Date not formatted correctly.";
226: }
227: }
228: if (UtilValidate.isNotEmpty(quantityStr)) {
229: try {
230: quantity = Double.valueOf(quantityStr);
231: } catch (Exception e) {
232: errMsg += "<li>Quantity not formatted correctly.";
233: }
234: }
235: if (UtilValidate.isNotEmpty(scrapFactorStr)) {
236: try {
237: scrapFactor = Double.valueOf(scrapFactorStr);
238: } catch (Exception e) {
239: errMsg += "<li>Scrap Factor not formatted correctly.";
240: }
241: }
242: if (UtilValidate.isNotEmpty(sequenceNumStr)) {
243: try {
244: sequenceNum = Long.valueOf(sequenceNumStr);
245: } catch (Exception e) {
246: errMsg += "<li>SequenceNum not formatted correctly.";
247: }
248: }
249: if (errMsg.length() > 0) {
250: errMsg = "<b>The following errors occurred:</b><br><ul>"
251: + errMsg + "</ul>";
252: request.setAttribute("_ERROR_MESSAGE_", errMsg);
253: return "error";
254: }
255:
256: tempProductAssoc.set("thruDate", thruDate);
257: tempProductAssoc.set("reason", reason);
258: tempProductAssoc.set("instruction", instruction);
259: tempProductAssoc.set("quantity", quantity);
260: tempProductAssoc.set("sequenceNum", sequenceNum);
261: tempProductAssoc.set("scrapFactor", scrapFactor);
262: // tempProductAssoc.set("routingWorkEffortId", routingWorkEffortId);
263:
264: if (updateMode.equals("CREATE")) {
265: // if no from date specified, set to now
266: if (fromDate == null) {
267: fromDate = new Timestamp(new java.util.Date().getTime());
268: tempProductAssoc.set("fromDate", fromDate);
269: request.setAttribute("ProductAssocCreateFromDate",
270: fromDate);
271: }
272:
273: GenericValue productAssoc = null;
274:
275: try {
276: productAssoc = delegator
277: .findByPrimaryKey(tempProductAssoc
278: .getPrimaryKey());
279: } catch (GenericEntityException e) {
280: Debug.logWarning(e.getMessage(), module);
281: productAssoc = null;
282: }
283: if (productAssoc != null) {
284: request
285: .setAttribute("_ERROR_MESSAGE_",
286: "Could not create product association (already exists)");
287: return "error";
288: }
289: try {
290: productAssoc = tempProductAssoc.create();
291: } catch (GenericEntityException e) {
292: request
293: .setAttribute("_ERROR_MESSAGE_",
294: "Could not create product association (write error)");
295: Debug
296: .logWarning(
297: "[BOMEvents.updateProductBom] Could not create product association (write error); message: "
298: + e.getMessage(), module);
299: return "error";
300: }
301: } else if (updateMode.equals("UPDATE")) {
302: try {
303: tempProductAssoc.store();
304: } catch (GenericEntityException e) {
305: request
306: .setAttribute("_ERROR_MESSAGE_",
307: "Could not update product association (write error)");
308: Debug
309: .logWarning(
310: "[BOMEvents.updateProductBom] Could not update product association (write error); message: "
311: + e.getMessage(), module);
312: return "error";
313: }
314: } else {
315: request.setAttribute("_ERROR_MESSAGE_",
316: "Specified update mode: \"" + updateMode
317: + "\" is not supported.");
318: return "error";
319: }
320:
321: return "success";
322: }
323: }
|