001    package ezvcard.property;
002    
003    import java.util.Date;
004    import java.util.List;
005    
006    import ezvcard.VCard;
007    import ezvcard.VCardVersion;
008    import ezvcard.Warning;
009    import ezvcard.parameter.Calscale;
010    import ezvcard.parameter.VCardParameters;
011    import ezvcard.util.PartialDate;
012    
013    /*
014     Copyright (c) 2013, Michael Angstadt
015     All rights reserved.
016    
017     Redistribution and use in source and binary forms, with or without
018     modification, are permitted provided that the following conditions are met: 
019    
020     1. Redistributions of source code must retain the above copyright notice, this
021     list of conditions and the following disclaimer. 
022     2. Redistributions in binary form must reproduce the above copyright notice,
023     this list of conditions and the following disclaimer in the documentation
024     and/or other materials provided with the distribution. 
025    
026     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
027     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
028     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
030     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
031     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
032     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
034     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036    
037     The views and conclusions contained in the software and documentation are those
038     of the authors and should not be interpreted as representing official policies, 
039     either expressed or implied, of the FreeBSD Project.
040     */
041    
042    /**
043     * Represents a property whose value contains a date and/or a time (for example,
044     * {@link Birthday}).
045     * @author Michael Angstadt
046     */
047    public class DateOrTimeProperty extends VCardProperty implements HasAltId {
048            private String text;
049            private Date date;
050            private PartialDate partialDate;
051            private boolean dateHasTime;
052    
053            /**
054             * Creates a date-and-or-time property.
055             * @param date the date value
056             */
057            public DateOrTimeProperty(Date date) {
058                    this(date, false);
059            }
060    
061            /**
062             * Creates a date-and-or-time property.
063             * @param date the date value
064             * @param hasTime true to include the date's time component, false if it's
065             * strictly a date
066             */
067            public DateOrTimeProperty(Date date, boolean hasTime) {
068                    setDate(date, hasTime);
069            }
070    
071            /**
072             * Creates a date-and-or-time property.
073             * @param partialDate the partial date value (vCard 4.0 only)
074             */
075            public DateOrTimeProperty(PartialDate partialDate) {
076                    setPartialDate(partialDate);
077            }
078    
079            /**
080             * Creates a date-and-or-time property.
081             * @param text the text value (vCard 4.0 only)
082             */
083            public DateOrTimeProperty(String text) {
084                    setText(text);
085            }
086    
087            /**
088             * Gets the date value.
089             * @return the date value or null if not set
090             */
091            public Date getDate() {
092                    return date;
093            }
094    
095            /**
096             * Sets the value of this property to a complete date.
097             * @param date the date
098             * @param hasTime true to include the date's time component, false if it's
099             * strictly a date
100             */
101            public void setDate(Date date, boolean hasTime) {
102                    this.date = date;
103                    this.dateHasTime = (date == null) ? false : hasTime;
104                    text = null;
105                    partialDate = null;
106            }
107    
108            /**
109             * Gets the reduced accuracy or truncated date. This is only supported by
110             * vCard 4.0.
111             * @return the reduced accuracy or truncated date or null if not set
112             * @see "<a href="
113             * http://tools.ietf.org/html/rfc6350">RFC 6350</a> p.12-14 for examples"
114             */
115            public PartialDate getPartialDate() {
116                    return partialDate;
117            }
118    
119            /**
120             * <p>
121             * Sets the value of this property to a reduced accuracy or truncated date.
122             * This is only supported by vCard 4.0.
123             * </p>
124             * 
125             * <pre class="brush:java">
126             * Birthday bday = new Birthday();
127             * bday.setPartialDate(PartialDate.date(null, 4, 20)); //April 20
128             * </pre>
129             * @param partialDate the reduced accuracy or truncated date
130             * @see "<a href="
131             * http://tools.ietf.org/html/rfc6350">RFC 6350</a> p.12-14 for examples"
132             */
133            public void setPartialDate(PartialDate partialDate) {
134                    this.partialDate = partialDate;
135                    dateHasTime = (partialDate == null) ? false : partialDate.hasTimeComponent();
136                    text = null;
137                    date = null;
138            }
139    
140            /**
141             * Gets the text value of this type. This is only supported by vCard 4.0.
142             * @return the text value or null if not set
143             */
144            public String getText() {
145                    return text;
146            }
147    
148            /**
149             * Sets the value of this property to a text string. This is only supported
150             * by vCard 4.0.
151             * @param text the text value
152             */
153            public void setText(String text) {
154                    this.text = text;
155                    date = null;
156                    partialDate = null;
157                    dateHasTime = false;
158            }
159    
160            /**
161             * Determines whether the "date" or "partialDate" fields have a time
162             * component.
163             * @return true if the date has a time component, false if it's strictly a
164             * date, and false if a text value is defined
165             */
166            public boolean hasTime() {
167                    return dateHasTime;
168            }
169    
170            /**
171             * <p>
172             * Gets the type of calendar that is used for a date or date-time property
173             * value.
174             * </p>
175             * <p>
176             * <b>Supported versions:</b> {@code 4.0}
177             * </p>
178             * @return the type of calendar or null if not found
179             * @see VCardParameters#getCalscale
180             */
181            public Calscale getCalscale() {
182                    return parameters.getCalscale();
183            }
184    
185            /**
186             * <p>
187             * Sets the type of calendar that is used for a date or date-time property
188             * value.
189             * </p>
190             * <p>
191             * <b>Supported versions:</b> {@code 4.0}
192             * </p>
193             * @param calscale the type of calendar or null to remove
194             * @see VCardParameters#setCalscale
195             */
196            public void setCalscale(Calscale calscale) {
197                    parameters.setCalscale(calscale);
198            }
199    
200            //@Override
201            public String getAltId() {
202                    return parameters.getAltId();
203            }
204    
205            //@Override
206            public void setAltId(String altId) {
207                    parameters.setAltId(altId);
208            }
209    
210            @Override
211            protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) {
212                    if (date == null && partialDate == null && text == null) {
213                            warnings.add(new Warning(8));
214                    }
215    
216                    if (version == VCardVersion.V2_1 || version == VCardVersion.V3_0) {
217                            if (text != null) {
218                                    warnings.add(new Warning(11));
219                            }
220                            if (partialDate != null) {
221                                    warnings.add(new Warning(12));
222                            }
223                    }
224            }
225    }