View Javadoc
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 Original Code is "Message.java".  Description: 
10  "Represents a complete HL7 message including all structures, segments, and fields" 
11  
12  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
13  2001.  All Rights Reserved. 
14  
15  Contributor(s): ______________________________________. 
16  
17  Alternatively, the contents of this file may be used under the terms of the 
18  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
19  applicable instead of those above.  If you wish to allow use of your version of this 
20  file only under the terms of the GPL and not to allow others to use your version 
21  of this file under the MPL, indicate your decision by deleting  the provisions above 
22  and replace  them with the notice and other provisions required by the GPL License.  
23  If you do not delete the provisions above, a recipient may use your version of 
24  this file under either the MPL or the GPL. 
25  
26   */
27  
28  package ca.uhn.hl7v2.model;
29  
30  import java.io.IOException;
31  
32  import ca.uhn.hl7v2.AcknowledgmentCode;
33  import ca.uhn.hl7v2.HL7Exception;
34  import ca.uhn.hl7v2.parser.Parser;
35  
36  /**
37   * <p>
38   * Represents a complete HL7 message including all structures, segments, and fields.
39   * </p>
40   * <p>
41   * Note this it is not recommended to implement this interface directly, as it is subject to change.
42   * Instead, extend abstract implementations for your model classes, such as {@link AbstractMessage}
43   * and {@link AbstractGroup}
44   * </p>
45   * 
46   * @author Bryan Tripp (bryan_tripp@sourceforge.net)
47   */
48  public interface Message extends Group {
49  
50  	/**
51  	 * Returns the version number of the HL7 version in which this message structure is defined
52  	 * (e.g. "2.4")
53       * @return version number of the HL7 version
54  	 */
55      String getVersion();
56  
57  	/**
58  	 * Convenience method which retrieves the field separator value from the first field of the
59  	 * first segment.
60  	 * 
61  	 * Typically, the first segment is MSH, so this method will retrieve the value of MSH-1.
62  	 * 
63  	 * @return The field separator
64  	 * @throws HL7Exception If an error occurs
65  	 * @since 1.0
66  	 */
67      Character getFieldSeparatorValue() throws HL7Exception;
68  
69  	/**
70  	 * Convenience method which retrieves the encoding characters value from the second field of the
71  	 * first segment.
72  	 * 
73  	 * Typically, the first segment is MSH, so this method will retrieve the value of MSH-2.
74  	 * 
75  	 * @return The encoding characters
76  	 * @throws HL7Exception If an error occurs
77  	 * @since 1.0
78  	 */
79      String getEncodingCharactersValue() throws HL7Exception;
80  
81  	/**
82  	 * Sets the parser to be used when parse/encode methods are called on this Message, as well as
83  	 * its children. It is recommended that if these methods are going to be called, a parser be
84  	 * supplied with the validation context wanted. Where possible, the parser should be reused for
85  	 * best performance, unless thread safety is an issue.
86  	 * 
87  	 * Note that not all parsers can be used. As of version 1.0, only PipeParser supports
88  	 * this functionality
89       *
90       * @param parser the parser to be used when parse/encode methods are called on this Message
91  	 */
92      void setParser(Parser parser);
93  
94  	/**
95  	 * Returns the parser to be used when parse/encode methods are called on this Message, as well
96  	 * as its children. The default value is a new PipeParser.
97       *
98       * @return the parser to be used when parse/encode methods are called on this Message
99  	 */
100     Parser getParser();
101 
102 	/**
103 	 * Parses the string into this message using the parser returned by {@link #getParser() }
104      * @param string the message to be parsed
105      * @throws HL7Exception if errors occurred during parsing
106 	 */
107     void parse(String string) throws HL7Exception;
108 
109 	/**
110 	 * Encodes this message using the parser returned by {@link #getParser() }
111      * @return the string-encoded message
112      * @throws HL7Exception if error occurred during encoding
113 	 */
114     String encode() throws HL7Exception;
115 
116 	/**
117 	 * <p>
118 	 * Generates and returns an ACK message which would be used to acknowledge this message
119 	 * successfully, with an MSA-1 code of "AA". The ACK generated will be of the same version as
120 	 * the value of MSH-12 in this message (as opposed to the version of the message class instance,
121 	 * if they are different)
122 	 * </p>
123 	 * 
124 	 * <p>
125 	 * Note that this method will fail if it is not possible to generate an ACK for any reason, such
126 	 * as
127 	 * <ul>
128 	 * <li>Message version is invalid</li>
129 	 * <li>First segment is not an MSH</li>
130 	 * </p>
131 	 *
132      * @return the acknowledgment message
133 	 * @throws HL7Exception If the message can not be constructed
134 	 * @throws IOException If a failure occurs in generating a control ID for the message
135 	 */
136     Message generateACK() throws HL7Exception, IOException;
137 
138 	/**
139 	 * <p>
140 	 * Generates and returns an ACK message which would be used to acknowledge this message
141 	 * successfully. The ACK generated will be of the same version as the value of MSH-12 in this
142 	 * message (as opposed to the version of the message class instance, if they are different)
143 	 * </p>
144 	 * 
145 	 * <p>
146 	 * Note that this method will fail if it is not possible to generate an ACK for any reason, such
147 	 * as
148 	 * <ul>
149 	 * <li>Message version is invalid</li>
150 	 * <li>First segment is not an MSH</li>
151 	 * </p>
152 	 * 
153 	 * @param theAcknowldegementCode The acknowledement code (MSA-1) to supply. If null, defaults to
154 	 *            "AA". To generate a typical NAK, use "AE"
155 	 * @param theException The exceptions used to populate the ERR segment (if any)
156 	 * @throws HL7Exception If the message can not be constructed
157 	 * @throws IOException If a failure occurs in generating a control ID for the message
158 	 * 
159 	 * @deprecated use {@link #generateACK(AcknowledgmentCode, HL7Exception)}
160 	 */
161     Message generateACK(String theAcknowldegementCode, HL7Exception theException)
162 			throws HL7Exception, IOException;
163 
164 	/**
165 	 * <p>
166 	 * Generates and returns an ACK message which would be used to acknowledge this message
167 	 * successfully. The ACK generated will be of the same version as the value of MSH-12 in this
168 	 * message (as opposed to the version of the message class instance, if they are different)
169 	 * </p>
170 	 * 
171 	 * <p>
172 	 * Note that this method will fail if it is not possible to generate an ACK for any reason, such
173 	 * as
174 	 * <ul>
175 	 * <li>Message version is invalid</li>
176 	 * <li>First segment is not an MSH</li>
177 	 * </p>
178 	 * 
179 	 * @param theAcknowlegementCode If null, defaults to
180 	 *            AcknowledgmentCode.AA. To generate a typical NAK, use AcknowledgmentCode.AE
181 	 * @param theException The exceptions used to populate the ERR segment (if any)
182      * @return the acknoeldgement message
183 	 * @throws HL7Exception If the message can not be constructed
184 	 * @throws IOException If a failure occurs in generating a control ID for the message
185 	 */
186     Message generateACK(AcknowledgmentCode theAcknowlegementCode, HL7Exception theException)
187 			throws HL7Exception, IOException;	
188 	/**
189 	 * <p>
190 	 * Prints a summary of the contents and structure of this message. This is useful for debugging
191 	 * purposes, if you want to figure out where in the structure of a message a given segment has
192 	 * been placed.
193 	 * </p>
194 	 * <p>
195 	 * For instance, the following message (containing a few quirks for demonstration purposes):
196 	 * <code>
197 	 * <pre>MSH|^~\\&|^QueryServices||||20021011161756.297-0500||ADT^A01|1|D|2.4\r
198 	 * EVN|R01
199 	 * EVN|R02
200 	 * PID|1
201 	 * IN1|1
202 	 * IN1|2
203 	 * PID|2</pre></code> ...produces the following output: <code>
204 	 * <pre>ADT_A01 (start)
205 	 *    MSH - MSH|^~\&|^QueryServices||||20021011161756.297-0500||ADT^A01|1|D|2.4
206 	 *    EVN - EVN|R01
207 	 *    [ { EVN2 } ] (non-standard) - EVN|R02
208 	 *    PID - PID|1
209 	 *    [ PD1 ] - Not populated
210 	 *    [ { ROL } ] - Not populated
211 	 *    [ { NK1 } ] - Not populated
212 	 *    PV1 - Not populated
213 	 *    [ PV2 ] - Not populated
214 	 *    [ { ROL2 } ] - Not populated
215 	 *    [ { DB1 } ] - Not populated
216 	 *    [ { OBX } ] - Not populated
217 	 *    [ { AL1 } ] - Not populated
218 	 *    [ { DG1 } ] - Not populated
219 	 *    [ DRG ] - Not populated
220 	 *    PROCEDURE (start)
221 	 *    [{
222 	 *       PR1 - Not populated
223 	 *       [ { ROL } ] - Not populated
224 	 *    }]
225 	 *    PROCEDURE (end)
226 	 *    [ { GT1 } ] - Not populated
227 	 *    INSURANCE (start)
228 	 *    [{
229 	 *       IN1 - IN1|1
230 	 *       [ IN2 ] - Not populated
231 	 *       [ { IN3 } ] - Not populated
232 	 *       [ { ROL } ] - Not populated
233 	 *    }]
234 	 *    [{
235 	 *       IN1 - IN1|2
236 	 *       [ { PID } ] (non-standard) - PID|2
237 	 *       [ IN2 ] - Not populated
238 	 *       [ { IN3 } ] - Not populated
239 	 *       [ { ROL } ] - Not populated
240 	 *    }]
241 	 *    INSURANCE (end)
242 	 *    [ ACC ] - Not populated
243 	 *    [ UB1 ] - Not populated
244 	 *    [ UB2 ] - Not populated
245 	 *    [ PDA ] - Not populated
246 	 * ADT_A01 (end)
247 	 * </pre></code>
248 	 * </p>
249 	 * 
250 	 * @return A summary of the structure
251 	 * @throws HL7Exception If any problems occur encoding the structure
252 	 */
253     String printStructure() throws HL7Exception;
254 
255 }