use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.
the class ConceptValidatorChangeSet method getInt.
/**
* returns an integer resulting from the execution of an sql statement
*
* @param connection a DatabaseConnection
* @param sql the sql statement to execute
* @return integer resulting from the execution of the sql statement
*/
private int getInt(JdbcConnection connection, String sql) {
Statement stmt = null;
int result = 0;
try {
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
result = rs.getInt(1);
} else {
log.warn("No row returned by getInt() method");
}
if (rs.next()) {
log.warn("Multiple rows returned by getInt() method");
}
return result;
} catch (DatabaseException | SQLException e) {
log.warn("Error generated", e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Failed to close the statement object");
}
}
}
return result;
}
use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.
the class ConceptValidatorChangeSet method getAllowedLocalesList.
/**
* Retrieves the list of allowed locales from the database, sets the default locale, english and
* the default locale will be added to the list allowed locales if not yet included
*
* @param connection The database connection
* @return A list of allowed locales
*/
@SuppressWarnings("unchecked")
private List<Locale> getAllowedLocalesList(JdbcConnection connection) {
Statement stmt = null;
ListOrderedSet allowedLocales = new ListOrderedSet();
try {
// get the default locale
stmt = connection.createStatement();
ResultSet rsDefaultLocale = stmt.executeQuery("SELECT property_value FROM global_property WHERE property = '" + OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE + "'");
if (rsDefaultLocale.next()) {
String defaultLocaleStr = rsDefaultLocale.getString("property_value");
if (!StringUtils.isBlank(defaultLocaleStr) && defaultLocaleStr.length() > 1) {
Locale defaultLocaleGP = LocaleUtility.fromSpecification(defaultLocaleStr);
if (defaultLocaleGP != null) {
defaultLocale = defaultLocaleGP;
}
} else {
updateWarnings.add("'" + defaultLocaleStr + "' is an invalid value for the global property default locale");
}
}
allowedLocales.add(defaultLocale);
// get the locale.allowed.list
ResultSet rsAllowedLocales = stmt.executeQuery("SELECT property_value FROM global_property WHERE property = '" + OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST + "'");
if (rsAllowedLocales.next()) {
String allowedLocaleStr = rsAllowedLocales.getString("property_value");
if (!StringUtils.isBlank(allowedLocaleStr)) {
String[] localesArray = allowedLocaleStr.split(",");
for (String localeStr : localesArray) {
if (localeStr.trim().length() > 1) {
allowedLocales.add(LocaleUtility.fromSpecification(localeStr.trim()));
} else {
updateWarnings.add("'" + localeStr + "' is an invalid value for the global property locale.allowed.list");
}
}
}
} else {
log.warn("The global property '" + OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST + "' isn't set");
}
} catch (DatabaseException | SQLException e) {
log.warn("Error generated", e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Failed to close the statement object");
}
}
}
// if it isn't among
allowedLocales.add(new Locale("en"));
return allowedLocales.asList();
}
use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.
the class CreateCodedOrderFrequencyForDrugOrderFrequencyChangeset method insertUniqueFrequencies.
private void insertUniqueFrequencies(JdbcConnection connection, Set<String> uniqueFrequencies) throws CustomChangeException, SQLException, DatabaseException {
PreparedStatement insertOrderFrequencyStatement = null;
Boolean autoCommit = null;
try {
autoCommit = connection.getAutoCommit();
connection.setAutoCommit(false);
insertOrderFrequencyStatement = connection.prepareStatement("insert into order_frequency " + "(concept_id, creator, date_created, retired, uuid) values (?, ?, ?, ?, ?)");
Date date = new Date(new java.util.Date().getTime());
for (String frequency : uniqueFrequencies) {
if (StringUtils.isBlank(frequency)) {
continue;
}
Integer conceptIdForFrequency = UpgradeUtil.getConceptIdForUnits(frequency);
if (conceptIdForFrequency == null) {
throw new CustomChangeException("No concept mapping found for frequency: " + frequency);
}
Integer orderFrequencyId = UpgradeUtil.getOrderFrequencyIdForConceptId(connection.getUnderlyingConnection(), conceptIdForFrequency);
if (orderFrequencyId != null) {
// a single concept is mapped to more than one text or there is an order frequency already
continue;
}
// Generating UUID for order frequency. Generated UUIDs will be the same if concepts UUIDs are the same.
String uuid = UpgradeUtil.getConceptUuid(connection.getUnderlyingConnection(), conceptIdForFrequency);
// Adding random value for order frequency
uuid += "-6925ebb0-7c69-11e3-baa7-0800200c9a66";
uuid = UUID.nameUUIDFromBytes(uuid.getBytes(StandardCharsets.UTF_8)).toString();
insertOrderFrequencyStatement.setInt(1, conceptIdForFrequency);
insertOrderFrequencyStatement.setInt(2, 1);
insertOrderFrequencyStatement.setDate(3, date);
insertOrderFrequencyStatement.setBoolean(4, false);
insertOrderFrequencyStatement.setString(5, uuid);
insertOrderFrequencyStatement.executeUpdate();
insertOrderFrequencyStatement.clearParameters();
}
connection.commit();
} catch (DatabaseException | SQLException e) {
handleError(connection, e);
} finally {
if (autoCommit != null) {
connection.setAutoCommit(autoCommit);
}
if (insertOrderFrequencyStatement != null) {
insertOrderFrequencyStatement.close();
}
}
}
use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.
the class DisableTriggersChangeSet method execute.
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection connection = (JdbcConnection) database.getConnection();
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
String[] types = { "TABLE" };
ResultSet rs = metadata.getTables(null, null, "%", types);
while (rs.next()) {
String tableName = rs.getString(3);
connection.prepareStatement("ALTER TABLE " + tableName + " DISABLE TRIGGER ALL").execute();
}
} catch (DatabaseException | SQLException ex) {
throw new CustomChangeException("Error disabling trigger: " + ex);
}
}
use of liquibase.exception.DatabaseException in project openmrs-core by openmrs.
the class EnableTriggersChangeSet method execute.
@Override
public void execute(Database database) throws CustomChangeException {
JdbcConnection connection = (JdbcConnection) database.getConnection();
DatabaseMetaData metadata;
try {
metadata = connection.getMetaData();
String[] types = { "TABLE" };
ResultSet rs = metadata.getTables(null, null, "%", types);
while (rs.next()) {
String tableName = rs.getString(3);
connection.prepareStatement("ALTER TABLE " + tableName + " ENABLE TRIGGER ALL").execute();
}
} catch (DatabaseException | SQLException ex) {
throw new CustomChangeException("Error enabling trigger: " + ex);
}
}
Aggregations