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 "AbstractConformanceDataType.java".  Description:
10   * "This class contains the functionality for a Data Type in the Conformance class set"
11   *
12   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   * 2001.  All Rights Reserved.
14   *
15   * Contributor(s): James Agnew
16   * Paul Brohman
17   * Mitch Delachevrotiere
18   * Shawn Dyck
19   * Cory Metcalf
20   *
21   * Alternatively, the contents of this file may be used under the terms of the
22   * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
23   * applicable instead of those above.  If you wish to allow use of your version of this
24   * file only under the terms of the GPL and not to allow others to use your version
25   * of this file under the MPL, indicate your decision by deleting  the provisions above
26   * and replace  them with the notice and other provisions required by the GPL License.
27   * If you do not delete the provisions above, a recipient may use your version of
28   * this file under either the MPL or the GPL.
29   *
30   */
31  
32  package ca.uhn.hl7v2.conf.classes.abs;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import ca.uhn.hl7v2.conf.classes.exceptions.ConfDataException;
38  import ca.uhn.hl7v2.model.DataTypeException;
39  import ca.uhn.hl7v2.model.Primitive;
40  
41  /** This class contains the functionality for a Data Type in the Conformance class set
42   * @author <table><tr>James Agnew</tr>
43   *                <tr>Paul Brohman</tr>
44   *                <tr>Mitch Delachevrotiere</tr>
45   *                <tr>Shawn Dyck</tr>
46   * 		  <tr>Cory Metcalf</tr></table>
47   */
48  public abstract class AbstractConformanceDataType {
49  	
50  	private static final Logger log = LoggerFactory.getLogger(AbstractConformanceDataType.class);
51      
52      private final Primitive hapiPrimitive;
53      
54      /** Constructor for AbstractConformanceDataType
55       * @param hapiPrimitive the underlying primitive that the extending class represents
56       */
57      public AbstractConformanceDataType(Primitive hapiPrimitive) {
58          this.hapiPrimitive = hapiPrimitive;
59          
60          try {
61              if ( getConstantValue() != null )
62                  setValue( getConstantValue() );
63          } catch ( ConfDataException e ) {
64  			log.error( "Could not enforce constant value.", e );
65          }
66      }
67      
68      /** This method returns the constant value for the extending class.
69       *  @return the constant value
70       */
71      public abstract String getConstantValue();
72      
73      /** This method returns the Maximum length of the extending object.
74       *  @return the maximum length
75       */
76      public abstract long getMaxLength();
77      
78      /** This method validates the <code>String</code> value passed in to be no greater then the
79       * maximum allowable length for the extending class.  If the <code>String</code> value is valid, this
80       * method will set the underlying HAPI class's value. If the data passed in is invalid for the given
81       * data type, a ConfDataException is thrown.
82       * @throws ConformanceException
83       * @param value the value of the Data Type
84       */
85      protected void setValue(java.lang.String value) throws ConfDataException {
86          if ((this.getMaxLength() > 0)
87          && (value.length() > this.getMaxLength())) {
88              throw new ConfDataException("DataType length exceeds the Maximum allowable length");
89          }
90          
91          try {
92              this.hapiPrimitive.setValue(value);
93          } catch (DataTypeException e) {
94              throw new ConfDataException("Invalid Data Populated");
95          }
96      }
97      
98  }