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.Version;
33  import ca.uhn.hl7v2.model.Message;
34  import ca.uhn.hl7v2.parser.PipeParser;
35  import ca.uhn.hl7v2.validation.DefaultValidationExceptionHandler;
36  import ca.uhn.hl7v2.validation.ValidationException;
37  import ca.uhn.hl7v2.validation.ValidationExceptionHandler;
38  import ca.uhn.hl7v2.validation.Validator;
39  import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
40  import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
41  import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder;
42  import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
43  
44  /**
45   * Developing and using custom Message Validation rules and applying them to messages.
46   * 
47   * @author Christian Ohr
48   */
49  @SuppressWarnings("serial")
50  public class CustomMessageValidation {
51  
52  	/**
53  	 * @param args
54       */
55  	public static void main(String[] args) {
56  
57  		/*
58  		 * In this example, we are looking at a few aspects of custom message validation using HAPI.
59  		 * 
60  		 * The following message will be used in the examples:
61  		 * 
62  		 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895
63  		 * |P^T|2.4 EVN|A31|200903230934
64  		 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||
65  		 */
66  		String message1 = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r"
67  				+ "EVN|A31|200903230934\r"
68  				+ "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
69  
70  		/*
71  		 * Let's start by adding a validation rule to the default validation that disallows PID-2 to
72  		 * be empty. Normally you would write this in its own .java file instead of as anonymous
73  		 * class.
74  		 */
75  
76  		ValidationRuleBuilder builder = new DefaultValidationBuilder() {
77  
78  			@Override
79  			protected void configure() {
80  				super.configure();
81  				forVersion(Version.V24)
82  				    .message("ADT", "*")
83  				    .terser("PID-2", not(empty()));
84  			}
85  
86  		};
87  
88  		// Set up a parser using the validation rules
89  		HapiContext context = new DefaultHapiContext();
90  		context.setValidationRuleBuilder(builder);
91  		PipeParser parser = context.getPipeParser();
92  
93  		// Let's try parsing the message:
94  		try {
95  			parser.parse(message1);
96  			// This should not happen
97  			System.out.println("Something went wrong!");
98  			System.exit(-1);
99  		} catch (HL7Exception e) {
100 			System.out.println("As expected, the message did not validate: " + e.getMessage());
101 		}
102 
103 		/*
104 		 * In the next example, we reject a message with "unknown" segments, but this time not
105 		 * during parsing but in a separate step.
106 		 */
107 		String message2 = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r"
108 				+ "EVN|A31|200903230934\r"
109 				+ "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||\r"
110 				+ "Z04|bogus segment";
111 
112 		// Change validation context for all dependent parsers
113 		context.setValidationContext(ValidationContextFactory.noValidation());
114 
115 		Message message = null;
116 		try {
117 			message = parser.parse(message2);
118 			System.out.println("Parsing succeeded, but ... ");
119 		} catch (HL7Exception e) {
120 			// This should not happen
121 			System.out.println("Something went wrong!");
122 			System.exit(-1);
123 		}
124 
125 		/*
126 		 * Let's add a validation rule that rejects custom segments
127 		 */
128 		ValidationRuleBuilder builder2 = new NoValidationBuilder() {
129 
130 			@Override
131 			protected void configure() {
132 				super.configure();
133 				forVersion(Version.V24)
134 				    .message("ADT", "*")
135 				    .onlyKnownSegments();
136 			}
137 
138 		};
139 		context.setValidationRuleBuilder(builder2);
140 
141 		try {
142 			context.getMessageValidator().validate(message);
143 			// This should not happen
144 			System.out.println("Something went wrong!");
145 			System.exit(-1);
146 		} catch (HL7Exception e) {
147 			System.out.println("as expected, the message did not validate: " + e.getMessage());
148 		}
149 
150 		/*
151 		 * Instead of throwing an HL7 Exception, the Validator can also be used in
152 		 * conjunction with a custom ValidationExceptionHandler, which would normally be defined in
153 		 * its own class file.
154 		 */
155 		ValidationExceptionHandler<Boolean> customHandler = new DefaultValidationExceptionHandler(context) {
156 
157 		    @Override
158 			public void onExceptions(ValidationException... exceptions) {
159 				super.onExceptions(exceptions);
160 				for (ValidationException e : exceptions) {
161 					System.out.println("Found Validation issues: " + e.getMessage());
162 				}
163 			}
164 		};
165 
166 		try {
167 		    Validator<Boolean> validator = context.getMessageValidator();
168 			boolean result = validator.validate(message, customHandler);
169 			System.out.println("The validator returned " + result);
170 		} catch (HL7Exception e) {
171 			// This should not happen
172 			System.out.println("Something went wrong!");
173 			System.exit(-1);
174 		}
175 	}
176 
177 }