001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/calendar/tags/sakai_2-4-1/calendar-impl/impl/src/java/org/sakaiproject/calendar/impl/ExclusionSeqRecurrenceRule.java $
003: * $Id: ExclusionSeqRecurrenceRule.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.w3c.dom.Document;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.NodeList;
037:
038: /**
039: * <p>ExclusionSeqRecurrenceRule is a rule which excludes specific sequence numbers from a list of instances.</p>
040: */
041: public class ExclusionSeqRecurrenceRule implements RecurrenceRule {
042: /** Our logger. */
043: private static Log M_log = LogFactory
044: .getLog(ExclusionSeqRecurrenceRule.class);
045:
046: /** The list of sequence number (Integer) values to exclude. */
047: protected List m_exclusions = null;
048:
049: /**
050: * Construct.
051: */
052: public ExclusionSeqRecurrenceRule() {
053: m_exclusions = new Vector();
054:
055: } // ExclusionSeqRecurrenceRule
056:
057: /**
058: * Construct with these limits.
059: * @param ranges The list of ranges to exclude
060: */
061: public ExclusionSeqRecurrenceRule(List ranges) {
062: m_exclusions = new Vector(ranges);
063:
064: } // ExclusionSeqRecurrenceRule
065:
066: /**
067: * Access the List of Integer sequence values excluded.
068: * @return the List of Integer sequence values excluded.
069: */
070: public List getExclusions() {
071: return m_exclusions;
072: }
073:
074: /**
075: * Take values from this xml element
076: * @param el The xml element.
077: */
078: public void set(Element el) {
079: // the children (time ranges)
080: NodeList children = el.getChildNodes();
081: final int length = children.getLength();
082: for (int i = 0; i < length; i++) {
083: Node child = children.item(i);
084: if (child.getNodeType() != Node.ELEMENT_NODE)
085: continue;
086: Element element = (Element) child;
087:
088: // look for a time range
089: if (element.getTagName().equals("exclude")) {
090: try {
091: m_exclusions.add(new Integer(element
092: .getAttribute("sequence")));
093: } catch (Exception e) {
094: M_log.warn("set: while reading exclude sequence: "
095: + 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.ExclusionSeqRecurrenceRule");
117:
118: // set the rule class name w/o package, for modern usage
119: rule.setAttribute("name", "ExclusionSeqRecurrenceRule");
120:
121: // set the ranges
122: for (Iterator iSeq = m_exclusions.iterator(); iSeq.hasNext();) {
123: Integer seq = (Integer) iSeq.next();
124:
125: Element exElement = doc.createElement("exclude");
126: rule.appendChild(exElement);
127: exElement.setAttribute("sequence", seq.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_exclusions.contains(ri.getSequence())) {
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 "xx";
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: } // ExclusionSeqRecurrenceRule
|