use of ca.uhn.hl7v2.util.Terser in project camel by apache.
the class HL7ValidateTest method createRouteBuilder.
protected RouteBuilder createRouteBuilder() throws Exception {
HapiContext hapiContext = new DefaultHapiContext();
hapiContext.setValidationContext(new NoValidation());
Parser p = new GenericParser(hapiContext);
hl7 = new HL7DataFormat();
hl7.setParser(p);
/*
* Let's start by adding a validation rule to the default validation
* that disallows PID-2 to be empty.
*/
ValidationRuleBuilder builder = new ValidationRuleBuilder() {
private static final long serialVersionUID = 1L;
@Override
protected void configure() {
forVersion(Version.V24).message("ADT", "*").terser("PID-2", not(empty()));
}
};
ValidationContext customValidationContext = ValidationContextFactory.fromBuilder(builder);
HapiContext customContext = new DefaultHapiContext(customValidationContext);
final Parser customParser = new GenericParser(customContext);
return new RouteBuilder() {
public void configure() throws Exception {
from("direct:unmarshalFailed").unmarshal().hl7().to("mock:unmarshal");
from("direct:unmarshalOk").unmarshal().hl7(false).to("mock:unmarshal");
from("direct:unmarshalOkCustom").unmarshal(hl7).to("mock:unmarshal");
from("direct:start1").marshal().hl7(customParser).to("mock:end");
from("direct:start2").marshal().hl7(true).to("mock:end");
}
};
}
use of ca.uhn.hl7v2.util.Terser in project camel by apache.
the class HL7XmlDataFormatTest method testUnmarshalOkXml.
@Test
public void testUnmarshalOkXml() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:unmarshal");
mock.expectedMessageCount(1);
String body = createHL7AsString();
Message msg = hl7.getParser().parse(body);
String xml = hl7.getParser().encode(msg, "XML");
assertTrue(xml.contains("<ORM_O01"));
template.sendBody("direct:unmarshalOkXml", xml);
assertMockEndpointsSatisfied();
Message received = mock.getReceivedExchanges().get(0).getIn().getMandatoryBody(Message.class);
assertEquals("O01", new Terser(received).get("MSH-9-2"));
}
use of ca.uhn.hl7v2.util.Terser in project pentaho-kettle by pentaho.
the class HL7KettleParser method parseStructure.
private static void parseStructure(List<HL7Value> values, Message message, Terser terser, Structure structure, String structureNumber) throws Exception {
Map<String, List<String>> nameMap = NamesUtil.getInstance().getMap();
if (structure instanceof Segment) {
Segment segment = (Segment) structure;
String[] names = segment.getNames();
for (int n = 1; n <= segment.numFields(); n++) {
Type[] types = segment.getField(n);
for (int t = 0; t < types.length; t++) {
int nrComponents = Terser.numComponents(types[t]);
for (int c = 1; c <= nrComponents; c++) {
int nrSub = Terser.numSubComponents(types[t], c);
for (int sc = 1; sc <= nrSub; sc++) {
String string = Terser.get(segment, n, t, c, sc);
// Primitive primitive = Terser.getPrimitive(types[t], c, sc);
String description = "?";
List<String> list = nameMap.get(types[t].getName());
if (list != null && c - 1 < list.size()) {
description = list.get(c - 1);
}
Group group = structure.getParent();
Group rootGroup = structure.getMessage();
String coordinates = n + "." + (t + 1) + "." + c + "." + sc;
HL7Value value = new HL7Value(message.getVersion(), rootGroup.getName(), group.getName(), structure.getName(), structureNumber, names[n - 1], coordinates, types[t].getName(), description, string);
values.add(value);
}
}
}
}
} else if (structure instanceof Group) {
Group group = (Group) structure;
String[] names = group.getNames();
for (int n = 1; n <= names.length; n++) {
String name = names[n - 1];
Structure subStructure = group.get(name);
parseStructure(values, message, terser, subStructure, structureNumber + "." + n);
}
} else {
throw new Exception("oops, not handled yet!");
}
}
use of ca.uhn.hl7v2.util.Terser in project pentaho-kettle by pentaho.
the class HL7KettleParser method extractValues.
public static List<HL7Value> extractValues(Message message) throws Exception {
Terser terser = new Terser(message);
SegmentFinder finder = terser.getFinder();
List<HL7Value> values = new ArrayList<HL7Value>();
int childNr = 1;
while (finder.hasNextChild()) {
// next group in the message (MSH, PID, EVN and so on)
//
finder.nextChild();
Structure[] structures = finder.getCurrentChildReps();
for (int i = 0; i < structures.length; i++) {
Structure structure = structures[i];
parseStructure(values, message, terser, structure, Integer.toString(childNr));
}
childNr++;
}
return values;
}
use of ca.uhn.hl7v2.util.Terser in project camel by apache.
the class HL7DataFormat method unmarshal.
public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
byte[] body = ExchangeHelper.convertToMandatoryType(exchange, byte[].class, inputStream);
String charsetName = HL7Charset.getCharsetName(body, guessCharsetName(body, exchange));
String bodyAsString = new String(body, charsetName);
Message message = HL7Converter.parse(bodyAsString, parser);
// add MSH fields as message out headers
Terser terser = new Terser(message);
for (Map.Entry<String, String> entry : HEADER_MAP.entrySet()) {
exchange.getOut().setHeader(entry.getKey(), terser.get(entry.getValue()));
}
exchange.getOut().setHeader(HL7_CONTEXT, hapiContext);
exchange.getOut().setHeader(Exchange.CHARSET_NAME, charsetName);
return message;
}
Aggregations