Example usage for org.apache.commons.collections.map ListOrderedMap getValue

List of usage examples for org.apache.commons.collections.map ListOrderedMap getValue

Introduction

In this page you can find the example usage for org.apache.commons.collections.map ListOrderedMap getValue.

Prototype

public Object getValue(int index) 

Source Link

Document

Gets the value at the specified index.

Usage

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.MetaThesaurusToSQL.java

/**
 * Adds qualification to concepts and associations in the LexGrid
 * repository.//from w  ww  .java  2  s  .c  o m
 * 
 * @param aq
 *            Qualification information from the UMLS source.
 * @param constructHCD
 *            Indicates whether artificial context values should be
 *            constructed if not provided in the UMLS information.
 * @param rela
 *            The relationship attribute defined by UMLS (can be empty or
 *            null).
 * @param totalCount
 *            The total number of context links qualified previously.
 * @return The number of contextual links qualified in the repository for
 *         the given UMLS info.
 * @throws SQLException
 */
protected int loadContext(AssociationQualification aq, boolean constructHCD, String rela, int totalCount)
        throws SQLException {
    // If a context identifier was assigned, use it.
    // If a context identifier is not assigned and the option to construct
    // is enabled,
    // derive one based on the root concept code and path to root AUI
    // values.
    int contextLinks = 0;
    String hcd = aq.qualifierValue;
    if (constructHCD && StringUtils.isBlank(hcd) && StringUtils.isNotBlank(aq.pathToRoot)
            && StringUtils.isNotBlank(aq.sourceConceptCode)) {
        MessageDigest md = getSHA1();
        md.reset();
        md.update(aq.pathToRoot.getBytes());
        hcd = String.valueOf(md.digest(aq.sourceConceptCode.getBytes()));
    }
    if (StringUtils.isBlank(hcd))
        return 0;

    // Iterate through the path to root and determine the codes for
    // participating AUIs. We maintain a LRU cache of AUIs to codes to
    // assist.
    // If the associated code is not in the cache, find and cache it here.
    ListOrderedMap orderedPtrAUIToCode = new ListOrderedMap();

    // Break up the path to root into AUIs ...
    String[] auis = aq.pathToRoot.split("\\.");
    if (auis.length > 0) {
        // Check the cache for each. If not found, perform and cache the
        // AUI to code mapping.
        PreparedStatement getPTRCode = umlsConnection2_
                .prepareStatement("SELECT CUI FROM MRCONSO WHERE AUI = ?");
        try {
            String nextCode, nextAUI;
            for (int i = 0; i < auis.length; i++) {
                // Check for registered code in the cache.
                nextAUI = auis[i];
                nextCode = (String) auiToCodeCache_.get(nextAUI);

                // If not cached, perform lookup ...
                if (nextCode == null) {
                    getPTRCode.setString(1, nextAUI);
                    ResultSet ptrCodes = getPTRCode.executeQuery();
                    int count = 0;
                    try {
                        while (ptrCodes.next()) {
                            count++;
                            nextCode = ptrCodes.getString(1);
                        }
                    } finally {
                        ptrCodes.close();
                    }
                    // If one to one mapping (probably should always be, but
                    // doesn't
                    // hurt to check), add to the cache for quick lookup
                    // later...
                    if (count == 1)
                        auiToCodeCache_.put(nextAUI, nextCode);
                }

                // Was it resolved?
                if (nextCode != null)
                    orderedPtrAUIToCode.put(nextAUI, nextCode);
            }
        } finally {
            getPTRCode.close();
        }
    }
    // Ensure we have included the original AUI to code mapping from the
    // provided UMLS qualification info; inserted last as the root
    // of the path.
    orderedPtrAUIToCode.put(aq.sourceConceptAUI, aq.sourceConceptCode);

    // /////////////////////////////////////////////////////////////////////
    // We have all the participating codes and AUIs.
    // Add context qualifiers to the text presentation of each concept
    // based on code/AUI pairs.
    // /////////////////////////////////////////////////////////////////////
    for (OrderedMapIterator omi = orderedPtrAUIToCode.orderedMapIterator(); omi.hasNext();) {
        omi.next();
        String aui = (String) omi.getKey();
        String code = (String) omi.getValue();
        if (code != null)
            qualifyConceptPresentation(code, aui, aq.codingSchemeName, aq.qualifierName, hcd);
    }

    // /////////////////////////////////////////////////////////////////////
    // At this point we have taken care of all the concept qualifiers.
    // Now find and similarly tag each participating association link
    // between AUIs in the path to root chain.
    // /////////////////////////////////////////////////////////////////////

    // Statements to find LexGrid association to concept mappings.
    // Check source to target (parent association as processed)
    // or target to source (child association as processed).

    // Honor the association specified in the MRHIER entry, if provided.
    // For example, the UMLS 'inverse_isa' is mapped on load to 'hasSubtype'
    // association name; account for that here.
    String assoc = mapRela(rela);

    // If a specific relation attribute (rela) was not provided, consider
    // all relevant
    // hierarchical associations (including UMLS standard or source-specific
    // names).
    String assocParam = StringUtils.isNotBlank(assoc) ? '\'' + assoc + '\''
            : toCommaDelimitedWithQuotes(getHierAssocNames(aq.codingSchemeName));

    // Create statements to navigate both directions (up & down the
    // contextual chain).
    PreparedStatement getRelationship_1 = sqlConnection_.prepareStatement(new StringBuffer(
            "SELECT " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + ", " + stc_.targetEntityCodeOrId + ", "
                    + stc_.entityCodeOrAssociationId + ", " + stc_.sourceEntityCodeOrId + " FROM ")
                            .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY))
                            .append(" WHERE " + stc_.sourceEntityCodeOrId + " = ? AND "
                                    + stc_.targetEntityCodeOrId + " = ? AND ")
                            .append(stc_.codingSchemeNameOrId + " = ? AND " + stc_.entityCodeOrAssociationId
                                    + " IN (")
                            .append(assocParam).append(")").toString());

    PreparedStatement getRelationship_2 = sqlConnection_.prepareStatement(new StringBuffer(
            "SELECT " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + ", " + stc_.targetEntityCodeOrId + ", "
                    + stc_.entityCodeOrAssociationId + ", " + stc_.sourceEntityCodeOrId + " FROM ")
                            .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY))
                            .append(" WHERE " + stc_.targetEntityCodeOrId + " = ? AND "
                                    + stc_.sourceEntityCodeOrId + " = ? AND ")
                            .append(stc_.codingSchemeNameOrId + " = ? AND " + stc_.entityCodeOrAssociationId
                                    + " IN (")
                            .append(assocParam).append(")").toString());

    // Statement to update a multi-attributes key for an association
    // mapping.
    PreparedStatement updateMAK = sqlConnection_.prepareStatement(new StringBuffer("UPDATE ")
            .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY))
            .append(" SET " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + " = ? " + " WHERE "
                    + stc_.codingSchemeNameOrId + " = ?")
            .append(" AND " + stc_.sourceEntityCodeOrId + " = ? AND " + stc_.targetEntityCodeOrId + " = ?")
            .append(" AND " + stc_.entityCodeOrAssociationId + " = ?").toString());

    // Locate and qualify each affected association link with the context ID
    // ...
    try {
        PreparedStatement[] stmts = new PreparedStatement[] { getRelationship_1, getRelationship_2 };
        for (int s = 0; s < stmts.length; s++) {
            PreparedStatement stmt = stmts[s];
            for (int i = orderedPtrAUIToCode.size() - 1; i > 0; i--) {
                String code = (String) orderedPtrAUIToCode.getValue(i);
                String codePrev = (String) orderedPtrAUIToCode.getValue(i - 1);
                stmt.setString(1, code);
                stmt.setString(2, codePrev);
                stmt.setString(3, aq.codingSchemeName);

                ResultSet results = stmt.executeQuery();
                try {
                    // Iterate through all relevant association links ...
                    while (results.next()) {
                        String multiAttributesKey = results
                                .getString(SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY);
                        String targetConceptCode = results.getString(stc_.targetEntityCodeOrId);
                        String sourceConceptCode = results.getString(stc_.sourceEntityCodeOrId);
                        String association = results.getString(stc_.entityCodeOrAssociationId);

                        // If there is no key to correlate to the
                        // multi-attributes table,
                        // construct and add now.
                        if (multiAttributesKey == null) {
                            StringBuffer key = new StringBuffer().append(System.currentTimeMillis())
                                    .append((int) Math.floor((Math.random() * 100000))).append(totalCount);
                            multiAttributesKey = key.substring(0, Math.min(50, key.length()));
                            updateMAK.setString(1, multiAttributesKey);
                            updateMAK.setString(2, aq.codingSchemeName);
                            updateMAK.setString(3, sourceConceptCode);
                            updateMAK.setString(4, targetConceptCode);
                            updateMAK.setString(5, association);
                            updateMAK.execute();
                        }

                        // Add a context qualifier to the multi-attributes
                        // table.
                        try {
                            addEntityAssociationQualifierToEntityAssociation(aq.codingSchemeName,
                                    multiAttributesKey, aq.qualifierName, hcd);
                            contextLinks++;
                        } catch (SQLException e) {
                            // Because we qualify all relationships along
                            // the PTR and
                            // the HCD is identical for siblings at the same
                            // PTR some
                            // exceptions with regards to identical keys
                            // will come up.

                            // We try to bypass altogether if the message
                            // indicates duplication.
                            // However, message text can vary based on the
                            // database engine.
                            // Rather than exit in error, log the message
                            // and continue.
                            if (!e.getMessage().contains("Duplicate")) {
                                messages_.warn("Unable to add context qualifier to association.", e);
                            }
                        }
                    }
                } finally {
                    results.close();
                }
            }
        }
    } finally {
        updateMAK.close();
        getRelationship_1.close();
        getRelationship_2.close();
    }
    return contextLinks;
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.UmlsCommon.UMLSBaseCode.java

/**
 * Adds qualification to concepts and associations in the LexGrid
 * repository.// ww  w .ja va2 s. com
 * 
 * @param aq
 *            Qualification information from the UMLS source.
 * @param constructHCD
 *            Indicates whether artificial context values should be
 *            constructed if not provided in the UMLS information.
 * @param rela
 *            The relationship attribute defined by UMLS (can be empty or
 *            null).
 * @param totalCount
 *            The total number of context links qualified previously.
 * @return The number of contextual links qualified in the repository for
 *         the given UMLS info.
 * @throws SQLException
 */
protected int loadContext(AssociationQualification aq, boolean constructHCD, String rela, int totalCount)
        throws SQLException {
    // If a context identifier was assigned, use it.
    // If a context identifier is not assigned and the option to construct
    // is enabled,
    // derive one based on the root concept code and path to root AUI
    // values.
    int contextLinks = 0;
    String hcd = aq.qualifierValue;
    if (constructHCD && StringUtils.isBlank(hcd) && StringUtils.isNotBlank(aq.pathToRoot)
            && StringUtils.isNotBlank(aq.sourceConceptCode)) {
        MessageDigest md = getSHA1();
        md.reset();
        md.update(aq.pathToRoot.getBytes());
        hcd = String.valueOf(md.digest(aq.sourceConceptCode.getBytes()));
    }
    if (StringUtils.isBlank(hcd))
        return 0;

    // Iterate through the path to root and determine the codes for
    // participating AUIs. We maintain a LRU cache of AUIs to codes to
    // assist.
    // If the associated code is not in the cache, find and cache it here.
    ListOrderedMap orderedPtrAUIToCode = new ListOrderedMap();

    // Break up the path to root into AUIs ...
    String[] auis = aq.pathToRoot.split("\\.");
    if (auis.length > 0) {
        // Check the cache for each. If not found, perform and cache the
        // AUI to code mapping.
        PreparedStatement getPTRCode = umlsConnection2_
                .prepareStatement("SELECT CODE FROM MRCONSO WHERE AUI = ?");
        try {
            String nextCode, nextAUI;
            for (int i = 0; i < auis.length; i++) {
                // Check for registered code in the cache.
                nextAUI = auis[i];
                nextCode = (String) auiToCodeCache_.get(nextAUI);

                // If not cached, perform lookup ...
                if (nextCode == null) {
                    getPTRCode.setString(1, nextAUI);
                    ResultSet ptrCodes = getPTRCode.executeQuery();
                    int count = 0;
                    try {
                        while (ptrCodes.next()) {
                            count++;
                            nextCode = ptrCodes.getString(1);
                        }
                    } finally {
                        ptrCodes.close();
                    }
                    // If one to one mapping (probably should always be, but
                    // doesn't
                    // hurt to check), add to the cache for quick lookup
                    // later...
                    if (count == 1)
                        auiToCodeCache_.put(nextAUI, nextCode);
                }

                // Was it resolved?
                if (nextCode != null)
                    orderedPtrAUIToCode.put(nextAUI, nextCode);
            }
        } finally {
            getPTRCode.close();
        }
    }
    // Ensure we have included the original AUI to code mapping from the
    // provided UMLS qualification info; inserted last as the root
    // of the path.
    orderedPtrAUIToCode.put(aq.sourceConceptAUI, aq.sourceConceptCode);

    // /////////////////////////////////////////////////////////////////////
    // We have all the participating codes and AUIs.
    // Add context qualifiers to the text presentation of each concept
    // based on code/AUI pairs.
    // /////////////////////////////////////////////////////////////////////
    for (OrderedMapIterator omi = orderedPtrAUIToCode.orderedMapIterator(); omi.hasNext();) {
        omi.next();
        String aui = (String) omi.getKey();
        String code = (String) omi.getValue();
        if (code != null)
            qualifyConceptPresentation(code, aui, aq.codingSchemeName, aq.qualifierName, hcd);
    }

    // /////////////////////////////////////////////////////////////////////
    // At this point we have taken care of all the concept qualifiers.
    // Now find and similarly tag each participating association link
    // between AUIs in the path to root chain.
    // /////////////////////////////////////////////////////////////////////

    // Statements to find LexGrid association to concept mappings.
    // Check source to target (parent association as processed)
    // or target to source (child association as processed).

    // Honor the association specified in the MRHIER entry, if provided.
    // For example, the UMLS 'inverse_isa' is mapped on load to 'hasSubtype'
    // association name; account for that here.
    String assoc = mapRela(rela);

    // If a specific relation attribute (rela) was not provided, consider
    // all relevant
    // hierarchical associations (including UMLS standard or source-specific
    // names).
    String assocParam = StringUtils.isNotBlank(assoc) ? '\'' + assoc + '\''
            : toCommaDelimitedWithQuotes(getHierAssocNames(aq.codingSchemeName));

    // Create statements to navigate both directions (up & down the
    // contextual chain).
    PreparedStatement getRelationship_1 = sqlConnection_.prepareStatement(new StringBuffer(
            "SELECT " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + ", " + stc_.targetEntityCodeOrId + ", "
                    + stc_.entityCodeOrAssociationId + ", " + stc_.sourceEntityCodeOrId + " FROM ")
                            .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY))
                            .append(" WHERE " + stc_.sourceEntityCodeOrId + " = ? AND "
                                    + stc_.targetEntityCodeOrId + " = ? AND")
                            .append(" " + stc_.codingSchemeNameOrId + " = ? AND "
                                    + stc_.entityCodeOrAssociationId + " IN (")
                            .append(assocParam).append(")").toString());

    PreparedStatement getRelationship_2 = sqlConnection_.prepareStatement(new StringBuffer(
            "SELECT " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + ", " + stc_.targetEntityCodeOrId + ", "
                    + stc_.entityCodeOrAssociationId + ", " + stc_.sourceEntityCodeOrId + " FROM ")
                            .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY))
                            .append(" WHERE " + stc_.targetEntityCodeOrId + " = ? AND "
                                    + stc_.sourceEntityCodeOrId + " = ? AND")
                            .append(" " + stc_.codingSchemeNameOrId + " = ? AND "
                                    + stc_.entityCodeOrAssociationId + " IN (")
                            .append(assocParam).append(")").toString());

    // Statement to update a multi-attributes key for an association
    // mapping.
    PreparedStatement updateMAK = sqlConnection_.prepareStatement(new StringBuffer("UPDATE ")
            .append(stc_.getTableName(SQLTableConstants.ENTITY_ASSOCIATION_TO_ENTITY))
            .append(" SET " + SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY + " = ? " + " WHERE "
                    + stc_.codingSchemeNameOrId + " = ?")
            .append(" AND " + stc_.sourceEntityCodeOrId + " = ? AND " + stc_.targetEntityCodeOrId + " = ?")
            .append(" AND " + stc_.entityCodeOrAssociationId + " = ?").toString());

    // Locate and qualify each affected association link with the context ID
    // ...
    try {
        PreparedStatement[] stmts = new PreparedStatement[] { getRelationship_1, getRelationship_2 };
        for (int s = 0; s < stmts.length; s++) {
            PreparedStatement stmt = stmts[s];
            for (int i = orderedPtrAUIToCode.size() - 1; i > 0; i--) {
                String code = (String) orderedPtrAUIToCode.getValue(i);
                String codePrev = (String) orderedPtrAUIToCode.getValue(i - 1);
                stmt.setString(1, code);
                stmt.setString(2, codePrev);
                stmt.setString(3, aq.codingSchemeName);

                ResultSet results = stmt.executeQuery();
                try {
                    // Iterate through all relevant association links ...
                    while (results.next()) {
                        String multiAttributesKey = results
                                .getString(SQLTableConstants.TBLCOL_MULTIATTRIBUTESKEY);
                        String targetConceptCode = results.getString(stc_.targetEntityCodeOrId);
                        String sourceConceptCode = results.getString(stc_.sourceEntityCodeOrId);
                        String association = results.getString(stc_.entityCodeOrAssociationId);

                        // If there is no key to correlate to the
                        // multi-attributes table,
                        // construct and add now.
                        if (multiAttributesKey == null) {
                            StringBuffer key = new StringBuffer().append(System.currentTimeMillis())
                                    .append((int) Math.floor((Math.random() * 100000))).append(totalCount);
                            multiAttributesKey = key.substring(0, Math.min(50, key.length()));
                            updateMAK.setString(1, multiAttributesKey);
                            updateMAK.setString(2, aq.codingSchemeName);
                            updateMAK.setString(3, sourceConceptCode);
                            updateMAK.setString(4, targetConceptCode);
                            updateMAK.setString(5, association);
                            updateMAK.execute();
                        }

                        // Add a context qualifier to the multi-attributes
                        // table.
                        try {
                            addEntityAssociationQualifierToEntityAssociation(aq.codingSchemeName,
                                    multiAttributesKey, aq.qualifierName, hcd);
                            contextLinks++;
                        } catch (SQLException e) {
                            // Because we qualify all relationships along
                            // the PTR and
                            // the HCD is identical for siblings at the same
                            // PTR some
                            // exceptions with regards to identical keys
                            // will come up.

                            // We try to bypass altogether if the message
                            // indicates duplication.
                            // However, message text can vary based on the
                            // database engine.
                            // Rather than exit in error, log the message
                            // and continue.
                            if (!e.getMessage().contains("Duplicate")) {
                                messages_.warn("Unable to add context qualifier to association.", e);
                            }
                        }
                    }
                } finally {
                    results.close();
                }
            }
        }
    } finally {
        updateMAK.close();
        getRelationship_1.close();
        getRelationship_2.close();
    }
    return contextLinks;
}