KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > calendar > BusinessCalendar


1 package org.jbpm.calendar;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.GregorianCalendar JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Properties JavaDoc;
10
11 import org.jbpm.instantiation.ClassLoaderUtil;
12
13 /**
14  * a calendar that knows about business hours.
15  */

16 public class BusinessCalendar implements Serializable JavaDoc {
17   
18   private static final long serialVersionUID = 1L;
19   
20   Day[] weekDays = null;
21   List JavaDoc holidays = null;
22
23   private static Properties JavaDoc businessCalendarProperties = null;
24   public static Properties JavaDoc getBusinessCalendarProperties() {
25     if (businessCalendarProperties==null) {
26       businessCalendarProperties = ClassLoaderUtil.getProperties("jbpm.business.calendar.properties", "org/jbpm/calendar");
27     }
28     return businessCalendarProperties;
29   }
30
31   public BusinessCalendar() {
32     try {
33       Properties JavaDoc calendarProperties = getBusinessCalendarProperties();
34       weekDays = Day.parseWeekDays(calendarProperties, this);
35       holidays = Holiday.parseHolidays(calendarProperties, this);
36
37     } catch (Exception JavaDoc e) {
38       throw new RuntimeException JavaDoc("couldn't create business calendar", e);
39     }
40   }
41
42   public Date JavaDoc add(Date JavaDoc date, Duration duration) {
43     Date JavaDoc end = null;
44     if (duration.isBusinessTime) {
45       DayPart dayPart = findDayPart(date);
46       boolean isInbusinessHours = (dayPart!=null);
47       if (! isInbusinessHours) {
48         Object JavaDoc[] result = new Object JavaDoc[2];
49         findDay(date).findNextDayPartStart(0, date, result);
50         date = (Date JavaDoc) result[0];
51         dayPart = (DayPart) result[1];
52       }
53       end = dayPart.add(date, duration);
54     } else {
55       end = new Date JavaDoc(date.getTime()+duration.milliseconds);
56     }
57     return end;
58   }
59
60   public DayPart findNextDayPart(Date JavaDoc date) {
61     DayPart nextDayPart = null;
62     while(nextDayPart==null) {
63       nextDayPart = findDayPart(date);
64       if (nextDayPart==null) {
65         date = findStartOfNextDay(date);
66       }
67     }
68     return nextDayPart;
69   }
70
71
72   public Date JavaDoc findStartOfNextDay(Date JavaDoc date) {
73     Calendar JavaDoc calendar = getCalendar();
74     calendar.setTime(date);
75     calendar.add(Calendar.DATE, 1);
76     calendar.set(Calendar.HOUR_OF_DAY, 0);
77     calendar.set(Calendar.MINUTE, 0);
78     calendar.set(Calendar.SECOND, 0);
79     calendar.set(Calendar.MILLISECOND, 0);
80     date = calendar.getTime();
81     while(isHoliday(date)) {
82       calendar.setTime(date);
83       calendar.add(Calendar.DATE, 1);
84       date = calendar.getTime();
85     }
86     return date;
87   }
88
89   public Day findDay(Date JavaDoc date) {
90     Calendar JavaDoc calendar = getCalendar();
91     calendar.setTime(date);
92     return weekDays[calendar.get(Calendar.DAY_OF_WEEK)];
93   }
94
95   public boolean isHoliday(Date JavaDoc date) {
96     Iterator JavaDoc iter = holidays.iterator();
97     while (iter.hasNext()) {
98       Holiday holiday = (Holiday) iter.next();
99       if (holiday.includes(date)) {
100         return true;
101       }
102     }
103     return false;
104   }
105
106   private DayPart findDayPart(Date JavaDoc date) {
107     DayPart dayPart = null;
108     if (! isHoliday(date)) {
109       Day day = findDay(date);
110       for (int i=0; ((i < day.dayParts.length)
111                      && (dayPart==null)); i++) {
112         DayPart candidate = day.dayParts[i];
113         if (candidate.includes(date)) {
114           dayPart = candidate;
115         }
116       }
117     }
118     return dayPart;
119   }
120
121   public boolean isInBusinessHours(Date JavaDoc date) {
122     return (findNextDayPart(date)!=null);
123   }
124
125   public static Calendar JavaDoc getCalendar() {
126     return new GregorianCalendar JavaDoc();
127   }
128 }
129
Popular Tags