KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > manufacturing > techdata > TechDataServices


1 /*
2  * $Id: TechDataServices.java 7238 2006-04-08 07:30:54Z jacopo $
3  *
4  * Copyright (c) 2003, 2004 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.manufacturing.techdata;
26
27 import java.sql.Time JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.HashMap 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
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilDateTime;
39 import org.ofbiz.base.util.UtilMisc;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.entity.condition.EntityExpr;
44 import org.ofbiz.entity.condition.EntityOperator;
45 import org.ofbiz.entity.util.EntityUtil;
46 import org.ofbiz.service.DispatchContext;
47 import org.ofbiz.service.ServiceUtil;
48
49 /**
50  * TechDataServices - TechData related Services
51  *
52  * @author <a HREF="mailto:olivier.heintz@nereide.biz">Olivier Heintz</a>
53  */

54 public class TechDataServices {
55     
56     public static final String JavaDoc module = TechDataServices.class.getName();
57     
58     /**
59      *
60      * Used to retreive some RoutingTasks (WorkEffort) selected by Name or MachineGroup ordered by Name
61      *
62      * @param ctx
63      * @param context: a map containing workEffortName (routingTaskName) and fixedAssetId (MachineGroup or ANY)
64      * @return result: a map containing lookupResult (list of RoutingTask <=> workEffortId with currentStatusId = "ROU_ACTIVE" and workEffortTypeId = "ROU_TASK"
65      */

66     public static Map JavaDoc lookupRoutingTask(DispatchContext ctx, Map JavaDoc context) {
67         GenericDelegator delegator = ctx.getDelegator();
68         GenericValue userLogin = (GenericValue) context.get("userLogin");
69         /* Security security = ctx.getSecurity(); a completer par la suite */
70         Map JavaDoc result = new HashMap JavaDoc();
71         
72         String JavaDoc workEffortName = (String JavaDoc) context.get("workEffortName");
73         String JavaDoc fixedAssetId = (String JavaDoc) context.get("fixedAssetId");
74         
75         List JavaDoc listRoutingTask = null;
76         List JavaDoc constraints = new LinkedList JavaDoc();
77         
78         if (workEffortName != null && workEffortName.length()>0)
79             constraints.add(new EntityExpr("workEffortName", EntityOperator.GREATER_THAN_EQUAL_TO, workEffortName));
80         if (fixedAssetId != null && fixedAssetId.length()>0 && ! "ANY".equals(fixedAssetId))
81             constraints.add(new EntityExpr("fixedAssetId", EntityOperator.EQUALS, fixedAssetId));
82         
83         constraints.add(new EntityExpr("currentStatusId", EntityOperator.EQUALS, "ROU_ACTIVE"));
84         constraints.add(new EntityExpr("workEffortTypeId", EntityOperator.EQUALS, "ROU_TASK"));
85         
86         try {
87             listRoutingTask = delegator.findByAnd("WorkEffort", constraints, UtilMisc.toList("workEffortName"));
88         } catch (GenericEntityException e) {
89             Debug.logWarning(e, module);
90             return ServiceUtil.returnError("Error finding desired WorkEffort records: " + e.toString());
91         }
92         if (listRoutingTask == null) listRoutingTask = new LinkedList JavaDoc();
93         if (listRoutingTask.size() == 0) listRoutingTask.add(UtilMisc.toMap("label","no Match","value","NO_MATCH"));
94         result.put("lookupResult", listRoutingTask);
95         return result;
96     }
97     /**
98      *
99      * Used to check if there is not two routing task with the same SeqId valid at the same period
100      *
101      * @param ctx The DispatchContext that this service is operating in.
102      * @param context a map containing workEffortIdFrom (routing) and SeqId, fromDate thruDate
103      * @return result a map containing sequenceNumNotOk which is equal to "Y" if it's not Ok
104      */

105     public static Map JavaDoc checkRoutingTaskAssoc(DispatchContext ctx, Map JavaDoc context) {
106         GenericDelegator delegator = ctx.getDelegator();
107         Map JavaDoc result = new HashMap JavaDoc();
108         String JavaDoc sequenceNumNotOk = "N";
109         
110         String JavaDoc workEffortIdFrom = (String JavaDoc) context.get("workEffortIdFrom");
111         String JavaDoc workEffortIdTo = (String JavaDoc) context.get("workEffortIdTo");
112         String JavaDoc workEffortAssocTypeId = (String JavaDoc) context.get("workEffortAssocTypeId");
113         Long JavaDoc sequenceNum = (Long JavaDoc) context.get("sequenceNum");
114         Timestamp JavaDoc fromDate = (Timestamp JavaDoc) context.get("fromDate");
115         Timestamp JavaDoc thruDate = (Timestamp JavaDoc) context.get("thruDate");
116         String JavaDoc create = (String JavaDoc) context.get("create");
117         
118         boolean createProcess = (create !=null && create.equals("Y")) ? true : false;
119         List JavaDoc listRoutingTaskAssoc = null;
120         
121         try {
122             listRoutingTaskAssoc = delegator.findByAnd("WorkEffortAssoc",UtilMisc.toMap("workEffortIdFrom", workEffortIdFrom,"sequenceNum",sequenceNum), UtilMisc.toList("fromDate"));
123         } catch (GenericEntityException e) {
124             Debug.logWarning(e, module);
125             return ServiceUtil.returnError("Error finding desired WorkEffortAssoc records: " + e.toString());
126         }
127         
128         if (listRoutingTaskAssoc != null) {
129             Iterator JavaDoc i = listRoutingTaskAssoc.iterator();
130             while (i.hasNext()) {
131                 GenericValue routingTaskAssoc = (GenericValue) i.next();
132                 if ( ! workEffortIdFrom.equals(routingTaskAssoc.getString("workEffortIdFrom")) ||
133                 ! workEffortIdTo.equals(routingTaskAssoc.getString("workEffortIdTo")) ||
134                 ! workEffortAssocTypeId.equals(routingTaskAssoc.getString("workEffortAssocTypeId")) ||
135                 ! sequenceNum.equals(routingTaskAssoc.getLong("sequenceNum"))
136                 ) {
137                     if (routingTaskAssoc.getTimestamp("thruDate") == null && routingTaskAssoc.getTimestamp("fromDate") == null) sequenceNumNotOk = "Y";
138                     else if (routingTaskAssoc.getTimestamp("thruDate") == null) {
139                         if (thruDate == null) sequenceNumNotOk = "Y";
140                         else if (thruDate.after(routingTaskAssoc.getTimestamp("fromDate"))) sequenceNumNotOk = "Y";
141                     }
142                     else if (routingTaskAssoc.getTimestamp("fromDate") == null) {
143                         if (fromDate == null) sequenceNumNotOk = "Y";
144                         else if (fromDate.before(routingTaskAssoc.getTimestamp("thruDate"))) sequenceNumNotOk = "Y";
145                     }
146                     else if ( fromDate == null && thruDate == null) sequenceNumNotOk = "Y";
147                     else if (thruDate == null) {
148                         if (fromDate.before(routingTaskAssoc.getTimestamp("thruDate"))) sequenceNumNotOk = "Y";
149                     }
150                     else if (fromDate == null) {
151                         if (thruDate.after(routingTaskAssoc.getTimestamp("fromDate"))) sequenceNumNotOk = "Y";
152                     }
153                     else if ( routingTaskAssoc.getTimestamp("fromDate").before(thruDate) && fromDate.before(routingTaskAssoc.getTimestamp("thruDate")) ) sequenceNumNotOk = "Y";
154                 } else if (createProcess) sequenceNumNotOk = "Y";
155             }
156         }
157         result.put("sequenceNumNotOk", sequenceNumNotOk);
158         return result;
159     }
160     /**
161      * Used to check if the routingtaskAssoc is valid for the testDate
162      *
163      * @param routingTaskAssoc the routingTaskAssoc to test
164      * @param testDate a date
165      * @return true if the routingTAskAssoc is valid
166      */

167     public static boolean routingTaskAssocIsValid(GenericValue routingTaskAssoc, Timestamp JavaDoc testDate) {
168         if (routingTaskAssoc.getTimestamp("fromDate") != null)
169             if (routingTaskAssoc.getTimestamp("thruDate") != null)
170                 if (testDate.after(routingTaskAssoc.getTimestamp("fromDate")) && testDate.before(routingTaskAssoc.getTimestamp("thruDate"))) return true;
171                 else return false;
172             else
173                 if (testDate.after(routingTaskAssoc.getTimestamp("fromDate"))) return true;
174                 else return false;
175         else
176             if (routingTaskAssoc.getTimestamp("thruDate") != null)
177                 if (testDate.before(routingTaskAssoc.getTimestamp("thruDate"))) return true;
178                 else return false;
179             else return true;
180     }
181     /**
182      * 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>}
183      *
184      * @param productBom the productBom to test
185      * @param testDate a date
186      * @return true if the productBom is valid
187      */

188     public static boolean productBomIsValid(GenericValue productBom, Timestamp JavaDoc testDate) {
189         if (productBom.getTimestamp("fromDate") != null)
190             if (productBom.getTimestamp("thruDate") != null)
191                 if (testDate.after(productBom.getTimestamp("fromDate")) && testDate.before(productBom.getTimestamp("thruDate"))) return true;
192                 else return false;
193             else
194                 if (testDate.after(productBom.getTimestamp("fromDate"))) return true;
195                 else return false;
196         else
197             if (productBom.getTimestamp("thruDate") != null)
198                 if (testDate.before(productBom.getTimestamp("thruDate"))) return true;
199                 else return false;
200             else return true;
201     }
202     /**
203      * Used to get the techDataCalendar for a routingTask, if there is a entity exception
204      * or routingTask associated with no MachineGroup the DEFAULT TechDataCalendar is return.
205      *
206      * @param routingTask the routingTask for which we are looking for
207      * @return the techDataCalendar associated
208      */

209     public static GenericValue getTechDataCalendar(GenericValue routingTask) {
210         GenericValue machineGroup = null, techDataCalendar = null;
211         try{
212             machineGroup = routingTask.getRelatedOneCache("FixedAsset");
213         } catch (GenericEntityException e) {
214             Debug.logError("Pb reading FixedAsset associated with routingTask"+e.getMessage(), module);
215         }
216         if (machineGroup != null) {
217             if (machineGroup.getString("calendarId") != null){
218                 try{
219                     techDataCalendar = machineGroup.getRelatedOneCache("TechDataCalendar");
220                 } catch (GenericEntityException e) {
221                     Debug.logError("Pb reading TechDataCalendar associated with machineGroup"+e.getMessage(), module);
222                 }
223             }else {
224                 try{
225                     List JavaDoc machines = machineGroup.getRelatedCache("ChildFixedAsset");
226                     if (machines != null && machines.size()>0) {
227                         GenericValue machine = EntityUtil.getFirst(machines);
228                         techDataCalendar = machine.getRelatedOneCache("TechDataCalendar");
229                     }
230                 } catch (GenericEntityException e) {
231                     Debug.logError("Pb reading machine child from machineGroup"+e.getMessage(), module);
232                 }
233             }
234         }
235         if (techDataCalendar == null) {
236             try {
237                 GenericDelegator delegator = routingTask.getDelegator();
238                 techDataCalendar = delegator.findByPrimaryKey("TechDataCalendar",UtilMisc.toMap("calendarId","DEFAULT"));
239             } catch (GenericEntityException e) {
240                 Debug.logError("Pb reading TechDataCalendar DEFAULT"+e.getMessage(), module);
241             }
242         }
243         return techDataCalendar;
244     }
245     
246     /** Used to find the fisrt day in the TechDataCalendarWeek where capacity != 0, beginning at dayStart, dayStart included.
247      *
248      * @param techDataCalendarWeek The TechDataCalendarWeek cover
249      * @param dayStart
250      * @return a map with the capacity (Double) available and moveDay (int): the number of day it's necessary to move to have capacity available
251      */

252     public static Map JavaDoc dayStartCapacityAvailable(GenericValue techDataCalendarWeek, int dayStart) {
253         Map JavaDoc result = new HashMap JavaDoc();
254         int moveDay = 0;
255         Double JavaDoc capacity = null;
256         Time JavaDoc startTime = null;
257         while (capacity == null || capacity.doubleValue()==0) {
258             switch( dayStart){
259                 case Calendar.MONDAY:
260                     capacity = techDataCalendarWeek.getDouble("mondayCapacity");
261                     startTime = techDataCalendarWeek.getTime("mondayStartTime");
262                     break;
263                 case Calendar.TUESDAY:
264                     capacity = techDataCalendarWeek.getDouble("tuesdayCapacity");
265                     startTime = techDataCalendarWeek.getTime("tuesdayStartTime");
266                     break;
267                 case Calendar.WEDNESDAY:
268                     capacity = techDataCalendarWeek.getDouble("wednesdayCapacity");
269                     startTime = techDataCalendarWeek.getTime("wednesdayStartTime");
270                     break;
271                 case Calendar.THURSDAY:
272                     capacity = techDataCalendarWeek.getDouble("thursdayCapacity");
273                     startTime = techDataCalendarWeek.getTime("thursdayStartTime");
274                     break;
275                 case Calendar.FRIDAY:
276                     capacity = techDataCalendarWeek.getDouble("fridayCapacity");
277                     startTime = techDataCalendarWeek.getTime("fridayStartTime");
278                     break;
279                 case Calendar.SATURDAY:
280                     capacity = techDataCalendarWeek.getDouble("saturdayCapacity");
281                     startTime = techDataCalendarWeek.getTime("saturdayStartTime");
282                     break;
283                 case Calendar.SUNDAY:
284                     capacity = techDataCalendarWeek.getDouble("sundayCapacity");
285                     startTime = techDataCalendarWeek.getTime("sundayStartTime");
286                     break;
287             }
288             if (capacity == null || capacity.doubleValue() == 0) {
289                 moveDay +=1;
290                 dayStart = (dayStart==7) ? 1 : dayStart +1;
291             }
292             // Debug.logInfo("capacity loop: " + capacity+ " moveDay=" +moveDay, module);
293
}
294         result.put("capacity",capacity);
295         result.put("startTime",startTime);
296         result.put("moveDay",new Integer JavaDoc(moveDay));
297         return result;
298     }
299     /** Used to to request the remain capacity available for dateFrom in a TechDataCalenda,
300      * If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is zero.
301      *
302      * @param techDataCalendar The TechDataCalendar cover
303      * @param dateFrom the date
304      * @return long capacityRemaining
305      */

306     public static long capacityRemaining(GenericValue techDataCalendar, Timestamp JavaDoc dateFrom) {
307         GenericValue techDataCalendarWeek = null;
308         // TODO read TechDataCalendarExcWeek to manage execption week (maybe it's needed to refactor the entity definition
309
try{
310             techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek");
311         } catch (GenericEntityException e) {
312             Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module);
313             // return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PbReadingTechDataCalendarWeekAssociated", locale));
314
return 0;
315         }
316         // TODO read TechDataCalendarExcDay to manage execption day
317
Calendar JavaDoc cDateTrav = Calendar.getInstance();
318         cDateTrav.setTime((Date JavaDoc) dateFrom);
319         Map JavaDoc position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
320         int moveDay = ((Integer JavaDoc) position.get("moveDay")).intValue();
321         if (moveDay != 0) return 0;
322         Time JavaDoc startTime = (Time JavaDoc) position.get("startTime");
323         Double JavaDoc capacity = (Double JavaDoc) position.get("capacity");
324         // TODO after test (01:00:00).getTime() = 0 and not 3600000 so currently we add this value to be correct but it's needed to find why (maybe GMT pb)
325
Timestamp JavaDoc startAvailablePeriod = new Timestamp JavaDoc(UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + 3600000);
326         if (dateFrom.before(startAvailablePeriod) ) return 0;
327         Timestamp JavaDoc endAvailablePeriod = new Timestamp JavaDoc(startAvailablePeriod.getTime()+capacity.longValue());
328         if (dateFrom.after(endAvailablePeriod)) return 0;
329         return endAvailablePeriod.getTime() - dateFrom.getTime();
330     }
331     /** Used to move in a TechDataCalenda, produce the Timestamp for the begining of the next day available and its associated capacity.
332      * If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is the next day available
333      *
334      * @param techDataCalendar The TechDataCalendar cover
335      * @param dateFrom the date
336      * @return a map with Timestamp dateTo, Double nextCapacity
337      */

338     public static Map JavaDoc startNextDay(GenericValue techDataCalendar, Timestamp JavaDoc dateFrom) {
339         Map JavaDoc result = new HashMap JavaDoc();
340         Timestamp JavaDoc dateTo = null;
341         GenericValue techDataCalendarWeek = null;
342         // TODO read TechDataCalendarExcWeek to manage execption week (maybe it's needed to refactor the entity definition
343
try{
344             techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek");
345         } catch (GenericEntityException e) {
346             Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module);
347             // return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PbReadingTechDataCalendarWeekAssociated", locale));
348
return ServiceUtil.returnError("Pb reading Calendar Week associated with calendar");
349         }
350         // TODO read TechDataCalendarExcDay to manage execption day
351
Calendar JavaDoc cDateTrav = Calendar.getInstance();
352         cDateTrav.setTime((Date JavaDoc) dateFrom);
353         Map JavaDoc position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
354         Time JavaDoc startTime = (Time JavaDoc) position.get("startTime");
355         int moveDay = ((Integer JavaDoc) position.get("moveDay")).intValue();
356         dateTo = (moveDay == 0) ? dateFrom : UtilDateTime.getDayStart(dateFrom,moveDay);
357         // TODO after test (01:00:00).getTime() = 0 and not 3600000 so currently we add this value to be correct but it's needed to find why (maybe GMT pb)
358
Timestamp JavaDoc startAvailablePeriod = new Timestamp JavaDoc(UtilDateTime.getDayStart(dateTo).getTime() + startTime.getTime() + 3600000);
359         if (dateTo.before(startAvailablePeriod) ) {
360             dateTo = startAvailablePeriod;
361         }
362         else {
363             dateTo = UtilDateTime.getNextDayStart(dateTo);
364             cDateTrav.setTime((Date JavaDoc) dateTo);
365             position = dayStartCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
366             startTime = (Time JavaDoc) position.get("startTime");
367             moveDay = ((Integer JavaDoc) position.get("moveDay")).intValue();
368             if (moveDay != 0) dateTo = UtilDateTime.getDayStart(dateTo,moveDay);
369             dateTo.setTime(dateTo.getTime() + startTime.getTime() + 3600000);
370         }
371         result.put("dateTo",dateTo);
372         result.put("nextCapacity",position.get("capacity"));
373         return result;
374     }
375     /** Used to move forward in a TechDataCalenda, start from the dateFrom and move forward only on available period.
376      * If the dateFrom (param in) is not a available TechDataCalendar period, the startDate is the begining of the next day available
377      *
378      * @param techDataCalendar The TechDataCalendar cover
379      * @param dateFrom the start date
380      * @param amount the amount of millisecond to move forward
381      * @return the dateTo
382      */

383     public static Timestamp JavaDoc addForward(GenericValue techDataCalendar, Timestamp JavaDoc dateFrom, long amount) {
384         Timestamp JavaDoc dateTo = (Timestamp JavaDoc) dateFrom.clone();
385         long nextCapacity = capacityRemaining(techDataCalendar, dateFrom);
386         if (amount <= nextCapacity){
387             dateTo.setTime(dateTo.getTime()+amount);
388             amount = 0;
389         }else amount -= nextCapacity;
390         
391         Map JavaDoc result = new HashMap JavaDoc();
392         while (amount > 0) {
393             result = startNextDay(techDataCalendar, dateTo);
394             dateTo = (Timestamp JavaDoc) result.get("dateTo");
395             nextCapacity = ((Double JavaDoc) result.get("nextCapacity")).longValue();
396             if (amount <= nextCapacity){
397                 dateTo.setTime(dateTo.getTime()+amount);
398                 amount = 0;
399             }else amount -= nextCapacity;
400         }
401         return dateTo;
402     }
403     
404     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
405
/** Used to find the last day in the TechDataCalendarWeek where capacity != 0, ending at dayEnd, dayEnd included.
406      *
407      * @param techDataCalendarWeek The TechDataCalendarWeek cover
408      * @param dayEnd
409      * @return a map with the capacity (Double) available, the startTime and moveDay (int): the number of day it's necessary to move to have capacity available
410      */

411     public static Map JavaDoc dayEndCapacityAvailable(GenericValue techDataCalendarWeek, int dayEnd) {
412         Map JavaDoc result = new HashMap JavaDoc();
413         int moveDay = 0;
414         Double JavaDoc capacity = null;
415         Time JavaDoc startTime = null;
416         while (capacity == null || capacity.doubleValue() == 0) {
417             switch( dayEnd){
418                 case Calendar.MONDAY:
419                     capacity = techDataCalendarWeek.getDouble("mondayCapacity");
420                     startTime = techDataCalendarWeek.getTime("mondayStartTime");
421                     break;
422                 case Calendar.TUESDAY:
423                     capacity = techDataCalendarWeek.getDouble("tuesdayCapacity");
424                     startTime = techDataCalendarWeek.getTime("tuesdayStartTime");
425                     break;
426                 case Calendar.WEDNESDAY:
427                     capacity = techDataCalendarWeek.getDouble("wednesdayCapacity");
428                     startTime = techDataCalendarWeek.getTime("wednesdayStartTime");
429                     break;
430                 case Calendar.THURSDAY:
431                     capacity = techDataCalendarWeek.getDouble("thursdayCapacity");
432                     startTime = techDataCalendarWeek.getTime("thursdayStartTime");
433                     break;
434                 case Calendar.FRIDAY:
435                     capacity = techDataCalendarWeek.getDouble("fridayCapacity");
436                     startTime = techDataCalendarWeek.getTime("fridayStartTime");
437                     break;
438                 case Calendar.SATURDAY:
439                     capacity = techDataCalendarWeek.getDouble("saturdayCapacity");
440                     startTime = techDataCalendarWeek.getTime("saturdayStartTime");
441                     break;
442                 case Calendar.SUNDAY:
443                     capacity = techDataCalendarWeek.getDouble("sundayCapacity");
444                     startTime = techDataCalendarWeek.getTime("sundayStartTime");
445                     break;
446             }
447             if (capacity == null || capacity.doubleValue() == 0) {
448                 moveDay -=1;
449                 dayEnd = (dayEnd==1) ? 7 : dayEnd - 1;
450             }
451             // Debug.logInfo("capacity loop: " + capacity+ " moveDay=" +moveDay, module);
452
}
453         result.put("capacity",capacity);
454         result.put("startTime",startTime);
455         result.put("moveDay",new Integer JavaDoc(moveDay));
456         return result;
457     }
458     /** Used to request the remaining capacity available for dateFrom in a TechDataCalenda,
459      * If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is zero.
460      *
461      * @param techDataCalendar The TechDataCalendar cover
462      * @param dateFrom the date
463      * @return long capacityRemaining
464      */

465     public static long capacityRemainingBackward(GenericValue techDataCalendar, Timestamp JavaDoc dateFrom) {
466         GenericValue techDataCalendarWeek = null;
467         // TODO read TechDataCalendarExcWeek to manage exception week (maybe it's needed to refactor the entity definition
468
try{
469             techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek");
470         } catch (GenericEntityException e) {
471             Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module);
472             // return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PbReadingTechDataCalendarWeekAssociated", locale));
473
return 0;
474         }
475         // TODO read TechDataCalendarExcDay to manage execption day
476
Calendar JavaDoc cDateTrav = Calendar.getInstance();
477         cDateTrav.setTime((Date JavaDoc) dateFrom);
478         Map JavaDoc position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
479         int moveDay = ((Integer JavaDoc) position.get("moveDay")).intValue();
480         if (moveDay != 0) return 0;
481         Time JavaDoc startTime = (Time JavaDoc) position.get("startTime");
482         Double JavaDoc capacity = (Double JavaDoc) position.get("capacity");
483         //TODO after test (01:00:00).getTime() = 0 and not 3600000 so currently we add this value to be correct but it's needed to find why (maybe GMT pb)
484
Timestamp JavaDoc startAvailablePeriod = new Timestamp JavaDoc(UtilDateTime.getDayStart(dateFrom).getTime() + startTime.getTime() + 3600000);
485         if (dateFrom.before(startAvailablePeriod) ) return 0;
486         Timestamp JavaDoc endAvailablePeriod = new Timestamp JavaDoc(startAvailablePeriod.getTime()+capacity.longValue());
487         if (dateFrom.after(endAvailablePeriod)) return 0;
488         return dateFrom.getTime() - startAvailablePeriod.getTime();
489     }
490     /** Used to move in a TechDataCalenda, produce the Timestamp for the end of the previous day available and its associated capacity.
491      * If the dateFrom (param in) is not in an available TechDataCalendar period, the return value is the previous day available
492      *
493      * @param techDataCalendar The TechDataCalendar cover
494      * @param dateFrom the date
495      * @return a map with Timestamp dateTo, Double previousCapacity
496      */

497     public static Map JavaDoc endPreviousDay(GenericValue techDataCalendar, Timestamp JavaDoc dateFrom) {
498         Map JavaDoc result = new HashMap JavaDoc();
499         Timestamp JavaDoc dateTo = null;
500         GenericValue techDataCalendarWeek = null;
501         // TODO read TechDataCalendarExcWeek to manage exception week (maybe it's needed to refactor the entity definition
502
try{
503             techDataCalendarWeek = techDataCalendar.getRelatedOneCache("TechDataCalendarWeek");
504         } catch (GenericEntityException e) {
505             Debug.logError("Pb reading Calendar Week associated with calendar"+e.getMessage(), module);
506             // return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PbReadingTechDataCalendarWeekAssociated", locale));
507
return ServiceUtil.returnError("Pb reading Calendar Week associated with calendar");
508         }
509         // TODO read TechDataCalendarExcDay to manage execption day
510
Calendar JavaDoc cDateTrav = Calendar.getInstance();
511         cDateTrav.setTime((Date JavaDoc) dateFrom);
512         Map JavaDoc position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
513         Time JavaDoc startTime = (Time JavaDoc) position.get("startTime");
514         int moveDay = ((Integer JavaDoc) position.get("moveDay")).intValue();
515         Double JavaDoc capacity = (Double JavaDoc) position.get("capacity");
516         dateTo = (moveDay == 0) ? dateFrom : UtilDateTime.getDayEnd(dateFrom,moveDay);
517         //TODO after test (01:00:00).getTime() = 0 and not 3600000 so currently we add this value to be correct but it's needed to find why (maybe GMT pb)
518
Timestamp JavaDoc endAvailablePeriod = new Timestamp JavaDoc(UtilDateTime.getDayStart(dateTo).getTime() + startTime.getTime() + 3600000 + capacity.longValue());
519         if (dateTo.after(endAvailablePeriod) ) {
520             dateTo = endAvailablePeriod;
521         }
522         else {
523             dateTo = UtilDateTime.getDayStart(dateTo, -1);
524             cDateTrav.setTime((Date JavaDoc) dateTo);
525             position = dayEndCapacityAvailable(techDataCalendarWeek, cDateTrav.get(Calendar.DAY_OF_WEEK));
526             startTime = (Time JavaDoc) position.get("startTime");
527             moveDay = ((Integer JavaDoc) position.get("moveDay")).intValue();
528             capacity = (Double JavaDoc) position.get("capacity");
529             if (moveDay != 0) dateTo = UtilDateTime.getDayStart(dateTo,moveDay);
530             dateTo.setTime(dateTo.getTime() + startTime.getTime() + 3600000 + capacity.longValue());
531         }
532         result.put("dateTo",dateTo);
533         result.put("previousCapacity",position.get("capacity"));
534         return result;
535     }
536     /** Used to move backward in a TechDataCalenda, start from the dateFrom and move backward only on available period.
537      * If the dateFrom (param in) is not a available TechDataCalendar period, the startDate is the end of the previous day available
538      *
539      * @param techDataCalendar The TechDataCalendar cover
540      * @param dateFrom the start date
541      * @param amount the amount of millisecond to move backward
542      * @return the dateTo
543      */

544     public static Timestamp JavaDoc addBackward(GenericValue techDataCalendar, Timestamp JavaDoc dateFrom, long amount) {
545         Timestamp JavaDoc dateTo = (Timestamp JavaDoc) dateFrom.clone();
546         long previousCapacity = capacityRemainingBackward(techDataCalendar, dateFrom);
547         if (amount <= previousCapacity){
548             dateTo.setTime(dateTo.getTime()-amount);
549             amount = 0;
550         }else amount -= previousCapacity;
551         
552         Map JavaDoc result = new HashMap JavaDoc();
553         while (amount > 0) {
554             result = endPreviousDay(techDataCalendar, dateTo);
555             dateTo = (Timestamp JavaDoc) result.get("dateTo");
556             previousCapacity = ((Double JavaDoc) result.get("previousCapacity")).longValue();
557             if (amount <= previousCapacity){
558                 dateTo.setTime(dateTo.getTime()-amount);
559                 amount = 0;
560             }else amount -= previousCapacity;
561         }
562         return dateTo;
563     }
564 }
565
Popular Tags