001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: HolidaySchedule.java 6895 2007-04-18 15:31:19Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.scheduler;
025:
026: import java.text.ParseException;
027: import java.text.SimpleDateFormat;
028: import java.util.Date;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Vector;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.jdom.Element;
036: import org.quartz.impl.calendar.HolidayCalendar;
037:
038: public class HolidaySchedule extends ConfigFileSupport {
039:
040: protected String SCHED_NAMESPACE = "http://cbesb.bostechcorp.com/scheduler/1.0";
041: protected String HOLIDAY_SCHEDULE = "holidaySchedule";
042: protected String HOLIDAY = "holiday";
043: protected String DATE = "date";
044: protected String DESCRIPTION = "description";
045:
046: private Log log;
047: private Vector<Holiday> holidays;
048:
049: /**
050: *
051: */
052: public HolidaySchedule() {
053: super ();
054: log = LogFactory.getLog(HolidaySchedule.class);
055: setNamespaceURI(SCHED_NAMESPACE);
056: setRootName(HOLIDAY_SCHEDULE);
057: holidays = new Vector<Holiday>();
058: }
059:
060: public List<Holiday> getHolidays() {
061: return holidays;
062: }
063:
064: public void addHoliday(Holiday holiday) {
065: holidays.add(holiday);
066: }
067:
068: public HolidayCalendar toQuartzHolidayCalendar() {
069: HolidayCalendar cal = new HolidayCalendar();
070: for (Iterator iter = holidays.iterator(); iter.hasNext();) {
071: Holiday holiday = (Holiday) iter.next();
072: cal.addExcludedDate(holiday.getDate());
073: }
074: return cal;
075: }
076:
077: /* (non-Javadoc)
078: * @see com.bostechcorp.cbesb.runtime.scheduler.ConfigFileSupport#buildDoc()
079: */
080: @Override
081: protected boolean buildDoc() {
082: createNewDoc();
083: Element rootElem = this .getRootElement();
084: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
085: for (Iterator iter = holidays.iterator(); iter.hasNext();) {
086: Holiday holiday = (Holiday) iter.next();
087: String dateStr = dateFormat.format(holiday.getDate());
088: Element holidayElem = createElement(HOLIDAY);
089: Element dateElem = createElement(DATE);
090: dateElem.setText(dateStr);
091: holidayElem.addContent(dateElem);
092: if (holiday.getDescription() != null
093: && holiday.getDescription().length() > 0) {
094: Element descElem = createElement(DESCRIPTION);
095: descElem.setText(holiday.getDescription());
096: holidayElem.addContent(descElem);
097: }
098: rootElem.addContent(holidayElem);
099: }
100:
101: return true;
102: }
103:
104: /* (non-Javadoc)
105: * @see com.bostechcorp.cbesb.runtime.scheduler.ConfigFileSupport#processParsedDoc()
106: */
107: @Override
108: protected boolean processParsedDoc() {
109: Element rootElem = getRootElement();
110: List children = rootElem.getChildren();
111: for (Iterator iter = children.iterator(); iter.hasNext();) {
112: Element child = (Element) iter.next();
113: if (child.getName().equals(HOLIDAY)) {
114: if (!processHolidayElement(child)) {
115: return false;
116: }
117: } else {
118: log.error("Unexpected element: " + child.getName());
119: return false;
120: }
121: }
122: return true;
123: }
124:
125: private boolean processHolidayElement(Element holidayElem) {
126: Date holDate = null;
127: String desc = null;
128:
129: Element dateElem = getChild(holidayElem, DATE);
130: if (dateElem != null) {
131: String dateValue = dateElem.getText();
132: SimpleDateFormat dateFormat = new SimpleDateFormat(
133: "yyyy-MM-dd");
134: try {
135: holDate = dateFormat.parse(dateValue);
136: } catch (ParseException e) {
137: log.error("Exception occurred while parsing date:", e);
138: return false;
139: }
140: } else {
141: log.error("Holiday element missing required date element");
142: return false;
143: }
144: Element descElem = getChild(holidayElem, DESCRIPTION);
145: if (descElem != null) {
146: desc = descElem.getText();
147: }
148:
149: Holiday holiday = new Holiday(holDate, desc);
150: holidays.add(holiday);
151:
152: return true;
153: }
154: }
|