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.SuperStructure;
032import ca.uhn.hl7v2.model.Type;
033
034/**
035 * Specialized version of ModelClassFactory that always returns the same version or even structure. This is useful when designing
036 * applications which are expected to handle multiple versions of HL7. The recommended approach is to
037 * configure this factory to handle the newest version of HL7 you intend to support. Since HL7 is a backwards
038 * compatible protocol, older versions should always be able to parse correctly into a newer message structure.  
039 * 
040 * @version $Revision: 1.2 $ updated on $Date: 2009-10-03 15:25:46 $ by $Author: jamesagnew $
041 * @author This ModelClassFactory implementation is modified by Niranjan.Sharma@med.ge.com on 27-Jul-2009 for CanonicalModel of V2.6
042 */
043public class CanonicalModelClassFactory extends DefaultModelClassFactory
044{
045
046    private static final long serialVersionUID = -1795680089524220526L;
047    
048    private String myVersion;
049
050        private Class<? extends Message> myMessageClass;
051
052    /**
053     * Constructor which selects the newest version of HAPI known to
054     */
055    public CanonicalModelClassFactory() {
056        myVersion = getHighestKnownVersion();
057    }
058
059    /**
060     * Constructor
061     * 
062     * @param theVersion The version to always return (e.g. "2.6")
063     */
064    public CanonicalModelClassFactory(String theVersion) {
065        if (theVersion == null || !Version.supportsVersion(theVersion)) {
066                throw new IllegalArgumentException("Unknown version: " + theVersion);
067        }
068        myVersion = theVersion;
069    }
070
071    /**
072     * Constructor for a model class factory which always returns the same
073     * message type.
074     * 
075     * @see SuperStructure
076     */
077    public CanonicalModelClassFactory(Class<? extends Message> theClass) {
078        if (theClass == null) {
079                throw new NullPointerException("Class may not be null");
080        }
081        myMessageClass = theClass;
082    }
083    
084    
085        /**
086     * {@inheritDoc}
087     */
088    @Override
089    public Class<? extends Group> getGroupClass(String theName, String theVersion) throws HL7Exception {
090        return super.getGroupClass(theName, myVersion);
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public Class<? extends Message> getMessageClass(String theName, String theVersion, boolean theIsExplicit) throws HL7Exception {
098        if (myMessageClass != null) {
099                return myMessageClass;
100        }
101        return super.getMessageClass(theName, myVersion, theIsExplicit);
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public Class<? extends Segment> getSegmentClass(String theName, String theVersion) throws HL7Exception {
109        return super.getSegmentClass(theName, myVersion);
110    }
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    public Class<? extends Type> getTypeClass(String theName, String theVersion) throws HL7Exception {
117        return super.getTypeClass(theName, myVersion);
118    }
119    
120}