michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.model.component; michael@0: michael@0: import java.util.HashMap; michael@0: import java.util.Iterator; michael@0: import java.util.Map; michael@0: michael@0: import net.fortuna.ical4j.model.Component; michael@0: import net.fortuna.ical4j.model.ComponentList; michael@0: import net.fortuna.ical4j.model.DateRange; michael@0: import net.fortuna.ical4j.model.DateTime; michael@0: import net.fortuna.ical4j.model.Dur; michael@0: import net.fortuna.ical4j.model.Period; michael@0: import net.fortuna.ical4j.model.PeriodList; michael@0: import net.fortuna.ical4j.model.Property; michael@0: import net.fortuna.ical4j.model.PropertyList; michael@0: import net.fortuna.ical4j.model.ValidationException; michael@0: import net.fortuna.ical4j.model.Validator; michael@0: import net.fortuna.ical4j.model.parameter.FbType; michael@0: import net.fortuna.ical4j.model.property.Contact; michael@0: import net.fortuna.ical4j.model.property.DtEnd; michael@0: import net.fortuna.ical4j.model.property.DtStamp; michael@0: import net.fortuna.ical4j.model.property.DtStart; michael@0: import net.fortuna.ical4j.model.property.Duration; michael@0: import net.fortuna.ical4j.model.property.FreeBusy; michael@0: import net.fortuna.ical4j.model.property.Method; michael@0: import net.fortuna.ical4j.model.property.Organizer; michael@0: import net.fortuna.ical4j.model.property.Uid; michael@0: import net.fortuna.ical4j.model.property.Url; michael@0: import net.fortuna.ical4j.util.CompatibilityHints; michael@0: import net.fortuna.ical4j.util.PropertyValidator; michael@0: michael@0: /** michael@0: * $Id$ [Apr 5, 2004] michael@0: * michael@0: * Defines an iCalendar VFREEBUSY component. michael@0: * michael@0: *
michael@0:  *  4.6.4 Free/Busy Component
michael@0:  *
michael@0:  *     Component Name: VFREEBUSY
michael@0:  *
michael@0:  *     Purpose: Provide a grouping of component properties that describe
michael@0:  *     either a request for free/busy time, describe a response to a request
michael@0:  *     for free/busy time or describe a published set of busy time.
michael@0:  *
michael@0:  *     Formal Definition: A "VFREEBUSY" calendar component is defined by the
michael@0:  *     following notation:
michael@0:  *
michael@0:  *       freebusyc  = "BEGIN" ":" "VFREEBUSY" CRLF
michael@0:  *                    fbprop
michael@0:  *                    "END" ":" "VFREEBUSY" CRLF
michael@0:  *
michael@0:  *       fbprop     = *(
michael@0:  *
michael@0:  *                  ; the following are optional,
michael@0:  *                  ; but MUST NOT occur more than once
michael@0:  *
michael@0:  *                  contact / dtstart / dtend / duration / dtstamp /
michael@0:  *                  organizer / uid / url /
michael@0:  *
michael@0:  *                  ; the following are optional,
michael@0:  *                  ; and MAY occur more than once
michael@0:  *
michael@0:  *                  attendee / comment / freebusy / rstatus / x-prop
michael@0:  *
michael@0:  *                  )
michael@0:  *
michael@0:  *     Description: A "VFREEBUSY" calendar component is a grouping of
michael@0:  *     component properties that represents either a request for, a reply to
michael@0:  *     a request for free or busy time information or a published set of
michael@0:  *     busy time information.
michael@0:  *
michael@0:  *     When used to request free/busy time information, the "ATTENDEE"
michael@0:  *     property specifies the calendar users whose free/busy time is being
michael@0:  *     requested; the "ORGANIZER" property specifies the calendar user who
michael@0:  *     is requesting the free/busy time; the "DTSTART" and "DTEND"
michael@0:  *     properties specify the window of time for which the free/busy time is
michael@0:  *     being requested; the "UID" and "DTSTAMP" properties are specified to
michael@0:  *     assist in proper sequencing of multiple free/busy time requests.
michael@0:  *
michael@0:  *     When used to reply to a request for free/busy time, the "ATTENDEE"
michael@0:  *     property specifies the calendar user responding to the free/busy time
michael@0:  *     request; the "ORGANIZER" property specifies the calendar user that
michael@0:  *     originally requested the free/busy time; the "FREEBUSY" property
michael@0:  *     specifies the free/busy time information (if it exists); and the
michael@0:  *     "UID" and "DTSTAMP" properties are specified to assist in proper
michael@0:  *     sequencing of multiple free/busy time replies.
michael@0:  *
michael@0:  *     When used to publish busy time, the "ORGANIZER" property specifies
michael@0:  *     the calendar user associated with the published busy time; the
michael@0:  *     "DTSTART" and "DTEND" properties specify an inclusive time window
michael@0:  *     that surrounds the busy time information; the "FREEBUSY" property
michael@0:  *     specifies the published busy time information; and the "DTSTAMP"
michael@0:  *     property specifies the date/time that iCalendar object was created.
michael@0:  *
michael@0:  *     The "VFREEBUSY" calendar component cannot be nested within another
michael@0:  *     calendar component. Multiple "VFREEBUSY" calendar components can be
michael@0:  *     specified within an iCalendar object. This permits the grouping of
michael@0:  *     Free/Busy information into logical collections, such as monthly
michael@0:  *     groups of busy time information.
michael@0:  *
michael@0:  *     The "VFREEBUSY" calendar component is intended for use in iCalendar
michael@0:  *     object methods involving requests for free time, requests for busy
michael@0:  *     time, requests for both free and busy, and the associated replies.
michael@0:  *
michael@0:  *     Free/Busy information is represented with the "FREEBUSY" property.
michael@0:  *     This property provides a terse representation of time periods. One or
michael@0:  *     more "FREEBUSY" properties can be specified in the "VFREEBUSY"
michael@0:  *     calendar component.
michael@0:  *
michael@0:  *     When present in a "VFREEBUSY" calendar component, the "DTSTART" and
michael@0:  *     "DTEND" properties SHOULD be specified prior to any "FREEBUSY"
michael@0:  *     properties. In a free time request, these properties can be used in
michael@0:  *     combination with the "DURATION" property to represent a request for a
michael@0:  *     duration of free time within a specified window of time.
michael@0:  *
michael@0:  *     The recurrence properties ("RRULE", "EXRULE", "RDATE", "EXDATE") are
michael@0:  *     not permitted within a "VFREEBUSY" calendar component. Any recurring
michael@0:  *     events are resolved into their individual busy time periods using the
michael@0:  *     "FREEBUSY" property.
michael@0:  *
michael@0:  *     Example: The following is an example of a "VFREEBUSY" calendar
michael@0:  *     component used to request free or busy time information:
michael@0:  *
michael@0:  *       BEGIN:VFREEBUSY
michael@0:  *       ORGANIZER:MAILTO:jane_doe@host1.com
michael@0:  *       ATTENDEE:MAILTO:john_public@host2.com
michael@0:  *       DTSTART:19971015T050000Z
michael@0:  *       DTEND:19971016T050000Z
michael@0:  *       DTSTAMP:19970901T083000Z
michael@0:  *       END:VFREEBUSY
michael@0:  *
michael@0:  *     The following is an example of a "VFREEBUSY" calendar component used
michael@0:  *     to reply to the request with busy time information:
michael@0:  *
michael@0:  *       BEGIN:VFREEBUSY
michael@0:  *       ORGANIZER:MAILTO:jane_doe@host1.com
michael@0:  *       ATTENDEE:MAILTO:john_public@host2.com
michael@0:  *       DTSTAMP:19970901T100000Z
michael@0:  *       FREEBUSY;VALUE=PERIOD:19971015T050000Z/PT8H30M,
michael@0:  *        19971015T160000Z/PT5H30M,19971015T223000Z/PT6H30M
michael@0:  *       URL:http://host2.com/pub/busy/jpublic-01.ifb
michael@0:  *       COMMENT:This iCalendar file contains busy time information for
michael@0:  *         the next three months.
michael@0:  *       END:VFREEBUSY
michael@0:  *
michael@0:  *     The following is an example of a "VFREEBUSY" calendar component used
michael@0:  *     to publish busy time information.
michael@0:  *
michael@0:  *       BEGIN:VFREEBUSY
michael@0:  *       ORGANIZER:jsmith@host.com
michael@0:  *       DTSTART:19980313T141711Z
michael@0:  *       DTEND:19980410T141711Z
michael@0:  *       FREEBUSY:19980314T233000Z/19980315T003000Z
michael@0:  *       FREEBUSY:19980316T153000Z/19980316T163000Z
michael@0:  *       FREEBUSY:19980318T030000Z/19980318T040000Z
michael@0:  *       URL:http://www.host.com/calendar/busytime/jsmith.ifb
michael@0:  *       END:VFREEBUSY
michael@0:  * 
michael@0: * michael@0: * Example 1 - Requesting all busy time slots for a given period: michael@0: * michael@0: *

michael@0:  * // request all busy times between today and 1 week from now..
michael@0:  * DateTime start = new DateTime();
michael@0:  * DateTime end = new DateTime(start.getTime() + 1000 * 60 * 60 * 24 * 7);
michael@0:  *
michael@0:  * VFreeBusy request = new VFreeBusy(start, end);
michael@0:  *
michael@0:  * VFreeBusy reply = new VFreeBusy(request, calendar.getComponents());
michael@0:  * 
michael@0: * michael@0: * Example 2 - Requesting all free time slots for a given period of at least the specified duration: michael@0: * michael@0: *

michael@0:  * // request all free time between today and 1 week from now of
michael@0:  * // duration 2 hours or more..
michael@0:  * DateTime start = new DateTime();
michael@0:  * DateTime end = new DateTime(start.getTime() + 1000 * 60 * 60 * 24 * 7);
michael@0:  *
michael@0:  * VFreeBusy request = new VFreeBusy(start, end, new Dur(0, 2, 0, 0));
michael@0:  *
michael@0:  * VFreeBusy response = new VFreeBusy(request, myCalendar.getComponents());
michael@0:  * 
michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class VFreeBusy extends CalendarComponent { michael@0: michael@0: private static final long serialVersionUID = 1046534053331139832L; michael@0: michael@0: private final Map methodValidators = new HashMap(); michael@0: { michael@0: methodValidators.put(Method.PUBLISH, new PublishValidator()); michael@0: methodValidators.put(Method.REPLY, new ReplyValidator()); michael@0: methodValidators.put(Method.REQUEST, new RequestValidator()); michael@0: } michael@0: michael@0: /** michael@0: * Default constructor. michael@0: */ michael@0: public VFreeBusy() { michael@0: super(VFREEBUSY); michael@0: getProperties().add(new DtStamp()); michael@0: } michael@0: michael@0: /** michael@0: * Constructor. michael@0: * @param properties a list of properties michael@0: */ michael@0: public VFreeBusy(final PropertyList properties) { michael@0: super(VFREEBUSY, properties); michael@0: } michael@0: michael@0: /** michael@0: * Constructs a new VFreeBusy instance with the specified start and end boundaries. This constructor should be used michael@0: * for requesting busy time for a specified period. michael@0: * @param start the starting boundary for the VFreeBusy michael@0: * @param end the ending boundary for the VFreeBusy michael@0: */ michael@0: public VFreeBusy(final DateTime start, final DateTime end) { michael@0: this(); michael@0: michael@0: // 4.8.2.4 Date/Time Start: michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // start date and time for the free or busy time information. The time michael@0: // MUST be specified in UTC time. michael@0: getProperties().add(new DtStart(start, true)); michael@0: michael@0: // 4.8.2.2 Date/Time End michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // end date and time for the free or busy time information. The time michael@0: // MUST be specified in the UTC time format. The value MUST be later in michael@0: // time than the value of the "DTSTART" property. michael@0: getProperties().add(new DtEnd(end, true)); michael@0: } michael@0: michael@0: /** michael@0: * Constructs a new VFreeBusy instance with the specified start and end boundaries. This constructor should be used michael@0: * for requesting free time for a specified duration in given period defined by the start date and end date. michael@0: * @param start the starting boundary for the VFreeBusy michael@0: * @param end the ending boundary for the VFreeBusy michael@0: * @param duration the length of the period being requested michael@0: */ michael@0: public VFreeBusy(final DateTime start, final DateTime end, final Dur duration) { michael@0: this(); michael@0: michael@0: // 4.8.2.4 Date/Time Start: michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // start date and time for the free or busy time information. The time michael@0: // MUST be specified in UTC time. michael@0: getProperties().add(new DtStart(start, true)); michael@0: michael@0: // 4.8.2.2 Date/Time End michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // end date and time for the free or busy time information. The time michael@0: // MUST be specified in the UTC time format. The value MUST be later in michael@0: // time than the value of the "DTSTART" property. michael@0: getProperties().add(new DtEnd(end, true)); michael@0: michael@0: getProperties().add(new Duration(duration)); michael@0: } michael@0: michael@0: /** michael@0: * Constructs a new VFreeBusy instance representing a reply to the specified VFREEBUSY request according to the michael@0: * specified list of components. michael@0: * If the request argument has its duration set, then the result michael@0: * represents a list of free times (that is, parameter FBTYPE michael@0: * is set to FbType.FREE). michael@0: * If the request argument does not have its duration set, then the result michael@0: * represents a list of busy times. michael@0: * @param request a VFREEBUSY request michael@0: * @param components a component list used to initialise busy time michael@0: * @throws ValidationException michael@0: */ michael@0: public VFreeBusy(final VFreeBusy request, final ComponentList components) { michael@0: this(); michael@0: michael@0: final DtStart start = (DtStart) request.getProperty(Property.DTSTART); michael@0: michael@0: final DtEnd end = (DtEnd) request.getProperty(Property.DTEND); michael@0: michael@0: final Duration duration = (Duration) request.getProperty(Property.DURATION); michael@0: michael@0: // 4.8.2.4 Date/Time Start: michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // start date and time for the free or busy time information. The time michael@0: // MUST be specified in UTC time. michael@0: getProperties().add(new DtStart(start.getDate(), true)); michael@0: michael@0: // 4.8.2.2 Date/Time End michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // end date and time for the free or busy time information. The time michael@0: // MUST be specified in the UTC time format. The value MUST be later in michael@0: // time than the value of the "DTSTART" property. michael@0: getProperties().add(new DtEnd(end.getDate(), true)); michael@0: michael@0: if (duration != null) { michael@0: getProperties().add(new Duration(duration.getDuration())); michael@0: // Initialise with all free time of at least the specified duration.. michael@0: final DateTime freeStart = new DateTime(start.getDate()); michael@0: final DateTime freeEnd = new DateTime(end.getDate()); michael@0: final FreeBusy fb = new FreeTimeBuilder().start(freeStart) michael@0: .end(freeEnd) michael@0: .duration(duration.getDuration()) michael@0: .components(components) michael@0: .build(); michael@0: if (fb != null && !fb.getPeriods().isEmpty()) { michael@0: getProperties().add(fb); michael@0: } michael@0: } michael@0: else { michael@0: // initialise with all busy time for the specified period.. michael@0: final DateTime busyStart = new DateTime(start.getDate()); michael@0: final DateTime busyEnd = new DateTime(end.getDate()); michael@0: final FreeBusy fb = new BusyTimeBuilder().start(busyStart) michael@0: .end(busyEnd) michael@0: .components(components) michael@0: .build(); michael@0: if (fb != null && !fb.getPeriods().isEmpty()) { michael@0: getProperties().add(fb); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Create a FREEBUSY property representing the busy time for the specified component list. If the component is not michael@0: * applicable to FREEBUSY time, or if the component is outside the bounds of the start and end dates, null is michael@0: * returned. If no valid busy periods are identified in the component an empty FREEBUSY property is returned (i.e. michael@0: * empty period list). michael@0: */ michael@0: private class BusyTimeBuilder { michael@0: michael@0: private DateTime start; michael@0: michael@0: private DateTime end; michael@0: michael@0: private ComponentList components; michael@0: michael@0: public BusyTimeBuilder start(DateTime start) { michael@0: this.start = start; michael@0: return this; michael@0: } michael@0: michael@0: public BusyTimeBuilder end(DateTime end) { michael@0: this.end = end; michael@0: return this; michael@0: } michael@0: michael@0: public BusyTimeBuilder components(ComponentList components) { michael@0: this.components = components; michael@0: return this; michael@0: } michael@0: michael@0: public FreeBusy build() { michael@0: final PeriodList periods = getConsumedTime(components, start, end); michael@0: final DateRange range = new DateRange(start, end); michael@0: // periods must be in UTC time for freebusy.. michael@0: periods.setUtc(true); michael@0: for (final Iterator i = periods.iterator(); i.hasNext();) { michael@0: final Period period = (Period) i.next(); michael@0: // check if period outside bounds.. michael@0: if (!range.intersects(period)) { michael@3: i.remove(); michael@0: } michael@0: } michael@0: return new FreeBusy(periods); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Create a FREEBUSY property representing the free time available of the specified duration for the given list of michael@0: * components. component. If the component is not applicable to FREEBUSY time, or if the component is outside the michael@0: * bounds of the start and end dates, null is returned. If no valid busy periods are identified in the component an michael@0: * empty FREEBUSY property is returned (i.e. empty period list). michael@0: */ michael@0: private class FreeTimeBuilder { michael@0: michael@0: private DateTime start; michael@0: michael@0: private DateTime end; michael@0: michael@0: private Dur duration; michael@0: michael@0: private ComponentList components; michael@0: michael@0: public FreeTimeBuilder start(DateTime start) { michael@0: this.start = start; michael@0: return this; michael@0: } michael@0: michael@0: public FreeTimeBuilder end(DateTime end) { michael@0: this.end = end; michael@0: return this; michael@0: } michael@0: michael@0: private FreeTimeBuilder duration(Dur duration) { michael@0: this.duration = duration; michael@0: return this; michael@0: } michael@0: michael@0: public FreeTimeBuilder components(ComponentList components) { michael@0: this.components = components; michael@0: return this; michael@0: } michael@0: michael@0: public FreeBusy build() { michael@0: final FreeBusy fb = new FreeBusy(); michael@0: fb.getParameters().add(FbType.FREE); michael@0: final PeriodList periods = getConsumedTime(components, start, end); michael@0: final DateRange range = new DateRange(start, end); michael@0: // Add final consumed time to avoid special-case end-of-list processing michael@0: periods.add(new Period(end, end)); michael@0: DateTime lastPeriodEnd = new DateTime(start); michael@0: // where no time is consumed set the last period end as the range start.. michael@0: for (final Iterator i = periods.iterator(); i.hasNext();) { michael@0: final Period period = (Period) i.next(); michael@0: michael@0: // check if period outside bounds.. or period intersects with the end of the range.. michael@0: if (range.contains(period) || michael@0: (range.intersects(period) && period.getStart().after(range.getRangeStart()))) { michael@0: michael@0: // calculate duration between this period start and last period end.. michael@0: final Duration freeDuration = new Duration(lastPeriodEnd, period.getStart()); michael@0: if (freeDuration.getDuration().compareTo(duration) >= 0) { michael@0: fb.getPeriods().add(new Period(lastPeriodEnd, freeDuration.getDuration())); michael@0: } michael@0: } michael@0: michael@0: if (period.getEnd().after(lastPeriodEnd)) { michael@0: lastPeriodEnd = period.getEnd(); michael@0: } michael@0: } michael@0: return fb; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Creates a list of periods representing the time consumed by the specified list of components. michael@0: * @param components michael@0: * @return michael@0: */ michael@0: private PeriodList getConsumedTime(final ComponentList components, final DateTime rangeStart, michael@0: final DateTime rangeEnd) { michael@0: michael@0: final PeriodList periods = new PeriodList(); michael@0: // only events consume time.. michael@0: for (final Iterator i = components.getComponents(Component.VEVENT).iterator(); i.hasNext();) { michael@0: final Component component = (Component) i.next(); michael@0: periods.addAll(((VEvent) component).getConsumedTime(rangeStart, rangeEnd, false)); michael@0: } michael@0: return periods.normalise(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void validate(final boolean recurse) throws ValidationException { michael@0: michael@0: if (!CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION)) { michael@0: michael@0: // From "4.8.4.7 Unique Identifier": michael@0: // Conformance: The property MUST be specified in the "VEVENT", "VTODO", michael@0: // "VJOURNAL" or "VFREEBUSY" calendar components. michael@0: PropertyValidator.getInstance().assertOne(Property.UID, michael@0: getProperties()); michael@0: michael@0: // From "4.8.7.2 Date/Time Stamp": michael@0: // Conformance: This property MUST be included in the "VEVENT", "VTODO", michael@0: // "VJOURNAL" or "VFREEBUSY" calendar components. michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTAMP, michael@0: getProperties()); michael@0: } michael@0: michael@0: final PropertyValidator validator = PropertyValidator.getInstance(); michael@0: michael@0: /* michael@0: * ; the following are optional, ; but MUST NOT occur more than once contact / dtstart / dtend / duration / michael@0: * dtstamp / organizer / uid / url / michael@0: */ michael@0: validator.assertOneOrLess(Property.CONTACT, getProperties()); michael@0: validator.assertOneOrLess(Property.DTSTART, getProperties()); michael@0: validator.assertOneOrLess(Property.DTEND, getProperties()); michael@0: validator.assertOneOrLess(Property.DURATION, getProperties()); michael@0: validator.assertOneOrLess(Property.DTSTAMP, getProperties()); michael@0: validator.assertOneOrLess(Property.ORGANIZER, getProperties()); michael@0: validator.assertOneOrLess(Property.UID, getProperties()); michael@0: validator.assertOneOrLess(Property.URL, getProperties()); michael@0: michael@0: /* michael@0: * ; the following are optional, ; and MAY occur more than once attendee / comment / freebusy / rstatus / x-prop michael@0: */ michael@0: michael@0: /* michael@0: * The recurrence properties ("RRULE", "EXRULE", "RDATE", "EXDATE") are not permitted within a "VFREEBUSY" michael@0: * calendar component. Any recurring events are resolved into their individual busy time periods using the michael@0: * "FREEBUSY" property. michael@0: */ michael@0: validator.assertNone(Property.RRULE, getProperties()); michael@0: validator.assertNone(Property.EXRULE, getProperties()); michael@0: validator.assertNone(Property.RDATE, getProperties()); michael@0: validator.assertNone(Property.EXDATE, getProperties()); michael@0: michael@0: // DtEnd value must be later in time that DtStart.. michael@0: final DtStart dtStart = (DtStart) getProperty(Property.DTSTART); michael@0: michael@0: // 4.8.2.4 Date/Time Start: michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // start date and time for the free or busy time information. The time michael@0: // MUST be specified in UTC time. michael@0: if (dtStart != null && !dtStart.isUtc()) { michael@0: throw new ValidationException("DTSTART must be specified in UTC time"); michael@0: } michael@0: michael@0: final DtEnd dtEnd = (DtEnd) getProperty(Property.DTEND); michael@0: michael@0: // 4.8.2.2 Date/Time End michael@0: // michael@0: // Within the "VFREEBUSY" calendar component, this property defines the michael@0: // end date and time for the free or busy time information. The time michael@0: // MUST be specified in the UTC time format. The value MUST be later in michael@0: // time than the value of the "DTSTART" property. michael@0: if (dtEnd != null && !dtEnd.isUtc()) { michael@0: throw new ValidationException("DTEND must be specified in UTC time"); michael@0: } michael@0: michael@0: if (dtStart != null && dtEnd != null michael@0: && !dtStart.getDate().before(dtEnd.getDate())) { michael@0: throw new ValidationException("Property [" + Property.DTEND michael@0: + "] must be later in time than [" + Property.DTSTART + "]"); michael@0: } michael@0: michael@0: if (recurse) { michael@0: validateProperties(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: protected Validator getValidator(Method method) { michael@0: return (Validator) methodValidators.get(method); michael@0: } michael@0: michael@0: /** michael@0: *
michael@0:      * Component/Property  Presence
michael@0:      * ------------------- ----------------------------------------------
michael@0:      * METHOD              1       MUST be "PUBLISH"
michael@0:      * 
michael@0:      * VFREEBUSY           1+
michael@0:      *     DTSTAMP         1
michael@0:      *     DTSTART         1       DateTime values must be in UTC
michael@0:      *     DTEND           1       DateTime values must be in UTC
michael@0:      *     FREEBUSY        1+      MUST be BUSYTIME. Multiple instances are
michael@0:      *                             allowed. Multiple instances must be sorted
michael@0:      *                             in ascending order
michael@0:      *     ORGANIZER       1       MUST contain the address of originator of
michael@0:      *                             busy time data.
michael@0:      *     UID             1
michael@0:      *     COMMENT         0 or 1
michael@0:      *     CONTACT         0+
michael@0:      *     X-PROPERTY      0+
michael@0:      *     URL             0 or 1  Specifies busy time URL
michael@0:      * 
michael@0:      *     ATTENDEE        0
michael@0:      *     DURATION        0
michael@0:      *     REQUEST-STATUS  0
michael@0:      * 
michael@0:      * X-COMPONENT         0+
michael@0:      * 
michael@0:      * VEVENT              0
michael@0:      * VTODO               0
michael@0:      * VJOURNAL            0
michael@0:      * VTIMEZONE           0
michael@0:      * VALARM              0
michael@0:      * 
michael@0: * michael@0: */ michael@0: private class PublishValidator implements Validator { michael@0: michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public void validate() throws ValidationException { michael@0: PropertyValidator.getInstance().assertOneOrMore(Property.FREEBUSY, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTAMP, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTART, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTEND, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.ORGANIZER, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.UID, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertOneOrLess(Property.URL, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertNone(Property.ATTENDEE, getProperties()); michael@0: PropertyValidator.getInstance().assertNone(Property.DURATION, getProperties()); michael@0: PropertyValidator.getInstance().assertNone(Property.REQUEST_STATUS, getProperties()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: *
michael@0:      * Component/Property  Presence
michael@0:      * ------------------- ----------------------------------------------
michael@0:      * METHOD              1      MUST be "REPLY"
michael@0:      * 
michael@0:      * VFREEBUSY           1
michael@0:      *     ATTENDEE        1      (address of recipient replying)
michael@0:      *     DTSTAMP         1
michael@0:      *     DTEND           1      DateTime values must be in UTC
michael@0:      *     DTSTART         1      DateTime values must be in UTC
michael@0:      *     FREEBUSY        0+      (values MUST all be of the same data
michael@0:      *                             type. Multiple instances are allowed.
michael@0:      *                             Multiple instances MUST be sorted in
michael@0:      *                             ascending order. Values MAY NOT overlap)
michael@0:      *     ORGANIZER       1       MUST be the request originator's address
michael@0:      *     UID             1
michael@0:      * 
michael@0:      *     COMMENT         0 or 1
michael@0:      *     CONTACT         0+
michael@0:      *     REQUEST-STATUS  0+
michael@0:      *     URL             0 or 1  (specifies busy time URL)
michael@0:      *     X-PROPERTY      0+
michael@0:      *     DURATION        0
michael@0:      *     SEQUENCE        0
michael@0:      * 
michael@0:      * X-COMPONENT         0+
michael@0:      * VALARM              0
michael@0:      * VEVENT              0
michael@0:      * VTODO               0
michael@0:      * VJOURNAL            0
michael@0:      * VTIMEZONE           0
michael@0:      * 
michael@0: * michael@0: */ michael@0: private class ReplyValidator implements Validator { michael@0: michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public void validate() throws ValidationException { michael@0: michael@0: // FREEBUSY is 1+ in RFC2446 but 0+ in Calsify michael@0: michael@0: PropertyValidator.getInstance().assertOne(Property.ATTENDEE, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTAMP, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTEND, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTART, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.ORGANIZER, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.UID, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertOneOrLess(Property.URL, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertNone(Property.DURATION, getProperties()); michael@0: PropertyValidator.getInstance().assertNone(Property.SEQUENCE, getProperties()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * METHOD:REQUEST Validator. michael@0: * michael@0: *
michael@0:      * Component/Property  Presence
michael@0:      * ------------------- ----------------------------------------------
michael@0:      * METHOD              1      MUST be "REQUEST"
michael@0:      * 
michael@0:      * VFREEBUSY           1
michael@0:      *     ATTENDEE        1+     contain the address of the calendar store
michael@0:      *     DTEND           1      DateTime values must be in UTC
michael@0:      *     DTSTAMP         1
michael@0:      *     DTSTART         1      DateTime values must be in UTC
michael@0:      *     ORGANIZER       1      MUST be the request originator's address
michael@0:      *     UID             1
michael@0:      *     COMMENT         0 or 1
michael@0:      *     CONTACT         0+
michael@0:      *     X-PROPERTY      0+
michael@0:      * 
michael@0:      *     FREEBUSY        0
michael@0:      *     DURATION        0
michael@0:      *     REQUEST-STATUS  0
michael@0:      *     URL             0
michael@0:      * 
michael@0:      * X-COMPONENT         0+
michael@0:      * VALARM              0
michael@0:      * VEVENT              0
michael@0:      * VTODO               0
michael@0:      * VJOURNAL            0
michael@0:      * VTIMEZONE           0
michael@0:      * 
michael@0: * michael@0: */ michael@0: private class RequestValidator implements Validator { michael@0: michael@0: private static final long serialVersionUID = 1L; michael@0: michael@0: public void validate() throws ValidationException { michael@0: PropertyValidator.getInstance().assertOneOrMore(Property.ATTENDEE, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertOne(Property.DTEND, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTAMP, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.DTSTART, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.ORGANIZER, getProperties()); michael@0: PropertyValidator.getInstance().assertOne(Property.UID, getProperties()); michael@0: michael@0: PropertyValidator.getInstance().assertNone(Property.FREEBUSY, getProperties()); michael@0: PropertyValidator.getInstance().assertNone(Property.DURATION, getProperties()); michael@0: PropertyValidator.getInstance().assertNone(Property.REQUEST_STATUS, getProperties()); michael@0: PropertyValidator.getInstance().assertNone(Property.URL, getProperties()); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @return the CONTACT property or null if not specified michael@0: */ michael@0: public final Contact getContact() { michael@0: return (Contact) getProperty(Property.CONTACT); michael@0: } michael@0: michael@0: /** michael@0: * @return the DTSTART propery or null if not specified michael@0: */ michael@0: public final DtStart getStartDate() { michael@0: return (DtStart) getProperty(Property.DTSTART); michael@0: } michael@0: michael@0: /** michael@0: * @return the DTEND property or null if not specified michael@0: */ michael@0: public final DtEnd getEndDate() { michael@0: return (DtEnd) getProperty(Property.DTEND); michael@0: } michael@0: michael@0: /** michael@0: * @return the DURATION property or null if not specified michael@0: */ michael@0: public final Duration getDuration() { michael@0: return (Duration) getProperty(Property.DURATION); michael@0: } michael@0: michael@0: /** michael@0: * @return the DTSTAMP property or null if not specified michael@0: */ michael@0: public final DtStamp getDateStamp() { michael@0: return (DtStamp) getProperty(Property.DTSTAMP); michael@0: } michael@0: michael@0: /** michael@0: * @return the ORGANIZER property or null if not specified michael@0: */ michael@0: public final Organizer getOrganizer() { michael@0: return (Organizer) getProperty(Property.ORGANIZER); michael@0: } michael@0: michael@0: /** michael@0: * @return the URL property or null if not specified michael@0: */ michael@0: public final Url getUrl() { michael@0: return (Url) getProperty(Property.URL); michael@0: } michael@0: michael@0: /** michael@0: * Returns the UID property of this component if available. michael@0: * @return a Uid instance, or null if no UID property exists michael@0: */ michael@0: public final Uid getUid() { michael@0: return (Uid) getProperty(Property.UID); michael@0: } michael@0: }