Greetings,

favorite
<https://stackoverflow.com/questions/50688192/hapi-hl7-validation-throws-exceptions#>

I am working on a Java endpoint that I intend to use for HL7 message
validation. I have a basic app running that uses a variation of the
standard HAPI HL7 validation example. 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. In that case it gets caught in the
catch block.

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;
    }


Thanks in advance!
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Hl7api-devel mailing list
Hl7api-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hl7api-devel

Reply via email to