Search in sources :

Example 1 with HL7Message

use of org.apache.nifi.hl7.model.HL7Message in project nifi by apache.

the class HL7Query method processDeclare.

private void processDeclare(final Tree declare) {
    for (int i = 0; i < declare.getChildCount(); i++) {
        final Tree declarationTree = declare.getChild(i);
        final String identifier = declarationTree.getChild(0).getText();
        final Tree requiredOrOptionalTree = declarationTree.getChild(1);
        final boolean required = requiredOrOptionalTree.getType() == REQUIRED;
        final String segmentName = declarationTree.getChild(2).getText();
        final Declaration declaration = new Declaration() {

            @Override
            public String getAlias() {
                return identifier;
            }

            @Override
            public boolean isRequired() {
                return required;
            }

            @Override
            public Object getDeclaredValue(final HL7Message message) {
                if (message == null) {
                    return null;
                }
                return message.getSegments(segmentName);
            }
        };
        declarations.add(declaration);
    }
}
Also used : Tree(org.antlr.runtime.tree.Tree) HL7Message(org.apache.nifi.hl7.model.HL7Message)

Example 2 with HL7Message

use of org.apache.nifi.hl7.model.HL7Message in project nifi by apache.

the class TestHL7Query method testSelectField.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testSelectField() throws HL7Exception, IOException {
    final HL7Query query = HL7Query.compile("SELECT PID.5");
    final HL7Message msg = hypoglycemia;
    final QueryResult result = query.evaluate(msg);
    assertTrue(result.isMatch());
    final List<String> labels = result.getLabels();
    assertEquals(1, labels.size());
    assertEquals(1, result.getHitCount());
    final Object names = result.nextHit().getValue("PID.5");
    assertTrue(names instanceof List);
    final List<Object> nameList = (List) names;
    assertEquals(1, nameList.size());
    final HL7Field nameField = (HL7Field) nameList.get(0);
    assertEquals("SMITH^JOHN", nameField.getValue());
}
Also used : HL7Field(org.apache.nifi.hl7.model.HL7Field) HL7Message(org.apache.nifi.hl7.model.HL7Message) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with HL7Message

use of org.apache.nifi.hl7.model.HL7Message in project nifi by apache.

the class TestHL7Query method testSelectMessage.

@Test
public void testSelectMessage() throws HL7Exception, IOException {
    final HL7Query query = HL7Query.compile("SELECT MESSAGE");
    final HL7Message msg = hypoglycemia;
    final QueryResult result = query.evaluate(msg);
    assertTrue(result.isMatch());
    final List<String> labels = result.getLabels();
    assertEquals(1, labels.size());
    assertEquals("MESSAGE", labels.get(0));
    assertEquals(1, result.getHitCount());
    assertEquals(msg, result.nextHit().getValue("MESSAGE"));
}
Also used : HL7Message(org.apache.nifi.hl7.model.HL7Message) Test(org.junit.Test)

Example 4 with HL7Message

use of org.apache.nifi.hl7.model.HL7Message in project nifi by apache.

the class TestHL7Query method createMessage.

private HL7Message createMessage(final String msgText) throws HL7Exception, IOException {
    final HapiContext hapiContext = new DefaultHapiContext();
    hapiContext.setValidationContext(ValidationContextFactory.noValidation());
    final PipeParser parser = hapiContext.getPipeParser();
    final Message message = parser.parse(msgText);
    return new HapiMessage(message);
}
Also used : HapiMessage(org.apache.nifi.hl7.hapi.HapiMessage) PipeParser(ca.uhn.hl7v2.parser.PipeParser) HapiMessage(org.apache.nifi.hl7.hapi.HapiMessage) Message(ca.uhn.hl7v2.model.Message) HL7Message(org.apache.nifi.hl7.model.HL7Message) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) HapiContext(ca.uhn.hl7v2.HapiContext) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext)

Example 5 with HL7Message

use of org.apache.nifi.hl7.model.HL7Message in project nifi by apache.

the class RouteHL7 method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).evaluateAttributeExpressions(flowFile).getValue());
    final byte[] buffer = new byte[(int) flowFile.getSize()];
    session.read(flowFile, new InputStreamCallback() {

        @Override
        public void process(final InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer);
        }
    });
    @SuppressWarnings("resource") final HapiContext hapiContext = new DefaultHapiContext();
    hapiContext.setValidationContext((ca.uhn.hl7v2.validation.ValidationContext) ValidationContextFactory.noValidation());
    final PipeParser parser = hapiContext.getPipeParser();
    final String hl7Text = new String(buffer, charset);
    final HL7Message message;
    try {
        final Message hapiMessage = parser.parse(hl7Text);
        message = new HapiMessage(hapiMessage);
    } catch (final Exception e) {
        getLogger().error("Failed to parse {} as HL7 due to {}; routing to failure", new Object[] { flowFile, e });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    final Set<String> matchingRels = new HashSet<>();
    final Map<Relationship, HL7Query> queryMap = queries;
    for (final Map.Entry<Relationship, HL7Query> entry : queryMap.entrySet()) {
        final Relationship relationship = entry.getKey();
        final HL7Query query = entry.getValue();
        final QueryResult result = query.evaluate(message);
        if (result.isMatch()) {
            FlowFile clone = session.clone(flowFile);
            clone = session.putAttribute(clone, "RouteHL7.Route", relationship.getName());
            session.transfer(clone, relationship);
            session.getProvenanceReporter().route(clone, relationship);
            matchingRels.add(relationship.getName());
        }
    }
    session.transfer(flowFile, REL_ORIGINAL);
    getLogger().info("Routed a copy of {} to {} relationships: {}", new Object[] { flowFile, matchingRels.size(), matchingRels });
}
Also used : HapiMessage(org.apache.nifi.hl7.hapi.HapiMessage) HapiMessage(org.apache.nifi.hl7.hapi.HapiMessage) Message(ca.uhn.hl7v2.model.Message) HL7Message(org.apache.nifi.hl7.model.HL7Message) QueryResult(org.apache.nifi.hl7.query.QueryResult) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) HapiContext(ca.uhn.hl7v2.HapiContext) HashSet(java.util.HashSet) FlowFile(org.apache.nifi.flowfile.FlowFile) HL7Query(org.apache.nifi.hl7.query.HL7Query) PipeParser(ca.uhn.hl7v2.parser.PipeParser) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) HL7Message(org.apache.nifi.hl7.model.HL7Message) IOException(java.io.IOException) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) HL7QueryParsingException(org.apache.nifi.hl7.query.exception.HL7QueryParsingException) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) Relationship(org.apache.nifi.processor.Relationship) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HL7Message (org.apache.nifi.hl7.model.HL7Message)6 DefaultHapiContext (ca.uhn.hl7v2.DefaultHapiContext)2 HapiContext (ca.uhn.hl7v2.HapiContext)2 Message (ca.uhn.hl7v2.model.Message)2 PipeParser (ca.uhn.hl7v2.parser.PipeParser)2 HapiMessage (org.apache.nifi.hl7.hapi.HapiMessage)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Tree (org.antlr.runtime.tree.Tree)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 HL7Field (org.apache.nifi.hl7.model.HL7Field)1 HL7Segment (org.apache.nifi.hl7.model.HL7Segment)1 HL7Query (org.apache.nifi.hl7.query.HL7Query)1