8

The following dependencies are being used from the maven central repository in this example:

<!-- provides HAPI library -->
<dependency>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-base</artifactId>
  <version>2.2</version>
</dependency>
<!-- provides HAPI library message version -->
<dependency>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-structures-v22</artifactId>
  <version>2.2</version>
</dependency>

<!-- provides ByteString -->
<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-actor_2.10</artifactId>
  <version>2.3.3</version>
</dependency>

Here is an example of my parsing code, written in scala:

  import akka.util.ByteString
  import ca.uhn.hl7v2.model.Message
  import ca.uhn.hl7v2.model.v22.datatype.{CM_PAT_ID, ST, TN, TSComponentOne}
  import ca.uhn.hl7v2.model.v22.segment.{EVN, MRG, PID}
  import ca.uhn.hl7v2.parser.CanonicalModelClassFactory
  import ca.uhn.hl7v2.{DefaultHapiContext, ErrorCode, HL7Exception}

  lazy val parser = {
    val context = new DefaultHapiContext()
    context.setModelClassFactory(new CanonicalModelClassFactory("2.2"))
    context.getGenericParser
  }

  def parseHL7Message(message: ByteString) = Try[Message] { 
    val msg: String = message.utf8String.trim
    parser.parse(msg) 
  }

This code can successfully parse the following HL7 message.

"MSH|^~\\&|XXXX|S|XXXXXX|S|201410280931||ADT^A31|123456|P|2.2\r" +
"EVN|A31|201410280930\r" +
"PID|||9999999^^^S^MR~88888888^^^^PI||xxxx^xxxxxxxxx||11111111||||||(123)456-7890\r" +
"PV1\r"

However, when a phone number with an extension is supplied in the message, the hapi parser fails to parse the message. Here is an example of the input message I am trying to parse with an extension in the phone number:

"MSH|^~\\&|XXXX|S|XXXXXX|S|201410280931||ADT^A31|123456|P|2.2\r" +
"EVN|A31|201410280930\r" +
"PID|||9999999^^^S^MR~88888888^^^^PI||xxxx^xxxxxxxxx||11111111||||||(123)456-7890 1\r" +
"PV1\r"

Trying to parse this message fails with the following error message:

ca.uhn.hl7v2.validation.ValidationException: Validation failed: Primitive value '(123)456-7890 1' requires to be empty or a US phone number at PID-13

I read everything I could find at http://hl7api.sourceforge.net/index.html to look for documentation on how to modify the validation rules but have not found anything useful.

An example would be most appreciated, but even pointing to the proper documentation, or a simple working example project will be sufficient.

How can the validation rules used by the HAPI parser be configured to allow a phone number extension to be included in a valid US phone number in the PID-13 field?

EDIT

With a little more searching, through this hapi developer mailing list thread, I figured out how to disable validation altogether. Here is an example:

  lazy val parser = {
    val context = new DefaultHapiContext()
    context.setModelClassFactory(new CanonicalModelClassFactory("2.2"))
    context.setValidationContext(new NoValidation)
    context.getGenericParser
  }

But if possible, I would like to continue validating the messages. If I have to disable validation I guess that will have to work, but I'd prefer to specify that validation remain turned on, but that phone numbers can include extensions.

axiopisty
  • 4,972
  • 8
  • 44
  • 73

2 Answers2

5

I must work with 3rd party service, and this service send to me invalid phones. Unfortunatly, I can't understand, how to do it as 'best practice'. But I found one hack:

@PostConstruct
public void postConstruct() {  
    List<RuleBinding<PrimitiveTypeRule>> rules = ((ValidationContextImpl)applicationRouter.getParser().getHapiContext().getValidationContext()).getPrimitiveRuleBindings();
    //initially was published with this line, but think it was mistake
    //for(int i = rules.size() - 1; i > 0; i--) {
    for(int i = rules.size() - 1; i >= 0; i--) {
        RuleBinding<PrimitiveTypeRule> item = rules.get(i);
        if("TN".equals(item.getScope())){
            rules.remove(i);
        }
    }
}

If somebody will know more good way to resolve it, please write.

degr
  • 1,559
  • 1
  • 19
  • 37
2

Phone numbers can include extensions. The problem is that you have the extension in the wrong format. See the HL7 Messaging Standard Version 2.2, section 2.8.10.9. Telephone numbers must be in the following format.

[NN] [(999)]999-9999[X99999][B99999][C any text]

Put the extension after the 'X'.

Nick Radov
  • 401
  • 2
  • 7
  • While this answer is nice to have information. We do not have access to change the extension. The HL7 message is sent to use by a 3rd party. – axiopisty Jul 26 '16 at 15:14
  • If the sending application is defective then you'll have to write custom code to clean up the bad data. HAPI can't solve that problem for you. – Nick Radov Jul 26 '16 at 15:25
  • Agreed. However, our application is not the source of truth for this data. While it is not HAPI's responsibility to solve the problem, it would be nice if HAPI would play nicely with bad players. I'm just curious if it is possible to enable a custom validation for this scenario, even though it is not a valid HL7 message format. – axiopisty Jul 27 '16 at 15:16