001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/calendar/tags/sakai_2-4-1/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/ExclusionRecurrenceRule.java $
003: * $Id: ExclusionRecurrenceRule.java 8050 2006-04-20 17:39:55Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.calendar.impl;
021:
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Stack;
025: import java.util.TimeZone;
026: import java.util.Vector;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.calendar.api.RecurrenceRule;
031: import org.sakaiproject.time.api.Time;
032: import org.sakaiproject.time.api.TimeRange;
033: import org.sakaiproject.time.cover.TimeService;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: /**
040: * <p>ExclusionRecurrenceRule is a rule which excludes specific time ranges from a list of instances.</p>
041: */
042: public class ExclusionRecurrenceRule implements RecurrenceRule {
043: /** Our logger. */
044: private static Log M_log = LogFactory
045: .getLog(ExclusionRecurrenceRule.class);
046:
047: /** The list of TimeRange values to exclude. */
048: protected List m_ranges = null;
049:
050: /**
051: * Construct.
052: */
053: public ExclusionRecurrenceRule() {
054: m_ranges = new Vector();
055:
056: } // ExclusionRecurrenceRule
057:
058: /**
059: * Construct with these limits.
060: * @param ranges The list of ranges to exclude
061: */
062: public ExclusionRecurrenceRule(List ranges) {
063: m_ranges = new Vector(ranges);
064:
065: } // ExclusionRecurrenceRule
066:
067: /**
068: * Access the List of TimeRange values excluded.
069: * @return the List of TimeRange values excluded.
070: */
071: public List getRanges() {
072: return m_ranges;
073: }
074:
075: /**
076: * Take values from this xml element
077: * @param el The xml element.
078: */
079: public void set(Element el) {
080: // the children (time ranges)
081: NodeList children = el.getChildNodes();
082: final int length = children.getLength();
083: for (int i = 0; i < length; i++) {
084: Node child = children.item(i);
085: if (child.getNodeType() != Node.ELEMENT_NODE)
086: continue;
087: Element element = (Element) child;
088:
089: // look for a time range
090: if (element.getTagName().equals("range")) {
091: try {
092: m_ranges.add(TimeService.newTimeRange(element
093: .getAttribute("range")));
094: } catch (Exception e) {
095: M_log.warn("set: while reading time range: " + e);
096: }
097: }
098: }
099:
100: } // set
101:
102: /**
103: * Serialize the resource into XML, adding an element to the doc under the top of the stack element.
104: * @param doc The DOM doc to contain the XML (or null for a string return).
105: * @param stack The DOM elements, the top of which is the containing element of the new "resource" element.
106: * @return The newly added element.
107: */
108: public Element toXml(Document doc, Stack stack) {
109: // add the "rule" element to the stack'ed element
110: Element rule = doc.createElement("ex-rule");
111: ((Element) stack.peek()).appendChild(rule);
112:
113: // set the class name - old style for CHEF 1.2.10 compatibility
114: rule
115: .setAttribute("class",
116: "org.chefproject.osid.calendar.ExclusionRecurrenceRule");
117:
118: // set the rule class name w/o package, for modern usage
119: rule.setAttribute("name", "ExclusionRecurrenceRule");
120:
121: // set the ranges
122: for (Iterator iRanges = m_ranges.iterator(); iRanges.hasNext();) {
123: TimeRange range = (TimeRange) iRanges.next();
124:
125: Element rangeElement = doc.createElement("range");
126: rule.appendChild(rangeElement);
127: rangeElement.setAttribute("range", range.toString());
128: }
129:
130: return rule;
131:
132: } // toXml
133:
134: /**
135: * Return a List of all RecurrenceInstance objects generated by this rule within the given time range, based on the
136: * prototype first range, in time order.
137: * @param prototype The prototype first TimeRange.
138: * @param range A time range to limit the generated ranges.
139: * @return a List of RecurrenceInstance generated by this rule in this range.
140: */
141: public List generateInstances(TimeRange prototype, TimeRange range,
142: TimeZone timeZone) {
143: return new Vector();
144:
145: } // generateInstances
146:
147: /**
148: * Remove from the ranges list any RecurrenceInstance excluded by this rule.
149: * @param ranges The list (RecurrenceInstance) of ranges.
150: */
151: public void excludeInstances(List ranges) {
152: Vector rv = new Vector();
153:
154: for (Iterator iInstances = ranges.iterator(); iInstances
155: .hasNext();) {
156: RecurrenceInstance ri = (RecurrenceInstance) iInstances
157: .next();
158:
159: if (!m_ranges.contains(ri.getRange())) {
160: rv.add(ri);
161: }
162: }
163:
164: ranges.clear();
165: ranges.addAll(rv);
166:
167: }
168:
169: /**
170: * {@inheritDoc}
171: */
172: public String getFrequencyDescription() {
173: return "x";
174: }
175:
176: /**
177: * {@inheritDoc}
178: */
179: public Time getUntil() {
180: return null;
181: }
182:
183: /**
184: * {@inheritDoc}
185: */
186: public int getCount() {
187: return 0;
188: }
189:
190: /**
191: * {@inheritDoc}
192: */
193: public int getInterval() {
194: return 0;
195: }
196:
197: } // ExclusionRecurrenceRule
|