001package ca.uhn.hl7v2.model;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006import ca.uhn.hl7v2.HL7Exception;
007import ca.uhn.hl7v2.Version;
008import ca.uhn.hl7v2.parser.ModelClassFactory;
009
010/**
011 * A generic HL7 message, meant for parsing message with unrecognized structures
012 * into a flat list of segments.
013 * 
014 * @author Bryan Tripp
015 * @author Christian Ohr
016 */
017@SuppressWarnings("serial")
018public abstract class GenericMessage extends AbstractMessage {
019    
020        private static final Logger log = LoggerFactory.getLogger(GenericMessage.class);
021        
022    /** 
023     * Creates a new instance of GenericMessage. 
024     * 
025     * @param factory class factory for contained structures 
026     */
027    public GenericMessage(ModelClassFactory factory) {
028        super(factory);
029        try {
030            this.addNonstandardSegment("MSH");
031        } catch(HL7Exception e) {
032            String message = "Unexpected error adding GenericSegment to GenericMessage.";
033            log.error(message, e);
034            throw new Error(message); // TODO better throw RuntimeException
035        }        
036    }
037    
038    /**
039     * Returns a subclass of GenericMessage corresponding to a certain version.  
040     * This is needed so that version-specific segments can be added as the message
041     * is parsed.
042     *
043     * @param version the HL7 version
044     * @return the class of the generic message
045     */
046        @SuppressWarnings("unchecked")
047        public static Class<? extends Message> getGenericMessageClass(String version) {
048                
049                Version v = Version.versionOf(version);
050        if (v != null) {       
051                try {
052                        String className = GenericMessage.class.getName() + "$" + v.name();
053                        return (Class<? extends Message>)Class.forName(className);
054                } catch (ClassNotFoundException e) {
055                        // should not happen as long Version corresponds with the static
056                        // subclasses of GenericMessage
057                }
058        }
059
060        log.debug("Unknown version for generic type {}", version);
061        return UnknownVersion.class;
062    }
063
064
065    public static class UnknownVersion extends GenericMessage {
066                private static final long serialVersionUID = 4773366840392833956L;
067                public UnknownVersion(ModelClassFactory factory) {
068            super(factory);
069        }
070        public String getVersion() {
071                return Version.latestVersion().getVersion();
072        }
073
074    }
075
076    public static class V21 extends GenericMessage {
077                private static final long serialVersionUID = 4773366840392833956L;
078                public V21(ModelClassFactory factory) {
079            super(factory);
080        }
081        public String getVersion() { return Version.V21.getVersion(); }
082    }
083    
084    public static class V22 extends GenericMessage {
085                private static final long serialVersionUID = 7124224621400437244L;
086                public V22(ModelClassFactory factory) {
087            super(factory);
088        }
089        public String getVersion() { return Version.V22.getVersion(); }
090    }
091    
092    public static class V23 extends GenericMessage {
093                private static final long serialVersionUID = -8794048442253389190L;
094                public V23(ModelClassFactory factory) {
095            super(factory);
096        }
097        public String getVersion() { return Version.V23.getVersion(); }
098    }
099    
100    public static class V231 extends GenericMessage {
101                private static final long serialVersionUID = 5979370667795858995L;
102                public V231(ModelClassFactory factory) {
103            super(factory);
104        }
105        public String getVersion() { return Version.V231.getVersion(); }
106    }
107    
108    public static class V24 extends GenericMessage {
109                private static final long serialVersionUID = -4944772617600551061L;
110                public V24(ModelClassFactory factory) {
111            super(factory);
112        }
113        public String getVersion() { return Version.V24.getVersion(); }
114    }
115    
116    public static class V25 extends GenericMessage {
117                private static final long serialVersionUID = 3937051332672967947L;
118                public V25(ModelClassFactory factory) {
119            super(factory);
120        }
121        public String getVersion() { return Version.V25.getVersion(); }
122    }
123    
124    public static class V251 extends GenericMessage {
125                private static final long serialVersionUID = -767282165482145544L;
126                public V251(ModelClassFactory factory) {
127            super(factory);
128        }
129        public String getVersion() { return Version.V251.getVersion(); }
130    }
131
132    public static class V26 extends GenericMessage {
133                private static final long serialVersionUID = -1693277295151324396L;
134                public V26(ModelClassFactory factory) {
135            super(factory);
136        }
137        public String getVersion() { return Version.V26.getVersion(); }
138    }
139
140    public static class V27 extends GenericMessage {
141                private static final long serialVersionUID = -1693277295151324396L;
142                public V27(ModelClassFactory factory) {
143            super(factory);
144        }
145        public String getVersion() { return Version.V27.getVersion(); }
146    }
147
148    public static class V271 extends GenericMessage {
149                private static final long serialVersionUID = -1693277295151324396L;
150                public V271(ModelClassFactory factory) {
151            super(factory);
152        }
153        public String getVersion() { return Version.V271.getVersion(); }
154    }
155
156    public static class V28 extends GenericMessage {
157                private static final long serialVersionUID = -1693277295151324396L;
158                public V28(ModelClassFactory factory) {
159            super(factory);
160        }
161        public String getVersion() { return Version.V28.getVersion(); }
162    }
163
164    public static class V281 extends GenericMessage {
165                private static final long serialVersionUID = -1693277295151324396L;
166                public V281(ModelClassFactory factory) {
167            super(factory);
168        }
169        public String getVersion() { return Version.V281.getVersion(); }
170    }
171
172}