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 "AbstractConformanceDataType.java".  Description:
010 * "This class contains the functionality for a Data Type in the Conformance class set"
011 *
012 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
013 * 2001.  All Rights Reserved.
014 *
015 * Contributor(s): James Agnew
016 * Paul Brohman
017 * Mitch Delachevrotiere
018 * Shawn Dyck
019 * Cory Metcalf
020 *
021 * Alternatively, the contents of this file may be used under the terms of the
022 * GNU General Public License (the "GPL"), in which case the provisions of the GPL are
023 * applicable instead of those above.  If you wish to allow use of your version of this
024 * file only under the terms of the GPL and not to allow others to use your version
025 * of this file under the MPL, indicate your decision by deleting  the provisions above
026 * and replace  them with the notice and other provisions required by the GPL License.
027 * If you do not delete the provisions above, a recipient may use your version of
028 * this file under either the MPL or the GPL.
029 *
030 */
031
032package ca.uhn.hl7v2.conf.classes.abs;
033
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037import ca.uhn.hl7v2.conf.classes.exceptions.ConfDataException;
038import ca.uhn.hl7v2.model.DataTypeException;
039import ca.uhn.hl7v2.model.Primitive;
040
041/** This class contains the functionality for a Data Type in the Conformance class set
042 * @author <table><tr>James Agnew</tr>
043 *                <tr>Paul Brohman</tr>
044 *                <tr>Mitch Delachevrotiere</tr>
045 *                <tr>Shawn Dyck</tr>
046 *                <tr>Cory Metcalf</tr></table>
047 */
048public abstract class AbstractConformanceDataType {
049        
050        private static final Logger log = LoggerFactory.getLogger(AbstractConformanceDataType.class);
051    
052    private Primitive hapiPrimitive;
053    
054    /** Constructor for AbstractConformanceDataType
055     * @param hapiPrimitive the underlying primitive that the extending class represents
056     */
057    public AbstractConformanceDataType(Primitive hapiPrimitive) {
058        this.hapiPrimitive = hapiPrimitive;
059        
060        try {
061            if ( getConstantValue() != null )
062                setValue( getConstantValue() );
063        } catch ( ConfDataException e ) {
064                        log.error( "Could not enforce constant value.", e );
065        }
066    }
067    
068    /** This method returns the constant value for the extending class.
069     *  @return the constant value
070     */
071    public abstract String getConstantValue();
072    
073    /** This method returns the Maximum length of the extending object.
074     *  @return the maximum length
075     */
076    public abstract long getMaxLength();
077    
078    /** This method validates the <code>String</code> value passed in to be no greater then the
079     * maximum allowable length for the extending class.  If the <code>String</code> value is valid, this
080     * method will set the underlying HAPI class's value. If the data passed in is invalid for the given
081     * data type, a ConfDataException is thrown.
082     * @throws ConformaceException
083     * @param value the value of the Data Type
084     */
085    protected void setValue(java.lang.String value) throws ConfDataException {
086        if ((this.getMaxLength() > 0)
087        && (value.length() > this.getMaxLength())) {
088            throw new ConfDataException("DataType length exceeds the Maximum allowable length");
089        }
090        
091        try {
092            this.hapiPrimitive.setValue(value);
093        } catch (DataTypeException e) {
094            throw new ConfDataException("Invalid Data Populated");
095        }
096    }
097    
098}