Search in sources :

Example 1 with Composite

use of ca.uhn.hl7v2.model.Composite in project tdi-studio-se by Talend.

the class TypeModel method getComponent.

private Type getComponent(Type type, int comp) {
    Type ret = null;
    if (Varies.class.isAssignableFrom(type.getClass())) {
        Varies v = (Varies) type;
        try {
            if (comp > 1 && GenericPrimitive.class.isAssignableFrom(v.getData().getClass())) {
                v.setData(new GenericComposite(v.getMessage()));
            }
        } catch (DataTypeException de) {
            String message = "Unexpected exception copying data to generic composite: " + de.getMessage();
            throw new Error(message);
        }
        ret = getComponent(v.getData(), comp);
    } else {
        if (Primitive.class.isAssignableFrom(type.getClass()) && comp == 1) {
            ret = type;
        } else if (GenericComposite.class.isAssignableFrom(type.getClass()) || (Composite.class.isAssignableFrom(type.getClass()) && comp <= numStandardComponents(type))) {
            try {
                ret = ((Composite) type).getComponent(comp - 1);
            } catch (Exception e) {
                throw new Error("Internal error: HL7Exception thrown on getComponent(x) where x < # standard components.", e);
            }
        } else {
            ret = type.getExtraComponents().getComponent(comp - numStandardComponents(type) - 1);
        }
    }
    return ret;
}
Also used : Type(ca.uhn.hl7v2.model.Type) Primitive(ca.uhn.hl7v2.model.Primitive) GenericPrimitive(ca.uhn.hl7v2.model.GenericPrimitive) DataTypeException(ca.uhn.hl7v2.model.DataTypeException) GenericComposite(ca.uhn.hl7v2.model.GenericComposite) Composite(ca.uhn.hl7v2.model.Composite) GenericComposite(ca.uhn.hl7v2.model.GenericComposite) GenericPrimitive(ca.uhn.hl7v2.model.GenericPrimitive) Varies(ca.uhn.hl7v2.model.Varies) DataTypeException(ca.uhn.hl7v2.model.DataTypeException) HL7Exception(ca.uhn.hl7v2.HL7Exception)

Example 2 with Composite

use of ca.uhn.hl7v2.model.Composite in project openmrs-core by openmrs.

the class HL7ServiceImpl method resolveUserId.

/**
 * @param xcn HL7 component of data type XCN (extended composite ID number and name for persons)
 *            (see HL7 2.5 manual Ch.2A.86)
 * @return Internal ID # of the specified user, or null if that user can't be found or is
 *         ambiguous
 */
@Override
@Transactional(readOnly = true)
public Integer resolveUserId(XCN xcn) throws HL7Exception {
    String idNumber = xcn.getIDNumber().getValue();
    String familyName = xcn.getFamilyName().getSurname().getValue();
    String givenName = xcn.getGivenName().getValue();
    if (idNumber != null && idNumber.length() > 0) {
        try {
            Integer userId = Integer.valueOf(idNumber);
            User user = Context.getUserService().getUser(userId);
            return user.getUserId();
        } catch (Exception e) {
            log.error("Invalid user ID '" + idNumber + "'", e);
            return null;
        }
    } else {
        try {
            List<User> users = Context.getUserService().getUsersByName(givenName, familyName, true);
            if (users.size() == 1) {
                return users.get(0).getUserId();
            } else if (users.size() > 1) {
                // Return null if that user ambiguous
                log.error(getFindingUserErrorMessage(idNumber, familyName, givenName) + ": Found " + users.size() + " ambiguous users.");
                return null;
            } else {
                // legacy behavior is looking up by username
                StringBuilder username = new StringBuilder();
                if (familyName != null) {
                    username.append(familyName);
                }
                if (givenName != null) {
                    if (username.length() > 0) {
                        // separate names with a space
                        username.append(" ");
                    }
                    username.append(givenName);
                }
                User user = Context.getUserService().getUserByUsername(username.toString());
                if (user == null) {
                    log.error(getFindingUserErrorMessage(idNumber, familyName, givenName) + ": User not found");
                    return null;
                }
                return user.getUserId();
            }
        } catch (Exception e) {
            log.error(getFindingUserErrorMessage(idNumber, familyName, givenName), e);
            return null;
        }
    }
}
Also used : User(org.openmrs.User) URISyntaxException(java.net.URISyntaxException) DAOException(org.openmrs.api.db.DAOException) FileNotFoundException(java.io.FileNotFoundException) APIException(org.openmrs.api.APIException) HL7Exception(ca.uhn.hl7v2.HL7Exception) EncodingNotSupportedException(ca.uhn.hl7v2.parser.EncodingNotSupportedException) IOException(java.io.IOException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException) ApplicationException(ca.uhn.hl7v2.app.ApplicationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Composite

use of ca.uhn.hl7v2.model.Composite 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 4 with Composite

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

Aggregations

HL7Exception (ca.uhn.hl7v2.HL7Exception)3 Composite (ca.uhn.hl7v2.model.Composite)3 Type (ca.uhn.hl7v2.model.Type)3 IOException (java.io.IOException)2 TreeMap (java.util.TreeMap)2 ApplicationException (ca.uhn.hl7v2.app.ApplicationException)1 DataTypeException (ca.uhn.hl7v2.model.DataTypeException)1 GenericComposite (ca.uhn.hl7v2.model.GenericComposite)1 GenericPrimitive (ca.uhn.hl7v2.model.GenericPrimitive)1 Primitive (ca.uhn.hl7v2.model.Primitive)1 Segment (ca.uhn.hl7v2.model.Segment)1 Varies (ca.uhn.hl7v2.model.Varies)1 EncodingNotSupportedException (ca.uhn.hl7v2.parser.EncodingNotSupportedException)1 FileNotFoundException (java.io.FileNotFoundException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1