clean up for MIFOSX-1145

This commit is contained in:
Vishwas Babu A J
2014-09-10 00:06:25 -07:00
parent e27613550f
commit 84ec7cd7b9
@@ -8,6 +8,7 @@ package org.mifosplatform.infrastructure.dataqueries.service;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@@ -82,6 +83,9 @@ public class ReadWriteNonCoreDataServiceImpl implements ReadWriteNonCoreDataServ
} }
}; };
private final static List<String> stringDataTypes = Arrays.asList("char", "varchar", "blob", "text", "tinyblob", "tinytext",
"mediumblob", "mediumtext", "longblob", "longtext");
private final JdbcTemplate jdbcTemplate; private final JdbcTemplate jdbcTemplate;
private final DataSource dataSource; private final DataSource dataSource;
private final PlatformSecurityContext context; private final PlatformSecurityContext context;
@@ -771,38 +775,23 @@ public class ReadWriteNonCoreDataServiceImpl implements ReadWriteNonCoreDataServ
} }
/** /**
* Update data table, set column value to empty string where current value is NULL. * Update data table, set column value to empty string where current value
* Run update SQL only if the "mandatory" property is set to true * is NULL. Run update SQL only if the "mandatory" property is set to true
* *
* @param datatableName Name of data table * @param datatableName
* @param column JSON encoded array of column properties * Name of data table
* @param column
* JSON encoded array of column properties
* @see https://mifosforge.jira.com/browse/MIFOSX-1145 * @see https://mifosforge.jira.com/browse/MIFOSX-1145
**/ **/
private void removeNullValuesFromColumn(final String datatableName, final JsonObject column, private void removeNullValuesFromStringColumn(final String datatableName, final JsonObject column,
final Map<String, ResultsetColumnHeaderData> mapColumnNameDefinition) { final Map<String, ResultsetColumnHeaderData> mapColumnNameDefinition) {
final Boolean mandatory = (column.has("mandatory")) ? column.get("mandatory").getAsBoolean() : false; final Boolean mandatory = (column.has("mandatory")) ? column.get("mandatory").getAsBoolean() : false;
final String name = (column.has("name")) ? column.get("name").getAsString() : ""; final String name = (column.has("name")) ? column.get("name").getAsString() : "";
final String type = (mapColumnNameDefinition.containsKey(name)) ? mapColumnNameDefinition.get(name).getColumnType() : ""; final String type = (mapColumnNameDefinition.containsKey(name)) ? mapColumnNameDefinition.get(name).getColumnType() : "";
final List<String> stringDataTypes = new ArrayList<String>();
// =========== STRING DATA TYPES ============= if (StringUtils.isNotEmpty(type)) {
// For string types other than ENUM, the default value is an empty string. if (mandatory && stringDataTypes.contains(type.toLowerCase())) {
// No errors are thrown when the value is set to an empty string.
// For ENUM, the default is the first enumeration value.
stringDataTypes.add("char");
stringDataTypes.add("varchar");
stringDataTypes.add("blob");
stringDataTypes.add("text");
stringDataTypes.add("tinyblob");
stringDataTypes.add("tinytext");
stringDataTypes.add("mediumblob");
stringDataTypes.add("mediumtext");
stringDataTypes.add("longblob");
stringDataTypes.add("longtext");
// =============================================
if(StringUtils.isNotEmpty(type)) {
if(mandatory && stringDataTypes.contains(type.toLowerCase())) {
StringBuilder sqlBuilder = new StringBuilder(); StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("UPDATE `" + datatableName + "` SET `" + name + "` = '' WHERE `" + name + "` IS NULL"); sqlBuilder.append("UPDATE `" + datatableName + "` SET `" + name + "` = '' WHERE `" + name + "` IS NULL");
@@ -915,8 +904,8 @@ public class ReadWriteNonCoreDataServiceImpl implements ReadWriteNonCoreDataServ
final Map<String, Long> codeMappings = new HashMap<>(); final Map<String, Long> codeMappings = new HashMap<>();
final List<String> removeMappings = new ArrayList<>(); final List<String> removeMappings = new ArrayList<>();
for (final JsonElement column : changeColumns) { for (final JsonElement column : changeColumns) {
// remove NULL values from column where mandatory is true // remove NULL values from column where mandatory is true
removeNullValuesFromColumn(datatableName, column.getAsJsonObject(), mapColumnNameDefinition); removeNullValuesFromStringColumn(datatableName, column.getAsJsonObject(), mapColumnNameDefinition);
parseDatatableColumnForUpdate(column.getAsJsonObject(), mapColumnNameDefinition, sqlBuilder, datatableName, parseDatatableColumnForUpdate(column.getAsJsonObject(), mapColumnNameDefinition, sqlBuilder, datatableName,
constrainBuilder, codeMappings, removeMappings, isConstraintApproach); constrainBuilder, codeMappings, removeMappings, isConstraintApproach);
@@ -936,14 +925,15 @@ public class ReadWriteNonCoreDataServiceImpl implements ReadWriteNonCoreDataServ
if (e.getMessage().contains("Error on rename")) { throw new PlatformServiceUnavailableException( if (e.getMessage().contains("Error on rename")) { throw new PlatformServiceUnavailableException(
"error.msg.datatable.column.update.not.allowed", "One of the column name modification not allowed"); } "error.msg.datatable.column.update.not.allowed", "One of the column name modification not allowed"); }
} catch (final Exception e) { } catch (final Exception e) {
// handle all other exceptions in here // handle all other exceptions in here
// check if exception message contains the "invalid use of null value" SQL exception message // check if exception message contains the
// throw a 503 HTTP error - PlatformServiceUnavailableException // "invalid use of null value" SQL exception message
if (e.getMessage().toLowerCase().contains("invalid use of null value")) { throw new PlatformServiceUnavailableException( // throw a 503 HTTP error -
"error.msg.datatable.column.update.not.allowed", "One of the data table columns contains null values"); // PlatformServiceUnavailableException
} if (e.getMessage().toLowerCase().contains("invalid use of null value")) { throw new PlatformServiceUnavailableException(
} "error.msg.datatable.column.update.not.allowed", "One of the data table columns contains null values"); }
}
} }
} catch (final SQLGrammarException e) { } catch (final SQLGrammarException e) {
final Throwable realCause = e.getCause(); final Throwable realCause = e.getCause();
@@ -1522,7 +1512,7 @@ public class ReadWriteNonCoreDataServiceImpl implements ReadWriteNonCoreDataServ
String paramValue = pValue; String paramValue = pValue;
if (columnHeader.isDateDisplayType() || columnHeader.isIntegerDisplayType() || columnHeader.isDecimalDisplayType()) { if (columnHeader.isDateDisplayType() || columnHeader.isIntegerDisplayType() || columnHeader.isDecimalDisplayType()) {
// only trim if string is not empty and is not null. // only trim if string is not empty and is not null.
// throws a NULL pointer exception if the check below is not applied // throws a NULL pointer exception if the check below is not applied
paramValue = StringUtils.isNotEmpty(paramValue) ? paramValue.trim() : paramValue; paramValue = StringUtils.isNotEmpty(paramValue) ? paramValue.trim() : paramValue;
} }