001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.calendar;
023:
024: import java.io.Serializable;
025: import java.util.Calendar;
026: import java.util.Date;
027: import java.util.GregorianCalendar;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Properties;
031:
032: import org.jbpm.JbpmConfiguration;
033: import org.jbpm.JbpmException;
034: import org.jbpm.util.ClassLoaderUtil;
035:
036: /**
037: * a calendar that knows about business hours.
038: */
039: public class BusinessCalendar implements Serializable {
040:
041: private static final long serialVersionUID = 1L;
042: static Properties businessCalendarProperties = null;
043:
044: Day[] weekDays = null;
045: List holidays = null;
046:
047: public static synchronized Properties getBusinessCalendarProperties() {
048: if (businessCalendarProperties == null) {
049: String resource = JbpmConfiguration.Configs
050: .getString("resource.business.calendar");
051: businessCalendarProperties = ClassLoaderUtil
052: .getProperties(resource);
053: }
054: return businessCalendarProperties;
055: }
056:
057: public BusinessCalendar() {
058: this (getBusinessCalendarProperties());
059: }
060:
061: public BusinessCalendar(Properties calendarProperties) {
062: try {
063: weekDays = Day.parseWeekDays(calendarProperties, this );
064: holidays = Holiday.parseHolidays(calendarProperties, this );
065:
066: } catch (Exception e) {
067: throw new JbpmException(
068: "couldn't create business calendar", e);
069: }
070: }
071:
072: public Date add(Date date, Duration duration) {
073: Date end = null;
074: if (duration.isBusinessTime) {
075: DayPart dayPart = findDayPart(date);
076: boolean isInbusinessHours = (dayPart != null);
077: if (!isInbusinessHours) {
078: Object[] result = new Object[2];
079: findDay(date).findNextDayPartStart(0, date, result);
080: date = (Date) result[0];
081: dayPart = (DayPart) result[1];
082: }
083: end = dayPart.add(date, duration);
084: } else {
085: end = new Date(date.getTime() + duration.milliseconds);
086: }
087: return end;
088: }
089:
090: public Date findStartOfNextDay(Date date) {
091: Calendar calendar = getCalendar();
092: calendar.setTime(date);
093: calendar.add(Calendar.DATE, 1);
094: calendar.set(Calendar.HOUR_OF_DAY, 0);
095: calendar.set(Calendar.MINUTE, 0);
096: calendar.set(Calendar.SECOND, 0);
097: calendar.set(Calendar.MILLISECOND, 0);
098: date = calendar.getTime();
099: while (isHoliday(date)) {
100: calendar.setTime(date);
101: calendar.add(Calendar.DATE, 1);
102: date = calendar.getTime();
103: }
104: return date;
105: }
106:
107: public Day findDay(Date date) {
108: Calendar calendar = getCalendar();
109: calendar.setTime(date);
110: return weekDays[calendar.get(Calendar.DAY_OF_WEEK)];
111: }
112:
113: public boolean isHoliday(Date date) {
114: Iterator iter = holidays.iterator();
115: while (iter.hasNext()) {
116: Holiday holiday = (Holiday) iter.next();
117: if (holiday.includes(date)) {
118: return true;
119: }
120: }
121: return false;
122: }
123:
124: DayPart findDayPart(Date date) {
125: DayPart dayPart = null;
126: if (!isHoliday(date)) {
127: Day day = findDay(date);
128: for (int i = 0; ((i < day.dayParts.length) && (dayPart == null)); i++) {
129: DayPart candidate = day.dayParts[i];
130: if (candidate.includes(date)) {
131: dayPart = candidate;
132: }
133: }
134: }
135: return dayPart;
136: }
137:
138: public DayPart findNextDayPart(Date date) {
139: DayPart nextDayPart = null;
140: while (nextDayPart == null) {
141: nextDayPart = findDayPart(date);
142: if (nextDayPart == null) {
143: date = findStartOfNextDay(date);
144: Object result[] = new Object[2];
145: Day day = findDay(date);
146: day.findNextDayPartStart(0, date, result);
147: nextDayPart = (DayPart) result[1];
148: }
149: }
150: return nextDayPart;
151: }
152:
153: public boolean isInBusinessHours(Date date) {
154: return (findDayPart(date) != null);
155: }
156:
157: public static Calendar getCalendar() {
158: return new GregorianCalendar();
159: }
160: }
|