KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workeffort > workeffort > WorkEffortServices


1 /*
2  * $Id: WorkEffortServices.java 7057 2006-03-23 14:28:44Z jacopo $
3  *
4  * Copyright (c) 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.workeffort.workeffort;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37
38 import org.ofbiz.base.util.Debug;
39 import org.ofbiz.base.util.UtilDateTime;
40 import org.ofbiz.base.util.UtilMisc;
41 import org.ofbiz.base.util.UtilValidate;
42 import org.ofbiz.entity.GenericDelegator;
43 import org.ofbiz.entity.GenericEntityException;
44 import org.ofbiz.entity.GenericValue;
45 import org.ofbiz.entity.condition.EntityExpr;
46 import org.ofbiz.entity.condition.EntityCondition;
47 import org.ofbiz.entity.condition.EntityConditionList;
48 import org.ofbiz.entity.condition.EntityJoinOperator;
49 import org.ofbiz.entity.condition.EntityOperator;
50 import org.ofbiz.entity.util.EntityUtil;
51 import org.ofbiz.security.Security;
52 import org.ofbiz.service.DispatchContext;
53 import org.ofbiz.service.ServiceUtil;
54
55 /**
56  * WorkEffortServices - WorkEffort related Services
57  *
58  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
59  * @author <a HREF="mailto:johan@ibibi.com">Johan Isacsson</a>
60  * @version $Rev: 7057 $
61  * @since 2.0
62  */

63 public class WorkEffortServices {
64     
65     public static final String JavaDoc module = WorkEffortServices.class.getName();
66
67     public static Map JavaDoc getWorkEffortAssignedTasks(DispatchContext ctx, Map JavaDoc context) {
68         GenericDelegator delegator = ctx.getDelegator();
69         GenericValue userLogin = (GenericValue) context.get("userLogin");
70
71         List JavaDoc validWorkEfforts = null;
72
73         if (userLogin != null && userLogin.get("partyId") != null) {
74             try {
75                 validWorkEfforts = delegator.findByAnd("WorkEffortAndPartyAssign",
76                             UtilMisc.toList(new EntityExpr("partyId", EntityOperator.EQUALS, userLogin.get("partyId")),
77                                 new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "TASK"),
78                                 new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"),
79                                 new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"),
80                                 new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"),
81                                 new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED")),
82                             UtilMisc.toList("priority"));
83             } catch (GenericEntityException e) {
84                 Debug.logWarning(e, module);
85                 return ServiceUtil.returnError("Error finding desired WorkEffort records: " + e.toString());
86             }
87         }
88
89         Map JavaDoc result = new HashMap JavaDoc();
90         if (validWorkEfforts == null) validWorkEfforts = new LinkedList JavaDoc();
91         result.put("tasks", validWorkEfforts);
92         return result;
93     }
94
95     public static Map JavaDoc getWorkEffortAssignedActivities(DispatchContext ctx, Map JavaDoc context) {
96         GenericDelegator delegator = ctx.getDelegator();
97         GenericValue userLogin = (GenericValue) context.get("userLogin");
98
99         List JavaDoc validWorkEfforts = null;
100
101         if (userLogin != null && userLogin.get("partyId") != null) {
102             try {
103                 List JavaDoc constraints = new LinkedList JavaDoc();
104
105                 constraints.add(new EntityExpr("partyId", EntityOperator.EQUALS, userLogin.get("partyId")));
106                 constraints.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "ACTIVITY"));
107                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"));
108                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"));
109                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"));
110                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED"));
111                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_COMPLETED"));
112                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_TERMINATED"));
113                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_ABORTED"));
114                 validWorkEfforts = delegator.findByAnd("WorkEffortAndPartyAssign", constraints, UtilMisc.toList("priority"));
115             } catch (GenericEntityException e) {
116                 Debug.logWarning(e, module);
117                 return ServiceUtil.returnError("Error finding desired WorkEffort records: " + e.toString());
118             }
119         }
120
121         Map JavaDoc result = new HashMap JavaDoc();
122         if (validWorkEfforts == null) validWorkEfforts = new LinkedList JavaDoc();
123         result.put("activities", validWorkEfforts);
124         return result;
125     }
126
127     public static Map JavaDoc getWorkEffortAssignedActivitiesByRole(DispatchContext ctx, Map JavaDoc context) {
128         GenericDelegator delegator = ctx.getDelegator();
129         GenericValue userLogin = (GenericValue) context.get("userLogin");
130
131         List JavaDoc roleWorkEfforts = null;
132
133         if (userLogin != null && userLogin.get("partyId") != null) {
134             try {
135                 List JavaDoc constraints = new LinkedList JavaDoc();
136
137                 constraints.add(new EntityExpr("partyId", EntityOperator.EQUALS, userLogin.get("partyId")));
138                 constraints.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "ACTIVITY"));
139                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"));
140                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"));
141                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"));
142                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED"));
143                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_COMPLETED"));
144                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_TERMINATED"));
145                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_ABORTED"));
146                 roleWorkEfforts = delegator.findByAnd("WorkEffortPartyAssignByRole", constraints, UtilMisc.toList("priority"));
147             } catch (GenericEntityException e) {
148                 Debug.logWarning(e, module);
149                 return ServiceUtil.returnError("Error finding desired WorkEffort records: " + e.toString());
150             }
151         }
152
153         Map JavaDoc result = new HashMap JavaDoc();
154         if (roleWorkEfforts == null) roleWorkEfforts = new LinkedList JavaDoc();
155         result.put("roleActivities", roleWorkEfforts);
156         return result;
157     }
158
159     public static Map JavaDoc getWorkEffortAssignedActivitiesByGroup(DispatchContext ctx, Map JavaDoc context) {
160         GenericDelegator delegator = ctx.getDelegator();
161         GenericValue userLogin = (GenericValue) context.get("userLogin");
162
163         List JavaDoc groupWorkEfforts = null;
164
165         if (userLogin != null && userLogin.get("partyId") != null) {
166             try {
167                 List JavaDoc constraints = new LinkedList JavaDoc();
168
169                 constraints.add(new EntityExpr("partyId", EntityOperator.EQUALS, userLogin.get("partyId")));
170                 constraints.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "ACTIVITY"));
171                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DECLINED"));
172                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_DELEGATED"));
173                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_COMPLETED"));
174                 constraints.add(new EntityExpr("statusId", EntityOperator.NOT_EQUAL, "CAL_CANCELLED"));
175                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_COMPLETED"));
176                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_TERMINATED"));
177                 constraints.add(new EntityExpr("currentStatusId", EntityOperator.NOT_EQUAL, "WF_ABORTED"));
178                 groupWorkEfforts = delegator.findByAnd("WorkEffortPartyAssignByGroup", constraints, UtilMisc.toList("priority"));
179             } catch (GenericEntityException e) {
180                 Debug.logWarning(e, module);
181                 return ServiceUtil.returnError("Error finding desired WorkEffort records: " + e.toString());
182             }
183         }
184
185         Map JavaDoc result = new HashMap JavaDoc();
186         if (groupWorkEfforts == null) groupWorkEfforts = new LinkedList JavaDoc();
187         result.put("groupActivities", groupWorkEfforts);
188         return result;
189     }
190     
191     public static Map JavaDoc getWorkEffort(DispatchContext ctx, Map JavaDoc context) {
192         GenericDelegator delegator = ctx.getDelegator();
193         GenericValue userLogin = (GenericValue) context.get("userLogin");
194         Security security = ctx.getSecurity();
195         Map JavaDoc resultMap = new HashMap JavaDoc();
196         
197         String JavaDoc workEffortId = (String JavaDoc) context.get("workEffortId");
198         GenericValue workEffort = null;
199         
200         try {
201             workEffort = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId));
202         } catch (GenericEntityException e) {
203             Debug.logWarning(e, module);
204         }
205         
206         Boolean JavaDoc canView = null;
207         Collection JavaDoc workEffortPartyAssignments = null;
208         Boolean JavaDoc tryEntity = null;
209         GenericValue currentStatus = null;
210         
211         if (workEffort == null) {
212             tryEntity = new Boolean JavaDoc(false);
213             canView = new Boolean JavaDoc(true);
214         
215             String JavaDoc statusId = (String JavaDoc) context.get("currentStatusId");
216         
217             if (statusId != null && statusId.length() > 0) {
218                 try {
219                     currentStatus = delegator.findByPrimaryKeyCache("StatusItem", UtilMisc.toMap("statusId", statusId));
220                 } catch (GenericEntityException e) {
221                     Debug.logWarning(e, module);
222                 }
223             }
224         } else {
225             // get a collection of workEffortPartyAssignments, if empty then this user CANNOT view the event, unless they have permission to view all
226
if (userLogin != null && userLogin.get("partyId") != null && workEffortId != null) {
227                 try {
228                     workEffortPartyAssignments = delegator.findByAnd("WorkEffortPartyAssignment", UtilMisc.toMap("workEffortId", workEffortId, "partyId", userLogin.get("partyId")));
229                 } catch (GenericEntityException e) {
230                     Debug.logWarning(e, module);
231                 }
232             }
233             canView = (workEffortPartyAssignments != null && workEffortPartyAssignments.size() > 0) ? Boolean.TRUE : Boolean.FALSE;
234             if (!canView.booleanValue() && security.hasEntityPermission("WORKEFFORTMGR", "_VIEW", userLogin)) {
235                 canView = new Boolean JavaDoc(true);
236             }
237         
238             tryEntity = new Boolean JavaDoc(true);
239         
240             if (workEffort.get("currentStatusId") != null) {
241                 try {
242                     currentStatus = delegator.findByPrimaryKeyCache("StatusItem", UtilMisc.toMap("statusId", workEffort.get("currentStatusId")));
243                 } catch (GenericEntityException e) {
244                     Debug.logWarning(e, module);
245                 }
246             }
247         }
248         
249         if (workEffortId != null) resultMap.put("workEffortId", workEffortId);
250         if (workEffort != null) resultMap.put("workEffort", workEffort);
251         if (canView != null) resultMap.put("canView", canView);
252         if (workEffortPartyAssignments != null) resultMap.put("partyAssigns", workEffortPartyAssignments);
253         if (tryEntity != null) resultMap.put("tryEntity", tryEntity);
254         if (currentStatus != null) resultMap.put("currentStatusItem", currentStatus);
255         return resultMap;
256     }
257         
258     private static List JavaDoc getWorkEffortEvents(DispatchContext ctx, Timestamp JavaDoc startStamp, Timestamp JavaDoc endStamp, String JavaDoc partyId, String JavaDoc facilityId, String JavaDoc fixedAssetId) {
259         GenericDelegator delegator = ctx.getDelegator();
260         List JavaDoc validWorkEfforts = new ArrayList JavaDoc();
261         try {
262             List JavaDoc entityExprList = UtilMisc.toList(
263                     new EntityExpr("estimatedCompletionDate", EntityOperator.GREATER_THAN_EQUAL_TO, startStamp),
264                     new EntityExpr("estimatedStartDate", EntityOperator.LESS_THAN, endStamp));
265             List JavaDoc typesList = UtilMisc.toList(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "EVENT"));
266             if (UtilValidate.isNotEmpty(partyId)) {
267                 entityExprList.add(new EntityExpr("partyId", EntityOperator.EQUALS, partyId));
268             }
269             if (UtilValidate.isNotEmpty(facilityId)) {
270                 entityExprList.add(new EntityExpr("facilityId", EntityOperator.EQUALS, facilityId));
271                 typesList.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "PROD_ORDER_HEADER"));
272             }
273             if (UtilValidate.isNotEmpty(fixedAssetId)) {
274                 entityExprList.add(new EntityExpr("fixedAssetId", EntityOperator.EQUALS, fixedAssetId));
275                 typesList.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "PROD_ORDER_TASK"));
276             }
277             EntityCondition typesCondition = null;
278             if (typesList.size() == 1) {
279                 typesCondition = (EntityExpr)typesList.get(0);
280             } else {
281                 typesCondition = new EntityConditionList(typesList, EntityJoinOperator.OR);
282             }
283             entityExprList.add(typesCondition);
284
285             List JavaDoc tempWorkEfforts = null;
286             if (UtilValidate.isNotEmpty(partyId)) {
287                 tempWorkEfforts = delegator.findByAnd("WorkEffortAndPartyAssign", entityExprList, UtilMisc.toList("estimatedStartDate"));
288             } else {
289                 tempWorkEfforts = delegator.findByAnd("WorkEffort", entityExprList, UtilMisc.toList("estimatedStartDate"));
290             }
291
292             // FIXME: I think that now the following code can be removed.
293
// It was probably here to remove duplicated workeffort ids caused
294
// by the query on the WorkEffortAndPartyAssign view when no party was
295
// specified; now it is no more necessary since, when no party is specified,
296
// the query is done on the WorkEffort entity.
297
Set JavaDoc tempWeKeys = new HashSet JavaDoc();
298             Iterator JavaDoc tempWorkEffortIter = tempWorkEfforts.iterator();
299             while (tempWorkEffortIter.hasNext()) {
300                 GenericValue tempWorkEffort = (GenericValue) tempWorkEffortIter.next();
301                 String JavaDoc tempWorkEffortId = tempWorkEffort.getString("workEffortId");
302                 if (tempWeKeys.contains(tempWorkEffortId)) {
303                     tempWorkEffortIter.remove();
304                 } else {
305                     tempWeKeys.add(tempWorkEffortId);
306                 }
307             }
308             
309             validWorkEfforts = new ArrayList JavaDoc(tempWorkEfforts);
310         } catch (GenericEntityException e) {
311             Debug.logWarning(e, module);
312         }
313         return validWorkEfforts;
314     }
315
316     public static Map JavaDoc getWorkEffortEventsByPeriod(DispatchContext ctx, Map JavaDoc context) {
317         GenericDelegator delegator = ctx.getDelegator();
318         Security security = ctx.getSecurity();
319         GenericValue userLogin = (GenericValue) context.get("userLogin");
320         Map JavaDoc resultMap = new HashMap JavaDoc();
321         
322         Timestamp JavaDoc startDay = (Timestamp JavaDoc) context.get("start");
323         Integer JavaDoc numPeriodsInteger = (Integer JavaDoc) context.get("numPeriods");
324         Integer JavaDoc periodInteger = (Integer JavaDoc) context.get("periodSeconds");
325
326         String JavaDoc partyId = (String JavaDoc) context.get("partyId");
327         String JavaDoc facilityId = (String JavaDoc) context.get("facilityId");
328         String JavaDoc fixedAssetId = (String JavaDoc) context.get("fixedAssetId");
329         Boolean JavaDoc filterOutCanceledEvents = (Boolean JavaDoc) context.get("filterOutCanceledEvents");
330         if (filterOutCanceledEvents == null) {
331             filterOutCanceledEvents = Boolean.FALSE;
332         }
333         
334         //To be returned, the max concurrent entries for a single period
335
int maxConcurrentEntries = 0;
336                 
337         long period = periodInteger.intValue()*1000;
338         
339         int numPeriods = 0;
340         if(numPeriodsInteger != null) numPeriods = numPeriodsInteger.intValue();
341         
342         // get a timestamp (date) for the beginning of today and for beginning of numDays+1 days from now
343
Timestamp JavaDoc startStamp = UtilDateTime.getDayStart(startDay);
344         Timestamp JavaDoc endStamp = new Timestamp JavaDoc(startStamp.getTime()+(period*(numPeriods+1)));
345         
346         startStamp.setNanos(0);
347         // Get the WorkEfforts
348
List JavaDoc validWorkEfforts = null;
349         String JavaDoc partyIdToUse = null;
350         
351         if (UtilValidate.isNotEmpty(partyId)) {
352             if (partyId.equals(userLogin.getString("partyId")) || security.hasEntityPermission("WORKEFFORTMGR", "_VIEW", userLogin)) {
353                 partyIdToUse = partyId;
354             } else {
355                 return ServiceUtil.returnError("You do not have permission to view information for party with ID [" + partyId + "], you must be logged in as a user associated with this party, or have the WORKEFFORTMGR_VIEW or WORKEFFORTMGR_ADMIN permissions.");
356             }
357         } else {
358             // if a facilityId or a fixedAssetId are not specified, don't set a default partyId...
359
if (UtilValidate.isEmpty(facilityId) && UtilValidate.isEmpty(fixedAssetId)) {
360                 partyIdToUse = userLogin.getString("partyId");
361             }
362         }
363                 
364         // Use the View Entity
365
if (UtilValidate.isNotEmpty(partyIdToUse) || UtilValidate.isNotEmpty(facilityId) || UtilValidate.isNotEmpty(fixedAssetId)) {
366             validWorkEfforts = getWorkEffortEvents(ctx, startStamp, endStamp, partyIdToUse, facilityId, fixedAssetId);
367         }
368         if (filterOutCanceledEvents.booleanValue()) {
369             validWorkEfforts = EntityUtil.filterOutByCondition(validWorkEfforts, new EntityExpr("currentStatusId", EntityOperator.EQUALS, "EVENT_CANCELLED"));
370         }
371         
372         // Split the WorkEffort list into a map with entries for each period, period start is the key
373
List JavaDoc periods = new ArrayList JavaDoc();
374         if (validWorkEfforts != null) {
375         
376             // For each day in the set we check all work efforts to see if they fall within range
377
for (int i = 0; i < numPeriods; i++) {
378                 Timestamp JavaDoc curPeriodStart = new Timestamp JavaDoc(startStamp.getTime()+(i*period));
379                 Timestamp JavaDoc curPeriodEnd = new Timestamp JavaDoc(curPeriodStart.getTime()+period);
380                 List JavaDoc curWorkEfforts = new ArrayList JavaDoc();
381                 Map JavaDoc entry = new HashMap JavaDoc();
382                 for (int j = 0; j < validWorkEfforts.size(); j++) {
383                     
384                     GenericValue workEffort = (GenericValue) validWorkEfforts.get(j);
385                     // Debug.log("Got workEffort: " + workEffort.toString(), module);
386

387                     Timestamp JavaDoc estimatedStartDate = workEffort.getTimestamp("estimatedStartDate");
388                     Timestamp JavaDoc estimatedCompletionDate = workEffort.getTimestamp("estimatedCompletionDate");
389             
390                     if (estimatedStartDate == null || estimatedCompletionDate == null) continue;
391                     
392                     if (estimatedStartDate.compareTo(curPeriodEnd) < 0 && estimatedCompletionDate.compareTo(curPeriodStart) > 0) {
393                         //Debug.logInfo("Task start: "+estimatedStartDate+" Task end: "+estimatedCompletionDate+" Period start: "+curPeriodStart+" Period end: "+curPeriodEnd, module);
394

395                         Map JavaDoc calEntry = new HashMap JavaDoc();
396                         calEntry.put("workEffort",workEffort);
397                                                
398                         long length = ((estimatedCompletionDate.after(endStamp) ? endStamp.getTime() : estimatedCompletionDate.getTime()) - (estimatedStartDate.before(startStamp) ? startStamp.getTime() : estimatedStartDate.getTime()));
399                         int periodSpan = (int) Math.ceil((double) length / period);
400                         calEntry.put("periodSpan", new Integer JavaDoc(periodSpan));
401
402                         if(i == 0) calEntry.put("startOfPeriod",new Boolean JavaDoc(true)); //If this is the first priod any valid entry is starting here
403
else {
404                             boolean startOfPeriod = ((estimatedStartDate.getTime() - curPeriodStart.getTime()) >= 0);
405                             calEntry.put("startOfPeriod", new Boolean JavaDoc(startOfPeriod));
406                         }
407                         curWorkEfforts.add(calEntry);
408                     }
409         
410                     // if startDate is after hourEnd, continue to the next day, we haven't gotten to this one yet...
411
if (estimatedStartDate.after(curPeriodEnd)) break;
412                     
413                     // if completionDate is before the hourEnd, remove from list, we are done with it
414
if (estimatedCompletionDate.before(curPeriodEnd)) {
415                         validWorkEfforts.remove(j);
416                         j--;
417                     }
418                 }
419                 //For calendar we want to include empty periods aswell
420
//if (curWorkEfforts.size() > 0)
421
int numEntries = curWorkEfforts.size();
422                 if(numEntries > maxConcurrentEntries) maxConcurrentEntries = numEntries;
423                 entry.put("start",curPeriodStart);
424                 entry.put("end",curPeriodEnd);
425                 entry.put("calendarEntries",curWorkEfforts);
426                 periods.add(entry);
427             }
428         }
429         Map JavaDoc result = new HashMap JavaDoc();
430         result.put("periods", periods);
431         result.put("maxConcurrentEntries", new Integer JavaDoc(maxConcurrentEntries));
432         return result;
433     }
434     
435     public static Map JavaDoc getProductManufacturingSummaryByFacility(DispatchContext ctx, Map JavaDoc context) {
436         GenericDelegator delegator = ctx.getDelegator();
437         Security security = ctx.getSecurity();
438         GenericValue userLogin = (GenericValue) context.get("userLogin");
439         String JavaDoc productId = (String JavaDoc) context.get("productId");
440         String JavaDoc facilityId = (String JavaDoc) context.get("facilityId"); // optional
441

442         Map JavaDoc summaryInByFacility = new HashMap JavaDoc();
443         Map JavaDoc summaryOutByFacility = new HashMap JavaDoc();
444         try {
445             //
446
// Information about the running production runs that are going
447
// to produce units of productId by facility.
448
//
449
List JavaDoc findIncomingProductionRunsConds = new LinkedList JavaDoc();
450
451             findIncomingProductionRunsConds.add(new EntityExpr("productId", EntityOperator.EQUALS, productId));
452             findIncomingProductionRunsConds.add(new EntityExpr("statusId", EntityOperator.EQUALS, "WEGS_CREATED"));
453             findIncomingProductionRunsConds.add(new EntityExpr("workEffortGoodStdTypeId", EntityOperator.EQUALS, "PRUN_PROD_DELIV"));
454             if (facilityId != null) {
455                 findIncomingProductionRunsConds.add(new EntityExpr("facilityId", EntityOperator.EQUALS, facilityId));
456             }
457
458             List JavaDoc findIncomingProductionRunsStatusConds = new LinkedList JavaDoc();
459             findIncomingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_CREATED"));
460             findIncomingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_DOC_PRINTED"));
461             findIncomingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_RUNNING"));
462             findIncomingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_COMPLETED"));
463             findIncomingProductionRunsConds.add(new EntityConditionList(findIncomingProductionRunsStatusConds, EntityOperator.OR));
464
465             EntityConditionList findIncomingProductionRunsCondition = new EntityConditionList(findIncomingProductionRunsConds, EntityOperator.AND);
466
467             List JavaDoc incomingProductionRuns = delegator.findByCondition("WorkEffortAndGoods", findIncomingProductionRunsCondition, null, UtilMisc.toList("-estimatedCompletionDate"));
468             Iterator JavaDoc incomingProductionRunsIter = incomingProductionRuns.iterator();
469             while (incomingProductionRunsIter.hasNext()) {
470                 GenericValue incomingProductionRun = (GenericValue)incomingProductionRunsIter.next();
471
472                 double producedQtyTot = 0.0;
473                 if (incomingProductionRun.getString("currentStatusId").equals("PRUN_COMPLETED")) {
474                     List JavaDoc inventoryItems = delegator.findByAnd("WorkEffortAndInventoryProduced", UtilMisc.toMap("productId", productId, "workEffortId", incomingProductionRun.getString("workEffortId")));
475                     Iterator JavaDoc inventoryItemsIter = inventoryItems.iterator();
476                     while (inventoryItemsIter.hasNext()) {
477                         GenericValue inventoryItem = (GenericValue)inventoryItemsIter.next();
478                         GenericValue inventoryItemDetail = EntityUtil.getFirst(delegator.findByAnd("InventoryItemDetail", UtilMisc.toMap("inventoryItemId", inventoryItem.getString("inventoryItemId")), UtilMisc.toList("inventoryItemDetailSeqId")));
479                         if (inventoryItemDetail != null && inventoryItemDetail.get("quantityOnHandDiff") != null) {
480                             Double JavaDoc inventoryItemQty = inventoryItemDetail.getDouble("quantityOnHandDiff");
481                             producedQtyTot = producedQtyTot + inventoryItemQty.doubleValue();
482                         }
483                     }
484                 }
485                 double estimatedQuantity = 0.0;
486                 if (incomingProductionRun.get("estimatedQuantity") != null) {
487                     estimatedQuantity = incomingProductionRun.getDouble("estimatedQuantity").doubleValue();
488                 }
489                 double remainingQuantity = estimatedQuantity - producedQtyTot; // the qty that still needs to be produced
490
if (remainingQuantity > 0) {
491                     incomingProductionRun.set("estimatedQuantity", new Double JavaDoc(remainingQuantity));
492                 } else {
493                     continue;
494                 }
495                 String JavaDoc weFacilityId = incomingProductionRun.getString("facilityId");
496
497                 Map JavaDoc quantitySummary = (Map JavaDoc)summaryInByFacility.get(weFacilityId);
498                 if (quantitySummary == null) {
499                     quantitySummary = new HashMap JavaDoc();
500                     quantitySummary.put("facilityId", weFacilityId);
501                     summaryInByFacility.put(weFacilityId, quantitySummary);
502                 }
503                 Double JavaDoc remainingQuantityTot = (Double JavaDoc)quantitySummary.get("estimatedQuantityTotal");
504                 if (remainingQuantityTot == null) {
505                     quantitySummary.put("estimatedQuantityTotal", new Double JavaDoc(remainingQuantity));
506                 } else {
507                     quantitySummary.put("estimatedQuantityTotal", new Double JavaDoc(remainingQuantity + remainingQuantityTot.doubleValue()));
508                 }
509
510                 List JavaDoc incomingProductionRunList = (List JavaDoc)quantitySummary.get("incomingProductionRunList");
511                 if (incomingProductionRunList == null) {
512                     incomingProductionRunList = new LinkedList JavaDoc();
513                     quantitySummary.put("incomingProductionRunList", incomingProductionRunList);
514                 }
515                 incomingProductionRunList.add(incomingProductionRun);
516             }
517             //
518
// Information about the running production runs that are going
519
// to consume units of productId by facility.
520
//
521
List JavaDoc findOutgoingProductionRunsConds = new LinkedList JavaDoc();
522
523             findOutgoingProductionRunsConds.add(new EntityExpr("productId", EntityOperator.EQUALS, productId));
524             findOutgoingProductionRunsConds.add(new EntityExpr("statusId", EntityOperator.EQUALS, "WEGS_CREATED"));
525             findOutgoingProductionRunsConds.add(new EntityExpr("workEffortGoodStdTypeId", EntityOperator.EQUALS, "PRUNT_PROD_NEEDED"));
526             if (facilityId != null) {
527                 findOutgoingProductionRunsConds.add(new EntityExpr("facilityId", EntityOperator.EQUALS, facilityId));
528             }
529
530             List JavaDoc findOutgoingProductionRunsStatusConds = new LinkedList JavaDoc();
531             findOutgoingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_CREATED"));
532             findOutgoingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_DOC_PRINTED"));
533             findOutgoingProductionRunsStatusConds.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "PRUN_RUNNING"));
534             findOutgoingProductionRunsConds.add(new EntityConditionList(findOutgoingProductionRunsStatusConds, EntityOperator.OR));
535
536             EntityConditionList findOutgoingProductionRunsCondition = new EntityConditionList(findOutgoingProductionRunsConds, EntityOperator.AND);
537             List JavaDoc outgoingProductionRuns = delegator.findByCondition("WorkEffortAndGoods", findOutgoingProductionRunsCondition, null, UtilMisc.toList("-estimatedStartDate"));
538             Iterator JavaDoc outgoingProductionRunsIter = outgoingProductionRuns.iterator();
539             while (outgoingProductionRunsIter.hasNext()) {
540                 GenericValue outgoingProductionRun = (GenericValue)outgoingProductionRunsIter.next();
541
542                 String JavaDoc weFacilityId = outgoingProductionRun.getString("facilityId");
543                 Double JavaDoc neededQuantity = outgoingProductionRun.getDouble("estimatedQuantity");
544                 if (neededQuantity == null) {
545                     neededQuantity = new Double JavaDoc(0);
546                 }
547
548                 Map JavaDoc quantitySummary = (Map JavaDoc)summaryOutByFacility.get(weFacilityId);
549                 if (quantitySummary == null) {
550                     quantitySummary = new HashMap JavaDoc();
551                     quantitySummary.put("facilityId", weFacilityId);
552                     summaryOutByFacility.put(weFacilityId, quantitySummary);
553                 }
554                 Double JavaDoc remainingQuantityTot = (Double JavaDoc)quantitySummary.get("estimatedQuantityTotal");
555                 if (remainingQuantityTot == null) {
556                     quantitySummary.put("estimatedQuantityTotal", neededQuantity);
557                 } else {
558                     quantitySummary.put("estimatedQuantityTotal", new Double JavaDoc(neededQuantity.doubleValue() + remainingQuantityTot.doubleValue()));
559                 }
560
561                 List JavaDoc outgoingProductionRunList = (List JavaDoc)quantitySummary.get("outgoingProductionRunList");
562                 if (outgoingProductionRunList == null) {
563                     outgoingProductionRunList = new LinkedList JavaDoc();
564                     quantitySummary.put("outgoingProductionRunList", outgoingProductionRunList);
565                 }
566                 outgoingProductionRunList.add(outgoingProductionRun);
567             }
568
569         } catch(GenericEntityException gee) {
570             return ServiceUtil.returnError("Error retrieving manufacturing data for productId [" + productId + "]: " + gee.getMessage());
571         }
572         Map JavaDoc resultMap = ServiceUtil.returnSuccess();
573         resultMap.put("summaryInByFacility", summaryInByFacility);
574         resultMap.put("summaryOutByFacility", summaryOutByFacility);
575         return resultMap;
576     }
577 }
578
Popular Tags