001: /*
002: * $Id: TechDataServices.java,v 1.4 2003/12/09 20:55:19 holivier Exp $
003: *
004: * Copyright (c) 2003 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.techdata;
026:
027: import java.sql.Timestamp;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.HashSet;
032: import java.util.Iterator;
033: import java.util.LinkedList;
034: import java.util.List;
035: import java.util.Map;
036: import java.util.Set;
037:
038: import org.ofbiz.base.util.Debug;
039: import org.ofbiz.base.util.UtilDateTime;
040: import org.ofbiz.base.util.UtilMisc;
041: import org.ofbiz.base.util.UtilValidate;
042: import org.ofbiz.entity.GenericDelegator;
043: import org.ofbiz.entity.GenericEntityException;
044: import org.ofbiz.entity.GenericValue;
045: import org.ofbiz.entity.condition.EntityExpr;
046: import org.ofbiz.entity.condition.EntityOperator;
047: import org.ofbiz.entity.util.EntityUtil;
048: import org.ofbiz.security.Security;
049: import org.ofbiz.service.DispatchContext;
050: import org.ofbiz.service.ServiceUtil;
051:
052: /**
053: * TechDataServices - TechData related Services
054: *
055: * @author <a href="mailto:olivier.heintz@nereide.biz">Olivier Heintz</a>
056: * @version $Revision: 1.4 $
057: * @since 3.0
058: */
059: public class TechDataServices {
060:
061: public static final String module = TechDataServices.class
062: .getName();
063:
064: /**
065: *
066: * Used to retreive some RoutingTasks (WorkEffort) selected by Name or MachineGroup ordered by Name
067: *
068: * @author holivier
069: * @param ctx
070: * @param context: a map containing workEffortName (routingTaskName) and fixedAssetId (MachineGroup or ANY)
071: * @return result: a map containing lookupResult (list of RoutingTask <=> workEffortId with currentStatusId = "ROU_ACTIVE" and workEffortTypeId = "ROU_TASK"
072: */
073: public static Map lookupRoutingTask(DispatchContext ctx, Map context) {
074: GenericDelegator delegator = ctx.getDelegator();
075: GenericValue userLogin = (GenericValue) context
076: .get("userLogin");
077: /* Security security = ctx.getSecurity(); a completer par la suite */
078: Map result = new HashMap();
079:
080: String workEffortName = (String) context.get("workEffortName");
081: String fixedAssetId = (String) context.get("fixedAssetId");
082:
083: List listRoutingTask = null;
084: List constraints = new LinkedList();
085:
086: if (workEffortName != null && workEffortName.length() > 0)
087: constraints.add(new EntityExpr("workEffortName",
088: EntityOperator.GREATER_THAN_EQUAL_TO,
089: workEffortName));
090: if (fixedAssetId != null && fixedAssetId.length() > 0
091: && !"ANY".equals(fixedAssetId))
092: constraints.add(new EntityExpr("fixedAssetId",
093: EntityOperator.EQUALS, fixedAssetId));
094:
095: constraints.add(new EntityExpr("currentStatusId",
096: EntityOperator.EQUALS, "ROU_ACTIVE"));
097: constraints.add(new EntityExpr("workEffortTypeId",
098: EntityOperator.EQUALS, "ROU_TASK"));
099:
100: try {
101: listRoutingTask = delegator.findByAnd("WorkEffort",
102: constraints, UtilMisc.toList("workEffortName"));
103: } catch (GenericEntityException e) {
104: Debug.logWarning(e, module);
105: return ServiceUtil
106: .returnError("Error finding desired WorkEffort records: "
107: + e.toString());
108: }
109: if (listRoutingTask == null)
110: listRoutingTask = new LinkedList();
111: if (listRoutingTask.size() == 0)
112: listRoutingTask.add(UtilMisc.toMap("label", "no Match",
113: "value", "NO_MATCH"));
114: result.put("lookupResult", listRoutingTask);
115: return result;
116: }
117:
118: /**
119: *
120: * Used to check if there is not two routing task with the same SeqId valid at the same period
121: *
122: * @author holivier
123: * @param ctx
124: * @param context: a map containing workEffortIdFrom (routing) and SeqId, fromDate thruDate
125: * @return result: a map containing sequenceNumNotOk which is equal to "Y" if it's not Ok
126: */
127: public static Map checkRoutingTaskAssoc(DispatchContext ctx,
128: Map context) {
129: GenericDelegator delegator = ctx.getDelegator();
130: Map result = new HashMap();
131: String sequenceNumNotOk = "N";
132:
133: String workEffortIdFrom = (String) context
134: .get("workEffortIdFrom");
135: String workEffortIdTo = (String) context.get("workEffortIdTo");
136: String workEffortAssocTypeId = (String) context
137: .get("workEffortAssocTypeId");
138: Long sequenceNum = (Long) context.get("sequenceNum");
139: java.sql.Timestamp fromDate = (java.sql.Timestamp) context
140: .get("fromDate");
141: java.sql.Timestamp thruDate = (java.sql.Timestamp) context
142: .get("thruDate");
143:
144: List listRoutingTaskAssoc = null;
145:
146: try {
147: listRoutingTaskAssoc = delegator.findByAnd(
148: "WorkEffortAssoc", UtilMisc.toMap(
149: "workEffortIdFrom", workEffortIdFrom,
150: "sequenceNum", sequenceNum), UtilMisc
151: .toList("fromDate"));
152: } catch (GenericEntityException e) {
153: Debug.logWarning(e, module);
154: return ServiceUtil
155: .returnError("Error finding desired WorkEffortAssoc records: "
156: + e.toString());
157: }
158:
159: if (listRoutingTaskAssoc != null) {
160: Iterator i = listRoutingTaskAssoc.iterator();
161: while (i.hasNext()) {
162: GenericValue routingTaskAssoc = (GenericValue) i.next();
163: if (!workEffortIdFrom.equals(routingTaskAssoc
164: .getString("workEffortIdFrom"))
165: || !workEffortIdTo.equals(routingTaskAssoc
166: .getString("workEffortIdTo"))
167: || !workEffortAssocTypeId
168: .equals(routingTaskAssoc
169: .getString("workEffortAssocTypeId"))
170: || !sequenceNum.equals(routingTaskAssoc
171: .getLong("sequenceNum"))) {
172: if (routingTaskAssoc.getTimestamp("thruDate") == null
173: && routingTaskAssoc
174: .getTimestamp("fromDate") == null)
175: sequenceNumNotOk = "Y";
176: else if (routingTaskAssoc.getTimestamp("thruDate") == null) {
177: if (thruDate == null)
178: sequenceNumNotOk = "Y";
179: else if (thruDate.after(routingTaskAssoc
180: .getTimestamp("fromDate")))
181: sequenceNumNotOk = "Y";
182: } else if (routingTaskAssoc
183: .getTimestamp("fromDate") == null) {
184: if (fromDate == null)
185: sequenceNumNotOk = "Y";
186: else if (fromDate.before(routingTaskAssoc
187: .getTimestamp("thruDate")))
188: sequenceNumNotOk = "Y";
189: } else if (fromDate == null && thruDate == null)
190: sequenceNumNotOk = "Y";
191: else if (thruDate == null) {
192: if (fromDate.before(routingTaskAssoc
193: .getTimestamp("thruDate")))
194: sequenceNumNotOk = "Y";
195: } else if (fromDate == null) {
196: if (thruDate.after(routingTaskAssoc
197: .getTimestamp("fromDate")))
198: sequenceNumNotOk = "Y";
199: } else if (routingTaskAssoc
200: .getTimestamp("fromDate").before(thruDate)
201: && fromDate.before(routingTaskAssoc
202: .getTimestamp("thruDate")))
203: sequenceNumNotOk = "Y";
204: }
205: }
206: }
207:
208: result.put("sequenceNumNotOk", sequenceNumNotOk);
209: return result;
210: }
211:
212: /**
213: *
214: * Used to check if the routingtaskAssoc is valid for the testDate
215: *
216: * @author holivier
217: * @param routingTaskAssoc: the routingTaskAssoc to test
218: * @param testDate: a date
219: * @return true if the routingTAskAssoc is valid
220: */
221: public static boolean routingTaskAssocIsValid(
222: GenericValue routingTaskAssoc, java.sql.Timestamp testDate) {
223: if (routingTaskAssoc.getTimestamp("fromDate") != null)
224: if (routingTaskAssoc.getTimestamp("thruDate") != null)
225: if (testDate.after(routingTaskAssoc
226: .getTimestamp("fromDate"))
227: && testDate.before(routingTaskAssoc
228: .getTimestamp("thruDate")))
229: return true;
230: else
231: return false;
232: else if (testDate.after(routingTaskAssoc
233: .getTimestamp("fromDate")))
234: return true;
235: else
236: return false;
237: else if (routingTaskAssoc.getTimestamp("thruDate") != null)
238: if (testDate.before(routingTaskAssoc
239: .getTimestamp("thruDate")))
240: return true;
241: else
242: return false;
243: else
244: return true;
245: }
246:
247: /**
248: *
249: * Used to check if the productBom is valid for the testDate, currently only tested on date but in futur, maybe there will be the option {valid until stock=0>}
250: *
251: * @author holivier
252: * @param productBom: the productBom to test
253: * @param testDate: a date
254: * @return true if the productBom is valid
255: */
256: public static boolean productBomIsValid(GenericValue productBom,
257: java.sql.Timestamp testDate) {
258: if (productBom.getTimestamp("fromDate") != null)
259: if (productBom.getTimestamp("thruDate") != null)
260: if (testDate.after(productBom.getTimestamp("fromDate"))
261: && testDate.before(productBom
262: .getTimestamp("thruDate")))
263: return true;
264: else
265: return false;
266: else if (testDate
267: .after(productBom.getTimestamp("fromDate")))
268: return true;
269: else
270: return false;
271: else if (productBom.getTimestamp("thruDate") != null)
272: if (testDate.before(productBom.getTimestamp("thruDate")))
273: return true;
274: else
275: return false;
276: else
277: return true;
278: }
279:
280: }
|