View Javadoc
1   /**
2    * The contents of this file are subject to the Mozilla Public License Version 1.1
3    * (the "License"); you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    * Software distributed under the License is distributed on an "AS IS" basis,
6    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    * specific language governing rights and limitations under the License.
8    *
9    * The Original Code is "TSComponentOne.java".  Description:
10   * "Represents an HL7 timestamp, which is related to the HL7 TS type."
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2005.  All Rights Reserved.
14   *
15   * Contributor(s): ______________________________________.
16   *
17   * Alternatively, the contents of this file may be used under the terms of the
18   * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
19   * applicable instead of those above.  If you wish to allow use of your version of this
20   * file only under the terms of the GPL and not to allow others to use your version
21   * of this file under the MPL, indicate your decision by deleting  the provisions above
22   * and replace  them with the notice and other provisions required by the GPL License.
23   * If you do not delete the provisions above, a recipient may use your version of
24   * this file under either the MPL or the GPL.
25   */
26  
27  package ca.uhn.hl7v2.model.primitive;
28  
29  import java.util.Calendar;
30  import java.util.Date;
31  
32  import ca.uhn.hl7v2.HL7Exception;
33  import ca.uhn.hl7v2.model.AbstractPrimitive;
34  import ca.uhn.hl7v2.model.DataTypeException;
35  import ca.uhn.hl7v2.model.Message;
36  
37  /**
38   * Represents an HL7 timestamp, which is related to the HL7 TS type.  In version 2.5, 
39   * TS is a composite type.  The first component is type DTM, which corresponds to this class
40   * (actually model.v25.datatype.DTM inherits from this class at time of writing).  In HL7 versions 
41   * 2.2-2.4, it wasn't perfectly clear whether TS was composite or primitive.  HAPI interprets  
42   * it as composite, with the first component having a type that isn't defined by HL7, and we call 
43   * this type TSComponentOne.  In v2.1, TS is primitive, and corresponds one-to-one with this class.   
44   *  
45   * @author <a href="mailto:neal.acharya@uhn.on.ca">Neal Acharya</a>
46   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
47   * @version $Revision: 1.2 $ updated on $Date: 2011-02-21 17:55:08 $ by $Author: jamesagnew $
48   */
49  @SuppressWarnings("serial")
50  public abstract class TSComponentOne extends AbstractPrimitive {
51  
52      private CommonTS myDetail;
53      
54      /**
55       * @param theMessage message to which this Type belongs
56       */
57      public TSComponentOne(Message theMessage) {
58          super(theMessage);
59      }
60  
61      private CommonTS getDetail() throws DataTypeException {
62          if (myDetail == null) {
63              myDetail = new CommonTS(getValue());
64          }
65          return myDetail;
66      }
67      
68      /**
69       * @see AbstractPrimitive#setValue(java.lang.String)
70       * @throws DataTypeException if the value is incorrectly formatted and either validation is 
71       *      enabled for this primitive or detail setters / getters have been called, forcing further
72       *      parsing.   
73       */
74      public void setValue(String theValue) throws DataTypeException {
75          super.setValue(theValue);
76          
77          if (myDetail != null) {
78              myDetail.setValue(theValue);
79          }
80      }
81      
82      /**
83       * @see AbstractPrimitive#getValue
84       */
85      public String getValue() {
86          String result = super.getValue();
87          
88          if (myDetail != null) {
89              result = myDetail.getValue();
90          }
91          
92          return result;
93      }
94      
95      /**
96       * @see CommonTS#setDatePrecision(int, int, int)
97       * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
98       *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
99       *      this method is called.  
100      */
101     public void setDatePrecision(int yr, int mnth, int dy) throws DataTypeException {
102         getDetail().setDatePrecision(yr,mnth,dy);        
103     }
104     
105     /**
106      * @see CommonTS#setDateMinutePrecision(int, int, int, int, int)
107      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
108      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
109      *      this method is called.  
110      */
111     public void setDateMinutePrecision(int yr, int mnth, int dy, int hr, int min) throws DataTypeException {
112         getDetail().setDateMinutePrecision(yr,mnth,dy,hr,min);        
113     }
114     
115     /**
116      * @see CommonTS#setDateSecondPrecision(int, int, int, int, int, float)
117      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
118      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
119      *      this method is called.  
120      */
121     public void setDateSecondPrecision(int yr, int mnth, int dy, int hr, int min, float sec) throws DataTypeException {
122         getDetail().setDateSecondPrecision(yr,mnth,dy,hr,min,sec);        
123     }
124     
125     /**
126      * @see CommonTS#setOffset(int)
127      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
128      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
129      *      this method is called.  
130      */
131     public void setOffset(int signedOffset) throws DataTypeException {
132         getDetail().setOffset(signedOffset);        
133     }
134     
135     /**
136      * Returns the year as an integer.
137      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
138      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
139      *      this method is called.  
140      */
141     public int getYear() throws DataTypeException {
142         return getDetail().getYear();
143     }
144     
145     /**
146      * Returns the month as an integer.
147      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
148      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
149      *      this method is called.  
150      */
151     public int getMonth() throws DataTypeException {
152         return getDetail().getMonth();
153     }
154     
155     /**
156      * Returns the day as an integer.
157      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
158      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
159      *      this method is called.  
160      */
161     public int getDay() throws DataTypeException {
162         return getDetail().getDay();
163     }
164     
165     /**
166      * Returns the hour as an integer.
167      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
168      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
169      *      this method is called.  
170      */
171     public int getHour() throws DataTypeException {
172         return getDetail().getHour();
173     }
174     
175     /**
176      * Returns the minute as an integer.
177      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
178      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
179      *      this method is called.  
180      */
181     public int getMinute() throws DataTypeException {
182        return getDetail().getMinute();
183     }
184     
185     /**
186      * Returns the second as an integer.
187      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
188      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
189      *      this method is called.  
190      */
191     public int getSecond() throws DataTypeException {
192         return getDetail().getSecond();
193     }
194     
195     /**
196      * Returns the fractional second value as a float.
197      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
198      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
199      *      this method is called.  
200      */
201     public float getFractSecond() throws DataTypeException {
202         return getDetail().getFractSecond();
203     }
204     
205     /**
206      * Returns the GMT offset value as an integer.
207      * @throws DataTypeException if the value is incorrectly formatted.  If validation is enabled, this 
208      *      exception should be thrown at setValue(), but if not, detailed parsing may be deferred until 
209      *      this method is called.  
210      */
211     public int getGMTOffset() throws DataTypeException {
212         return getDetail().getGMTOffset();
213     }
214     
215     /**
216      * Convenience setter which sets the value using a {@link Calendar} object.
217      * 
218      * Note: Sets fields using precision up to the millisecond, including timezone offset
219      * 
220      * @param theCalendar The calendar object from which to retrieve values 
221      * @since 1.1 
222      */
223     public void setValue(Calendar theCalendar) throws DataTypeException {
224     	setValue((String)null);
225         getDetail().setValue(theCalendar);
226     }
227 
228     /**
229      * Convenience setter which sets the value using a {@link Calendar} object.
230      * 
231      * Note: Sets fields using precision up to the millisecond, and sets the timezone offset to
232      * the current system offset
233      * Note: Date is timezone-agnostic, representing always GMT time
234      * 
235      * @param theDate The date object from which to retrieve values 
236      * @since 1.1 
237      */
238     public void setValue(Date theDate) throws DataTypeException {
239     	setValue((String)null);
240         getDetail().setValue(theDate);
241     }
242     
243     /**
244      * Convenience setter which sets the value using a {@link Calendar} object.
245      * 
246      * Note: Sets fields using precision up to the minute
247      * 
248      * @param theCalendar The calendar object from which to retrieve values 
249      * @since 1.1 
250      */
251     public void setValueToMinute(Calendar theCalendar) throws DataTypeException {
252     	setValue((String)null);
253         getDetail().setValueToMinute(theCalendar);
254     }
255 
256     /**
257      * Convenience setter which sets the value using a {@link Date} object.
258      * 
259      * Note: Sets fields using precision up to the minute
260      * Note: Date is timezone-agnostic, representing always GMT time
261      * 
262      * @param theDate The date object from which to retrieve values 
263      * @since 1.1 
264      */
265     public void setValueToMinute(Date theDate) throws DataTypeException {
266     	setValue((String)null);
267         getDetail().setValueToMinute(theDate);
268     }
269 
270     /**
271      * Convenience setter which sets the value using a {@link Calendar} object.
272      * 
273      * Note: Sets fields using precision up to the second
274      * 
275      * @param theCalendar The calendar object from which to retrieve values 
276      * @since 1.1 
277      */
278     public void setValueToSecond(Calendar theCalendar) throws DataTypeException {
279     	setValue((String)null);
280         getDetail().setValueToSecond(theCalendar);
281     }
282 
283     /**
284      * Convenience setter which sets the value using a {@link Date} object.
285      * 
286      * Note: Sets fields using precision up to the second
287      * 
288      * @param theDate The date object from which to retrieve values 
289      * @since 1.1 
290      */
291     public void setValueToSecond(Date theDate) throws DataTypeException {
292     	setValue((String)null);
293         getDetail().setValueToSecond(theDate);
294     }
295 
296     /**
297      * Return the value as a calendar object
298      * @since 1.1 
299      * @throws DataTypeException If the current underlying string representation can not be parsed into a valid date/time
300      */
301     public Calendar getValueAsCalendar() throws DataTypeException {
302     	return getDetail().getValueAsCalendar();
303     }
304 
305     /**
306      * Return the value as a date object
307      * Note: Sets fields using precision up to the second
308      * 
309      * @since 1.1 
310      * @throws DataTypeException If the current underlying string representation can not be parsed into a valid date/time
311      */
312     public Date getValueAsDate() throws DataTypeException {
313         return getDetail().getValueAsDate();
314     }
315 
316     @Override
317     public boolean isEmpty() throws HL7Exception {
318         return super.isEmpty() && myDetail == null;
319     }
320 
321     @Override
322     public void clear() {
323         super.clear();
324         myDetail = null;
325     }
326 }