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.text.DateFormat;
026: import java.text.SimpleDateFormat;
027: import java.util.ArrayList;
028: import java.util.Calendar;
029: import java.util.Date;
030: import java.util.List;
031: import java.util.Properties;
032: import java.util.StringTokenizer;
033:
034: /**
035: * is a day on a business calendar.
036: */
037: public class Day implements Serializable {
038:
039: private static final long serialVersionUID = 1L;
040:
041: DayPart[] dayParts = null;
042: BusinessCalendar businessCalendar = null;
043:
044: public static Day[] parseWeekDays(Properties calendarProperties,
045: BusinessCalendar businessCalendar) {
046: DateFormat dateFormat = new SimpleDateFormat(calendarProperties
047: .getProperty("hour.format"));
048: Day[] weekDays = new Day[8];
049: weekDays[Calendar.MONDAY] = new Day(calendarProperties
050: .getProperty("weekday.monday"), dateFormat,
051: businessCalendar);
052: weekDays[Calendar.TUESDAY] = new Day(calendarProperties
053: .getProperty("weekday.tuesday"), dateFormat,
054: businessCalendar);
055: weekDays[Calendar.WEDNESDAY] = new Day(calendarProperties
056: .getProperty("weekday.wednesday"), dateFormat,
057: businessCalendar);
058: weekDays[Calendar.THURSDAY] = new Day(calendarProperties
059: .getProperty("weekday.thursday"), dateFormat,
060: businessCalendar);
061: weekDays[Calendar.FRIDAY] = new Day(calendarProperties
062: .getProperty("weekday.friday"), dateFormat,
063: businessCalendar);
064: weekDays[Calendar.SATURDAY] = new Day(calendarProperties
065: .getProperty("weekday.saturday"), dateFormat,
066: businessCalendar);
067: weekDays[Calendar.SUNDAY] = new Day(calendarProperties
068: .getProperty("weekday.sunday"), dateFormat,
069: businessCalendar);
070: return weekDays;
071: }
072:
073: public Day(String dayPartsText, DateFormat dateFormat,
074: BusinessCalendar businessCalendar) {
075: this .businessCalendar = businessCalendar;
076:
077: List dayPartsList = new ArrayList();
078: StringTokenizer tokenizer = new StringTokenizer(dayPartsText,
079: "&");
080: while (tokenizer.hasMoreTokens()) {
081: String dayPartText = tokenizer.nextToken().trim();
082: dayPartsList.add(new DayPart(dayPartText, dateFormat, this ,
083: dayPartsList.size()));
084: }
085:
086: dayParts = (DayPart[]) dayPartsList
087: .toArray(new DayPart[dayPartsList.size()]);
088: }
089:
090: public void findNextDayPartStart(int dayPartIndex, Date date,
091: Object[] result) {
092: // if there is a day part in this day that starts after the given date
093: if (dayPartIndex < dayParts.length) {
094: if (dayParts[dayPartIndex].isStartAfter(date)) {
095: result[0] = dayParts[dayPartIndex].getStartTime(date);
096: result[1] = dayParts[dayPartIndex];
097: } else {
098: findNextDayPartStart(dayPartIndex + 1, date, result);
099: }
100: } else {
101: // descend recustively
102: date = businessCalendar.findStartOfNextDay(date);
103: Day nextDay = businessCalendar.findDay(date);
104: nextDay.findNextDayPartStart(0, date, result);
105: }
106: }
107: }
|