Search in sources :

Example 36 with ConceptName

use of org.openmrs.ConceptName in project openmrs-core by openmrs.

the class HibernateConceptDAOTest method isConceptNameDuplicate_shouldNotFailIfConceptDoesNotHaveADefaultNameForLocale.

// TRUNK-4967
@Test
public void isConceptNameDuplicate_shouldNotFailIfConceptDoesNotHaveADefaultNameForLocale() {
    // given
    ConceptClass diagnosis = dao.getConceptClasses("Diagnosis").get(0);
    ConceptDatatype na = dao.getConceptDatatypeByName("N/A");
    Concept tuberculosis = new Concept();
    tuberculosis.addName(new ConceptName("Tuberculosis", Locale.US));
    tuberculosis.setDatatype(na);
    tuberculosis.setConceptClass(diagnosis);
    dao.saveConcept(tuberculosis);
    ConceptName shortName = new ConceptName("TB", Locale.FRANCE);
    shortName.setConceptNameType(ConceptNameType.SHORT);
    tuberculosis.addName(shortName);
    // when
    boolean duplicate = dao.isConceptNameDuplicate(shortName);
    // then
    // no NPE exception thrown
    assertThat(duplicate, is(false));
}
Also used : Concept(org.openmrs.Concept) ConceptClass(org.openmrs.ConceptClass) ConceptName(org.openmrs.ConceptName) ConceptDatatype(org.openmrs.ConceptDatatype) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 37 with ConceptName

use of org.openmrs.ConceptName in project openmrs-core by openmrs.

the class ConceptNameSaveHandlerTest method handle_shouldNotReplaceTagsThatHaveIds.

/**
 * @see ConceptNameSaveHandler#handle(ConceptName,User,Date,String)
 */
@Test
public void handle_shouldNotReplaceTagsThatHaveIds() {
    ConceptNameSaveHandler handler = new ConceptNameSaveHandler();
    ConceptName name = new ConceptName();
    ConceptNameTag tag = new ConceptNameTag("some randome tag name with an id", "");
    // this tag has an id
    tag.setConceptNameTagId(34);
    name.addTag(tag);
    handler.handle(name, null, null, null);
    ConceptNameTag newTag = name.getTags().iterator().next();
    Assert.assertEquals(34, newTag.getConceptNameTagId().intValue());
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag) ConceptName(org.openmrs.ConceptName) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 38 with ConceptName

use of org.openmrs.ConceptName in project openmrs-core by openmrs.

the class ConceptNameSaveHandlerTest method handle_shouldReplaceTagsWithoutIdsWithDatabaseFetchedTag.

/**
 * @see ConceptNameSaveHandler#handle(ConceptName,User,Date,String)
 */
@Test
public void handle_shouldReplaceTagsWithoutIdsWithDatabaseFetchedTag() {
    ConceptNameSaveHandler handler = new ConceptNameSaveHandler();
    ConceptName name = new ConceptName();
    // this tag has a null id
    name.addTag("preferred");
    // this tag has a null id
    name.addTag("short");
    handler.handle(name, null, null, null);
    for (ConceptNameTag tag : name.getTags()) {
        if (tag.getTag().equals("preferred")) {
            Assert.assertEquals(4, tag.getConceptNameTagId().intValue());
        } else if (tag.getTag().equals("short")) {
            Assert.assertEquals(2, tag.getConceptNameTagId().intValue());
        }
    }
}
Also used : ConceptNameTag(org.openmrs.ConceptNameTag) ConceptName(org.openmrs.ConceptName) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

Example 39 with ConceptName

use of org.openmrs.ConceptName in project openmrs-core by openmrs.

the class ConceptValidatorChangeSet method runBatchUpdate.

/**
 * Executes all the changes to the concept names as a batch update.
 *
 * @param connection The database connection
 */
private void runBatchUpdate(JdbcConnection connection) {
    PreparedStatement pStmt = null;
    try {
        connection.setAutoCommit(false);
        pStmt = connection.prepareStatement("UPDATE concept_name SET locale = ?, concept_name_type = ?, locale_preferred = ?, voided = ?, date_voided = ?, void_reason = ?, voided_by = ? WHERE concept_name_id = ?");
        Integer userId = DatabaseUpdater.getAuthenticatedUserId();
        // is we have no authenticated user(for API users), set as Daemon
        if (userId == null || userId < 1) {
            userId = getInt(connection, "SELECT min(user_id) FROM users");
            // leave it as null rather than setting it to 0
            if (userId < 1) {
                userId = null;
            }
        }
        for (ConceptName conceptName : updatedConceptNames) {
            pStmt.setString(1, conceptName.getLocale().toString());
            pStmt.setString(2, (conceptName.getConceptNameType() != null) ? conceptName.getConceptNameType().toString() : null);
            pStmt.setBoolean(3, conceptName.getLocalePreferred());
            pStmt.setBoolean(4, conceptName.getVoided());
            pStmt.setDate(5, conceptName.getVoided() ? new Date(System.currentTimeMillis()) : null);
            pStmt.setString(6, conceptName.getVoidReason());
            // "Not all databases allow for a non-typed Null to be sent to the backend", so we can't use setInt
            pStmt.setObject(7, (conceptName.getVoided() && userId != null) ? userId : null, Types.INTEGER);
            pStmt.setInt(8, conceptName.getConceptNameId());
            pStmt.addBatch();
        }
        try {
            int[] updateCounts = pStmt.executeBatch();
            for (int updateCount : updateCounts) {
                if (updateCount > -1) {
                    log.debug("Successfully executed: updateCount=" + updateCount);
                } else if (updateCount == Statement.SUCCESS_NO_INFO) {
                    log.debug("Successfully executed; No Success info");
                } else if (updateCount == Statement.EXECUTE_FAILED) {
                    log.warn("Failed to execute update");
                }
            }
            log.debug("Committing updates...");
            connection.commit();
        } catch (BatchUpdateException be) {
            log.warn("Error generated while processsing batch update", be);
            int[] updateCounts = be.getUpdateCounts();
            for (int updateCount : updateCounts) {
                if (updateCount > -1) {
                    log.warn("Executed with exception: updateCount=" + updateCount);
                } else if (updateCount == Statement.SUCCESS_NO_INFO) {
                    log.warn("Executed with exception; No Success info");
                } else if (updateCount == Statement.EXECUTE_FAILED) {
                    log.warn("Failed to execute update with exception");
                }
            }
            try {
                log.warn("Rolling back batch", be);
                connection.rollback();
            } catch (Exception rbe) {
                log.warn("Error generated while rolling back batch update", be);
            }
        }
    } catch (SQLException | DatabaseException e) {
        log.warn("Error generated", e);
    } finally {
        // reset to auto commit mode
        try {
            connection.setAutoCommit(true);
        } catch (DatabaseException e) {
            log.warn("Failed to reset auto commit back to true", e);
        }
        if (pStmt != null) {
            try {
                pStmt.close();
            } catch (SQLException e) {
                log.warn("Failed to close the prepared statement object");
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) ConceptName(org.openmrs.ConceptName) PreparedStatement(java.sql.PreparedStatement) DatabaseException(liquibase.exception.DatabaseException) Date(java.sql.Date) BatchUpdateException(java.sql.BatchUpdateException) CustomChangeException(liquibase.exception.CustomChangeException) SetupException(liquibase.exception.SetupException) SQLException(java.sql.SQLException) DatabaseException(liquibase.exception.DatabaseException) BatchUpdateException(java.sql.BatchUpdateException)

Example 40 with ConceptName

use of org.openmrs.ConceptName 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 &lt;Locale List&lt;ConceptNames&gt;&gt;.
 *
 * @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

ConceptName (org.openmrs.ConceptName)100 Test (org.junit.Test)78 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)71 Concept (org.openmrs.Concept)62 ConceptDescription (org.openmrs.ConceptDescription)42 ConceptDatatype (org.openmrs.ConceptDatatype)34 ConceptClass (org.openmrs.ConceptClass)33 Locale (java.util.Locale)32 OpenmrsMatchers.hasConcept (org.openmrs.test.OpenmrsMatchers.hasConcept)22 ArrayList (java.util.ArrayList)11 Date (java.util.Date)9 Obs (org.openmrs.Obs)9 BindException (org.springframework.validation.BindException)8 ConceptMap (org.openmrs.ConceptMap)7 Errors (org.springframework.validation.Errors)7 LinkedList (java.util.LinkedList)6 List (java.util.List)6 Patient (org.openmrs.Patient)6 Encounter (org.openmrs.Encounter)5 OrderFrequency (org.openmrs.OrderFrequency)5