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.
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.