Search code examples
hapihl7-v2

Parse HL7 v2.3 REF message with local customizations in HAPI


I am trying parse a HL7 REF I12 message with local customization(NZ).

When I tried using the GenericParser, I keep getting Validation exceptions. For example for the segment below, I keep get the output

ca.uhn.hl7v2.validation.ValidationException: Validation failed: Primitive value '(08)569-7555' requires to be empty or a US phone number

PRD|PP|See T Tan^""^""^^""|""^^^^""^New Zealand||(08)569-7555||14134^NZMC

My question is:

  • Is there a way to avoid the validation by using the conformance class generator
  • Is it possible to create own validation classes using CustomModelClasses?

In either case, is there any example code for that or tutorial example documentation?


Solution

  • If disabling validation altogether is an option for your application, then you can set the validation context to use NoValidation.

    See this thread in the hapi developers mailing list: http://sourceforge.net/p/hl7api/mailman/message/31244500/

    Here is an example of how to disable validation:

    HapiContext context = new DefaultHapiContext();
    context.setValidationContext(new NoValidation());
    GenericParser parser = context.getGenericParser();
    String message = ...
    try {
      parser.parse(message);
    } catch (Exception e) {
      e.printStackTrace();
    }
    

    If you still require validation, but just want to change the validator for specific rules, then you'll have to create your own implementation of ValidationContext. This would be done by sub classing ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder and overriding the configure method and use this to instantiate an instance of ValidationContextImpl.

    For an example of how to implement the configure method in your subclass of NoValidationBuilder, see the source code for ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder. This is the default validation context that is generating the error message you're seeing. To make it easier for you, I'm including the class listing here:

    public class DefaultValidationBuilder extends DefaultValidationWithoutTNBuilder {
    
        @Override
        protected void configure() {
            super.configure();
    
            forAllVersions()
                .primitive("TN")
                    .refersToSection("Version 2.4 Section 2.9.45")
                    .is(emptyOr(usPhoneNumber()));
        }
    
    }
    

    Notice this is the implementation of the usPhoneNumber method defined in BuilderSupport:

    public Predicate usPhoneNumber() {
        return matches("(\\d{1,2} )?(\\(\\d{3}\\))?\\d{3}-\\d{4}(X\\d{1,5})?(B\\d{1,5})?(C.*)?",
                "a US phone number");
    }