1 package org.jbpm.calendar; 2 3 import java.io.Serializable ; 4 import java.util.Calendar ; 5 import java.util.Date ; 6 import java.util.GregorianCalendar ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Properties ; 10 11 import org.jbpm.instantiation.ClassLoaderUtil; 12 13 16 public class BusinessCalendar implements Serializable { 17 18 private static final long serialVersionUID = 1L; 19 20 Day[] weekDays = null; 21 List holidays = null; 22 23 private static Properties businessCalendarProperties = null; 24 public static Properties 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 calendarProperties = getBusinessCalendarProperties(); 34 weekDays = Day.parseWeekDays(calendarProperties, this); 35 holidays = Holiday.parseHolidays(calendarProperties, this); 36 37 } catch (Exception e) { 38 throw new RuntimeException ("couldn't create business calendar", e); 39 } 40 } 41 42 public Date add(Date date, Duration duration) { 43 Date end = null; 44 if (duration.isBusinessTime) { 45 DayPart dayPart = findDayPart(date); 46 boolean isInbusinessHours = (dayPart!=null); 47 if (! isInbusinessHours) { 48 Object [] result = new Object [2]; 49 findDay(date).findNextDayPartStart(0, date, result); 50 date = (Date ) result[0]; 51 dayPart = (DayPart) result[1]; 52 } 53 end = dayPart.add(date, duration); 54 } else { 55 end = new Date (date.getTime()+duration.milliseconds); 56 } 57 return end; 58 } 59 60 public DayPart findNextDayPart(Date 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 findStartOfNextDay(Date date) { 73 Calendar 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 date) { 90 Calendar calendar = getCalendar(); 91 calendar.setTime(date); 92 return weekDays[calendar.get(Calendar.DAY_OF_WEEK)]; 93 } 94 95 public boolean isHoliday(Date date) { 96 Iterator 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 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 date) { 122 return (findNextDayPart(date)!=null); 123 } 124 125 public static Calendar getCalendar() { 126 return new GregorianCalendar (); 127 } 128 } 129 | Popular Tags |