API refactoring Additional Fields

This commit is contained in:
John Woodlock
2012-05-17 21:09:16 +10:00
parent 62ec20112f
commit 45965a3f50
4 changed files with 88 additions and 27 deletions
+3 -6
View File
@@ -505,10 +505,7 @@ Returns the Set of Additional Fields data for the ID provided. The ID is the id
<div class="method-description"> <div class="method-description">
<h4>Update Additional Fields</h4> <h4>Update Additional Fields</h4>
<p> <p>
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. 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.
</p>
<p>
JPW note: currently using ed_transType = "E" for edit and ed_transType = "A" for new... probably need to not do that.
</p> </p>
<h5>Arguments</h5> <h5>Arguments</h5>
<span>None</span> <span>None</span>
@@ -519,10 +516,10 @@ JPW note: currently using ed_transType = "E" for edit and ed_transType = "A" for
<code class="method-declaration">POST https://thedomainname/v1/additionalfields/{TYPE}/{SET}/{ID}</code> <code class="method-declaration">POST https://thedomainname/v1/additionalfields/{TYPE}/{SET}/{ID}</code>
<code class="method-request">http://localhost:8080/mifosng-provider/api/v1/additionalfields/portfolio_client/Additional Information/2 <code class="method-request">http://localhost:8080/mifosng-provider/api/v1/additionalfields/portfolio_client/Additional Information/2
Request Body:
Content-Type: application/json Content-Type: application/json
Request Body:
{ {
"ed_transType":"E",
"Ethnic_Group":"Berber", "Ethnic_Group":"Berber",
"Ethnic_Group_Other":"", "Ethnic_Group_Other":"",
"Household_Location":"East Sikkim", "Household_Location":"East Sikkim",
@@ -290,14 +290,8 @@
function extraDataBuildTemplate(data) { function extraDataBuildTemplate(data) {
var dataLength = data.data.length; var dataLength = data.data.length;
var transType = 'E';
if (dataLength == 0)
transType = 'A';
var extraDataTemplateVar = '<form id="entityform"> <div id="formerrors"></div>'; var extraDataTemplateVar = '<form id="entityform"> <div id="formerrors"></div>';
extraDataTemplateVar += '<input id="ed_transType" name="ed_transType" type="hidden" value="'
+ transType + '"/>';
extraDataTemplateVar += '<table width="100%"><tr>'; extraDataTemplateVar += '<table width="100%"><tr>';
var colsPerRow = 2; var colsPerRow = 2;
@@ -34,7 +34,7 @@
<script type="text/javascript" src="${resValue}/libs/jquery.views.js"></script> <script type="text/javascript" src="${resValue}/libs/jquery.views.js"></script>
<script type="text/javascript" src="${resValue}/libs/jquery.observable.min.js"></script> <script type="text/javascript" src="${resValue}/libs/jquery.observable.min.js"></script>
<script type="text/javascript" src="${resValue}/libs/globalize/globalize.js"></script> <script type="text/javascript" src="${resValue}/libs/globalize/globalize.js"></script>
<script type="text/javascript" src="${resValue}/stretchydata-0.93.js"></script> <script type="text/javascript" src="${resValue}/stretchydata-0.94.js"></script>
<c:choose> <c:choose>
<c:when test="${not empty language}"> <c:when test="${not empty language}">
@@ -399,6 +399,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
String set, Long id) { String set, Long id) {
GenericResultset result = new GenericResultset(); GenericResultset result = new GenericResultset();
try { try {
Connection db_connection = dataSource.getConnection(); Connection db_connection = dataSource.getConnection();
Statement db_statement1 = db_connection.createStatement(); Statement db_statement1 = db_connection.createStatement();
@@ -533,11 +534,15 @@ public class ReadExtraDataAndReportingServiceImpl implements
pValue = queryParams.get(key); pValue = queryParams.get(key);
logger.info("jpw: " + key + " - " + pValue); logger.info("jpw: " + key + " - " + pValue);
} }
logger.info("endjpw: "); logger.info("endjpw: ");
checkResourceTypeThere(type, set);
String fullDatasetName = getFullDatasetName(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 { try {
Connection db_connection = dataSource.getConnection(); Connection db_connection = dataSource.getConnection();
Statement db_statement = db_connection.createStatement(); Statement db_statement = db_connection.createStatement();
@@ -556,19 +561,85 @@ public class ReadExtraDataAndReportingServiceImpl implements
} }
private String getSaveSql(String fullSetName, Long id, private void checkResourceTypeThere(String type, String set) {
Map<String, String> queryParams) { 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 = ""; ResultSet rs = db_statement.executeQuery(sql);
String transType = queryParams.get("ed_transType");
if (!(transType.equals("E") || transType.equals("A"))) { if (!(rs.next())) {
errMsg = "transType not E or A - Value is: " + transType; 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", throw new PlatformDataIntegrityException("error.msg.sql.error",
"JPWWRONGMSG - " + errMsg, "JPWWRONGMSG - " + e.getMessage(),
"Additional Fields Full Set Name: " + fullSetName "Additional Fields Type: " + type + " Set: " + set
+ " Id: " + id); + " 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<String, String> queryParams) {
String pValue = ""; String pValue = "";
String pValueWrite = ""; String pValueWrite = "";
String saveSql = ""; String saveSql = "";
@@ -582,7 +653,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
saveSql = "update `" + fullSetName + "` "; saveSql = "update `" + fullSetName + "` ";
for (String key : keys) { for (String key : keys) {
if (!(key.equals("ed_transType") || key.equals("id"))) { if (!(key.equalsIgnoreCase("id"))) {
if (firstColumn) { if (firstColumn) {
saveSql += " set "; saveSql += " set ";
firstColumn = false; firstColumn = false;
@@ -610,7 +681,7 @@ public class ReadExtraDataAndReportingServiceImpl implements
String columnName = ""; String columnName = "";
for (String key : keys) { for (String key : keys) {
pValue = queryParams.get(key); pValue = queryParams.get(key);
if (!(key.equals("ed_transType") || key.equals("id"))) { if (!(key.equalsIgnoreCase("id"))) {
pValue = queryParams.get(key); pValue = queryParams.get(key);
if (pValue == null || pValue.equals("")) { if (pValue == null || pValue.equals("")) {
@@ -629,7 +700,6 @@ public class ReadExtraDataAndReportingServiceImpl implements
saveSql = "insert into `" + fullSetName + "` (id" + insertColumns saveSql = "insert into `" + fullSetName + "` (id" + insertColumns
+ ")" + " select " + id + " as id" + selectColumns; + ")" + " select " + id + " as id" + selectColumns;
} }
// logger.info("Save SQL: " + saveSql);
return saveSql; return saveSql;
} }
} }