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.r5.model;
031
032/*-
033 * #%L
034 * org.hl7.fhir.r5
035 * %%
036 * Copyright (C) 2014 - 2019 Health Level 7
037 * %%
038 * Licensed under the Apache License, Version 2.0 (the "License");
039 * you may not use this file except in compliance with the License.
040 * You may obtain a copy of the License at
041 * 
042 *      http://www.apache.org/licenses/LICENSE-2.0
043 * 
044 * Unless required by applicable law or agreed to in writing, software
045 * distributed under the License is distributed on an "AS IS" BASIS,
046 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
047 * See the License for the specific language governing permissions and
048 * limitations under the License.
049 * #L%
050 */
051
052
053import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
054import ca.uhn.fhir.model.api.annotation.DatatypeDef;
055import org.apache.commons.lang3.Validate;
056
057import java.util.Calendar;
058import java.util.Date;
059import java.util.GregorianCalendar;
060import java.util.TimeZone;
061
062/**
063 * Primitive type "date" in FHIR: any day in a gregorian calendar
064 */
065
066/**
067 * Represents a FHIR date datatype. Valid precisions values for this type are:
068 * <ul>
069 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
070 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
071 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
072 * </ul>
073 */
074@DatatypeDef(name = "date")
075public class DateType extends BaseDateTimeType {
076
077        private static final long serialVersionUID = 3L;
078        
079        /**
080         * The default precision for this type
081         */
082        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY;
083
084        /**
085         * Constructor
086         */
087        public DateType() {
088                super();
089        }
090
091        /**
092         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type
093         */
094        public DateType(Date theDate) {
095                super(theDate, DEFAULT_PRECISION);
096        }
097
098        /**
099         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
100         * <ul>
101         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
102         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
103         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
104         * </ul>
105         *
106         * @throws ca.uhn.fhir.parser.DataFormatException
107         *             If the specified precision is not allowed for this type
108         */
109        public DateType(Date theDate, TemporalPrecisionEnum thePrecision) {
110                super(theDate, thePrecision);
111        }
112
113        /**
114         * Constructor which accepts a date as a string in FHIR format
115         *
116         * @throws ca.uhn.fhir.parser.DataFormatException
117         *             If the precision in the date string is not allowed for this type
118         */
119        public DateType(String theDate) {
120                super(theDate);
121        }
122
123        /**
124         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
125         */
126        public DateType(Calendar theCalendar) {
127                super(theCalendar.getTime(), DEFAULT_PRECISION);
128                setTimeZone(theCalendar.getTimeZone());
129        }
130
131        /**
132         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
133         * <p>
134         * <b>Use caution when using this constructor</b>: The month is 0-indexed but the day is 1-indexed 
135         * in order to match the bahaviour of the Java {@link Calendar} type.
136         * </p>
137         * 
138         * @param theYear The year, e.g. 2015
139         * @param theMonth The month, e.g. 0 for January
140         * @param theDay The day (1 indexed) e.g. 1 for the first day of the month
141         */
142        public DateType(int theYear, int theMonth, int theDay) {
143                this(toCalendarZulu(theYear, theMonth, theDay));
144        }
145
146        private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) {
147                Validate.isTrue(theMonth >= 0, "theMonth must be between 0 and 11");
148                Validate.isTrue(theMonth <= 11, "theMonth must be between 0 and 11");
149                Validate.isTrue(theDay >= 1, "theMonth must be between 0 and 11");
150                Validate.isTrue(theDay <= 31, "theMonth must be between 0 and 11");
151                
152                GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
153                retVal.set(Calendar.YEAR, theYear);
154                retVal.set(Calendar.MONTH, theMonth);
155                retVal.set(Calendar.DATE, theDay);
156                return retVal;
157        }
158
159        @Override
160        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
161                switch (thePrecision) {
162                        case YEAR:
163                        case MONTH:
164                        case DAY:
165                                return true;
166                        default:
167                                return false;
168                }
169        }
170
171        /**
172         * Returns the default precision for this datatype
173         *
174         * @see #DEFAULT_PRECISION
175         */
176        @Override
177        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
178                return DEFAULT_PRECISION;
179        }
180
181        @Override
182        public DateType copy() {
183                DateType ret = new DateType(getValueAsString());
184    copyValues(ret);
185    return ret;
186        }
187        
188        public static InstantType today() {
189                return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault());
190        }
191
192        /**
193         * Creates a new instance by parsing an HL7 v3 format date time string
194         */
195        public static DateType parseV3(String theV3String) {
196                DateType retVal = new DateType();
197                retVal.setValueAsV3String(theV3String);
198                return retVal;
199        }
200
201        @Override
202        public String fhirType() {
203                return "date";          
204        }
205
206  @Override
207  public boolean isDateTime() {
208    return true;
209  }
210}