001/**
002 * The contents of this file are subject to the Mozilla Public License Version 1.1
003 * (the "License"); you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 * Software distributed under the License is distributed on an "AS IS" basis,
006 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 * specific language governing rights and limitations under the License.
008 *
009 * The Original Code is "TSComponentOne.java".  Description:
010 * "Represents an HL7 timestamp, which is related to the HL7 TS type."
011 *
012 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 * 2005.  All Rights Reserved.
014 *
015 * Contributor(s): ______________________________________.
016 *
017 * Alternatively, the contents of this file may be used under the terms of the
018 * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
019 * applicable instead of those above.  If you wish to allow use of your version of this
020 * file only under the terms of the GPL and not to allow others to use your version
021 * of this file under the MPL, indicate your decision by deleting  the provisions above
022 * and replace  them with the notice and other provisions required by the GPL License.
023 * If you do not delete the provisions above, a recipient may use your version of
024 * this file under either the MPL or the GPL.
025 */
026
027package ca.uhn.hl7v2.model.primitive;
028
029import java.util.Calendar;
030import java.util.Date;
031
032import ca.uhn.hl7v2.model.AbstractPrimitive;
033import ca.uhn.hl7v2.model.DataTypeException;
034import ca.uhn.hl7v2.model.Message;
035
036/**
037 * Represents an HL7 timestamp, which is related to the HL7 TS type.  In version 2.5, 
038 * TS is a composite type.  The first component is type DTM, which corresponds to this class
039 * (actually model.v25.datatype.DTM inherits from this class at time of writing).  In HL7 versions 
040 * 2.2-2.4, it wasn't perfectly clear whether TS was composite or primitive.  HAPI interprets  
041 * it as composite, with the first component having a type that isn't defined by HL7, and we call 
042 * this type TSComponentOne.  In v2.1, TS is primitive, and corresponds one-to-one with this class.   
043 *  
044 * @author <a href="mailto:neal.acharya@uhn.on.ca">Neal Acharya</a>
045 * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
046 * @version $Revision: 1.2 $ updated on $Date: 2011-02-21 17:55:08 $ by $Author: jamesagnew $
047 */
048@SuppressWarnings("serial")
049public abstract class TSComponentOne extends AbstractPrimitive {
050
051    private CommonTS myDetail;
052    
053    /**
054     * @param theMessage message to which this Type belongs
055     */
056    public TSComponentOne(Message theMessage) {
057        super(theMessage);
058    }
059
060    private CommonTS getDetail() throws DataTypeException {
061        if (myDetail == null) {
062            myDetail = new CommonTS(getValue());
063        }
064        return myDetail;
065    }
066    
067    /**
068     * @see AbstractPrimitive#setValue(java.lang.String)
069     * @throws DataTypeException if the value is incorrectly formatted and either validation is 
070     *      enabled for this primitive or detail setters / getters have been called, forcing further
071     *      parsing.   
072     */
073    public void setValue(String theValue) throws DataTypeException {
074        super.setValue(theValue);
075        
076        if (myDetail != null) {
077            myDetail.setValue(theValue);
078        }
079    }
080    
081    /**
082     * @see AbstractPrimitive#getValue
083     */
084    public String getValue() {
085        String result = super.getValue();
086        
087        if (myDetail != null) {
088            result = myDetail.getValue();
089        }
090        
091        return result;
092    }
093    
094    /**
095     * @see CommonTS#setDatePrecision(int, int, int)
096     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
097     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
098     *      this method is called.  
099     */
100    public void setDatePrecision(int yr, int mnth, int dy) throws DataTypeException {
101        getDetail().setDatePrecision(yr,mnth,dy);        
102    }
103    
104    /**
105     * @see CommonTS#setDateMinutePrecision(int, int, int, int, int)
106     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
107     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
108     *      this method is called.  
109     */
110    public void setDateMinutePrecision(int yr, int mnth, int dy, int hr, int min) throws DataTypeException {
111        getDetail().setDateMinutePrecision(yr,mnth,dy,hr,min);        
112    }
113    
114    /**
115     * @see CommonTS#setDateSecondPrecision(int, int, int, int, int, float)
116     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
117     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
118     *      this method is called.  
119     */
120    public void setDateSecondPrecision(int yr, int mnth, int dy, int hr, int min, float sec) throws DataTypeException {
121        getDetail().setDateSecondPrecision(yr,mnth,dy,hr,min,sec);        
122    }
123    
124    /**
125     * @see CommonTS#setOffset(int)
126     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
127     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
128     *      this method is called.  
129     */
130    public void setOffset(int signedOffset) throws DataTypeException {
131        getDetail().setOffset(signedOffset);        
132    }
133    
134    /**
135     * Returns the year as an integer.
136     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
137     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
138     *      this method is called.  
139     */
140    public int getYear() throws DataTypeException {
141        return getDetail().getYear();
142    }
143    
144    /**
145     * Returns the month as an integer.
146     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
147     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
148     *      this method is called.  
149     */
150    public int getMonth() throws DataTypeException {
151        return getDetail().getMonth();
152    }
153    
154    /**
155     * Returns the day as an integer.
156     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
157     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
158     *      this method is called.  
159     */
160    public int getDay() throws DataTypeException {
161        return getDetail().getDay();
162    }
163    
164    /**
165     * Returns the hour as an integer.
166     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
167     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
168     *      this method is called.  
169     */
170    public int getHour() throws DataTypeException {
171        return getDetail().getHour();
172    }
173    
174    /**
175     * Returns the minute as an integer.
176     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
177     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
178     *      this method is called.  
179     */
180    public int getMinute() throws DataTypeException {
181       return getDetail().getMinute();
182    }
183    
184    /**
185     * Returns the second as an integer.
186     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
187     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
188     *      this method is called.  
189     */
190    public int getSecond() throws DataTypeException {
191        return getDetail().getSecond();
192    }
193    
194    /**
195     * Returns the fractional second value as a float.
196     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
197     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
198     *      this method is called.  
199     */
200    public float getFractSecond() throws DataTypeException {
201        return getDetail().getFractSecond();
202    }
203    
204    /**
205     * Returns the GMT offset value as an integer.
206     * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
207     *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
208     *      this method is called.  
209     */
210    public int getGMTOffset() throws DataTypeException {
211        return getDetail().getGMTOffset();
212    }
213    
214    /**
215     * Convenience setter which sets the value using a {@link Calendar} object.
216     * 
217     * Note: Sets fields using precision up to the millisecond, including timezone offset
218     * 
219     * @param theCalendar The calendar object from which to retrieve values 
220     * @since 1.1 
221     */
222    public void setValue(Calendar theCalendar) throws DataTypeException {
223        setValue((String)null);
224        getDetail().setValue(theCalendar);
225    }
226
227    /**
228     * Convenience setter which sets the value using a {@link Calendar} object.
229     * 
230     * Note: Sets fields using precision up to the millisecond, and sets the timezone offset to
231     * the current system offset
232     * Note: Date is timezone-agnostic, representing always GMT time
233     * 
234     * @param theDate The date object from which to retrieve values 
235     * @since 1.1 
236     */
237    public void setValue(Date theDate) throws DataTypeException {
238        setValue((String)null);
239        getDetail().setValue(theDate);
240    }
241    
242    /**
243     * Convenience setter which sets the value using a {@link Calendar} object.
244     * 
245     * Note: Sets fields using precision up to the minute
246     * 
247     * @param theCalendar The calendar object from which to retrieve values 
248     * @since 1.1 
249     */
250    public void setValueToMinute(Calendar theCalendar) throws DataTypeException {
251        setValue((String)null);
252        getDetail().setValueToMinute(theCalendar);
253    }
254
255    /**
256     * Convenience setter which sets the value using a {@link Date} object.
257     * 
258     * Note: Sets fields using precision up to the minute
259     * Note: Date is timezone-agnostic, representing always GMT time
260     * 
261     * @param theDate The date object from which to retrieve values 
262     * @since 1.1 
263     */
264    public void setValueToMinute(Date theDate) throws DataTypeException {
265        setValue((String)null);
266        getDetail().setValueToMinute(theDate);
267    }
268
269    /**
270     * Convenience setter which sets the value using a {@link Calendar} object.
271     * 
272     * Note: Sets fields using precision up to the second
273     * 
274     * @param theCalendar The calendar object from which to retrieve values 
275     * @since 1.1 
276     */
277    public void setValueToSecond(Calendar theCalendar) throws DataTypeException {
278        setValue((String)null);
279        getDetail().setValueToSecond(theCalendar);
280    }
281
282    /**
283     * Convenience setter which sets the value using a {@link Date} object.
284     * 
285     * Note: Sets fields using precision up to the second
286     * 
287     * @param theDate The date object from which to retrieve values 
288     * @since 1.1 
289     */
290    public void setValueToSecond(Date theDate) throws DataTypeException {
291        setValue((String)null);
292        getDetail().setValueToSecond(theDate);
293    }
294
295    /**
296     * Return the value as a calendar object
297     * @since 1.1 
298     * @throws DataTypeException If the current underlying string representation can not be parsed into a valid date/time
299     */
300    public Calendar getValueAsCalendar() throws DataTypeException {
301        return getDetail().getValueAsCalendar();
302    }
303
304    /**
305     * Return the value as a date object
306     * Note: Sets fields using precision up to the second
307     * 
308     * @since 1.1 
309     * @throws DataTypeException If the current underlying string representation can not be parsed into a valid date/time
310     */
311    public Date getValueAsDate() throws DataTypeException {
312        return getDetail().getValueAsDate();
313    }
314    
315    
316    /** Returns the name of the type (used in XML encoding and profile checking)  */
317//    public String getName() {
318//        return "NM"; //seems to be called an NM in XML representation prior to 2.5  
319//    }
320    
321}