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.property; michael@0: michael@0: import java.text.ParseException; michael@0: michael@0: import net.fortuna.ical4j.model.DateTime; michael@0: import net.fortuna.ical4j.model.Dur; michael@0: import net.fortuna.ical4j.model.Parameter; michael@0: import net.fortuna.ical4j.model.ParameterList; michael@0: import net.fortuna.ical4j.model.PropertyFactoryImpl; michael@0: import net.fortuna.ical4j.model.ValidationException; michael@0: import net.fortuna.ical4j.model.parameter.Value; michael@0: import net.fortuna.ical4j.util.ParameterValidator; michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created: [Apr 6, 2004] michael@0: * michael@0: * Defines a TRIGGER iCalendar component property. michael@0: * michael@0: *
michael@0:  *     4.8.6.3 Trigger
michael@0:  *     
michael@0:  *        Property Name: TRIGGER
michael@0:  *     
michael@0:  *        Purpose: This property specifies when an alarm will trigger.
michael@0:  *     
michael@0:  *        Value Type: The default value type is DURATION. The value type can be
michael@0:  *        set to a DATE-TIME value type, in which case the value MUST specify a
michael@0:  *        UTC formatted DATE-TIME value.
michael@0:  *     
michael@0:  *        Property Parameters: Non-standard, value data type, time zone
michael@0:  *        identifier or trigger relationship property parameters can be
michael@0:  *        specified on this property. The trigger relationship property
michael@0:  *        parameter MUST only be specified when the value type is DURATION.
michael@0:  *     
michael@0:  *        Conformance: This property MUST be specified in the "VALARM" calendar
michael@0:  *        component.
michael@0:  *     
michael@0:  *        Description: Within the "VALARM" calendar component, this property
michael@0:  *        defines when the alarm will trigger. The default value type is
michael@0:  *        DURATION, specifying a relative time for the trigger of the alarm.
michael@0:  *        The default duration is relative to the start of an event or to-do
michael@0:  *        that the alarm is associated with. The duration can be explicitly set
michael@0:  *     
michael@0:  *        to trigger from either the end or the start of the associated event
michael@0:  *        or to-do with the "RELATED" parameter. A value of START will set the
michael@0:  *        alarm to trigger off the start of the associated event or to-do. A
michael@0:  *        value of END will set the alarm to trigger off the end of the
michael@0:  *        associated event or to-do.
michael@0:  *     
michael@0:  *        Either a positive or negative duration may be specified for the
michael@0:  *        "TRIGGER" property. An alarm with a positive duration is triggered
michael@0:  *        after the associated start or end of the event or to-do. An alarm
michael@0:  *        with a negative duration is triggered before the associated start or
michael@0:  *        end of the event or to-do.
michael@0:  *     
michael@0:  *        The "RELATED" property parameter is not valid if the value type of
michael@0:  *        the property is set to DATE-TIME (i.e., for an absolute date and time
michael@0:  *        alarm trigger). If a value type of DATE-TIME is specified, then the
michael@0:  *        property value MUST be specified in the UTC time format. If an
michael@0:  *        absolute trigger is specified on an alarm for a recurring event or
michael@0:  *        to-do, then the alarm will only trigger for the specified absolute
michael@0:  *        date/time, along with any specified repeating instances.
michael@0:  *     
michael@0:  *        If the trigger is set relative to START, then the "DTSTART" property
michael@0:  *        MUST be present in the associated "VEVENT" or "VTODO" calendar
michael@0:  *        component. If an alarm is specified for an event with the trigger set
michael@0:  *        relative to the END, then the "DTEND" property or the "DSTART" and
michael@0:  *        "DURATION' properties MUST be present in the associated "VEVENT"
michael@0:  *        calendar component. If the alarm is specified for a to-do with a
michael@0:  *        trigger set relative to the END, then either the "DUE" property or
michael@0:  *        the "DSTART" and "DURATION' properties MUST be present in the
michael@0:  *        associated "VTODO" calendar component.
michael@0:  *     
michael@0:  *        Alarms specified in an event or to-do which is defined in terms of a
michael@0:  *        DATE value type will be triggered relative to 00:00:00 UTC on the
michael@0:  *        specified date. For example, if "DTSTART:19980205, then the duration
michael@0:  *        trigger will be relative to19980205T000000Z.
michael@0:  *     
michael@0:  *        Format Definition: The property is defined by the following notation:
michael@0:  *     
michael@0:  *          trigger    = "TRIGGER" (trigrel / trigabs)
michael@0:  *     
michael@0:  *          trigrel    = *(
michael@0:  *     
michael@0:  *                     ; the following are optional,
michael@0:  *                     ; but MUST NOT occur more than once
michael@0:  *     
michael@0:  *                       (";" "VALUE" "=" "DURATION") /
michael@0:  *                       (";" trigrelparam) /
michael@0:  *     
michael@0:  *                     ; the following is optional,
michael@0:  *                     ; and MAY occur more than once
michael@0:  *     
michael@0:  *                       (";" xparam)
michael@0:  *                       ) ":"  dur-value
michael@0:  *     
michael@0:  *          trigabs    = 1*(
michael@0:  *     
michael@0:  *                     ; the following is REQUIRED,
michael@0:  *                     ; but MUST NOT occur more than once
michael@0:  *     
michael@0:  *                       (";" "VALUE" "=" "DATE-TIME") /
michael@0:  *     
michael@0:  *                     ; the following is optional,
michael@0:  *                     ; and MAY occur more than once
michael@0:  *     
michael@0:  *                       (";" xparam)
michael@0:  *     
michael@0:  *                       ) ":" date-time
michael@0:  * 
michael@0: * michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class Trigger extends UtcProperty { michael@0: michael@0: private static final long serialVersionUID = 5049421499261722194L; michael@0: michael@0: private Dur duration; michael@0: michael@0: /** michael@0: * Default constructor. michael@0: */ michael@0: public Trigger() { michael@0: super(TRIGGER, PropertyFactoryImpl.getInstance()); michael@0: } michael@0: michael@0: /** michael@0: * @param aList a list of parameters for this component michael@0: * @param aValue a value string for this component michael@0: */ michael@0: public Trigger(final ParameterList aList, final String aValue) { michael@0: super(TRIGGER, aList, PropertyFactoryImpl.getInstance()); michael@0: setValue(aValue); michael@0: } michael@0: michael@0: /** michael@0: * @param duration a duration in milliseconds michael@0: */ michael@0: public Trigger(final Dur duration) { michael@0: super(TRIGGER, PropertyFactoryImpl.getInstance()); michael@0: setDuration(duration); michael@0: } michael@0: michael@0: /** michael@0: * @param aList a list of parameters for this component michael@0: * @param duration a duration in milliseconds michael@0: */ michael@0: public Trigger(final ParameterList aList, final Dur duration) { michael@0: super(TRIGGER, aList, PropertyFactoryImpl.getInstance()); michael@0: setDuration(duration); michael@0: } michael@0: michael@0: /** michael@0: * @param dateTime a date representation of a date-time michael@0: */ michael@0: public Trigger(final DateTime dateTime) { michael@0: super(TRIGGER, PropertyFactoryImpl.getInstance()); michael@0: setDateTime(dateTime); michael@0: } michael@0: michael@0: /** michael@0: * @param aList a list of parameters for this component michael@0: * @param dateTime a date representation of a date-time michael@0: */ michael@0: public Trigger(final ParameterList aList, final DateTime dateTime) { michael@0: super(TRIGGER, aList, PropertyFactoryImpl.getInstance()); michael@0: setDateTime(dateTime); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void validate() throws ValidationException { michael@0: super.validate(); michael@0: michael@0: final Parameter relParam = getParameter(Parameter.RELATED); michael@0: final Parameter valueParam = getParameter(Parameter.VALUE); michael@0: michael@0: if (relParam != null || !Value.DATE_TIME.equals(valueParam)) { michael@0: michael@0: ParameterValidator.getInstance().assertOneOrLess(Parameter.RELATED, michael@0: getParameters()); michael@0: michael@0: ParameterValidator.getInstance().assertNullOrEqual(Value.DURATION, michael@0: getParameters()); michael@0: michael@0: if (getDuration() == null) { michael@0: throw new ValidationException("Duration value not specified"); michael@0: } michael@0: } michael@0: else { michael@0: ParameterValidator.getInstance().assertOne(Parameter.VALUE, michael@0: getParameters()); michael@0: michael@0: ParameterValidator.getInstance().assertNullOrEqual(Value.DATE_TIME, michael@0: getParameters()); michael@0: michael@0: if (getDateTime() == null) { michael@0: throw new ValidationException("DATE-TIME value not specified"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the duration. michael@0: */ michael@0: public final Dur getDuration() { michael@0: return duration; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void setValue(final String aValue) { michael@0: try { michael@0: super.setValue(aValue); michael@0: duration = null; michael@0: } michael@0: catch (ParseException pe) { michael@0: duration = new Dur(aValue); michael@0: super.setDateTime(null); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final String getValue() { michael@0: if (duration != null) { michael@0: return duration.toString(); michael@0: } michael@0: return super.getValue(); michael@0: } michael@0: michael@0: /** michael@0: * @param dateTime The dateTime to set. michael@0: */ michael@0: public final void setDateTime(final DateTime dateTime) { michael@0: super.setDateTime(dateTime); michael@0: duration = null; michael@0: getParameters().replace(Value.DATE_TIME); michael@0: } michael@0: michael@0: /** michael@0: * @param duration The duration to set. michael@0: */ michael@0: public final void setDuration(final Dur duration) { michael@0: this.duration = duration; michael@0: super.setDateTime(null); michael@0: // duration is the default value type for Trigger.. michael@0: if (getParameter(Parameter.VALUE) != null) { michael@0: getParameters().replace(Value.DURATION); michael@0: } michael@0: } michael@0: }