[英]HAPI HL7 Validation throws Exceptions
I am working on a Java endpoint that I intend to use for HL7 message validation. 我正在研究打算用于HL7消息验证的Java端点。 I have a basic app running that uses a variation of the standard HAPI HL7 validation example. 我有一个运行的基本应用程序,它使用标准HAPI HL7验证示例的变体。 If I pass in a valid message I get the "Success" response. 如果我输入了有效消息,则会收到“成功”响应。 If I pass in a invalid message I still get a "Success" response. 如果我传递了无效的消息,我仍然会收到“成功”响应。
The only way I get an error response is if the HL7 is badly formatted and the PipeParser throws an exception. 我得到错误响应的唯一方法是,如果HL7格式错误,并且PipeParser引发异常。 In that case it gets caught in the catch block. 在这种情况下,它会被捕获在catch块中。
What I want to see is if I pass in an invalid message that it actually gets validated and returns all the validation errors. 我想看看的是,如果我传递了一条无效消息,表明该消息实际上已经过验证并返回所有验证错误。 But I don't ever actually see any validation. 但是我从来没有真正看到过任何验证。 It either parses or crashes trying to parse. 它要么解析要么崩溃尝试解析。
What am I missing here? 我在这里想念什么?
HapiContext context = new DefaultHapiContext();
ValidationContext validationContext = ValidationContextFactory.defaultValidation();
context.setValidationContext(validationContext);
try
{
context.getParserConfiguration().setUnexpectedSegmentBehaviour(UnexpectedSegmentBehaviourEnum.THROW_HL7_EXCEPTION);
Message messageValidationResults = context.getPipeParser().parse(hl7Message);
SimpleValidationExceptionHandler handler = new SimpleValidationExceptionHandler(context);
handler.setMinimumSeverityToCollect(Severity.INFO);
Validator<Boolean> validator = context.getMessageValidator();
if (!validator.validate(messageValidationResults, handler))
{
if (handler.getExceptions().size() == 0)
{
hl7ValidationResult = "SUCCESS - Message Validated Successfully";
}
else
{
hl7ValidationResult = "ERROR - Found " + handler.getExceptions().size() + " problems\n\n";
for (Exception e : handler.getExceptions())
{
hl7ValidationResult += (e.getClass().getSimpleName() + " - " + e.getMessage()) + "\n";
}
}
}
}
catch (Exception e)
{
hl7ValidationResult = "ERROR - " + e.getMessage();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString();
hl7ValidationResult += "\n\n" + sStackTrace;
}
Please ignore the answer if do you think is not correct, I stopped to work with HL7 but, looking at my old project I have found this and maybe it can help you to find the solution of your problem: 如果您认为不正确,请忽略答案,我停止使用HL7,但是在我的旧项目中,我发现了这一点,也许它可以帮助您找到问题的解决方案:
{
DefaultValidationBuilder builder = new DefaultValidationBuilder() {
private static final long serialVersionUID = 1L;
@Override
protected void configure() {
super.configure();
forVersion(Version.V26);
}
};
HapiContext context = new DefaultHapiContext();
context.setValidationRuleBuilder(builder);
PipeParser hapiParser = context.getPipeParser();
try {
hapiParser.parse(hl7Message);
} catch (ca.uhn.hl7v2.HL7Exception e) {
// String error, String language, String requisitionNumber, String controlId, String processinId, String senderApplication, String senderFacility
errors.add(new HL7ValidationError(
"HAPI Validator error found: " + e.getMessage(),
extractor.accessPatientDirectly().getLanguage(),
extractor.accessPatientDirectly().getRequisitionNumber(),
extractor.accessPatientDirectly().getControlID(),
"",
extractor.accessPatientDirectly().getSenderApplication(),
extractor.accessPatientDirectly().getSenderFacility())
);
log.debug("HAPI Validator error found: " + e.getMessage());
}
try {
context.close();
}
catch (Exception ex) {
log.debug("Unable to close HapiContext(): " + ex.getMessage());
}
}
Basically I used hapiParser.parse(hl7Message); 基本上我使用了hapiParser.parse(hl7Message); and catch the HL7Exception 并捕获HL7Exception
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.