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.ParseException;
027: import java.text.SimpleDateFormat;
028: import java.util.ArrayList;
029: import java.util.Calendar;
030: import java.util.Date;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Properties;
034:
035: import org.jbpm.JbpmException;
036:
037: /**
038: * identifies a continuous set of days.
039: */
040: public class Holiday implements Serializable {
041:
042: private static final long serialVersionUID = 1L;
043:
044: Date fromDay = null;
045: Date toDay = null;
046: BusinessCalendar businessCalendar = null;
047:
048: public static List parseHolidays(Properties calendarProperties,
049: BusinessCalendar businessCalendar) {
050: List holidays = new ArrayList();
051:
052: DateFormat dateFormat = new SimpleDateFormat(calendarProperties
053: .getProperty("day.format"));
054: Iterator iter = calendarProperties.keySet().iterator();
055: while (iter.hasNext()) {
056: String key = (String) iter.next();
057: if (key.startsWith("holiday")) {
058: Holiday holiday = new Holiday(calendarProperties
059: .getProperty(key), dateFormat, businessCalendar);
060: holidays.add(holiday);
061: }
062: }
063:
064: return holidays;
065: }
066:
067: public Holiday(String holidayText, DateFormat dateFormat,
068: BusinessCalendar businessCalendar) {
069: this .businessCalendar = businessCalendar;
070: try {
071: int separatorIndex = holidayText.indexOf('-');
072: if (separatorIndex == -1) {
073: fromDay = dateFormat.parse(holidayText.trim());
074: toDay = fromDay;
075: } else {
076: String fromText = holidayText.substring(0,
077: separatorIndex).trim();
078: String toText = holidayText.substring(
079: separatorIndex + 1).trim();
080: fromDay = dateFormat.parse(fromText);
081: toDay = dateFormat.parse(toText);
082: }
083: // now we are going to set the toDay to the end of the day, rather then the beginning.
084: // we take the start of the next day as the end of the toDay.
085: Calendar calendar = BusinessCalendar.getCalendar();
086: calendar.setTime(toDay);
087: calendar.add(Calendar.DATE, 1);
088: toDay = calendar.getTime();
089:
090: } catch (ParseException e) {
091: throw new JbpmException("couldn't parse holiday '"
092: + holidayText + "'", e);
093: }
094: }
095:
096: public boolean includes(Date date) {
097: return ((fromDay.getTime() <= date.getTime()) && (date
098: .getTime() < toDay.getTime()));
099: }
100: }
|