1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/component/VAvailability.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,260 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model.component; 1.36 + 1.37 +import java.util.Iterator; 1.38 + 1.39 +import net.fortuna.ical4j.model.Component; 1.40 +import net.fortuna.ical4j.model.ComponentList; 1.41 +import net.fortuna.ical4j.model.Parameter; 1.42 +import net.fortuna.ical4j.model.Property; 1.43 +import net.fortuna.ical4j.model.PropertyList; 1.44 +import net.fortuna.ical4j.model.ValidationException; 1.45 +import net.fortuna.ical4j.model.Validator; 1.46 +import net.fortuna.ical4j.model.parameter.Value; 1.47 +import net.fortuna.ical4j.model.property.DtEnd; 1.48 +import net.fortuna.ical4j.model.property.DtStamp; 1.49 +import net.fortuna.ical4j.model.property.DtStart; 1.50 +import net.fortuna.ical4j.model.property.Method; 1.51 +import net.fortuna.ical4j.util.PropertyValidator; 1.52 +import net.fortuna.ical4j.util.Strings; 1.53 + 1.54 +/** 1.55 + * $Id$ [Apr 5, 2004] 1.56 + * 1.57 + * Defines an iCalendar VAVAILABILITY component. 1.58 + * 1.59 + * <pre> 1.60 + Component Name: VAVAILABILITY 1.61 + 1.62 + Purpose: Provide a grouping of component properties that describe 1.63 + the availability associated with a calendar user. 1.64 + 1.65 + Format Definition: A "VAVAILABILITY" calendar component is defined 1.66 + by the following notation: 1.67 + 1.68 + availabilityc = "BEGIN" ":" "VAVAILABILITY" CRLF 1.69 + availabilityprop *availablec 1.70 + "END" ":" "VAVAILABILITY" CRLF 1.71 + 1.72 + availabilityprop = *( 1.73 + 1.74 + ; the following are REQUIRED, 1.75 + ; but MUST NOT occur more than once 1.76 + 1.77 + dtstamp / dtstart / uid 1.78 + 1.79 + ; the following are OPTIONAL, 1.80 + ; but MUST NOT occur more than once 1.81 + 1.82 + busytype / created / last-mod / 1.83 + organizer / seq / summary / url / 1.84 + 1.85 + ; either 'dtend' or 'duration' may appear 1.86 + ; in a 'availabilityprop', but 'dtend' and 1.87 + ; 'duration' MUST NOT occur in the same 1.88 + ; 'availabilityprop' 1.89 + 1.90 + dtend / duration / 1.91 + 1.92 + ; the following are OPTIONAL, 1.93 + ; and MAY occur more than once 1.94 + 1.95 + categories / comment / contact / x-prop 1.96 + 1.97 + ) 1.98 + 1.99 + * 1.100 + * </pre> 1.101 + * 1.102 + * @author Ben Fortuna 1.103 + * @author Mike Douglass 1.104 + */ 1.105 +public class VAvailability extends CalendarComponent { 1.106 + 1.107 + private static final long serialVersionUID = -3001603309266267258L; 1.108 + 1.109 + private ComponentList available; 1.110 + 1.111 + /** 1.112 + * Default constructor. 1.113 + */ 1.114 + public VAvailability() { 1.115 + super(VAVAILABILITY); 1.116 + this.available = new ComponentList(); 1.117 + getProperties().add(new DtStamp()); 1.118 + } 1.119 + 1.120 + /** 1.121 + * Constructs a new instance containing the specified properties. 1.122 + * @param properties a list of properties 1.123 + */ 1.124 + public VAvailability(final PropertyList properties) { 1.125 + super(VAVAILABILITY, properties); 1.126 + this.available = new ComponentList(); 1.127 + } 1.128 + 1.129 + /** 1.130 + * Constructor. 1.131 + * @param properties a list of properties 1.132 + * @param available a list of available components 1.133 + */ 1.134 + public VAvailability(final PropertyList properties, final ComponentList available) { 1.135 + super(VEVENT, properties); 1.136 + this.available = available; 1.137 + } 1.138 + 1.139 + /** 1.140 + * Returns the list of available times. 1.141 + * @return a component list 1.142 + */ 1.143 + public final ComponentList getAvailable() { 1.144 + return available; 1.145 + } 1.146 + 1.147 + /** 1.148 + * {@inheritDoc} 1.149 + */ 1.150 + public final String toString() { 1.151 + final StringBuffer b = new StringBuffer(); 1.152 + b.append(BEGIN); 1.153 + b.append(':'); 1.154 + b.append(getName()); 1.155 + b.append(Strings.LINE_SEPARATOR); 1.156 + b.append(getProperties()); 1.157 + b.append(getAvailable()); 1.158 + b.append(END); 1.159 + b.append(':'); 1.160 + b.append(getName()); 1.161 + b.append(Strings.LINE_SEPARATOR); 1.162 + return b.toString(); 1.163 + } 1.164 + 1.165 + /** 1.166 + * {@inheritDoc} 1.167 + */ 1.168 + public final void validate(final boolean recurse) 1.169 + throws ValidationException { 1.170 + 1.171 + // validate that getAvailable() only contains Available components 1.172 + final Iterator iterator = getAvailable().iterator(); 1.173 + while (iterator.hasNext()) { 1.174 + final Component component = (Component) iterator.next(); 1.175 + 1.176 + if (!(component instanceof Available)) { 1.177 + throw new ValidationException("Component [" 1.178 + + component.getName() + "] may not occur in VAVAILABILITY"); 1.179 + } 1.180 + } 1.181 + 1.182 + /* 1.183 + * ; dtstamp / dtstart / uid are required, but MUST NOT occur more than once / 1.184 + */ 1.185 + PropertyValidator.getInstance().assertOne(Property.DTSTART, 1.186 + getProperties()); 1.187 + PropertyValidator.getInstance().assertOne(Property.DTSTAMP, 1.188 + getProperties()); 1.189 + PropertyValidator.getInstance().assertOne(Property.UID, 1.190 + getProperties()); 1.191 + 1.192 + /* If specified, the "DTSTART" and "DTEND" properties in 1.193 + * "VAVAILABILITY" components and "AVAILABLE" sub-components MUST be 1.194 + * "DATE-TIME" values specified as either date with UTC time or date 1.195 + * with local time and a time zone reference. 1.196 + */ 1.197 + final DtStart start = (DtStart) getProperty(Property.DTSTART); 1.198 + if (Value.DATE.equals(start.getParameter(Parameter.VALUE))) { 1.199 + throw new ValidationException("Property [" + Property.DTSTART 1.200 + + "] must be a " + Value.DATE_TIME); 1.201 + } 1.202 + 1.203 + /* 1.204 + * ; either 'dtend' or 'duration' may appear in ; a 'eventprop', but 'dtend' and 'duration' ; MUST NOT occur in 1.205 + * the same 'eventprop' dtend / duration / 1.206 + */ 1.207 + if (getProperty(Property.DTEND) != null) { 1.208 + PropertyValidator.getInstance().assertOne(Property.DTEND, 1.209 + getProperties()); 1.210 + /* Must be DATE_TIME */ 1.211 + final DtEnd end = (DtEnd) getProperty(Property.DTEND); 1.212 + if (Value.DATE.equals(end.getParameter(Parameter.VALUE))) { 1.213 + throw new ValidationException("Property [" + Property.DTEND 1.214 + + "] must be a " + Value.DATE_TIME); 1.215 + } 1.216 + 1.217 + if (getProperty(Property.DURATION) != null) { 1.218 + throw new ValidationException("Only one of Property [" + Property.DTEND 1.219 + + "] or [" + Property.DURATION + 1.220 + " must appear a VAVAILABILITY"); 1.221 + } 1.222 + } 1.223 + 1.224 + /* 1.225 + * ; the following are optional, 1.226 + * ; but MUST NOT occur more than once 1.227 + * 1.228 + * busytype / created / last-mod / 1.229 + * organizer / seq / summary / url / 1.230 + */ 1.231 + PropertyValidator.getInstance().assertOneOrLess(Property.BUSYTYPE, 1.232 + getProperties()); 1.233 + PropertyValidator.getInstance().assertOneOrLess(Property.CREATED, 1.234 + getProperties()); 1.235 + PropertyValidator.getInstance().assertOneOrLess(Property.LAST_MODIFIED, 1.236 + getProperties()); 1.237 + PropertyValidator.getInstance().assertOneOrLess(Property.ORGANIZER, 1.238 + getProperties()); 1.239 + PropertyValidator.getInstance().assertOneOrLess(Property.SEQUENCE, 1.240 + getProperties()); 1.241 + PropertyValidator.getInstance().assertOneOrLess(Property.SUMMARY, 1.242 + getProperties()); 1.243 + PropertyValidator.getInstance().assertOneOrLess(Property.URL, 1.244 + getProperties()); 1.245 + 1.246 + /* 1.247 + * ; the following are optional, ; and MAY occur more than once 1.248 + * categories / comment / contact / x-prop 1.249 + */ 1.250 + 1.251 + if (recurse) { 1.252 + validateProperties(); 1.253 + } 1.254 + } 1.255 + 1.256 + /** 1.257 + * {@inheritDoc} 1.258 + */ 1.259 + protected Validator getValidator(Method method) { 1.260 + // TODO Auto-generated method stub 1.261 + return null; 1.262 + } 1.263 +}