From 326fd4f25c9a7344ba80285173346293e4ae548b Mon Sep 17 00:00:00 2001 From: John Woodlock Date: Fri, 11 May 2012 12:37:18 +1000 Subject: [PATCH] WIP post extra data and parson json string for edits --- .../WEB-INF/static/stretchydata-0.90.js | 21 +++++++- .../platform/api/ExtraDataApiResource.java | 52 ++++++++++--------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.90.js b/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.90.js index b278ef8e0..faba70cc5 100644 --- a/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.90.js +++ b/mifosng-individual-lending-app/src/main/webapp/WEB-INF/static/stretchydata-0.90.js @@ -348,6 +348,23 @@ } } + $.fn.serializeObject = function() + { + var o = {}; + var a = this.serializeArray(); + $.each(a, function() { + if (o[this.name] !== undefined) { + if (!o[this.name].push) { + o[this.name] = [o[this.name]]; + } + o[this.name].push(this.value || ''); + } else { + o[this.name] = this.value || ''; + } + }); + return o; + }; + function extraDataOpenDialog(url) { // var saveButton = jQuery.i18n.prop('dialog.button.save'); @@ -361,7 +378,9 @@ $(this).attr("selected", "selected"); }); - var form_data = $('#entityform').serialize(); + //var form_data = $('#entityform').serialize(); + var form_data = JSON.stringify($('#entityform').serializeObject()); + console.log(form_data); var jqxhr = $.ajax({ url : url, type : 'POST', diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java index a50de6559..3687c3a16 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/ExtraDataApiResource.java @@ -1,5 +1,6 @@ package org.mifosng.platform.api; +import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -7,6 +8,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; import javax.ws.rs.GET; @@ -22,6 +24,9 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; +import org.codehaus.jettison.json.JSONArray; +import org.codehaus.jettison.json.JSONException; +import org.codehaus.jettison.json.JSONObject; import org.mifosng.data.EntityIdentifier; import org.mifosng.data.ErrorResponse; import org.mifosng.data.ErrorResponseList; @@ -93,35 +98,34 @@ public class ExtraDataApiResource { @Path("{datasetType}/{datasetName}/{datasetPKValue}") @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON}) - public Response saveExtraData(@PathParam("datasetType") final String datasetType,@PathParam("datasetName") final String datasetName, @PathParam("datasetPKValue") final String datasetPKValue, @Context HttpServletRequest req) { + public Response saveExtraData(@PathParam("datasetType") final String datasetType,@PathParam("datasetName") final String datasetName, @PathParam("datasetPKValue") final String datasetPKValue, String reqbody) { try { - - //MultivaluedMap incomingParams = uriInfo.getQueryParameters(); + Map queryParams = new HashMap(); - - //Set keys = incomingParams.keySet(); - - logger.info("Request JPW: " + req.toString()); - Enumeration paramNames = req.getParameterNames(); String pValue = ""; String pName; - String[] paramValues; - while(paramNames.hasMoreElements()) { - pName = (String) paramNames.nextElement(); - paramValues = req.getParameterValues(pName); - if (paramValues.length > 1) { - logger.info("Unexpected Parameter Error: " + pName + " has " + paramValues.length + " value(s)"); - } else { - if (paramValues.length == 1) { - pValue = paramValues[0]; - } else { - pValue = ""; - } - } - logger.info(pName + " - " + pValue); - queryParams.put(pName, pValue); - } + try { + JSONObject jsonObj = new JSONObject(reqbody); + JSONArray jsonArr = jsonObj.names(); + if (jsonArr != null) { + for (int i = 0; i < jsonArr.length(); i++) { + pName = (String) jsonArr.get(i); + pValue = jsonObj.getString(pName); + logger.info( pName + " - " + pValue); + queryParams.put(pName, pValue); + } + } else { + throw new WebApplicationException(Response + .status(Status.BAD_REQUEST) + .entity("JSON body empty").build()); + } + } catch (JSONException e) { + throw new WebApplicationException(Response + .status(Status.BAD_REQUEST) + .entity("JSON body is wrong").build()); + } + this.readPlatformService.tempSaveExtraData(datasetType, datasetName, datasetPKValue, queryParams);