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