001/**
002The 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.
004You may obtain a copy of the License at http://www.mozilla.org/MPL/
005Software distributed under the License is distributed on an "AS IS" basis,
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007specific language governing rights and limitations under the License.
008
009The Initial Developer of the Original Code is University Health Network. Copyright (C)
0102001.  All Rights Reserved.
011
012Contributor(s): ______________________________________.
013
014Alternatively, the contents of this file may be used under the terms of the
015GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
016applicable instead of those above.  If you wish to allow use of your version of this
017file only under the terms of the GPL and not to allow others to use your version
018of this file under the MPL, indicate your decision by deleting  the provisions above
019and replace  them with the notice and other provisions required by the GPL License.
020If you do not delete the provisions above, a recipient may use your version of
021this file under either the MPL or the GPL.
022
023 */
024package ca.uhn.hl7v2.parser;
025
026import ca.uhn.hl7v2.HL7Exception;
027import ca.uhn.hl7v2.Version;
028import ca.uhn.hl7v2.model.Group;
029import ca.uhn.hl7v2.model.Message;
030import ca.uhn.hl7v2.model.Segment;
031import ca.uhn.hl7v2.model.Type;
032import ca.uhn.hl7v2.util.ReflectionUtil;
033
034/**
035 * Specialized version of ModelClassFactory that always returns the same version
036 * or even structure. This is useful when designing applications which are
037 * expected to handle multiple versions of HL7. The recommended approach is to
038 * configure this factory to handle the newest version of HL7 you intend to
039 * support. Since HL7 is a backwards compatible protocol, older versions should
040 * always be able to parse correctly into a newer message structure.
041 * 
042 * @version $Revision: 1.2 $ updated on $Date: 2009-10-03 15:25:46 $ by $Author:
043 *          jamesagnew $
044 * @author This ModelClassFactory implementation is modified by
045 *         Niranjan.Sharma@med.ge.com on 27-Jul-2009 for CanonicalModel of V2.6
046 */
047public class CanonicalModelClassFactory extends DefaultModelClassFactory {
048
049        private static final long serialVersionUID = -1795680089524220526L;
050
051        private Class<? extends Message> myMessageClass;
052
053        private String myVersion;
054
055        /**
056         * Constructor which selects the newest version of HAPI known to
057         */
058        public CanonicalModelClassFactory() {
059                myVersion = getHighestKnownVersion();
060        }
061
062        /**
063         * Constructor for a model class factory which always returns the same
064         * message type.
065         * 
066         * @see ca.uhn.hl7v2.model.SuperStructure
067         */
068        public CanonicalModelClassFactory(Class<? extends Message> theClass) {
069                if (theClass == null) {
070                        throw new NullPointerException("Class may not be null");
071                }
072                myMessageClass = theClass;
073        }
074
075        /**
076         * Constructor
077         * 
078         * @param theVersion
079         *            The version to always return (e.g. "2.6")
080         */
081        public CanonicalModelClassFactory(String theVersion) {
082                if (theVersion == null || !Version.supportsVersion(theVersion)) {
083                        throw new IllegalArgumentException("Unknown version: " + theVersion);
084                }
085                myVersion = theVersion;
086        }
087
088        /**
089         * {@inheritDoc}
090         */
091        @Override
092        public Class<? extends Group> getGroupClass(String theName, String theVersion) throws HL7Exception {
093                return super.getGroupClass(theName, myVersion);
094        }
095
096        /**
097         * {@inheritDoc}
098         */
099        @Override
100        public Class<? extends Message> getMessageClass(String theName, String theVersion, boolean theIsExplicit) throws HL7Exception {
101                if (myMessageClass != null) {
102                        return myMessageClass;
103                }
104                initVersionIfNeeded();
105                return super.getMessageClass(theName, myVersion, theIsExplicit);
106        }
107
108        /**
109         * {@inheritDoc}
110         */
111        @Override
112        public Class<? extends Segment> getSegmentClass(String theName, String theVersion) throws HL7Exception {
113                initVersionIfNeeded();
114                return super.getSegmentClass(theName, myVersion);
115        }
116
117        /**
118         * {@inheritDoc}
119         */
120        @Override
121        public Class<? extends Type> getTypeClass(String theName, String theVersion) throws HL7Exception {
122                initVersionIfNeeded();
123                return super.getTypeClass(theName, myVersion);
124        }
125
126        private void initVersionIfNeeded() throws HL7Exception {
127                if (myMessageClass != null && myVersion == null) {
128                        myVersion = ReflectionUtil.instantiateMessage(myMessageClass, this).getVersion();
129                }
130        }
131
132}