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 "AbstractPrimitive.java".  Description:
010     * "Base class for Primitives.  Performs validation in setValue()."
011     *
012     * The Initial Developer of the Original Code is University Health Network. Copyright (C)
013     * 2001-2005.  All Rights Reserved.
014     *
015     * Contributor(s): ______________________________________.
016     *
017     * Alternatively, the contents of this file may be used under the terms of the
018     * GNU General Public License (the  ???GPL???), in which case the provisions of the GPL are
019     * applicable instead of those above.  If you wish to allow use of your version of this
020     * file only under the terms of the GPL and not to allow others to use your version
021     * of this file under the MPL, indicate your decision by deleting  the provisions above
022     * and replace  them with the notice and other provisions required by the GPL License.
023     * If you do not delete the provisions above, a recipient may use your version of
024     * this file under either the MPL or the GPL.
025     *
026     */
027    
028    package ca.uhn.hl7v2.model;
029    
030    import ca.uhn.hl7v2.HL7Exception;
031    import ca.uhn.hl7v2.validation.PrimitiveTypeRule;
032    import ca.uhn.hl7v2.validation.ValidationContext;
033    
034    /**
035     * Base class for Primitives.  Performs validation in setValue().
036     *    
037     * @author Bryan Tripp
038     */
039    public abstract class AbstractPrimitive extends AbstractType implements Primitive {
040        
041        /**
042         * @param message message to which this type belongs
043         */
044        public AbstractPrimitive(Message message) {
045            super(message);
046        }
047    
048        private String myValue;
049        
050        /** 
051         * Returns the value of getValue() 
052         * @see java.lang.Object#toString 
053         */
054        public String toString() {
055            return this.getValue();
056        }
057        
058        /**
059         * @see ca.uhn.hl7v2.model.Primitive#getValue()
060         */
061        public String getValue() {
062            return myValue;
063        }
064    
065        /**
066         * Sets the value of this Primitive, first performing validation as specified 
067         * by <code>getMessage().getValidationContext()</code>.  No validation is performed 
068         * if getMessage() returns null. 
069         * 
070         * @see ca.uhn.hl7v2.model.Primitive#setValue(String)
071         */    
072        public void setValue(String theValue) throws DataTypeException {
073            Message message = getMessage();
074    
075            if (message != null) {
076                ValidationContext context = message.getValidationContext();
077                String version = message.getVersion();
078    
079                if (context != null) {
080                    PrimitiveTypeRule[] rules = context.getPrimitiveRules(version, getName(), this); 
081            
082                    for (int i = 0; i < rules.length; i++) {
083                        theValue = rules[i].correct(theValue);
084                        if (!rules[i].test(theValue)) {
085                            throw new DataTypeException("Failed validation rule for value \"" + theValue + "\": " + rules[i].getDescription());
086                        }
087                    }
088                }
089            }
090            
091            myValue = theValue;
092        }
093    
094        
095        /**
096         * {@inheritDoc }
097         */
098        @Override
099        public String encode() throws HL7Exception {
100            return getValue();
101        }
102    
103    
104        /**
105         * {@inheritDoc }
106         */
107        @Override
108        public void parse(String string) throws HL7Exception {
109            clear();
110                    setValue(string);
111        }
112    
113    
114            /**
115             * {@inheritDoc }
116             */
117            @Override
118            public void clear() {
119                    super.clear();
120                    myValue = null;
121            }
122    
123    
124    
125    }