use of liquibase.exception.CustomChangeException in project openmrs-core by openmrs.
the class MigrateDrugOrderFrequencyToCodedOrderFrequencyChangeset method execute.
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection connection = (JdbcConnection) database.getConnection();
try {
Set<String> uniqueFrequencies = DatabaseUtil.getUniqueNonNullColumnValues("frequency_text", "drug_order", String.class, connection.getUnderlyingConnection());
migrateFrequenciesToCodedValue(connection, uniqueFrequencies);
} catch (Exception e) {
throw new CustomChangeException(e);
}
}
use of liquibase.exception.CustomChangeException in project openmrs-core by openmrs.
the class MoveDeletedHL7sChangeSet method execute.
/**
* @see CustomTaskChange#execute(Database)
*/
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection connection = (JdbcConnection) database.getConnection();
StringBuilder getDeletedHL7sSql = new StringBuilder();
getDeletedHL7sSql.append("SELECT hl7_source, hl7_source_key, hl7_data, date_created, uuid, hl7_in_archive_id");
getDeletedHL7sSql.append(" FROM hl7_in_archive WHERE message_state=");
getDeletedHL7sSql.append(HL7Constants.HL7_STATUS_DELETED);
StringBuilder insertHL7Sql = new StringBuilder();
insertHL7Sql.append("INSERT INTO hl7_in_queue");
insertHL7Sql.append(" (hl7_source, hl7_source_key, hl7_data, date_created, uuid, message_state)");
insertHL7Sql.append(" VALUES (?, ?, ?, ?, ?, ");
insertHL7Sql.append(HL7Constants.HL7_STATUS_DELETED);
insertHL7Sql.append(")");
PreparedStatement insertStatement;
PreparedStatement deleteStatement;
try {
insertStatement = connection.prepareStatement(insertHL7Sql.toString());
deleteStatement = connection.prepareStatement("DELETE FROM hl7_in_archive WHERE hl7_in_archive_id=?");
// iterate over deleted HL7s
ResultSet archives = connection.createStatement().executeQuery(getDeletedHL7sSql.toString());
while (archives.next()) {
// add to the queue
// set hl7_source
insertStatement.setString(1, archives.getString(1));
// set hl7_source_key
insertStatement.setString(2, archives.getString(2));
// set hl7_data
insertStatement.setString(3, archives.getString(3));
// set date_created
insertStatement.setDate(4, archives.getDate(4));
// set uuid
insertStatement.setString(5, archives.getString(5));
insertStatement.executeUpdate();
// remove from the archives
deleteStatement.setInt(1, archives.getInt(6));
deleteStatement.executeUpdate();
}
// cleanup
if (insertStatement != null) {
insertStatement.close();
}
if (deleteStatement != null) {
deleteStatement.close();
}
} catch (SQLException | DatabaseException e) {
throw new CustomChangeException("Unable to move deleted HL7s from archive table to queue table", e);
}
}
use of liquibase.exception.CustomChangeException in project openmrs-core by openmrs.
the class CreateDiscontinueOrders method execute.
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection connection = (JdbcConnection) database.getConnection();
try {
List<DiscontinuedOrder> discontinuedOrders = getDiscontinuedOrders(connection);
createDiscontinueOrders(connection, discontinuedOrders);
} catch (SQLException | DatabaseException e) {
throw new CustomChangeException(e);
}
}
use of liquibase.exception.CustomChangeException in project openmrs-core by openmrs.
the class DuplicateEncounterRoleNameChangeSet method execute.
/**
* Method to perform validation and resolution of duplicate EncounterRole names
*/
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection connection = (JdbcConnection) database.getConnection();
Map<String, HashSet<Integer>> duplicates = new HashMap<>();
Statement stmt = null;
PreparedStatement pStmt = null;
ResultSet rs = null;
Boolean initialAutoCommit = null;
try {
initialAutoCommit = connection.getAutoCommit();
// set auto commit mode to false for UPDATE action
connection.setAutoCommit(false);
stmt = connection.createStatement();
rs = stmt.executeQuery("SELECT * FROM encounter_role INNER JOIN (SELECT name FROM encounter_role GROUP BY name HAVING count(name) > 1) dup ON encounter_role.name = dup.name");
Integer id;
String name;
while (rs.next()) {
id = rs.getInt("encounter_role_id");
name = rs.getString("name");
if (duplicates.get(name) == null) {
HashSet<Integer> results = new HashSet<>();
results.add(id);
duplicates.put(name, results);
} else {
HashSet<Integer> results = duplicates.get(name);
results.add(id);
}
}
for (Object o : duplicates.entrySet()) {
Map.Entry pairs = (Map.Entry) o;
HashSet values = (HashSet) pairs.getValue();
List<Integer> ids = new ArrayList<Integer>(values);
int duplicateNameId = 1;
for (int i = 1; i < ids.size(); i++) {
String newName = pairs.getKey() + "_" + duplicateNameId;
List<List<Object>> duplicateResult;
boolean duplicateName;
Connection con = DatabaseUpdater.getConnection();
do {
String sqlValidatorString = "select * from encounter_role where name = '" + newName + "'";
duplicateResult = DatabaseUtil.executeSQL(con, sqlValidatorString, true);
if (!duplicateResult.isEmpty()) {
duplicateNameId += 1;
newName = pairs.getKey() + "_" + duplicateNameId;
duplicateName = true;
} else {
duplicateName = false;
}
} while (duplicateName);
pStmt = connection.prepareStatement("update encounter_role set name = ?, changed_by = ?, date_changed = ? where encounter_role_id = ?");
if (!duplicateResult.isEmpty()) {
pStmt.setString(1, newName);
}
pStmt.setString(1, newName);
pStmt.setInt(2, DatabaseUpdater.getAuthenticatedUserId());
Calendar cal = Calendar.getInstance();
Date date = new Date(cal.getTimeInMillis());
pStmt.setDate(3, date);
pStmt.setInt(4, ids.get(i));
duplicateNameId += 1;
pStmt.executeUpdate();
}
}
} catch (BatchUpdateException e) {
log.warn("Error generated while processsing batch insert", e);
try {
log.debug("Rolling back batch", e);
connection.rollback();
} catch (Exception rbe) {
log.warn("Error generated while rolling back batch insert", e);
}
// marks the changeset as a failed one
throw new CustomChangeException("Failed to update one or more duplicate EncounterRole names", e);
} catch (Exception e) {
throw new CustomChangeException(e);
} finally {
// set auto commit to its initial state
try {
connection.commit();
if (initialAutoCommit != null) {
connection.setAutoCommit(initialAutoCommit);
}
} catch (DatabaseException e) {
log.warn("Failed to set auto commit to ids initial state", e);
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Failed to close the resultset object");
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Failed to close the select statement used to identify duplicate EncounterRole object names");
}
}
if (pStmt != null) {
try {
pStmt.close();
} catch (SQLException e) {
log.warn("Failed to close the prepared statement used to update duplicate EncounterRole object names");
}
}
}
}
use of liquibase.exception.CustomChangeException in project openmrs-core by openmrs.
the class BooleanConceptChangeSet method changeObs.
/**
* changes all obs which have boolean values to the new (coded) representation of boolean
* values.
*
* @param connection a DatabaseConnection
* @param trueConceptName the concept name for boolean true values
* @param falseConceptName the concept name for boolean false values
* @throws CustomChangeException
*/
private void changeObs(JdbcConnection connection) throws CustomChangeException {
PreparedStatement updateStatement = null;
try {
/* replace value_numerical boolean values by coded boolean values */
updateStatement = connection.prepareStatement("UPDATE obs SET value_coded = ?, value_numeric = NULL WHERE value_numeric != 0 AND concept_id IN (SELECT concept_id FROM concept WHERE datatype_id = 10)");
updateStatement.setInt(1, trueConceptId);
updateStatement.executeUpdate();
updateStatement.close();
updateStatement = connection.prepareStatement("UPDATE obs SET value_coded = ?, value_numeric = NULL WHERE value_numeric = 0 AND concept_id IN (SELECT concept_id FROM concept WHERE datatype_id = 10)");
updateStatement.setInt(1, falseConceptId);
updateStatement.executeUpdate();
} catch (DatabaseException | SQLException e) {
throw new CustomChangeException("Unable to change obs", e);
} finally {
if (updateStatement != null) {
try {
updateStatement.close();
} catch (SQLException e) {
}
}
}
}
Aggregations