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 "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.DefaultHapiContext;
30  import ca.uhn.hl7v2.HL7Exception;
31  import ca.uhn.hl7v2.HapiContext;
32  import ca.uhn.hl7v2.model.Message;
33  import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
34  import ca.uhn.hl7v2.parser.PipeParser;
35  import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
36  
37  /**
38   * Message Validation using the provided ValidationContext implementations
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 HL7Exception {
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  		HapiContext context = new DefaultHapiContext();
73  		
74  		/*
75  		 * This is 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  		context.setValidationContext(ValidationContextFactory.defaultValidation());	
81  
82  		// Let's try parsing the valid message:
83  		PipeParser parser = context.getPipeParser();
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  		/*
94  		 * Next, let's set EVN-2 to a string that is longer than 200 chars.
95  		 * DefaultValidation specified that ID datatypes must not exceed this
96  		 *length
97  		 */
98  		String invalidMessage = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n"
99  				+ "EVN|0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|200903230934\r\n"
100 				+ "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
101 
102 		// Let's try parsing the valid message:
103 		try {
104 			parser.parse(invalidMessage);
105 			// This shouldn't happen!
106 			System.out.println("Something went wrong!");
107 			System.exit(-1);
108 		} catch (HL7Exception e) {
109 			// This time, we are expecting an exception, because the message
110 			// should fail validation.
111 			System.out.println("As expected, the message did not validate: "
112 					+ e.getMessage());
113 			/*
114 			 * Prints: 
115 			 * As expected, the message did not validate: Failed validation rule: Maxumim size <= 200 characters: Segment: EVN (rep 0) Field #1
116 			 */
117 		}
118 
119         /*
120          * The ValidationContext is used during parsing and well as during
121          * validation using {@link ca.uhn.hl7v2.validation.Validator} objects.
122          * Sometimes we want parsing without validation followed by a
123          * separate validation step. We can still use a single HapiContext.
124          */
125 
126         context.getParserConfiguration().setValidating(false);
127         try {
128             parser.parse(invalidMessage);
129             System.out.println("Successfully parsed valid 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 		 * Now, suppose we want to throw caution to the wind, and not do 
138 		 * any validation. This is fairly common practice in the real
139 		 * world, since sending systems don't always behave as nicely as
140 		 * we might want.
141 		 */
142 		context.setValidationContext(ValidationContextFactory.noValidation());
143 		
144 		try {
145 			parser.parse(invalidMessage);
146 			System.out.println("Successfully parsed invalid message");
147 		} catch (HL7Exception e) {
148 			// This shouldn't happen!
149 			System.out.println("Something went wrong!");
150 			System.exit(-1);
151 		}
152 		
153 		/*
154 		 * One important thing to note is that NoValidation still includes one
155 		 * rule: A rule which strips leading space from FT, ST, and trailing
156 		 * space from TX fields.
157 		 * 
158 		 * Let's add some leading space to MSH-10 (this isn't something you would 
159 		 * want to do normally, but it does demonstrate leading space trimming from
160 		 * ST datatypes) 
161 		 */
162 		invalidMessage = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|             CONTROLID|P^T|2.4\r\n"
163 			+ "EVN|A03|200903230934\r\n"
164 			+ "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
165 		try {
166 			Message parsedMessage = parser.parse(invalidMessage);
167 			
168 			// Print the message back out
169 			System.out.println(parser.encode(parsedMessage));
170 			
171 			/*
172 			 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|CONTROLID|P^T|2.4
173              * EVN|A03|200903230934
174 			 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M
175 			 */
176 			
177 		} catch (HL7Exception e) {
178 			e.printStackTrace();
179 		}
180 		
181 		
182 	}
183 
184 }