1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.openehealth.ipf.modules.hl7.validation;
17
18 import ca.uhn.hl7v2.HL7Exception;
19 import ca.uhn.hl7v2.HapiContext;
20 import ca.uhn.hl7v2.Severity;
21 import ca.uhn.hl7v2.model.Message;
22 import ca.uhn.hl7v2.validation.impl.SimpleValidationExceptionHandler;
23 import org.openehealth.ipf.commons.core.modules.api.ValidationException;
24
25 public class Validator {
26
27
28
29
30
31
32
33
34 public static void validate(Message msg, HapiContext context) {
35 HapiContext ctx = context == null ? msg.getParser().getHapiContext() : context;
36
37
38
39 SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(ctx);
40 handler.setMinimumSeverityToCollect(Severity.ERROR);
41 try {
42 if (ctx.<Boolean>getMessageValidator().validate(msg, handler)) {
43 throw new ValidationException("Message validation failed", handler.getExceptions());
44 }
45 } catch (HL7Exception hl7Exception) {
46 throw new ValidationException("Message validation failed", hl7Exception);
47 }
48 }
49
50 }