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