WIP error handling in reports and additionalfields api

This commit is contained in:
John Woodlock
2012-05-17 16:29:13 +10:00
parent 6693b45003
commit 49b90212ac
3 changed files with 86 additions and 87 deletions
@@ -18,8 +18,8 @@ public interface ReadExtraDataAndReportingService {
AdditionalFieldsSets retrieveExtraDatasetNames(String type); AdditionalFieldsSets retrieveExtraDatasetNames(String type);
GenericResultset retrieveExtraData(String type, String set, String id); GenericResultset retrieveExtraData(String type, String set, Long id);
void updateExtraData(String type, String set, String id, void updateExtraData(String type, String set, Long id,
Map<String, String> queryParams); Map<String, String> queryParams);
} }
@@ -25,6 +25,8 @@ import org.mifosng.data.AdditionalFieldsSets;
import org.mifosng.data.reports.GenericResultset; import org.mifosng.data.reports.GenericResultset;
import org.mifosng.data.reports.ResultsetColumnHeader; import org.mifosng.data.reports.ResultsetColumnHeader;
import org.mifosng.data.reports.ResultsetDataRow; import org.mifosng.data.reports.ResultsetDataRow;
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
import org.mifosng.platform.exceptions.PlatformResourceNotFoundException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -137,8 +139,9 @@ public class ReadExtraDataAndReportingServiceImpl implements
final String type, final Map<String, String> queryParams) { final String type, final Map<String, String> queryParams) {
if (name == null) { if (name == null) {
logger.info("Report Name not Found"); throw new PlatformDataIntegrityException(
return null; "error.msg.report.name.null",
"JPWWRONGMSG - Report Name is null.");
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
@@ -166,24 +169,10 @@ public class ReadExtraDataAndReportingServiceImpl implements
+ " left join stretchy_parameter p on p.parameter_id = rp.parameter_id" + " left join stretchy_parameter p on p.parameter_id = rp.parameter_id"
+ " order by r.report_name, rp.parameter_id"; + " order by r.report_name, rp.parameter_id";
} else { } else {
try { sql = getSQLtoRun(name, type, queryParams);
sql = getSQLtoRun(name, type, queryParams);
} catch (SQLException e) {
logger.info(name + ": Failed in getSQLtoRun");
throw new WebApplicationException(Response
.status(Status.BAD_REQUEST).entity(e.getMessage())
.build());
}
} }
GenericResultset result = null; GenericResultset result = fillReportingGenericResultSet(sql);
try {
result = fillReportingGenericResultSet(sql);
} catch (SQLException e) {
logger.info("Error - SQL: " + sql);
throw new WebApplicationException(Response
.status(Status.BAD_REQUEST).entity(e.getMessage()).build());
}
long elapsed = System.currentTimeMillis() - startTime; long elapsed = System.currentTimeMillis() - startTime;
logger.info("FINISHING Report/Request Name: " + name + " - " + type logger.info("FINISHING Report/Request Name: " + name + " - " + type
@@ -191,53 +180,57 @@ public class ReadExtraDataAndReportingServiceImpl implements
return result; return result;
} }
private GenericResultset fillReportingGenericResultSet(final String sql) private GenericResultset fillReportingGenericResultSet(final String sql) {
throws SQLException {
GenericResultset result = new GenericResultset(); GenericResultset result = new GenericResultset();
Connection db_connection = dataSource.getConnection(); try {
Statement db_statement = db_connection.createStatement(); Connection db_connection = dataSource.getConnection();
ResultSet rs = db_statement.executeQuery(sql); Statement db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData(); ResultSetMetaData rsmd = rs.getMetaData();
String columnName = null; String columnName = null;
String columnValue = null; String columnValue = null;
List<ResultsetColumnHeader> columnHeaders = new ArrayList<ResultsetColumnHeader>(); List<ResultsetColumnHeader> columnHeaders = new ArrayList<ResultsetColumnHeader>();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
ResultsetColumnHeader rsch = new ResultsetColumnHeader();
rsch.setColumnName(rsmd.getColumnName(i + 1));
rsch.setColumnType(rsmd.getColumnTypeName(i + 1));
columnHeaders.add(rsch);
}
result.setColumnHeaders(columnHeaders);
List<ResultsetDataRow> resultsetDataRows = new ArrayList<ResultsetDataRow>();
ResultsetDataRow resultsetDataRow;
while (rs.next()) {
resultsetDataRow = new ResultsetDataRow();
List<String> columnValues = new ArrayList<String>();
for (int i = 0; i < rsmd.getColumnCount(); i++) { for (int i = 0; i < rsmd.getColumnCount(); i++) {
columnName = rsmd.getColumnName(i + 1); ResultsetColumnHeader rsch = new ResultsetColumnHeader();
columnValue = rs.getString(columnName); rsch.setColumnName(rsmd.getColumnName(i + 1));
columnValues.add(columnValue); rsch.setColumnType(rsmd.getColumnTypeName(i + 1));
columnHeaders.add(rsch);
} }
resultsetDataRow.setRow(columnValues); result.setColumnHeaders(columnHeaders);
resultsetDataRows.add(resultsetDataRow);
List<ResultsetDataRow> resultsetDataRows = new ArrayList<ResultsetDataRow>();
ResultsetDataRow resultsetDataRow;
while (rs.next()) {
resultsetDataRow = new ResultsetDataRow();
List<String> columnValues = new ArrayList<String>();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
columnName = rsmd.getColumnName(i + 1);
columnValue = rs.getString(columnName);
columnValues.add(columnValue);
}
resultsetDataRow.setRow(columnValues);
resultsetDataRows.add(resultsetDataRow);
}
result.setData(resultsetDataRows);
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error",
"JPWWRONGMSG - " + e.getMessage(), "Sql: " + sql);
} }
result.setData(resultsetDataRows);
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
return result; return result;
} }
private String getSQLtoRun(final String name, final String type, private String getSQLtoRun(final String name, final String type,
final Map<String, String> queryParams) throws SQLException { final Map<String, String> queryParams) {
String sql = null; String sql = null;
String rptDB = queryParams.get("${rptDB}"); String rptDB = queryParams.get("${rptDB}");
if ((rptDB == null) || rptDB.equals("")) { if ((rptDB == null) || rptDB.equals("")) {
@@ -293,42 +286,45 @@ public class ReadExtraDataAndReportingServiceImpl implements
// return true; // return true;
// } // }
private String getReportSql(String rptDB, String reportName) private String getReportSql(String rptDB, String reportName) {
throws SQLException {
String sql = "select report_sql as the_sql from " + rptDB String sql = "select report_sql as the_sql from " + rptDB
+ ".stretchy_report where report_name = '" + reportName + "'"; + ".stretchy_report where report_name = '" + reportName + "'";
logger.info("Report SQL: " + sql);
return getSql(sql); return getSql(sql);
} }
private String getParameterSql(String rptDB, String parameterName) private String getParameterSql(String rptDB, String parameterName) {
throws SQLException {
String sql = "select parameter_sql as the_sql from " + rptDB String sql = "select parameter_sql as the_sql from " + rptDB
+ ".stretchy_parameter where parameter_name = '" + ".stretchy_parameter where parameter_name = '"
+ parameterName + "'"; + parameterName + "'";
logger.info("Parameter SQL: " + sql);
return getSql(sql); return getSql(sql);
} }
private String getSql(String inputSql) throws SQLException { private String getSql(String inputSql) {
Connection db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(inputSql);
String sql = null; String sql = null;
try {
Connection db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(inputSql);
while (rs.next()) { if (rs.next()) {
sql = rs.getString("the_sql"); sql = rs.getString("the_sql");
} else {
throw new PlatformResourceNotFoundException(
"error.msg.report.name.not.found",
"Reporting Meta Data Entry Not Found", "Input Sql: "
+ inputSql);
}
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
} catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error",
"JPWWRONGMSG - " + e.getMessage(), "Input Sql: " + inputSql);
} }
db_statement.close();
db_statement = null;
db_connection.close();
db_connection = null;
return sql; return sql;
} }
@@ -372,19 +368,22 @@ public class ReadExtraDataAndReportingServiceImpl implements
} }
@Override @Override
public GenericResultset retrieveExtraData(String type, String set, String id) { public GenericResultset retrieveExtraData(String type, String set, Long id) {
if (type == null) { if (type == null) {
logger.info("Type not Found"); throw new PlatformDataIntegrityException(
return null; "error.msg.additional.fields.type.null",
"JPWWRONGMSG - Additional Fields Type is null.");
} }
if (set == null) { if (set == null) {
logger.info("Set not Found"); throw new PlatformDataIntegrityException(
return null; "error.msg.additional.fields.set.null",
"JPWWRONGMSG - Additional Fields Set is null.");
} }
if (id == null) { if (id == null) {
logger.info("Id not Found"); throw new PlatformDataIntegrityException(
return null; "error.msg.additional.fields.id.null",
"JPWWRONGMSG - Additional Fields Id is null.");
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
@@ -405,7 +404,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
} }
private GenericResultset fillExtraDataGenericResultSet(String type, private GenericResultset fillExtraDataGenericResultSet(String type,
String set, String id) throws SQLException { String set, Long id) throws SQLException {
GenericResultset result = new GenericResultset(); GenericResultset result = new GenericResultset();
String fullDatasetName = getFullDatasetName(type, set); String fullDatasetName = getFullDatasetName(type, set);
@@ -506,7 +505,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
} }
@Override @Override
public void updateExtraData(String type, String set, String id, public void updateExtraData(String type, String set, Long id,
Map<String, String> queryParams) { Map<String, String> queryParams) {
logger.info("updateExtraData - type: " + type + " set: " + set logger.info("updateExtraData - type: " + type + " set: " + set
+ " id: " + id); + " id: " + id);
@@ -539,7 +538,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
} }
private String getSaveSql(String fullSetName, String id, private String getSaveSql(String fullSetName, Long id,
Map<String, String> queryParams) { Map<String, String> queryParams) {
String errMsg = ""; String errMsg = "";
@@ -66,7 +66,7 @@ public class AdditionalFieldsApiResource {
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public Response extraData(@PathParam("type") final String type, public Response extraData(@PathParam("type") final String type,
@PathParam("set") final String set, @PathParam("set") final String set,
@PathParam("id") final String id) { @PathParam("id") final Long id) {
try { try {
GenericResultset result = this.readExtraDataAndReportingService GenericResultset result = this.readExtraDataAndReportingService
@@ -87,7 +87,7 @@ public class AdditionalFieldsApiResource {
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public Response saveExtraData(@PathParam("type") final String type, public Response saveExtraData(@PathParam("type") final String type,
@PathParam("set") final String set, @PathParam("set") final String set,
@PathParam("id") final String id, String reqbody) { @PathParam("id") final Long id, String reqbody) {
try { try {