View Javadoc

1   /*
2    * Created on Mar 23, 2009
3    */
4   package ca.uhn.hl7v2.examples;
5   
6   import java.io.IOException;
7   
8   import ca.uhn.hl7v2.DefaultHapiContext;
9   import ca.uhn.hl7v2.HL7Exception;
10  import ca.uhn.hl7v2.HapiContext;
11  import ca.uhn.hl7v2.Version;
12  import ca.uhn.hl7v2.conf.ProfileException;
13  import ca.uhn.hl7v2.conf.store.ClasspathProfileStore;
14  import ca.uhn.hl7v2.model.Message;
15  import ca.uhn.hl7v2.validation.Validator;
16  import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
17  import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
18  import ca.uhn.hl7v2.validation.impl.SimpleValidationExceptionHandler;
19  
20  /**
21   * TODO: add!
22   * 
23   * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
24   * @version $Revision: 1.2 $ updated on $Date: 2009-03-28 17:18:02 $ by $Author:
25   *          jamesagnew $
26   */
27  public class MessageValidationUsingConformanceProfile {
28  
29  	/**
30  	 * @param args
31  	 * @throws IOException 
32  	 * @throws ProfileException 
33  	 * @throws HL7Exception 
34  	 */
35  	public static void main(String[] args) throws ProfileException, IOException, HL7Exception {
36  
37  		/*
38  		 * In this example, we are looking at a few aspects of message
39  		 * validation using HAPI and conformance profiles.
40  		 * 
41  		 * The following message will be used in the examples:
42  		 * 
43  		 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4
44  		 * EVN|A31|200903230934
45  		 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||
46  		 */
47  
48          // Build a conformance validation rule (referring to the file ADT_A31.xml)
49          // on top of the standard validation rules
50          ValidationRuleBuilder builder = new DefaultValidationBuilder() {
51  
52              @Override
53              protected void configure() {
54                  super.configure();
55                  forVersion(Version.V24)
56                          .message("ADT", "*")
57                          .conformance("ADT_A31");
58              }
59  
60          };
61  
62          // Use this validation for our HapiContext, but disable validation during parsing
63          // and use a custom profile store that finds out conformance profile file.
64          HapiContext context = new DefaultHapiContext();
65          context.setValidationRuleBuilder(builder);
66          context.getParserConfiguration().setValidating(false);
67          context.setProfileStore(new ClasspathProfileStore("/ca/uhn/hl7v2/examples/profiles"));
68  
69  		String validMessageString = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n"
70  				+ "EVN|A31|200903230934\r\n"
71  				+ "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
72  		Message validMessage = context.getPipeParser().parse(validMessageString);
73  
74          // Instantiate a simple validation handler that just collects the validation
75          SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(context);
76  
77          Validator<Boolean> validator = context.getMessageValidator();
78  
79          if (!validator.validate(validMessage, handler)) {
80              System.out.println("Found " + handler.getExceptions().size() + " problems");
81              for (Exception e : handler.getExceptions()) {
82                  System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
83              }
84          }
85  
86  	}
87  
88  }