001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029
030package org.hl7.fhir.dstu3.model;
031
032import java.util.Calendar;
033
034/**
035 * Primitive type "date" in FHIR: any day in a gregorian calendar
036 */
037
038import java.util.Date;
039import java.util.GregorianCalendar;
040import java.util.TimeZone;
041
042import org.apache.commons.lang3.Validate;
043
044import ca.uhn.fhir.model.api.annotation.DatatypeDef;
045
046/**
047 * Represents a FHIR date datatype. Valid precisions values for this type are:
048 * <ul>
049 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
050 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
051 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
052 * </ul>
053 */
054@DatatypeDef(name = "date")
055public class DateType extends BaseDateTimeType {
056
057        private static final long serialVersionUID = 3L;
058        
059        /**
060         * The default precision for this type
061         */
062        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY;
063
064        /**
065         * Constructor
066         */
067        public DateType() {
068                super();
069        }
070
071        /**
072         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type
073         */
074        public DateType(Date theDate) {
075                super(theDate, DEFAULT_PRECISION);
076        }
077
078        /**
079         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
080         * <ul>
081         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
082         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
083         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
084         * </ul>
085         *
086         * @throws ca.uhn.fhir.parser.DataFormatException
087         *             If the specified precision is not allowed for this type
088         */
089        public DateType(Date theDate, TemporalPrecisionEnum thePrecision) {
090                super(theDate, thePrecision);
091        }
092
093        /**
094         * Constructor which accepts a date as a string in FHIR format
095         *
096         * @throws ca.uhn.fhir.parser.DataFormatException
097         *             If the precision in the date string is not allowed for this type
098         */
099        public DateType(String theDate) {
100                super(theDate);
101        }
102
103        /**
104         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
105         */
106        public DateType(Calendar theCalendar) {
107                super(theCalendar.getTime(), DEFAULT_PRECISION);
108                setTimeZone(theCalendar.getTimeZone());
109        }
110
111        /**
112         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
113         * <p>
114         * <b>Use caution when using this constructor</b>: The month is 0-indexed but the day is 1-indexed 
115         * in order to match the bahaviour of the Java {@link Calendar} type.
116         * </p>
117         * 
118         * @param theYear The year, e.g. 2015
119         * @param theMonth The month, e.g. 0 for January
120         * @param theDay The day (1 indexed) e.g. 1 for the first day of the month
121         */
122        public DateType(int theYear, int theMonth, int theDay) {
123                this(toCalendarZulu(theYear, theMonth, theDay));
124        }
125
126        private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) {
127                Validate.isTrue(theMonth >= 0, "theMonth must be between 0 and 11");
128                Validate.isTrue(theMonth <= 11, "theMonth must be between 0 and 11");
129                Validate.isTrue(theDay >= 1, "theMonth must be between 0 and 11");
130                Validate.isTrue(theDay <= 31, "theMonth must be between 0 and 11");
131                
132                GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
133                retVal.set(Calendar.YEAR, theYear);
134                retVal.set(Calendar.MONTH, theMonth);
135                retVal.set(Calendar.DATE, theDay);
136                return retVal;
137        }
138
139        @Override
140        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
141                switch (thePrecision) {
142                        case YEAR:
143                        case MONTH:
144                        case DAY:
145                                return true;
146                        default:
147                                return false;
148                }
149        }
150
151        /**
152         * Returns the default precision for this datatype
153         *
154         * @see #DEFAULT_PRECISION
155         */
156        @Override
157        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
158                return DEFAULT_PRECISION;
159        }
160
161        @Override
162        public DateType copy() {
163                return new DateType(getValueAsString());
164        }
165        
166        public static InstantType today() {
167                return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault());
168        }
169
170        /**
171         * Creates a new instance by parsing an HL7 v3 format date time string
172         */
173        public static DateType parseV3(String theV3String) {
174                DateType retVal = new DateType();
175                retVal.setValueAsV3String(theV3String);
176                return retVal;
177        }
178
179        @Override
180        public String fhirType() {
181                return "date";          
182        }
183}