001    package biweekly.property;
002    
003    import java.util.Date;
004    import java.util.List;
005    
006    import biweekly.Warning;
007    import biweekly.component.ICalComponent;
008    import biweekly.component.VTimezone;
009    import biweekly.util.DateTimeComponents;
010    import biweekly.util.ICalDateFormatter;
011    
012    /*
013     Copyright (c) 2013, Michael Angstadt
014     All rights reserved.
015    
016     Redistribution and use in source and binary forms, with or without
017     modification, are permitted provided that the following conditions are met: 
018    
019     1. Redistributions of source code must retain the above copyright notice, this
020     list of conditions and the following disclaimer. 
021     2. Redistributions in binary form must reproduce the above copyright notice,
022     this list of conditions and the following disclaimer in the documentation
023     and/or other materials provided with the distribution. 
024    
025     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035     */
036    
037    /**
038     * Represents a property whose value is a date or a date-time.
039     * @author Michael Angstadt
040     */
041    public class DateOrDateTimeProperty extends ICalProperty {
042            protected Date value;
043            protected DateTimeComponents rawComponents;
044            protected boolean hasTime;
045            protected boolean localTime;
046    
047            /**
048             * Creates a new property.
049             * @param rawComponents the raw components of the date-time value
050             */
051            public DateOrDateTimeProperty(DateTimeComponents rawComponents) {
052                    setRawComponents(rawComponents);
053            }
054    
055            /**
056             * Creates a new property.
057             * @param value the date-time value
058             * @param hasTime true if the value has a time component, false if it is
059             * strictly a date
060             */
061            public DateOrDateTimeProperty(Date value, boolean hasTime) {
062                    setValue(value, hasTime);
063            }
064    
065            /**
066             * Gets the date-time value.
067             * @return the date-time value
068             */
069            public Date getValue() {
070                    return value;
071            }
072    
073            /**
074             * Sets the date-time value.
075             * @param value the date-time value
076             * @param hasTime true if the value has a time component, false if it is
077             * strictly a date
078             */
079            public void setValue(Date value, boolean hasTime) {
080                    this.value = value;
081                    this.hasTime = hasTime;
082            }
083    
084            /**
085             * Gets the raw components of the date-time value.
086             * @return the raw components
087             */
088            public DateTimeComponents getRawComponents() {
089                    return rawComponents;
090            }
091    
092            /**
093             * Sets the raw components of the date-time value.
094             * @param rawComponents the raw components
095             */
096            public void setRawComponents(DateTimeComponents rawComponents) {
097                    this.rawComponents = rawComponents;
098            }
099    
100            /**
101             * Determines whether the date-time value has a time component.
102             * @return true if the value has a time component, false if it is strictly a
103             * date
104             */
105            public boolean hasTime() {
106                    return hasTime;
107            }
108    
109            /**
110             * Gets whether the date should be outputted in local time (without a
111             * timezone).
112             * @return true to output the date without a timezone, false to include a
113             * timezone
114             */
115            public boolean isLocalTime() {
116                    return localTime;
117            }
118    
119            /**
120             * Sets whether the date should be outputted in local time (without a
121             * timezone). Use {@link #setTimezoneId(String)} to specify a timezone.
122             * Dates are written in UTC time by default.
123             * @param localTime true to output the date without a timezone, false to
124             * include a timezone (defaults to false)
125             */
126            public void setLocalTime(boolean localTime) {
127                    this.localTime = localTime;
128                    if (localTime) {
129                            setTimezoneId(null);
130                    }
131            }
132    
133            @Override
134            public String getTimezoneId() {
135                    return super.getTimezoneId();
136            }
137    
138            @Override
139            public void setTimezoneId(String timezoneId) {
140                    super.setTimezoneId(timezoneId);
141            }
142    
143            @Override
144            public void setTimezone(VTimezone timezone) {
145                    super.setTimezone(timezone);
146            }
147    
148            @Override
149            protected void validate(List<ICalComponent> components, List<Warning> warnings) {
150                    if (value == null && components == null) {
151                            warnings.add(Warning.validate(26));
152                    }
153    
154                    String tzid = getTimezoneId();
155                    if (tzid != null && tzid.contains("/") && ICalDateFormatter.parseTimeZoneId(tzid) == null) {
156                            warnings.add(Warning.validate(27, tzid));
157                    }
158            }
159    }