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 java.util.HashMap; 30 import java.util.Map; 31 32 import ca.uhn.hl7v2.DefaultHapiContext; 33 import ca.uhn.hl7v2.HL7Exception; 34 import ca.uhn.hl7v2.HapiContext; 35 import ca.uhn.hl7v2.Version; 36 import ca.uhn.hl7v2.parser.PipeParser; 37 import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder; 38 import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder; 39 40 /** 41 * Message Validation using the provided ValidationContext implementations 42 * 43 */ 44 public class CustomMessageValidationWithValidatingVisitor { 45 46 public static void main(String[] args) { 47 48 /* 49 * In this example, we are looking at a more advanced aspect of message 50 * validation using HAPI. We will use a MessageVisitor instead of a Terser 51 * to validate our message during parsing. 52 * 53 * The following message will be used in the example: 54 * 55 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4 56 * EVN|A31|200903230934 57 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M|||||||||||||||||||||| 58 */ 59 String message = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n" 60 + "EVN|A31|200903230934\r\n" 61 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||"; 62 63 64 HapiContext context = new DefaultHapiContext(); 65 66 /* 67 * Define the mandatory fields and visitor factory 68 */ 69 final Map<String, Integer[]> map = new HashMap<>(); 70 map.put("MSH", new Integer[] {2, 3, 4, 5, 6, 7, 9, 10, 12}); 71 map.put("PID", new Integer[] {2, 3, 5, 6, 7}); 72 final MandatoryFieldsdatoryFields">MandatoryFields mandatoryFields = new MandatoryFields(map); 73 74 /* 75 * Extend the default validation context by a message rule that uses the 76 * MandatoryFields visitor to check if the mandatory fields are actually 77 * present. 78 */ 79 ValidationRuleBuilder builder = new DefaultValidationBuilder() { 80 81 @Override 82 protected void configure() { 83 super.configure(); 84 forVersion(Version.V24) 85 .message("ADT", "*") 86 .inspect(mandatoryFields); 87 } 88 89 }; 90 context.setValidationRuleBuilder(builder); 91 context.getParserConfiguration().setValidating(true); 92 93 // Let's parse and validate the message: 94 PipeParser parser = context.getPipeParser(); 95 try { 96 parser.parse(message); 97 System.out.println("Something went wrong!"); 98 } catch (HL7Exception e) { 99 System.out.println("As expected we found a problem:"); 100 System.out.println(e.getMessage()); 101 } 102 103 } 104 105 }