From 45965a3f502bb4b93ac0f76c36b1536c6ef0af59 Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Thu, 17 May 2012 21:09:16 +1000 Subject: [PATCH] API refactoring Additional Fields --- APIFolder/api.htm | 9 +- ...etchydata-0.93.js => stretchydata-0.94.js} | 6 -- .../main/webapp/WEB-INF/views/common-head.jsp | 2 +- .../ReadExtraDataAndReportingServiceImpl.java | 98 ++++++++++++++++--- 4 files changed, 88 insertions(+), 27 deletions(-) rename mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/{stretchydata-0.93.js => stretchydata-0.94.js} (98%) diff --git a/APIFolder/api.htm b/APIFolder/api.htm index f007036f1..6f74caa97 100644 --- a/APIFolder/api.htm +++ b/APIFolder/api.htm @@ -505,10 +505,7 @@ Returns the Set of Additional Fields data for the ID provided. The ID is the id

Update Additional Fields

-Updates the Set of Additional Fields data for the ID provided. The ID is the id field of the Type table e.g. portfolio_client might have an id value of 4. -

-

-JPW note: currently using ed_transType = "E" for edit and ed_transType = "A" for new... probably need to not do that. +Updates (or creates) the Set of Additional Fields data for the ID provided. The ID is the id field of the Type table e.g. portfolio_client might have an id value of 4.

Arguments
None @@ -519,10 +516,10 @@ JPW note: currently using ed_transType = "E" for edit and ed_transType = "A" for POST https://thedomainname/v1/additionalfields/{TYPE}/{SET}/{ID} http://localhost:8080/mifosng-provider/api/v1/additionalfields/portfolio_client/Additional Information/2 -Request Body: + Content-Type: application/json +Request Body: { -"ed_transType":"E", "Ethnic_Group":"Berber", "Ethnic_Group_Other":"", "Household_Location":"East Sikkim", diff --git a/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.93.js b/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.94.js similarity index 98% rename from mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.93.js rename to mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.94.js index 09ee4f1fd..b6a4d0210 100644 --- a/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.93.js +++ b/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.94.js @@ -290,14 +290,8 @@ function extraDataBuildTemplate(data) { var dataLength = data.data.length; - var transType = 'E'; - if (dataLength == 0) - transType = 'A'; var extraDataTemplateVar = '
'; - extraDataTemplateVar += ''; - extraDataTemplateVar += ''; var colsPerRow = 2; diff --git a/mifosng-individual-lending-app/src/main/webapp/WEB-INF/views/common-head.jsp b/mifosng-individual-lending-app/src/main/webapp/WEB-INF/views/common-head.jsp index 0c6cd059f..3e7d623ac 100644 --- a/mifosng-individual-lending-app/src/main/webapp/WEB-INF/views/common-head.jsp +++ b/mifosng-individual-lending-app/src/main/webapp/WEB-INF/views/common-head.jsp @@ -34,7 +34,7 @@ - + diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java index 72842b5fa..34134eebc 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/ReadExtraDataAndReportingServiceImpl.java @@ -399,6 +399,7 @@ public class ReadExtraDataAndReportingServiceImpl implements String set, Long id) { GenericResultset result = new GenericResultset(); + try { Connection db_connection = dataSource.getConnection(); Statement db_statement1 = db_connection.createStatement(); @@ -533,11 +534,15 @@ public class ReadExtraDataAndReportingServiceImpl implements pValue = queryParams.get(key); logger.info("jpw: " + key + " - " + pValue); } - logger.info("endjpw: "); + checkResourceTypeThere(type, set); String fullDatasetName = getFullDatasetName(type, set); - String saveSql = getSaveSql(fullDatasetName, id, queryParams); + String transType = getTransType(type, fullDatasetName, id); + + String saveSql = getSaveSql(fullDatasetName, id, transType, queryParams); + + logger.info("saveSQL: " + saveSql); try { Connection db_connection = dataSource.getConnection(); Statement db_statement = db_connection.createStatement(); @@ -556,19 +561,85 @@ public class ReadExtraDataAndReportingServiceImpl implements } - private String getSaveSql(String fullSetName, Long id, - Map queryParams) { + private void checkResourceTypeThere(String type, String set) { + String sql = "select 'f' from stretchydata_datasettype t join stretchydata_dataset d on d.datasettype_id = t.id where d.`name` = '" + + set + "' and t.`name` = '" + type + "'"; + try { + Connection db_connection = dataSource.getConnection(); + Statement db_statement = db_connection.createStatement(); - String errMsg = ""; - String transType = queryParams.get("ed_transType"); - if (!(transType.equals("E") || transType.equals("A"))) { - errMsg = "transType not E or A - Value is: " + transType; + ResultSet rs = db_statement.executeQuery(sql); + + if (!(rs.next())) { + db_statement.close(); + db_statement = null; + db_connection.close(); + db_connection = null; + throw new PlatformResourceNotFoundException( + "error.msg.set.not.found", + "Additional Fields Set Not Found", "Type: " + type + + " Set: " + set); + } + db_statement.close(); + db_statement = null; + db_connection.close(); + db_connection = null; + + } catch (SQLException e) { throw new PlatformDataIntegrityException("error.msg.sql.error", - "JPWWRONGMSG - " + errMsg, - "Additional Fields Full Set Name: " + fullSetName - + " Id: " + id); + "JPWWRONGMSG - " + e.getMessage(), + "Additional Fields Type: " + type + " Set: " + set + + " Sql: " + sql); } + } + + private String getTransType(String type, String fullDatasetName, Long id) { + String transType = null; + + String sql = "select s.id from `" + type + "` t left join `" + + fullDatasetName + "` s on s.id = t.id where t.id = " + + id; + + try { + Connection db_connection = dataSource.getConnection(); + Statement db_statement = db_connection.createStatement(); + ResultSet rs = db_statement.executeQuery(sql); + + if (rs.next()) { + Long idValue = rs.getLong("id"); + if (idValue != null) { + transType = "E"; + } else { + transType = "A"; + } + } else { + db_statement.close(); + db_statement = null; + db_connection.close(); + db_connection = null; + throw new PlatformResourceNotFoundException( + "error.msg.type.value.not.found", + "Additional Fields Type: " + type + + " Id Not Found for " + id); + } + 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(), + "Additional Fields Type: " + type + " Full Set Name: " + + fullDatasetName + " Sql: " + sql); + } + + return transType; + } + + private String getSaveSql(String fullSetName, Long id, String transType, + Map queryParams) { + String pValue = ""; String pValueWrite = ""; String saveSql = ""; @@ -582,7 +653,7 @@ public class ReadExtraDataAndReportingServiceImpl implements saveSql = "update `" + fullSetName + "` "; for (String key : keys) { - if (!(key.equals("ed_transType") || key.equals("id"))) { + if (!(key.equalsIgnoreCase("id"))) { if (firstColumn) { saveSql += " set "; firstColumn = false; @@ -610,7 +681,7 @@ public class ReadExtraDataAndReportingServiceImpl implements String columnName = ""; for (String key : keys) { pValue = queryParams.get(key); - if (!(key.equals("ed_transType") || key.equals("id"))) { + if (!(key.equalsIgnoreCase("id"))) { pValue = queryParams.get(key); if (pValue == null || pValue.equals("")) { @@ -629,7 +700,6 @@ public class ReadExtraDataAndReportingServiceImpl implements saveSql = "insert into `" + fullSetName + "` (id" + insertColumns + ")" + " select " + id + " as id" + selectColumns; } - // logger.info("Save SQL: " + saveSql); return saveSql; } } \ No newline at end of file