Search in sources :

Example 1 with Terser

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");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) NoValidation(ca.uhn.hl7v2.validation.impl.NoValidation) HapiContext(ca.uhn.hl7v2.HapiContext) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) ValidationRuleBuilder(ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder) Parser(ca.uhn.hl7v2.parser.Parser) GenericParser(ca.uhn.hl7v2.parser.GenericParser) GenericParser(ca.uhn.hl7v2.parser.GenericParser) ValidationContext(ca.uhn.hl7v2.validation.ValidationContext)

Example 2 with Terser

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"));
}
Also used : Message(ca.uhn.hl7v2.model.Message) Terser(ca.uhn.hl7v2.util.Terser) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 3 with Terser

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!");
    }
}
Also used : Group(ca.uhn.hl7v2.model.Group) Segment(ca.uhn.hl7v2.model.Segment) Type(ca.uhn.hl7v2.model.Type) List(java.util.List) ArrayList(java.util.ArrayList) Structure(ca.uhn.hl7v2.model.Structure)

Example 4 with Terser

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;
}
Also used : Terser(ca.uhn.hl7v2.util.Terser) SegmentFinder(ca.uhn.hl7v2.util.SegmentFinder) ArrayList(java.util.ArrayList) Structure(ca.uhn.hl7v2.model.Structure)

Example 5 with Terser

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;
}
Also used : Message(ca.uhn.hl7v2.model.Message) Terser(ca.uhn.hl7v2.util.Terser) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Terser (ca.uhn.hl7v2.util.Terser)3 DefaultHapiContext (ca.uhn.hl7v2.DefaultHapiContext)2 Message (ca.uhn.hl7v2.model.Message)2 Structure (ca.uhn.hl7v2.model.Structure)2 ValidationRuleBuilder (ca.uhn.hl7v2.validation.builder.ValidationRuleBuilder)2 ArrayList (java.util.ArrayList)2 HapiContext (ca.uhn.hl7v2.HapiContext)1 Group (ca.uhn.hl7v2.model.Group)1 Segment (ca.uhn.hl7v2.model.Segment)1 Type (ca.uhn.hl7v2.model.Type)1 GenericParser (ca.uhn.hl7v2.parser.GenericParser)1 Parser (ca.uhn.hl7v2.parser.Parser)1 SegmentFinder (ca.uhn.hl7v2.util.SegmentFinder)1 ValidationContext (ca.uhn.hl7v2.validation.ValidationContext)1 NoValidation (ca.uhn.hl7v2.validation.impl.NoValidation)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1