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;
}
Aggregations