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