ca.uhn.hl7v2.model.Segment Java Examples

The following examples show how to use ca.uhn.hl7v2.model.Segment. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HapiSegment.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
public HapiSegment(final Segment segment) throws HL7Exception {
    this.segment = segment;

    final List<HL7Field> fieldList = new ArrayList<>();
    for (int i = 1; i <= segment.numFields(); i++) {
        final Type[] types = segment.getField(i);

        if (types == null || types.length == 0) {
            fieldList.add(new EmptyField());
            continue;
        }

        for (final Type type : types) {
            fieldList.add(new HapiField(type));
        }
    }

    this.fields = Collections.unmodifiableList(fieldList);
}
 
Example #2
Source File: ExtractHL7Attributes.java    From localization_nifi with Apache License 2.0 7 votes vote down vote up
private static Map<String, Type> getAllFields(final String segmentKey, final Segment segment, final boolean useNames) throws HL7Exception {
    final Map<String, Type> fields = new TreeMap<>();
    final String[] segmentNames = segment.getNames();
    for (int i = 1; i <= segment.numFields(); i++) {
        final Type field = segment.getField(i, 0);
        if (!isEmpty(field)) {
            final String fieldName;
            if (useNames) {
                fieldName = WordUtils.capitalize(segmentNames[i-1]).replaceAll("\\W+", "");
            } else {
                fieldName = String.valueOf(i);
            }

            final String fieldKey = new StringBuilder()
                .append(segmentKey)
                .append(".")
                .append(fieldName)
                .toString();

            fields.put(fieldKey, field);
        }
    }
    return fields;
}
 
Example #3
Source File: SampleApp.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Message processMessage(Message theIn) throws ApplicationException, HL7Exception {

    String encodedMessage = new PipeParser().encode(theIn);
    System.out.println("Received message:\n" + encodedMessage + "\n\n");

    // Now we need to generate a message to return. This will generally be an ACK message.
    Segment msh = (Segment) theIn.get("MSH");
    Message retVal;
    try {
        // This method takes in the MSH segment of an incoming message, and generates an
        // appropriate ACK
        retVal = DefaultApplication.makeACK(msh);
    } catch (IOException e) {
        throw new HL7Exception(e);
    }

    return retVal;
}
 
Example #4
Source File: HapiSegment.java    From nifi with Apache License 2.0 6 votes vote down vote up
public HapiSegment(final Segment segment) throws HL7Exception {
    this.segment = segment;

    final List<HL7Field> fieldList = new ArrayList<>();
    for (int i = 1; i <= segment.numFields(); i++) {
        final Type[] types = segment.getField(i);

        if (types == null || types.length == 0) {
            fieldList.add(new EmptyField());
            continue;
        }

        for (final Type type : types) {
            fieldList.add(new HapiField(type));
        }
    }

    this.fields = Collections.unmodifiableList(fieldList);
}
 
Example #5
Source File: ExtractHL7Attributes.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
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();
                // These maybe should used the escaped values, but that would
                // 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;
}
 
Example #6
Source File: ExtractHL7Attributes.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private static void addSegments(final Group group, final Map<String, Segment> segments, final Map<String, Integer> segmentIndexes) throws HL7Exception {
    if (!isEmpty(group)) {
        for (final String name : group.getNames()) {
            for (final Structure structure : group.getAll(name)) {
                if (group.isGroup(name) && structure instanceof Group) {
                    addSegments((Group) structure, segments, segmentIndexes);
                } else if (structure instanceof Segment) {
                    addSegments((Segment) structure, segments, segmentIndexes);
                }
            }
            segmentIndexes.put(name, segmentIndexes.getOrDefault(name, 1) + 1);
        }
    }
}
 
Example #7
Source File: ExtractHL7Attributes.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private static void addSegments(final Segment segment, final Map<String, Segment> segments, final Map<String, Integer> segmentIndexes) throws HL7Exception {
    if (!isEmpty(segment)) {
        final String segmentName = segment.getName();
        final StringBuilder sb = new StringBuilder().append(segmentName);
        if (isRepeating(segment)) {
            final int segmentIndex = segmentIndexes.getOrDefault(segmentName, 1);
            sb.append("_").append(segmentIndex);
        }
        final String segmentKey = sb.toString();
        segments.put(segmentKey, segment);
    }
}
 
Example #8
Source File: ExtractHL7Attributes.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #9
Source File: ExtractHL7Attributes.java    From nifi with Apache License 2.0 5 votes vote down vote up
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();
                // These maybe should used the escaped values, but that would
                // 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;
}
 
Example #10
Source File: ExtractHL7Attributes.java    From nifi with Apache License 2.0 5 votes vote down vote up
private static void addSegments(final Group group, final Map<String, Segment> segments, final Map<String, Integer> segmentIndexes) throws HL7Exception {
    if (!isEmpty(group)) {
        for (final String name : group.getNames()) {
            for (final Structure structure : group.getAll(name)) {
                if (group.isGroup(name) && structure instanceof Group) {
                    addSegments((Group) structure, segments, segmentIndexes);
                } else if (structure instanceof Segment) {
                    addSegments((Segment) structure, segments, segmentIndexes);
                }
            }
            segmentIndexes.put(name, segmentIndexes.getOrDefault(name, 1) + 1);
        }
    }
}
 
Example #11
Source File: ExtractHL7Attributes.java    From nifi with Apache License 2.0 5 votes vote down vote up
private static void addSegments(final Segment segment, final Map<String, Segment> segments, final Map<String, Integer> segmentIndexes) throws HL7Exception {
    if (!isEmpty(segment)) {
        final String segmentName = segment.getName();
        final StringBuilder sb = new StringBuilder().append(segmentName);
        if (isRepeating(segment)) {
            final int segmentIndex = segmentIndexes.getOrDefault(segmentName, 1);
            sb.append("_").append(segmentIndex);
        }
        final String segmentKey = sb.toString();
        segments.put(segmentKey, segment);
    }
}
 
Example #12
Source File: ExtractHL7Attributes.java    From nifi with Apache License 2.0 5 votes vote down vote up
private static Map<String, Type> getAllFields(final String segmentKey, final Segment segment, final boolean useNames) throws HL7Exception {
    final Map<String, Type> fields = new TreeMap<>();
    final String[] segmentNames = segment.getNames();
    for (int i = 1; i <= segment.numFields(); i++) {
        final Type field = segment.getField(i, 0);
        if (!isEmpty(field)) {
            final String fieldName;
            //Some user defined segments (e.g. Z segments) will not have corresponding names returned
            //from segment.getNames() above. If we encounter one of these, do the next best thing
            //and return what we otherwise would if we were not in useNames mode.
            String segmentName = segmentNames[i-1];
            if (useNames && StringUtils.isNotBlank(segmentName)) {
                fieldName = WordUtils.capitalize(segmentName).replaceAll("\\W+", "");
            } else {
                fieldName = String.valueOf(i);
            }

            final String fieldKey = new StringBuilder()
                .append(segmentKey)
                .append(".")
                .append(fieldName)
                .toString();

            fields.put(fieldKey, field);
        }
    }
    return fields;
}
 
Example #13
Source File: ExtractHL7Attributes.java    From nifi with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #14
Source File: ExtractHL7Attributes.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private static Map<String, Segment> getAllSegments(final Group group) throws HL7Exception {
    final Map<String, Segment> segments = new TreeMap<>();
    addSegments(group, segments, new HashMap<String, Integer>());
    return Collections.unmodifiableMap(segments);
}
 
Example #15
Source File: ExtractHL7Attributes.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static Map<String, Segment> getAllSegments(final Group group) throws HL7Exception {
    final Map<String, Segment> segments = new TreeMap<>();
    addSegments(group, segments, new HashMap<String, Integer>());
    return Collections.unmodifiableMap(segments);
}