1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.PipeParser;
35 import ca.uhn.hl7v2.validation.DefaultValidationExceptionHandler;
36 import ca.uhn.hl7v2.validation.ValidationException;
37 import ca.uhn.hl7v2.validation.ValidationExceptionHandler;
38 import ca.uhn.hl7v2.validation.Validator;
39 import ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder;
40 import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
41 import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder;
42 import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
43
44
45
46
47
48
49 @SuppressWarnings("serial")
50 public class CustomMessageValidation {
51
52
53
54
55 public static void main(String[] args) {
56
57
58
59
60
61
62
63
64
65
66 String message1 = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r"
67 + "EVN|A31|200903230934\r"
68 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||";
69
70
71
72
73
74
75
76 ValidationRuleBuilder builder = new DefaultValidationBuilder() {
77
78 @Override
79 protected void configure() {
80 super.configure();
81 forVersion(Version.V24)
82 .message("ADT", "*")
83 .terser("PID-2", not(empty()));
84 }
85
86 };
87
88
89 HapiContext context = new DefaultHapiContext();
90 context.setValidationRuleBuilder(builder);
91 PipeParser parser = context.getPipeParser();
92
93
94 try {
95 parser.parse(message1);
96
97 System.out.println("Something went wrong!");
98 System.exit(-1);
99 } catch (HL7Exception e) {
100 System.out.println("As expected, the message did not validate: " + e.getMessage());
101 }
102
103
104
105
106
107 String message2 = "MSH|^~\\&|MedSeries|CAISI_1-2|PLS|3910|200903230934||ADT^A31^ADT_A05|75535037-1237815294895|P^T|2.4\r"
108 + "EVN|A31|200903230934\r"
109 + "PID|1||29^^CAISI_1-2^PI~\"\"||Test300^Leticia^^^^^L||19770202|M||||||||||||||||||||||\r"
110 + "Z04|bogus segment";
111
112
113 context.setValidationContext(ValidationContextFactory.noValidation());
114
115 Message message = null;
116 try {
117 message = parser.parse(message2);
118 System.out.println("Parsing succeeded, but ... ");
119 } catch (HL7Exception e) {
120
121 System.out.println("Something went wrong!");
122 System.exit(-1);
123 }
124
125
126
127
128 ValidationRuleBuilder builder2 = new NoValidationBuilder() {
129
130 @Override
131 protected void configure() {
132 super.configure();
133 forVersion(Version.V24)
134 .message("ADT", "*")
135 .onlyKnownSegments();
136 }
137
138 };
139 context.setValidationRuleBuilder(builder2);
140
141 try {
142 context.getMessageValidator().validate(message);
143
144 System.out.println("Something went wrong!");
145 System.exit(-1);
146 } catch (HL7Exception e) {
147 System.out.println("as expected, the message did not validate: " + e.getMessage());
148 }
149
150
151
152
153
154
155 ValidationExceptionHandler<Boolean> customHandler = new DefaultValidationExceptionHandler(context) {
156
157 @Override
158 public void onExceptions(ValidationException... exceptions) {
159 super.onExceptions(exceptions);
160 for (ValidationException e : exceptions) {
161 System.out.println("Found Validation issues: " + e.getMessage());
162 }
163 }
164 };
165
166 try {
167 Validator<Boolean> validator = context.getMessageValidator();
168 boolean result = validator.validate(message, customHandler);
169 System.out.println("The validator returned " + result);
170 } catch (HL7Exception e) {
171
172 System.out.println("Something went wrong!");
173 System.exit(-1);
174 }
175 }
176
177 }