Search in sources :

Example 46 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project nifi by apache.

the class ExtractHL7Attributes method getAttributes.

public static Map<String, String> getAttributes(final Group group, final boolean useNames, final boolean parseFields) throws HL7Exception {
    final Map<String, String> attributes = new TreeMap<>();
    if (!isEmpty(group)) {
        for (final Map.Entry<String, Segment> segmentEntry : getAllSegments(group).entrySet()) {
            final String segmentKey = segmentEntry.getKey();
            final Segment segment = segmentEntry.getValue();
            final Map<String, Type> fields = getAllFields(segmentKey, segment, useNames);
            for (final Map.Entry<String, Type> fieldEntry : fields.entrySet()) {
                final String fieldKey = fieldEntry.getKey();
                final Type field = fieldEntry.getValue();
                // change the existing non-broken behavior of the processor
                if (parseFields && (field instanceof Composite) && !isTimestamp(field)) {
                    for (final Map.Entry<String, Type> componentEntry : getAllComponents(fieldKey, field, useNames).entrySet()) {
                        final String componentKey = componentEntry.getKey();
                        final Type component = componentEntry.getValue();
                        final String componentValue = HL7_ESCAPING.unescape(component.encode(), HL7_ENCODING);
                        if (!StringUtils.isEmpty(componentValue)) {
                            attributes.put(componentKey, componentValue);
                        }
                    }
                } else {
                    final String fieldValue = HL7_ESCAPING.unescape(field.encode(), HL7_ENCODING);
                    if (!StringUtils.isEmpty(fieldValue)) {
                        attributes.put(fieldKey, fieldValue);
                    }
                }
            }
        }
    }
    return attributes;
}
Also used : Type(ca.uhn.hl7v2.model.Type) Composite(ca.uhn.hl7v2.model.Composite) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Segment(ca.uhn.hl7v2.model.Segment)

Example 47 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project nifi by apache.

the class ExtractHL7Attributes method getAllComponents.

private static Map<String, Type> getAllComponents(final String fieldKey, final Type field, final boolean useNames) throws HL7Exception {
    final Map<String, Type> components = new TreeMap<>();
    if (!isEmpty(field) && (field instanceof Composite)) {
        if (useNames) {
            final Pattern p = Pattern.compile("^(cm_msg|[a-z][a-z][a-z]?)([0-9]+)_(\\w+)$");
            try {
                final java.beans.PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(field);
                for (final java.beans.PropertyDescriptor property : properties) {
                    final String propertyName = property.getName();
                    final Matcher matcher = p.matcher(propertyName);
                    if (matcher.find()) {
                        final Type type = (Type) PropertyUtils.getProperty(field, propertyName);
                        if (!isEmpty(type)) {
                            final String componentName = matcher.group(3);
                            final String typeKey = new StringBuilder().append(fieldKey).append(".").append(componentName).toString();
                            components.put(typeKey, type);
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        } else {
            final Type[] types = ((Composite) field).getComponents();
            for (int i = 0; i < types.length; i++) {
                final Type type = types[i];
                if (!isEmpty(type)) {
                    String fieldName = field.getName();
                    if (fieldName.equals("CM_MSG")) {
                        fieldName = "CM";
                    }
                    final String typeKey = new StringBuilder().append(fieldKey).append(".").append(fieldName).append(".").append(i + 1).toString();
                    components.put(typeKey, type);
                }
            }
        }
    }
    return components;
}
Also used : Pattern(java.util.regex.Pattern) Composite(ca.uhn.hl7v2.model.Composite) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Matcher(java.util.regex.Matcher) TreeMap(java.util.TreeMap) ProcessException(org.apache.nifi.processor.exception.ProcessException) HL7Exception(ca.uhn.hl7v2.HL7Exception) IOException(java.io.IOException) Type(ca.uhn.hl7v2.model.Type)

Example 48 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project nifi by apache.

the class ExtractHL7Attributes method isRepeating.

private static boolean isRepeating(final Segment segment) throws HL7Exception {
    if (isEmpty(segment)) {
        return false;
    }
    final Group parent = segment.getParent();
    final Group grandparent = parent.getParent();
    if (parent == grandparent) {
        return false;
    }
    return grandparent.isRepeating(parent.getName());
}
Also used : Group(ca.uhn.hl7v2.model.Group)

Example 49 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception 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)

Example 50 with HL7Exception

use of ca.uhn.hl7v2.HL7Exception in project camel by apache.

the class AckExpression method evaluate.

@Override
public Object evaluate(Exchange exchange) {
    Throwable t = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
    Message msg = exchange.getIn().getBody(Message.class);
    try {
        HL7Exception hl7e = generateHL7Exception(t);
        AcknowledgmentCode code = acknowledgementCode;
        if (t != null && code == null) {
            code = AcknowledgmentCode.AE;
        }
        return msg.generateACK(code == null ? AcknowledgmentCode.AA : code, hl7e);
    } catch (Exception e) {
        throw ObjectHelper.wrapRuntimeCamelException(e);
    }
}
Also used : Message(ca.uhn.hl7v2.model.Message) HL7Exception(ca.uhn.hl7v2.HL7Exception) AcknowledgmentCode(ca.uhn.hl7v2.AcknowledgmentCode) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Aggregations

Message (ca.uhn.hl7v2.model.Message)36 HL7Exception (ca.uhn.hl7v2.HL7Exception)34 Test (org.junit.Test)24 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)24 ORU_R01 (ca.uhn.hl7v2.model.v25.message.ORU_R01)22 NK1 (ca.uhn.hl7v2.model.v25.segment.NK1)16 ORUR01Handler (org.openmrs.hl7.handler.ORUR01Handler)15 Person (org.openmrs.Person)11 ApplicationException (ca.uhn.hl7v2.app.ApplicationException)10 CX (ca.uhn.hl7v2.model.v25.datatype.CX)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 Patient (org.openmrs.Patient)7 APIException (org.openmrs.api.APIException)7 PatientIdentifierException (org.openmrs.api.PatientIdentifierException)7 Segment (ca.uhn.hl7v2.model.Segment)6 Structure (ca.uhn.hl7v2.model.Structure)6 Type (ca.uhn.hl7v2.model.Type)6 PID (ca.uhn.hl7v2.model.v25.segment.PID)6 EncodingNotSupportedException (ca.uhn.hl7v2.parser.EncodingNotSupportedException)6