Coverage Report - ca.uhn.hl7v2.model.primitive.CommonDT
 
Classes in this File Line Coverage Branch Coverage Complexity
CommonDT
92%
130/140
89%
25/28
3.667
 
 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 "CommonDT.java".  Description:
 10  
  * "Note: The class description below has been excerpted from the Hl7 2.4 documentation"
 11  
  *
 12  
  * The Initial Developer of the Original Code is University Health Network. Copyright (C)
 13  
  * 2001.  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  
 
 28  
 package ca.uhn.hl7v2.model.primitive;
 29  
 import java.util.Calendar;
 30  
 import java.util.Date;
 31  
 import java.util.GregorianCalendar;
 32  
 import java.io.Serializable;
 33  
 
 34  
 import ca.uhn.hl7v2.model.DataTypeException;
 35  
 import ca.uhn.hl7v2.model.DataTypeUtil;
 36  
 
 37  
 /**
 38  
  * This class contains functionality used by the DT class
 39  
  * in the version 2.3.0, 2.3.1, and 2.4 packages
 40  
  *
 41  
  * Note: The class description below has been excerpted from the Hl7 2.4 documentation. Sectional
 42  
  * references made below also refer to the same documentation.
 43  
  *
 44  
  * Format: YYYY[MM[DD]]
 45  
  * In prior versions of HL7, this data type was always specified to be in the format YYYYMMDD. In the current and future
 46  
  * versions, the precision of a date may be expressed by limiting the number of digits used with the format specification
 47  
  * YYYY[MM[DD]]. Thus, YYYY is used to specify a precision of "year," YYYYMM specifies a precision of "month,"
 48  
  * and YYYYMMDD specifies a precision of "day."
 49  
  * By site-specific agreement, YYYYMMDD may be used where backward compatibility must be maintained.
 50  
  * Examples:   |19880704|  |199503|
 51  
  * @author Neal Acharya
 52  
  */
 53  
 
 54  
 @SuppressWarnings("serial")
 55  
 public class CommonDT implements Serializable {
 56  
 
 57  
     private String value;
 58  
     private int year;
 59  
     private int month;
 60  
     private int day;
 61  
 
 62  
     /**
 63  
      * Constructs a DT datatype with fields initialzed to zero and value initialized
 64  
      * to null.
 65  
      */
 66  535
     public CommonDT() {
 67  
         //initialize all DT fields
 68  535
         value = null;
 69  535
         year = 0;
 70  535
         month = 0;
 71  535
         day = 0;
 72  535
     } //end constructor
 73  
 
 74  
     /**
 75  
      * Constructs a DT object with the given value.
 76  
      * The stored value will be in the following
 77  
      * format YYYY[MM[DD]].
 78  
      */
 79  49
     public CommonDT(String val) throws DataTypeException {
 80  49
         this.setValue(val);
 81  49
     } //end constructor
 82  
 
 83  
     /**
 84  
      * Convenience setter which sets the value using a {@link Calendar} object. Passing in <code>null</code> clears any existing value.
 85  
      * 
 86  
      * Note: Sets fields using maximum possible precision
 87  
      * 
 88  
      * @param theCalendar The calendar object from which to retrieve values
 89  
      * @since 1.1 
 90  
      */
 91  
     public void setValue(Calendar theCalendar) throws DataTypeException {
 92  2
                 if (theCalendar == null) {
 93  0
                         setValue((String)null);
 94  0
                         return;
 95  
                 }
 96  
 
 97  2
         int yr = theCalendar.get(Calendar.YEAR);
 98  2
         int mnth = theCalendar.get(Calendar.MONTH) + 1;
 99  2
         int dy = theCalendar.get(Calendar.DATE);
 100  2
         setYearMonthDayPrecision(yr, mnth, dy);
 101  2
     }
 102  
 
 103  
     /**
 104  
      * Convenience setter which sets the value using a {@link Date} object. Passing in <code>null</code> clears any existing value.
 105  
      * 
 106  
      * Note: Sets fields using maximum possible precision
 107  
      * 
 108  
      * @param theDate The date object from which to retrieve values
 109  
      * @since 1.1 
 110  
      */
 111  
     public void setValue(Date theDate) throws DataTypeException {
 112  1
                 if (theDate == null) {
 113  0
                         setValue((String)null);
 114  0
                         return;
 115  
                 }
 116  
 
 117  1
         Calendar calendar = Calendar.getInstance();
 118  1
         calendar.setTime(theDate);
 119  1
         setValue(calendar);
 120  1
     }
 121  
     
 122  
     
 123  
     /**
 124  
      * Return the value as a calendar object
 125  
      * @since 1.1 
 126  
      */
 127  
     public Calendar getValueAsCalendar() {
 128  2
         Calendar retVal = Calendar.getInstance();
 129  2
         retVal.set(Calendar.DATE, getDay());
 130  2
         retVal.set(Calendar.MONTH, getMonth() - 1);
 131  2
         retVal.set(Calendar.YEAR, getYear());
 132  
 
 133  
         // Truncate
 134  2
         retVal.set(Calendar.HOUR_OF_DAY, 0);
 135  2
         retVal.set(Calendar.MINUTE, 0);
 136  2
         retVal.set(Calendar.SECOND, 0);
 137  2
         retVal.set(Calendar.MILLISECOND, 0);
 138  
         
 139  2
         return retVal;
 140  
     }
 141  
 
 142  
     
 143  
     /**
 144  
      * Return the value as a date object
 145  
      * @since 1.1 
 146  
      */
 147  
     public Date getValueAsDate() {
 148  1
         return getValueAsCalendar().getTime();
 149  
     }
 150  
     
 151  
     
 152  
     /**
 153  
      * This method takes in a string HL7 date value and performs validations
 154  
      * then sets the value field. The stored value will be in the following
 155  
      * format YYYY[MM[DD]]. Passing in <code>null</code> clears any existing value.
 156  
      *
 157  
      */
 158  
     public void setValue(String val) throws DataTypeException {
 159  
 
 160  221
         if (val != null && !val.equals("") && !val.equals("\"\"")){
 161  
             try {
 162  173
                 GregorianCalendar cal = new GregorianCalendar();
 163  173
                 cal.clear();
 164  173
                 cal.setLenient(false);
 165  
 
 166  
                 //check the length, must be either four, six, or eight digits
 167  173
                 if ((val.length() != 4) && (val.length() != 6) && (val.length() != 8)) {
 168  10
                     String msg =
 169  
                         "The length of the DT datatype value does not conform to an allowable"
 170  
                             + " format. Format should conform to YYYY[MM[DD]]";
 171  10
                     DataTypeException e = new DataTypeException(msg);
 172  10
                     throw e;
 173  
                 }
 174  
 
 175  163
                 if (val.length() >= 4) {
 176  
                     //extract the year from the input value
 177  163
                     int yrInt = Integer.parseInt(val.substring(0, 4));
 178  
                     //check to see if the year is valid by creating a Gregorian calendar object with
 179  
                     //this value.  If an error occurs then processing will stop in this try block
 180  161
                     cal.set(yrInt, 0, 1);
 181  161
                     cal.getTime(); //for error detection
 182  161
                     year = yrInt;
 183  
                 }
 184  
 
 185  161
                 if (val.length() >= 6) {
 186  
                     //extract the month from the input value
 187  144
                     int mnthInt = Integer.parseInt(val.substring(4, 6));
 188  
                     //check to see if the month is valid by creating a Gregorian calendar object with
 189  
                     //this value.  If an error occurs then processing will stop in this try block
 190  144
                     cal.set(year, mnthInt - 1, 1);
 191  144
                     cal.getTime(); //for error detection
 192  140
                     month = mnthInt;
 193  
 
 194  
                 }
 195  
 
 196  157
                 if (val.length() == 8) {
 197  
                     //extract the day from the input value
 198  122
                     int dayInt = Integer.parseInt(val.substring(6, 8));
 199  
                     //check to see if the day is valid by creating a Gregorian calendar object with
 200  
                     //the year/month/day combination.  If an error occurs then processing will stop
 201  
                     // in this try block
 202  122
                     cal.set(year, month - 1, dayInt);
 203  122
                     cal.getTime(); //for error detection
 204  118
                     day = dayInt;
 205  
                 }
 206  
                 //validations are complete now store the input value into the private value field
 207  153
                 value = val;
 208  
             } //end try
 209  
 
 210  10
             catch (DataTypeException e) {
 211  10
                 throw e;
 212  
             } //end catch
 213  
 
 214  10
             catch (Exception e) {
 215  10
                 throw new DataTypeException( e );
 216  153
             } //end catch
 217  
         } //end if
 218  
         else {
 219  
             //set the private value field to null or empty space.
 220  48
             value = val;
 221  
         } //end else       
 222  
 
 223  201
     } //end method
 224  
 
 225  
     /**
 226  
      * This method takes in an integer value for the year and performs validations,
 227  
      * it then sets the value field formatted as an HL7 date.
 228  
      * value with year precision (YYYY)
 229  
      */
 230  
     public void setYearPrecision(int yr) throws DataTypeException {
 231  
         try {
 232  18
             GregorianCalendar cal = new GregorianCalendar();
 233  18
             cal.clear();
 234  18
             cal.setLenient(false);
 235  
 
 236  
             //ensure that the year field is four digits long
 237  18
             if (Integer.toString(yr).length() != 4) {
 238  10
                 String msg = "The input year value must be four digits long";
 239  10
                 DataTypeException e = new DataTypeException(msg);
 240  10
                 throw e;
 241  
             }
 242  
             //check is input year is valid
 243  
             //GregorianCalendar cal = new GregorianCalendar(yr,0,1);
 244  8
             cal.set(yr, 0, 1);
 245  8
             cal.getTime(); //for error detection
 246  8
             year = yr;
 247  8
             month = 0;
 248  8
             day = 0;
 249  8
             value = Integer.toString(yr);
 250  
         } //end try
 251  
 
 252  10
         catch (DataTypeException e) {
 253  10
             throw e;
 254  
         } //end catch
 255  
 
 256  0
         catch (Exception e) {
 257  0
             throw new DataTypeException( e );
 258  8
         } //end catch
 259  
 
 260  8
     } //end method
 261  
 
 262  
     /**
 263  
      * This method takes in integer values for the year and month and performs validations,
 264  
      * it then sets the value field formatted as an HL7 date
 265  
      * value with year&month precision (YYYYMM).
 266  
      * Note: The first month = 1 = January.
 267  
      */
 268  
     public void setYearMonthPrecision(int yr, int mnth) throws DataTypeException {
 269  
         try {
 270  28
             GregorianCalendar cal = new GregorianCalendar();
 271  28
             cal.clear();
 272  28
             cal.setLenient(false);
 273  
             //ensure that the year field is four digits long
 274  28
             if (Integer.toString(yr).length() != 4) {
 275  8
                 String msg = "The input year value must be four digits long";
 276  8
                 DataTypeException e = new DataTypeException(msg);
 277  8
                 throw e;
 278  
             }
 279  
             //validate the input month
 280  
             //GregorianCalendar cal = new GregorianCalendar(yr,(mnth-1),1);
 281  20
             cal.set(yr, (mnth - 1), 1);
 282  20
             cal.getTime(); //for error detection
 283  14
             year = yr;
 284  14
             month = mnth;
 285  14
             day = 0;
 286  14
             value = Integer.toString(yr) + DataTypeUtil.preAppendZeroes(mnth, 2);
 287  
         }
 288  
 
 289  8
         catch (DataTypeException e) {
 290  8
             throw e;
 291  
         } //end catch
 292  
 
 293  6
         catch (Exception e) {
 294  6
             throw new DataTypeException( e );
 295  14
         } //end catch
 296  14
     } //end method
 297  
 
 298  
     /**
 299  
      * This method takes in integer values for the year and month and day
 300  
      * and performs validations, it then sets the value in the object
 301  
      * formatted as an HL7 date value with year&month&day precision (YYYYMMDD).
 302  
      */
 303  
     public void setYearMonthDayPrecision(int yr, int mnth, int dy) throws DataTypeException {
 304  
         try {
 305  357
             GregorianCalendar cal = new GregorianCalendar();
 306  357
             cal.clear();
 307  357
             cal.setLenient(false);
 308  
 
 309  
             //ensure that the year field is four digits long
 310  357
             if (Integer.toString(yr).length() != 4) {
 311  22
                 String msg = "The input year value must be four digits long";
 312  22
                 DataTypeException e = new DataTypeException(msg);
 313  22
                 throw e;
 314  
             }
 315  
             //validate the input month/day combination
 316  335
             cal.set(yr, (mnth - 1), dy);
 317  335
             cal.getTime(); //for error detection
 318  309
             year = yr;
 319  309
             month = mnth;
 320  309
             day = dy;
 321  309
             value = Integer.toString(yr) + DataTypeUtil.preAppendZeroes(mnth, 2) + DataTypeUtil.preAppendZeroes(dy, 2);
 322  
         }
 323  
 
 324  22
         catch (DataTypeException e) {
 325  22
             throw e;
 326  
         } //end catch
 327  
 
 328  26
         catch (Exception e) {
 329  26
             throw new DataTypeException( e );
 330  309
         } //end catch
 331  
 
 332  309
     } //end method
 333  
 
 334  
     /**
 335  
      * Returns the HL7 DT string value.
 336  
      */
 337  
     public String getValue() {
 338  431
         return value;
 339  
     } //end method
 340  
 
 341  
     /**
 342  
      * Returns the year as an integer.
 343  
      */
 344  
     public int getYear() {
 345  22
         return year;
 346  
     } //end method
 347  
 
 348  
     /**
 349  
      * Returns the month as an integer.
 350  
      */
 351  
     public int getMonth() {
 352  22
         return month;
 353  
     } //end method
 354  
 
 355  
     /**
 356  
      * Returns the day as an integer.
 357  
      */
 358  
     public int getDay() {
 359  22
         return day;
 360  
     } //end method
 361  
 
 362  
     
 363  
     /**
 364  
      * Returns a string value representing the input Gregorian Calendar object in
 365  
      * an Hl7 Date Format.
 366  
      */
 367  
     public static String toHl7DTFormat(GregorianCalendar cal) throws DataTypeException {
 368  5
         String val = "";
 369  
         try {
 370  
             //set the input cal object so that it can report errors
 371  
             //on it's value
 372  5
             cal.setLenient(false);
 373  5
             int calYear = cal.get(GregorianCalendar.YEAR);
 374  5
             int calMonth = cal.get(GregorianCalendar.MONTH) + 1;
 375  5
             int calDay = cal.get(GregorianCalendar.DAY_OF_MONTH);
 376  5
             CommonDT dt = new CommonDT();
 377  5
             dt.setYearMonthDayPrecision(calYear, calMonth, calDay);
 378  5
             val = dt.getValue();
 379  
         } //end try
 380  
 
 381  0
         catch (DataTypeException e) {
 382  0
             throw e;
 383  
         } //end catch
 384  
 
 385  0
         catch (Exception e) {
 386  0
             throw new DataTypeException( e );
 387  5
         } //end catch
 388  5
         return val;
 389  
     } //end method
 390  
 
 391  
 } //end class