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.dstu2016may.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 ca.uhn.fhir.model.api.annotation.DatatypeDef;
043
044/**
045 * Represents a FHIR date datatype. Valid precisions values for this type are:
046 * <ul>
047 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
048 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
049 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
050 * </ul>
051 */
052@DatatypeDef(name = "date")
053public class DateType extends BaseDateTimeType {
054
055        private static final long serialVersionUID = 3L;
056        
057        /**
058         * The default precision for this type
059         */
060        public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY;
061
062        /**
063         * Constructor
064         */
065        public DateType() {
066                super();
067        }
068
069        /**
070         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type
071         */
072        public DateType(Date theDate) {
073                super(theDate, DEFAULT_PRECISION);
074        }
075
076        /**
077         * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
078         * <ul>
079         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR}
080         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH}
081         * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY}
082         * </ul>
083         *
084         * @throws ca.uhn.fhir.parser.DataFormatException
085         *             If the specified precision is not allowed for this type
086         */
087        public DateType(Date theDate, TemporalPrecisionEnum thePrecision) {
088                super(theDate, thePrecision);
089        }
090
091        /**
092         * Constructor which accepts a date as a string in FHIR format
093         *
094         * @throws ca.uhn.fhir.parser.DataFormatException
095         *             If the precision in the date string is not allowed for this type
096         */
097        public DateType(String theDate) {
098                super(theDate);
099        }
100
101        /**
102         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
103         */
104        public DateType(Calendar theCalendar) {
105                super(theCalendar.getTime(), DEFAULT_PRECISION);
106                setTimeZone(theCalendar.getTimeZone());
107        }
108
109        /**
110         * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
111         * 
112         * @param theYear The year, e.g. 2015
113         * @param theMonth The month, e.g. 0 for January
114         * @param theDay The day (1 indexed) e.g. 1 for the first day of the month
115         */
116        public DateType(int theYear, int theMonth, int theDay) {
117                this(toCalendarZulu(theYear, theMonth, theDay));
118        }
119
120        private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) {
121                GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
122                retVal.set(Calendar.YEAR, theYear);
123                retVal.set(Calendar.MONTH, theMonth);
124                retVal.set(Calendar.DATE, theDay);
125                return retVal;
126        }
127
128        @Override
129        boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
130                switch (thePrecision) {
131                        case YEAR:
132                        case MONTH:
133                        case DAY:
134                                return true;
135                        default:
136                                return false;
137                }
138        }
139
140        /**
141         * Returns the default precision for this datatype
142         *
143         * @see #DEFAULT_PRECISION
144         */
145        @Override
146        protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
147                return DEFAULT_PRECISION;
148        }
149
150        @Override
151        public DateType copy() {
152                return new DateType(getValueAsString());
153        }
154        
155        public static InstantType today() {
156                return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault());
157        }
158
159        /**
160         * Creates a new instance by parsing an HL7 v3 format date time string
161         */
162        public static DateType parseV3(String theV3String) {
163                DateType retVal = new DateType();
164                retVal.setValueAsV3String(theV3String);
165                return retVal;
166        }
167
168        public String fhirType() {
169                return "date";          
170        }
171}