Search in sources :

Example 1 with ConceptNameType

use of org.openmrs.api.ConceptNameType in project openmrs-core by openmrs.

the class ConceptValidatorChangeSet method getLocaleConceptNamesMap.

/**
 * Convenience Method that fetches all non-voided concept names from the database associated to
 * a concept with a matching concept id, stores the names in a map with locales as the keys and
 * the lists of conceptNames in each locale as the values i.e <Locale List<ConceptNames>>.
 *
 * @param connection a DatabaseConnection
 * @param conceptId the conceptId for the conceptNames to fetch
 * @return a map of Locale with ConceptNames in them associated to the concept identified by the
 *         given conceptId
 */
private Map<Locale, List<ConceptName>> getLocaleConceptNamesMap(JdbcConnection connection, int conceptId) {
    PreparedStatement pStmt = null;
    Map<Locale, List<ConceptName>> localeConceptNamesMap = null;
    try {
        pStmt = connection.prepareStatement("SELECT concept_name_id, name, concept_name_type, locale, locale_preferred FROM concept_name WHERE voided = '0' AND concept_id = ?");
        pStmt.setInt(1, conceptId);
        ResultSet rs = pStmt.executeQuery();
        while (rs.next()) {
            if (localeConceptNamesMap == null) {
                localeConceptNamesMap = new HashMap<>();
            }
            ConceptName conceptName = new ConceptName();
            conceptName.setConceptNameId(rs.getInt("concept_name_id"));
            conceptName.setName(rs.getString("name"));
            String cnType = rs.getString("concept_name_type");
            if (!StringUtils.isBlank(cnType)) {
                ConceptNameType conceptNameType = null;
                if (cnType.equals(ConceptNameType.FULLY_SPECIFIED.toString())) {
                    conceptNameType = ConceptNameType.FULLY_SPECIFIED;
                } else if (cnType.equals(ConceptNameType.SHORT.toString())) {
                    conceptNameType = ConceptNameType.SHORT;
                } else if (cnType.equals(ConceptNameType.INDEX_TERM.toString())) {
                    conceptNameType = ConceptNameType.INDEX_TERM;
                }
                conceptName.setConceptNameType(conceptNameType);
            }
            String localeString = rs.getString("locale");
            conceptName.setLocale(!StringUtils.isBlank(localeString) ? LocaleUtility.fromSpecification(localeString) : null);
            conceptName.setLocalePreferred(rs.getBoolean("locale_preferred"));
            conceptName.setVoided(false);
            if (!localeConceptNamesMap.containsKey(conceptName.getLocale())) {
                localeConceptNamesMap.put(conceptName.getLocale(), new LinkedList<>());
            }
            localeConceptNamesMap.get(conceptName.getLocale()).add(conceptName);
        }
    } catch (DatabaseException | SQLException e) {
        log.warn("Error generated", e);
    } finally {
        if (pStmt != null) {
            try {
                pStmt.close();
            } catch (SQLException e) {
                log.warn("Failed to close the prepared statement object");
            }
        }
    }
    return localeConceptNamesMap;
}
Also used : Locale(java.util.Locale) SQLException(java.sql.SQLException) ConceptNameType(org.openmrs.api.ConceptNameType) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) ConceptName(org.openmrs.ConceptName) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Locale (java.util.Locale)1 DatabaseException (liquibase.exception.DatabaseException)1 ConceptName (org.openmrs.ConceptName)1 ConceptNameType (org.openmrs.api.ConceptNameType)1