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 Original Code is "Message.java".  Description: 
010"Represents a complete HL7 message including all structures, segments, and fields" 
011
012The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0132001.  All Rights Reserved. 
014
015Contributor(s): ______________________________________. 
016
017Alternatively, the contents of this file may be used under the terms of the 
018GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
019applicable instead of those above.  If you wish to allow use of your version of this 
020file only under the terms of the GPL and not to allow others to use your version 
021of this file under the MPL, indicate your decision by deleting  the provisions above 
022and replace  them with the notice and other provisions required by the GPL License.  
023If you do not delete the provisions above, a recipient may use your version of 
024this file under either the MPL or the GPL. 
025
026 */
027
028package ca.uhn.hl7v2.model;
029
030import java.io.IOException;
031
032import ca.uhn.hl7v2.AcknowledgmentCode;
033import ca.uhn.hl7v2.HL7Exception;
034import ca.uhn.hl7v2.parser.Parser;
035
036/**
037 * <p>
038 * Represents a complete HL7 message including all structures, segments, and fields.
039 * </p>
040 * <p>
041 * Note this it is not recommended to implement this interface directly, as it is subject to change.
042 * Instead, extend abstract implementations for your model classes, such as {@link AbstractMessage}
043 * and {@link AbstractGroup}
044 * </p>
045 * 
046 * @author Bryan Tripp (bryan_tripp@sourceforge.net)
047 */
048public interface Message extends Group {
049
050        /**
051         * Returns the version number of the HL7 version in which this message structure is defined
052         * (e.g. "2.4")
053     * @return version number of the HL7 version
054         */
055    String getVersion();
056
057        /**
058         * Convenience method which retrieves the field separator value from the first field of the
059         * first segment.
060         * 
061         * Typically, the first segment is MSH, so this method will retrieve the value of MSH-1.
062         * 
063         * @return The field separator
064         * @throws HL7Exception If an error occurs
065         * @since 1.0
066         */
067    Character getFieldSeparatorValue() throws HL7Exception;
068
069        /**
070         * Convenience method which retrieves the encoding characters value from the second field of the
071         * first segment.
072         * 
073         * Typically, the first segment is MSH, so this method will retrieve the value of MSH-2.
074         * 
075         * @return The encoding characters
076         * @throws HL7Exception If an error occurs
077         * @since 1.0
078         */
079    String getEncodingCharactersValue() throws HL7Exception;
080
081        /**
082         * Sets the parser to be used when parse/encode methods are called on this Message, as well as
083         * its children. It is recommended that if these methods are going to be called, a parser be
084         * supplied with the validation context wanted. Where possible, the parser should be reused for
085         * best performance, unless thread safety is an issue.
086         * 
087         * Note that not all parsers can be used. As of version 1.0, only PipeParser supports
088         * this functionality
089     *
090     * @param parser the parser to be used when parse/encode methods are called on this Message
091         */
092    void setParser(Parser parser);
093
094        /**
095         * Returns the parser to be used when parse/encode methods are called on this Message, as well
096         * as its children. The default value is a new PipeParser.
097     *
098     * @return the parser to be used when parse/encode methods are called on this Message
099         */
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}