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 "MessageValidation.java". Description:
10 * "Example Code"
11 *
12 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13 * 2001. All Rights Reserved.
14 *
15 * Contributor(s): James Agnew
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 package ca.uhn.hl7v2.examples;
28
29 import ca.uhn.hl7v2.HL7Exception;
30 import ca.uhn.hl7v2.model.Message;
31 import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
32 import ca.uhn.hl7v2.parser.PipeParser;
33 import ca.uhn.hl7v2.validation.impl.DefaultValidation;
34 import ca.uhn.hl7v2.validation.impl.NoValidation;
35 import ca.uhn.hl7v2.validation.impl.ValidationContextImpl;
36
37 /**
38 * TODO: add!
39 *
40 * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
41 * @version $Revision: 1.3 $ updated on $Date: 2009/08/09 13:59:46 $ by $Author:
42 * jamesagnew $
43 */
44 public class MessageValidation {
45
46 /**
47 * @param args
48 * @throws HL7Exception
49 * @throws EncodingNotSupportedException
50 */
51 public static void main(String[] args) throws EncodingNotSupportedException {
52
53 /*
54 * In this example, we are looking at a few aspects of message
55 * validation using HAPI.
56 *
57 * The following message will be used in the examples:
58 *
59 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4
60 * EVN|A31|200903230934
61 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||
62 */
63 String validMessage = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n"
64 + "EVN|A31|200903230934\r\n"
65 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
66
67 /*
68 * Let's start by constructing a parser using default settings. By
69 * default, a parser has certain validation settings, as defined by the
70 * DefaultValidation class.
71 */
72 PipeParser parser = new PipeParser();
73
74 /*
75 * These two lines are actually redundant, since this is the default
76 * validator. The default validation includes a number of sensible
77 * defaults including maximum lengths on string types, formats for
78 * telephone numbers and timestamps, etc.
79 */
80 DefaultValidation defaultValidation = new DefaultValidation();
81 parser.setValidationContext(defaultValidation);
82
83 // Let's try parsing the valid message:
84 try {
85 parser.parse(validMessage);
86 System.out.println("Successfully parsed valid message");
87 } catch (HL7Exception e) {
88 // This shouldn't happen!
89 System.out.println("Something went wrong!");
90 System.exit(-1);
91 }
92
93 // Next, let's set EVN-2 to a string that is longer than 200 chars.
94 // DefaultValidation specified that ID datatypes must not exceed this
95 // length
96 String invalidMessage = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n"
97 + "EVN|0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|200903230934\r\n"
98 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
99
100 // Let's try parsing the valid message:
101 try {
102 parser.parse(invalidMessage);
103 // This shouldn't happen!
104 System.out.println("Something went wrong!");
105 System.exit(-1);
106 } catch (HL7Exception e) {
107 // This time, we are expecing an exception, because the message
108 // should fail validation.
109 System.out.println("As expected, the message did not validate: "
110 + e.getMessage());
111 /*
112 * Prints:
113 * As expected, the message did not validate: Failed validation rule: Maxumim size <= 200 characters: Segment: EVN (rep 0) Field #1
114 */
115
116 }
117
118 /*
119 * Now, suppose we want to throw caution to the wind, and not do
120 * any validation. This is fairly common practice in the real
121 * world, since sending systems don't always behave as nicely as
122 * we might want.
123 */
124 NoValidation noValidation = new NoValidation();
125 parser.setValidationContext(noValidation);
126
127 try {
128 parser.parse(invalidMessage);
129 System.out.println("Successfully parsed invalid message");
130 } catch (HL7Exception e) {
131 // This shouldn't happen!
132 System.out.println("Something went wrong!");
133 System.exit(-1);
134 }
135
136 /*
137 * One important thing to note is that NoValidation still includes one
138 * rule: A rule which strips leading space from FT, ST, and TX fields.
139 *
140 * Let's add some leading space to MSH-10 (this isn't something you would
141 * want to do normally, but it does demonstrate leading space trimming from
142 * ST datatypes)
143 */
144 invalidMessage = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05| CONTROLID|P^T|2.4\r\n"
145 + "EVN|A03|200903230934\r\n"
146 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
147 try {
148 Message parsedMessage = parser.parse(invalidMessage);
149
150 // Print the mesage back out
151 System.out.println(new PipeParser().encode(parsedMessage));
152
153 /*
154 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|CONTROLID|P^T|2.4
155 * EVN|A03|200903230934
156 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M
157 */
158
159 } catch (HL7Exception e) {
160 e.printStackTrace();
161 }
162
163
164 }
165
166 }