Search in sources :

Example 1 with CanonicalModelClassFactory

use of ca.uhn.hl7v2.parser.CanonicalModelClassFactory in project streamsx.health by IBMStreams.

the class TestServer method main.

public static void main(String[] args) {
    try {
        HapiContext ctx = new DefaultHapiContext();
        CanonicalModelClassFactory mcf = new CanonicalModelClassFactory("2.6");
        ctx.setModelClassFactory(mcf);
        ctx.setValidationRuleBuilder(new NoValidationBuilder());
        ctx.setExecutorService(Executors.newCachedThreadPool());
        HL7Service server = ctx.newServer(8082, false);
        server.registerApplication(new HapiMessageHandler(null));
        server.registerConnectionListener(new ConnectionListener() {

            @Override
            public void connectionReceived(Connection c) {
                System.out.println("Connection received.");
            }

            @Override
            public void connectionDiscarded(Connection c) {
                System.out.println("Connection discarded.");
            }
        });
        server.startAndWait();
        ctx.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : NoValidationBuilder(ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) HL7Service(ca.uhn.hl7v2.app.HL7Service) Connection(ca.uhn.hl7v2.app.Connection) ConnectionListener(ca.uhn.hl7v2.app.ConnectionListener) IOException(java.io.IOException) HapiContext(ca.uhn.hl7v2.HapiContext) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) CanonicalModelClassFactory(ca.uhn.hl7v2.parser.CanonicalModelClassFactory)

Example 2 with CanonicalModelClassFactory

use of ca.uhn.hl7v2.parser.CanonicalModelClassFactory in project nifi by apache.

the class ExtractHL7Attributes 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 Boolean useSegmentNames = context.getProperty(USE_SEGMENT_NAMES).asBoolean();
    final Boolean parseSegmentFields = context.getProperty(PARSE_SEGMENT_FIELDS).asBoolean();
    final Boolean skipValidation = context.getProperty(SKIP_VALIDATION).asBoolean();
    final String inputVersion = context.getProperty(HL7_INPUT_VERSION).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();
    if (!inputVersion.equals("autodetect")) {
        hapiContext.setModelClassFactory(new CanonicalModelClassFactory(inputVersion));
    }
    if (skipValidation) {
        hapiContext.setValidationContext((ValidationContext) ValidationContextFactory.noValidation());
    }
    final PipeParser parser = hapiContext.getPipeParser();
    final String hl7Text = new String(buffer, charset);
    try {
        final Message message = parser.parse(hl7Text);
        final Map<String, String> attributes = getAttributes(message, useSegmentNames, parseSegmentFields);
        flowFile = session.putAllAttributes(flowFile, attributes);
        getLogger().debug("Added the following attributes for {}: {}", new Object[] { flowFile, attributes });
    } catch (final HL7Exception e) {
        getLogger().error("Failed to extract attributes from {} due to {}", new Object[] { flowFile, e });
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    session.transfer(flowFile, REL_SUCCESS);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) PipeParser(ca.uhn.hl7v2.parser.PipeParser) Message(ca.uhn.hl7v2.model.Message) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) CanonicalModelClassFactory(ca.uhn.hl7v2.parser.CanonicalModelClassFactory) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) InputStreamCallback(org.apache.nifi.processor.io.InputStreamCallback) HL7Exception(ca.uhn.hl7v2.HL7Exception) DefaultHapiContext(ca.uhn.hl7v2.DefaultHapiContext) HapiContext(ca.uhn.hl7v2.HapiContext)

Aggregations

DefaultHapiContext (ca.uhn.hl7v2.DefaultHapiContext)2 HapiContext (ca.uhn.hl7v2.HapiContext)2 CanonicalModelClassFactory (ca.uhn.hl7v2.parser.CanonicalModelClassFactory)2 IOException (java.io.IOException)2 HL7Exception (ca.uhn.hl7v2.HL7Exception)1 Connection (ca.uhn.hl7v2.app.Connection)1 ConnectionListener (ca.uhn.hl7v2.app.ConnectionListener)1 HL7Service (ca.uhn.hl7v2.app.HL7Service)1 Message (ca.uhn.hl7v2.model.Message)1 PipeParser (ca.uhn.hl7v2.parser.PipeParser)1 NoValidationBuilder (ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)1