View Javadoc

1   package ca.uhn.hl7v2.model;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   
6   import ca.uhn.hl7v2.HL7Exception;
7   import ca.uhn.hl7v2.Version;
8   import ca.uhn.hl7v2.parser.ModelClassFactory;
9   
10  /**
11   * A generic HL7 message, meant for parsing message with unrecognized structures
12   * into a flat list of segments.
13   * 
14   * @author Bryan Tripp
15   * @author Christian Ohr
16   */
17  @SuppressWarnings("serial")
18  public abstract class GenericMessage extends AbstractMessage {
19      
20  	private static final Logger log = LoggerFactory.getLogger(GenericMessage.class);
21  	
22      /** 
23       * Creates a new instance of GenericMessage. 
24       * 
25       * @param factory class factory for contained structures 
26       */
27      public GenericMessage(ModelClassFactory factory) {
28          super(factory);
29          try {
30              this.addNonstandardSegment("MSH");
31          } catch(HL7Exception e) {
32              String message = "Unexpected error adding GenericSegment to GenericMessage.";
33              log.error(message, e);
34              throw new Error(message); // TODO better throw RuntimeException
35          }        
36      }
37      
38      /**
39       * Returns a subclass of GenericMessage corresponding to a certain version.  
40       * This is needed so that version-specific segments can be added as the message
41       * is parsed.
42       *
43       * @param version the HL7 version
44       * @return the class of the generic message
45       */
46  	@SuppressWarnings("unchecked")
47  	public static Class<? extends Message> getGenericMessageClass(String version) {
48  		
49  		Version v = Version.versionOf(version);
50          if (v != null) {       
51          	try {
52          		String className = GenericMessage.class.getName() + "$" + v.name();
53          		return (Class<? extends Message>)Class.forName(className);
54          	} catch (ClassNotFoundException e) {
55          		// should not happen as long Version corresponds with the static
56          		// subclasses of GenericMessage
57          	}
58          }
59  
60          log.debug("Unknown version for generic type {}", version);
61          return UnknownVersion.class;
62      }
63  
64  
65      public static class UnknownVersion extends GenericMessage {
66  		private static final long serialVersionUID = 4773366840392833956L;
67  
68  		public UnknownVersion(ModelClassFactory factory) {
69              super(factory);
70          }
71          public String getVersion() {
72          	return Version.latestVersion().getVersion();
73          }
74  
75      }
76  
77      public static class V21 extends GenericMessage {
78  		private static final long serialVersionUID = 4773366840392833956L;
79  		public V21(ModelClassFactory factory) {
80              super(factory);
81          }
82          public String getVersion() { return Version.V21.getVersion(); }
83      }
84      
85      public static class V22 extends GenericMessage {
86  		private static final long serialVersionUID = 7124224621400437244L;
87  		public V22(ModelClassFactory factory) {
88              super(factory);
89          }
90          public String getVersion() { return Version.V22.getVersion(); }
91      }
92      
93      public static class V23 extends GenericMessage {
94  		private static final long serialVersionUID = -8794048442253389190L;
95  		public V23(ModelClassFactory factory) {
96              super(factory);
97          }
98          public String getVersion() { return Version.V23.getVersion(); }
99      }
100     
101     public static class V231 extends GenericMessage {
102 		private static final long serialVersionUID = 5979370667795858995L;
103 		public V231(ModelClassFactory factory) {
104             super(factory);
105         }
106         public String getVersion() { return Version.V231.getVersion(); }
107     }
108     
109     public static class V24 extends GenericMessage {
110 		private static final long serialVersionUID = -4944772617600551061L;
111 		public V24(ModelClassFactory factory) {
112             super(factory);
113         }
114         public String getVersion() { return Version.V24.getVersion(); }
115     }
116     
117     public static class V25 extends GenericMessage {
118 		private static final long serialVersionUID = 3937051332672967947L;
119 		public V25(ModelClassFactory factory) {
120             super(factory);
121         }
122         public String getVersion() { return Version.V25.getVersion(); }
123     }
124     
125     public static class V251 extends GenericMessage {
126 		private static final long serialVersionUID = -767282165482145544L;
127 		public V251(ModelClassFactory factory) {
128             super(factory);
129         }
130         public String getVersion() { return Version.V251.getVersion(); }
131     }
132 
133     public static class V26 extends GenericMessage {
134 		private static final long serialVersionUID = -1693277295151324396L;
135 		public V26(ModelClassFactory factory) {
136             super(factory);
137         }
138         public String getVersion() { return Version.V26.getVersion(); }
139     }
140 
141 }