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 Initial Developer of the Original Code is University Health Network. Copyright (C)
10 2001. All Rights Reserved.
11
12 Contributor(s): ______________________________________.
13
14 Alternatively, the contents of this file may be used under the terms of the
15 GNU General Public License (the "GPL"), in which case the provisions of the GPL are
16 applicable instead of those above. If you wish to allow use of your version of this
17 file only under the terms of the GPL and not to allow others to use your version
18 of this file under the MPL, indicate your decision by deleting the provisions above
19 and replace them with the notice and other provisions required by the GPL License.
20 If you do not delete the provisions above, a recipient may use your version of
21 this file under either the MPL or the GPL.
22
23 */
24 package ca.uhn.hl7v2.parser;
25
26 import ca.uhn.hl7v2.HL7Exception;
27 import ca.uhn.hl7v2.Version;
28 import ca.uhn.hl7v2.model.Group;
29 import ca.uhn.hl7v2.model.Message;
30 import ca.uhn.hl7v2.model.Segment;
31 import ca.uhn.hl7v2.model.Type;
32 import ca.uhn.hl7v2.util.ReflectionUtil;
33
34 /**
35 * Specialized version of ModelClassFactory that always returns the same version
36 * or even structure. This is useful when designing applications which are
37 * expected to handle multiple versions of HL7. The recommended approach is to
38 * configure this factory to handle the newest version of HL7 you intend to
39 * support. Since HL7 is a backwards compatible protocol, older versions should
40 * always be able to parse correctly into a newer message structure.
41 *
42 * @version $Revision: 1.2 $ updated on $Date: 2009-10-03 15:25:46 $ by $Author:
43 * jamesagnew $
44 * @author This ModelClassFactory implementation is modified by
45 * Niranjan.Sharma@med.ge.com on 27-Jul-2009 for CanonicalModel of V2.6
46 */
47 public class CanonicalModelClassFactory extends DefaultModelClassFactory {
48
49 private static final long serialVersionUID = -1795680089524220526L;
50
51 private Class<? extends Message> myMessageClass;
52
53 private String myVersion;
54
55 /**
56 * Constructor which selects the newest version of HAPI known to
57 */
58 public CanonicalModelClassFactory() {
59 myVersion = getHighestKnownVersion();
60 }
61
62 /**
63 * Constructor for a model class factory which always returns the same
64 * message type.
65 *
66 * @see ca.uhn.hl7v2.model.SuperStructure
67 */
68 public CanonicalModelClassFactory(Class<? extends Message> theClass) {
69 if (theClass == null) {
70 throw new NullPointerException("Class may not be null");
71 }
72 myMessageClass = theClass;
73 }
74
75 /**
76 * Constructor
77 *
78 * @param theVersion
79 * The version to always return (e.g. "2.6")
80 */
81 public CanonicalModelClassFactory(String theVersion) {
82 if (theVersion == null || !Version.supportsVersion(theVersion)) {
83 throw new IllegalArgumentException("Unknown version: " + theVersion);
84 }
85 myVersion = theVersion;
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public Class<? extends Group> getGroupClass(String theName, String theVersion) throws HL7Exception {
93 return super.getGroupClass(theName, myVersion);
94 }
95
96 /**
97 * {@inheritDoc}
98 */
99 @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 }