I was working in parsing the HL7 ORU Message using HAPI. Found an interesting article by here by Ignacio Suay. Thanks mate for simple and quick example
Based on that, here is my example, But still finding a way to extract OBR, OBX and NTE Segments.
package com.product.HL7;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.util.Terser;
import ca.uhn.hl7v2.validation.impl.NoValidation;
import com.product.domain.LabResults;
public class ParseFile {
static final Logger LOG = LoggerFactory.getLogger(ParseFile.class);
private String HL7Message;
private List<HL7Data> MSH = new ArrayList<HL7Data>();
private List<HL7Data> orderInfo = new ArrayList<HL7Data>();
private ParsingEvent parseingEvent;
public List<HL7Data> getOrderInfo() {
return orderInfo;
}
public void setOrderInfo(List<HL7Data> orderInfo) {
this.orderInfo = orderInfo;
}
public ParseFile(ParsingEvent parseingEvent) {
this.parseingEvent = parseingEvent;
}
public String getHL7Message() {
return HL7Message;
}
public List<HL7Data> getMSH() {
return MSH;
}
public void setMSH(List<HL7Data> mSH) {
MSH = mSH;
}
public void setHL7Message(String hL7Message) {
HL7Message = hL7Message;
}
public void parseMessage() throws HL7Exception {
String element;
String value;
HapiContext context = new DefaultHapiContext();
context.setValidationContext(new NoValidation());
PipeParser pipeParser = context.getPipeParser();
/* pipeParser.setValidationContext(new NoValidation()); */
Message message = pipeParser.parse(HL7Message);
Terser terser = new Terser(message);
LabResults labResults = new LabResults();
element = "Receiving Facility";
value = terser.get("/.MSH-6");
LOG.debug(element + " : " + value);
labResults.setDocAccountNo(value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Date";
value = terser.get("/.MSH-7");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient No";
value = terser.get("/.PID-2");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Lab Accession Number";
value = terser.get("/.PID-4");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Last Name";
value = terser.get("/.PID-5-1");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient First Name";
value = terser.get("/.PID-5-2");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Middle Name";
value = terser.get("/.PID-5-3");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient DOB";
value = terser.get("/.PID-7");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Sex";
value = terser.get("/.PID-8");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Address";
value = terser.get("/.PID-11-1");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient City";
value = terser.get("/.PID-11-2");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient State";
value = terser.get("/.PID-11-3");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Zip";
value = terser.get("/.PID-11-4");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Phone";
value = terser.get("/.PID-13");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Maritial Status";
value = terser.get("/.PID-16");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient EHR Number";
value = terser.get("/.PID-18");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient SS";
value = terser.get("/.PID-19");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Care Number";
value = terser.get("/.PID-20");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Caid Number";
value = terser.get("/.PID-21");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
element = "Patient Age";
value = terser.get("/.PID-23");
LOG.debug(element + " : " + value);
MSH.add(new HL7Data(element, value));
value = null;
element = null;
/*********************** Order Informaton ************************************/
element = "Lab Accession Number";
value = terser.get("/.ORC-2");
LOG.debug(element + " : " + value);
orderInfo.add(new HL7Data(element, value));
value = null;
element = null;
parseingEvent.parsingCompleted();
}
}
Some important note.
1. Hapi always expect \r at the end of the each segment. So if you have manually append the character at the end of the each segment as i did in my example
// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line + "\r");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
2. HAPI will also validate the Message while parsing. If you want to avoid that, see my example above.
3. But i don’t think so my job is going to easy to fetch OBR and OBX and NTE segments using HAPI. I am trying with that, latest will post if i am able to complete.
Sample File
MSH|^~\&|mas|My LAB|mas|3535|201305280003||ORU^R01||P|2.3|
PID|1|PAT393929|^^^MRN~^^^CID~^^^ACCN|000035100|MOOAFEFERE^SCOOPDSTT^||19690608|M|||^^FL^|||||U|||||||43|y|
ORC|1|000035100|||CM||||201302271648|PAH|201302271700|^^^^ ^^^^^|||||355^MY HOUSE|0011 NW 10 STR^MYCITY^NY^11111^111-371-1111|3|PAH|
OBR|1|000035100||BATH SALTS^BATH SALTS|||20130131----|201302271651||||||201302271648||^^^^ ^^^^^||D|DRUGS||||||F|
OBX|1|TX|BATH SALTS^BATH SALTS| ^D^[|NEGATIVE||-||||F||7|201302271651||PAH||