| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CommonDT |
|
| 3.6666666666666665;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 | 4613 | public CommonDT() { |
| 67 | //initialize all DT fields | |
| 68 | 4613 | value = null; |
| 69 | 4613 | year = 0; |
| 70 | 4613 | month = 0; |
| 71 | 4613 | day = 0; |
| 72 | 4613 | } //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 | 245 | public CommonDT(String val) throws DataTypeException { |
| 80 | 245 | this.setValue(val); |
| 81 | 245 | } //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 | 10 | if (theCalendar == null) { |
| 93 | 0 | setValue((String)null); |
| 94 | 0 | return; |
| 95 | } | |
| 96 | ||
| 97 | 10 | int yr = theCalendar.get(Calendar.YEAR); |
| 98 | 10 | int mnth = theCalendar.get(Calendar.MONTH) + 1; |
| 99 | 10 | int dy = theCalendar.get(Calendar.DATE); |
| 100 | 10 | setYearMonthDayPrecision(yr, mnth, dy); |
| 101 | 10 | } |
| 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 | 5 | if (theDate == null) { |
| 113 | 0 | setValue((String)null); |
| 114 | 0 | return; |
| 115 | } | |
| 116 | ||
| 117 | 5 | Calendar calendar = Calendar.getInstance(); |
| 118 | 5 | calendar.setTime(theDate); |
| 119 | 5 | setValue(calendar); |
| 120 | 5 | } |
| 121 | ||
| 122 | ||
| 123 | /** | |
| 124 | * Return the value as a calendar object | |
| 125 | * @since 1.1 | |
| 126 | */ | |
| 127 | public Calendar getValueAsCalendar() { | |
| 128 | 10 | Calendar retVal = Calendar.getInstance(); |
| 129 | 10 | retVal.set(Calendar.DATE, getDay()); |
| 130 | 10 | retVal.set(Calendar.MONTH, getMonth() - 1); |
| 131 | 10 | retVal.set(Calendar.YEAR, getYear()); |
| 132 | ||
| 133 | // Truncate | |
| 134 | 10 | retVal.set(Calendar.HOUR_OF_DAY, 0); |
| 135 | 10 | retVal.set(Calendar.MINUTE, 0); |
| 136 | 10 | retVal.set(Calendar.SECOND, 0); |
| 137 | 10 | retVal.set(Calendar.MILLISECOND, 0); |
| 138 | ||
| 139 | 10 | return retVal; |
| 140 | } | |
| 141 | ||
| 142 | ||
| 143 | /** | |
| 144 | * Return the value as a date object | |
| 145 | * @since 1.1 | |
| 146 | */ | |
| 147 | public Date getValueAsDate() { | |
| 148 | 5 | 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 | 1095 | if (val != null && !val.equals("") && !val.equals("\"\"")){ |
| 161 | try { | |
| 162 | 855 | GregorianCalendar cal = new GregorianCalendar(); |
| 163 | 855 | cal.clear(); |
| 164 | 855 | cal.setLenient(false); |
| 165 | ||
| 166 | //check the length, must be either four, six, or eight digits | |
| 167 | 855 | if ((val.length() != 4) && (val.length() != 6) && (val.length() != 8)) { |
| 168 | 50 | 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 | 50 | DataTypeException e = new DataTypeException(msg); |
| 172 | 50 | throw e; |
| 173 | } | |
| 174 | ||
| 175 | 805 | if (val.length() >= 4) { |
| 176 | //extract the year from the input value | |
| 177 | 805 | 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 | 805 | cal.set(yrInt, 0, 1); |
| 181 | 805 | cal.getTime(); //for error detection |
| 182 | 805 | year = yrInt; |
| 183 | } | |
| 184 | ||
| 185 | 805 | if (val.length() >= 6) { |
| 186 | //extract the month from the input value | |
| 187 | 720 | 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 | 720 | cal.set(year, mnthInt - 1, 1); |
| 191 | 720 | cal.getTime(); //for error detection |
| 192 | 700 | month = mnthInt; |
| 193 | ||
| 194 | } | |
| 195 | ||
| 196 | 785 | if (val.length() == 8) { |
| 197 | //extract the day from the input value | |
| 198 | 610 | 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 | 610 | cal.set(year, month - 1, dayInt); |
| 203 | 610 | cal.getTime(); //for error detection |
| 204 | 590 | day = dayInt; |
| 205 | } | |
| 206 | //validations are complete now store the input value into the private value field | |
| 207 | 765 | value = val; |
| 208 | } //end try | |
| 209 | ||
| 210 | 50 | catch (DataTypeException e) { |
| 211 | 50 | throw e; |
| 212 | } //end catch | |
| 213 | ||
| 214 | 40 | catch (Exception e) { |
| 215 | 40 | throw new DataTypeException( e ); |
| 216 | 765 | } //end catch |
| 217 | } //end if | |
| 218 | else { | |
| 219 | //set the private value field to null or empty space. | |
| 220 | 240 | value = val; |
| 221 | } //end else | |
| 222 | ||
| 223 | 1005 | } //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 | 90 | GregorianCalendar cal = new GregorianCalendar(); |
| 233 | 90 | cal.clear(); |
| 234 | 90 | cal.setLenient(false); |
| 235 | ||
| 236 | //ensure that the year field is four digits long | |
| 237 | 90 | if (Integer.toString(yr).length() != 4) { |
| 238 | 50 | String msg = "The input year value must be four digits long"; |
| 239 | 50 | DataTypeException e = new DataTypeException(msg); |
| 240 | 50 | throw e; |
| 241 | } | |
| 242 | //check is input year is valid | |
| 243 | //GregorianCalendar cal = new GregorianCalendar(yr,0,1); | |
| 244 | 40 | cal.set(yr, 0, 1); |
| 245 | 40 | cal.getTime(); //for error detection |
| 246 | 40 | year = yr; |
| 247 | 40 | month = 0; |
| 248 | 40 | day = 0; |
| 249 | 40 | value = Integer.toString(yr); |
| 250 | } //end try | |
| 251 | ||
| 252 | 50 | catch (DataTypeException e) { |
| 253 | 50 | throw e; |
| 254 | } //end catch | |
| 255 | ||
| 256 | 0 | catch (Exception e) { |
| 257 | 0 | throw new DataTypeException( e ); |
| 258 | 40 | } //end catch |
| 259 | ||
| 260 | 40 | } //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 | 140 | GregorianCalendar cal = new GregorianCalendar(); |
| 271 | 140 | cal.clear(); |
| 272 | 140 | cal.setLenient(false); |
| 273 | //ensure that the year field is four digits long | |
| 274 | 140 | if (Integer.toString(yr).length() != 4) { |
| 275 | 40 | String msg = "The input year value must be four digits long"; |
| 276 | 40 | DataTypeException e = new DataTypeException(msg); |
| 277 | 40 | throw e; |
| 278 | } | |
| 279 | //validate the input month | |
| 280 | //GregorianCalendar cal = new GregorianCalendar(yr,(mnth-1),1); | |
| 281 | 100 | cal.set(yr, (mnth - 1), 1); |
| 282 | 100 | cal.getTime(); //for error detection |
| 283 | 70 | year = yr; |
| 284 | 70 | month = mnth; |
| 285 | 70 | day = 0; |
| 286 | 70 | value = Integer.toString(yr) + DataTypeUtil.preAppendZeroes(mnth, 2); |
| 287 | } | |
| 288 | ||
| 289 | 40 | catch (DataTypeException e) { |
| 290 | 40 | throw e; |
| 291 | } //end catch | |
| 292 | ||
| 293 | 30 | catch (Exception e) { |
| 294 | 30 | throw new DataTypeException( e ); |
| 295 | 70 | } //end catch |
| 296 | 70 | } //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 | 3733 | GregorianCalendar cal = new GregorianCalendar(); |
| 306 | 3733 | cal.clear(); |
| 307 | 3733 | cal.setLenient(false); |
| 308 | ||
| 309 | //ensure that the year field is four digits long | |
| 310 | 3733 | if (Integer.toString(yr).length() != 4) { |
| 311 | 110 | String msg = "The input year value must be four digits long"; |
| 312 | 110 | DataTypeException e = new DataTypeException(msg); |
| 313 | 110 | throw e; |
| 314 | } | |
| 315 | //validate the input month/day combination | |
| 316 | 3623 | cal.set(yr, (mnth - 1), dy); |
| 317 | 3623 | cal.getTime(); //for error detection |
| 318 | 3493 | year = yr; |
| 319 | 3493 | month = mnth; |
| 320 | 3493 | day = dy; |
| 321 | 3493 | value = Integer.toString(yr) + DataTypeUtil.preAppendZeroes(mnth, 2) + DataTypeUtil.preAppendZeroes(dy, 2); |
| 322 | } | |
| 323 | ||
| 324 | 110 | catch (DataTypeException e) { |
| 325 | 110 | throw e; |
| 326 | } //end catch | |
| 327 | ||
| 328 | 130 | catch (Exception e) { |
| 329 | 130 | throw new DataTypeException( e ); |
| 330 | 3493 | } //end catch |
| 331 | ||
| 332 | 3493 | } //end method |
| 333 | ||
| 334 | /** | |
| 335 | * Returns the HL7 DT string value. | |
| 336 | */ | |
| 337 | public String getValue() { | |
| 338 | 4028 | return value; |
| 339 | } //end method | |
| 340 | ||
| 341 | /** | |
| 342 | * Returns the year as an integer. | |
| 343 | */ | |
| 344 | public int getYear() { | |
| 345 | 1460 | return year; |
| 346 | } //end method | |
| 347 | ||
| 348 | /** | |
| 349 | * Returns the month as an integer. | |
| 350 | */ | |
| 351 | public int getMonth() { | |
| 352 | 1460 | return month; |
| 353 | } //end method | |
| 354 | ||
| 355 | /** | |
| 356 | * Returns the day as an integer. | |
| 357 | */ | |
| 358 | public int getDay() { | |
| 359 | 1460 | 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 | 25 | String val = ""; |
| 369 | try { | |
| 370 | //set the input cal object so that it can report errors | |
| 371 | //on it's value | |
| 372 | 25 | cal.setLenient(false); |
| 373 | 25 | int calYear = cal.get(GregorianCalendar.YEAR); |
| 374 | 25 | int calMonth = cal.get(GregorianCalendar.MONTH) + 1; |
| 375 | 25 | int calDay = cal.get(GregorianCalendar.DAY_OF_MONTH); |
| 376 | 25 | CommonDT dt = new CommonDT(); |
| 377 | 25 | dt.setYearMonthDayPrecision(calYear, calMonth, calDay); |
| 378 | 25 | 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 | 25 | } //end catch |
| 388 | 25 | return val; |
| 389 | } //end method | |
| 390 | ||
| 391 | } //end class |