WIP - more generic json response builder

This commit is contained in:
John Woodlock
2012-05-25 16:22:39 +10:00
parent e43f007bd4
commit 07b909206c
4 changed files with 42 additions and 20 deletions
@@ -74,14 +74,20 @@ public class AdditionalFieldsApiResource {
@Path("{type}/{set}/{id}") @Path("{type}/{set}/{id}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public Response extraData(@PathParam("type") final String type, public String extraData(@PathParam("type") final String type,
@PathParam("set") final String set, @PathParam("id") final Long id) { @PathParam("set") final String set, @PathParam("id") final Long id,
@QueryParam("pretty") String pretty) {
try { try {
GenericResultset result = this.readExtraDataAndReportingService GenericResultset result = this.readExtraDataAndReportingService
.retrieveExtraData(type, set, id); .retrieveExtraData(type, set, id);
return Response.ok().entity(result).build(); String filterType = "E";
String fieldList = "";
return this.jsonFormattingService.convertDataObjectJSON(result,
filterType, fieldList,
this.jsonFormattingService.isTrue(pretty));
} catch (InvalidSqlException e) { } catch (InvalidSqlException e) {
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>(); List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
ApiParameterError error = ApiParameterError.parameterError( ApiParameterError error = ApiParameterError.parameterError(
@@ -10,8 +10,10 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.mifosng.data.EntityIdentifier; import org.mifosng.data.EntityIdentifier;
@@ -45,39 +47,31 @@ public class OfficeApiResource {
@GET @GET
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public String retrieveOffices(@QueryParam("fields") String fields, public String retrieveOffices(@Context UriInfo uriInfo) {
@QueryParam("pretty") String pretty) {
Collection<OfficeData> offices = this.readPlatformService Collection<OfficeData> offices = this.readPlatformService
.retrieveAllOffices(); .retrieveAllOffices();
String filterType = "E"; String filterType = "E";
String fieldList = "allowedParents"; String templateFields = "allowedParents";
if (this.jsonFormattingService.isPassed(fields)) { return this.jsonFormattingService.convertRequest(offices, filterType,
filterType = "I"; templateFields, uriInfo.getQueryParameters());
fieldList = fields;
}
return this.jsonFormattingService.convertDataObjectJSON(offices,
filterType, fieldList,
this.jsonFormattingService.isTrue(pretty));
} }
@GET @GET
@Path("template") @Path("template")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON })
public String retrieveOfficeTemplate(@QueryParam("pretty") String pretty) { public String retrieveOfficeTemplate(@Context UriInfo uriInfo) {
OfficeData officeData = this.readPlatformService OfficeData officeData = this.readPlatformService
.retrieveNewOfficeTemplate(); .retrieveNewOfficeTemplate();
String filterType = "I"; String filterType = "I";
String fieldList = "openingDate,allowedParents"; String templateFields = "openingDate,allowedParents";
return this.jsonFormattingService.convertDataObjectJSON(officeData, return this.jsonFormattingService.convertRequest(officeData,
filterType, fieldList, filterType, templateFields, uriInfo.getQueryParameters());
this.jsonFormattingService.isTrue(pretty));
} }
@POST @POST
@@ -1,9 +1,12 @@
package org.mifosng.platform.api.infrastructure; package org.mifosng.platform.api.infrastructure;
import java.util.Set; import javax.ws.rs.core.MultivaluedMap;
public interface ApiJSONFormattingService { public interface ApiJSONFormattingService {
String convertRequest(Object dataObject, String filterType,
String filterFields, MultivaluedMap<String, String> queryParams);
String convertDataObjectJSON(Object dataObject, String filterType, String convertDataObjectJSON(Object dataObject, String filterType,
String fields, boolean prettyOutput); String fields, boolean prettyOutput);
@@ -5,6 +5,8 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.ws.rs.core.MultivaluedMap;
import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
@@ -18,6 +20,23 @@ import org.springframework.stereotype.Service;
@Service @Service
public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService { public class ApiJSONFormattingServiceImpl implements ApiJSONFormattingService {
@Override
public String convertRequest(Object dataObject, String filterType, String filterFields,
MultivaluedMap<String, String> queryParams) {
String fields = queryParams.getFirst("fields");
String calcFilterType = filterType;
String fieldList = filterFields;
if (isPassed(fields)) {
calcFilterType = "I";
fieldList = fields;
}
return convertDataObjectJSON(dataObject,
calcFilterType, fieldList,
isTrue(queryParams.getFirst("pretty")));
}
@Override @Override
public String convertDataObjectJSON(Object dataObject, String filterType, public String convertDataObjectJSON(Object dataObject, String filterType,
String fields, boolean prettyOutput) { String fields, boolean prettyOutput) {