0

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;
    }
Brian Teeter
  • 844
  • 1
  • 8
  • 17
  • Since you don't specify any specific message profile (ORU_R01, ADT_A08, etc) or even what HL7 version, any sort of content that doesn't have major syntax errors is going to be 'valid'. Or is there something going on here I am not seeing? – daveloyall Jun 07 '18 at 17:49
  • In general what I would like is to pass in any message and have it validate. However if I need to specify versions and profiles I can do that too. How would I do so? – Brian Teeter Jun 08 '18 at 19:06

1 Answers1

1

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:

{

    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); and catch the HL7Exception

Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98
  • Thanks for the reply - that's basically what I'm doing. The issue I have is I want to see all errors with the HL7 vs just catch the first one. – Brian Teeter Jun 05 '18 at 15:37