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.HL7Exception;
9   import ca.uhn.hl7v2.conf.ProfileException;
10  import ca.uhn.hl7v2.conf.check.DefaultValidator;
11  import ca.uhn.hl7v2.conf.parser.ProfileParser;
12  import ca.uhn.hl7v2.conf.spec.RuntimeProfile;
13  import ca.uhn.hl7v2.model.Message;
14  import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
15  import ca.uhn.hl7v2.parser.PipeParser;
16  import ca.uhn.hl7v2.validation.impl.DefaultValidation;
17  import ca.uhn.hl7v2.validation.impl.NoValidation;
18  import ca.uhn.hl7v2.validation.impl.ValidationContextImpl;
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 HL7Exception
32  	 * @throws IOException 
33  	 * @throws ProfileException 
34  	 * @throws HL7Exception 
35  	 */
36  	public static void main(String[] args) throws ProfileException, IOException, HL7Exception {
37  
38  		/*
39  		 * In this example, we are looking at a few aspects of message
40  		 * validation using HAPI.
41  		 * 
42  		 * The following message will be used in the examples:
43  		 * 
44  		 * MSH|^~\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4
45  		 * EVN|A31|200903230934
46  		 * PID|1||29^^CAISI_1-2^PI~""||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||
47  		 */
48  		String validMessageString = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r\n"
49  				+ "EVN|A31|200903230934\r\n"
50  				+ "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
51  		Message validMessage = new PipeParser().parse(validMessageString);
52  
53  		// Load a conformance profile
54  		ProfileParser profileParser = new ProfileParser(false);
55  		RuntimeProfile profile = profileParser.parseClasspath("ca/uhn/hl7v2/examples/profiles/ADT_A31.xml");
56  
57  		// Create a conformance validator, and validate
58  		DefaultValidator validator = new DefaultValidator();
59  		HL7Exception[] exceptions = validator.validate(validMessage, profile.getMessage());
60  		
61  		System.out.println("Found " + exceptions.length + " problems");
62  		for (int i = 0; i < exceptions.length; i++) {
63  			HL7Exception exception = exceptions[i];
64  			System.out.println(" * " + exception.getClass().getSimpleName() + " - " + exception.getMessage());
65  		}
66  		
67  		/*
68  		 * Prints:
69  		 * 
70  		 * Found 10 problems
71  		 * ProfileNotHL7CompliantException - HL7 datatype ST doesn't match profile datatype NM: Segment: MSH Field #7
72  		 * ProfileNotHL7CompliantException - HL7 datatype MSG doesn't match profile datatype CM_MSG: Segment: MSH Field #9
73  		 * ProfileNotHL7CompliantException - HL7 datatype ST doesn't match profile datatype NM: Segment: EVN Field #2
74  		 * XElementPresentException - Field 1 in EVN appears in the message but not in the profile: Segment: EVN
75  		 * XElementPresentException - Element code identifying the check digit scheme employed is present but specified as not used (X): Segment: PID Field #3
76  		 * ProfileNotFollowedException - Required element identifier type code (ID) is missing: Segment: PID Field #3
77  		 * ProfileNotFollowedException - Required element assigning authority is missing: Segment: PID Field #3
78  		 * ProfileNotFollowedException - Required element identifier type code (ID) is missing: Segment: PID Field #3
79  		 * ProfileNotHL7CompliantException - HL7 datatype ST doesn't match profile datatype NM: Segment: PID Field #7
80  		 * XElementPresentException - Field 1 in PID appears in the message but not in the profile: Segment: PID
81  		 */
82  		
83  	}
84  
85  }