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,11 +180,11 @@ 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();
try {
Connection db_connection = dataSource.getConnection(); Connection db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); Statement db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(sql); ResultSet rs = db_statement.executeQuery(sql);
@@ -232,12 +221,16 @@ public class ReadExtraDataAndReportingServiceImpl implements
db_connection.close(); db_connection.close();
db_connection = null; db_connection = null;
} catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error",
"JPWWRONGMSG - " + e.getMessage(), "Sql: " + sql);
}
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,41 +286,44 @@ 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) {
String sql = null;
try {
Connection db_connection = dataSource.getConnection(); Connection db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); Statement db_statement = db_connection.createStatement();
ResultSet rs = db_statement.executeQuery(inputSql); ResultSet rs = db_statement.executeQuery(inputSql);
String sql = null; if (rs.next()) {
while (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.close();
db_statement = null; db_statement = null;
db_connection.close(); db_connection.close();
db_connection = null; db_connection = null;
} catch (SQLException e) {
throw new PlatformDataIntegrityException("error.msg.sql.error",
"JPWWRONGMSG - " + e.getMessage(), "Input Sql: " + inputSql);
}
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 {