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