WIP - more generic json response builder
This commit is contained in:
+9
-3
@@ -74,14 +74,20 @@ public class AdditionalFieldsApiResource {
|
||||
@Path("{type}/{set}/{id}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public Response extraData(@PathParam("type") final String type,
|
||||
@PathParam("set") final String set, @PathParam("id") final Long id) {
|
||||
public String extraData(@PathParam("type") final String type,
|
||||
@PathParam("set") final String set, @PathParam("id") final Long id,
|
||||
@QueryParam("pretty") String pretty) {
|
||||
|
||||
try {
|
||||
GenericResultset result = this.readExtraDataAndReportingService
|
||||
.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) {
|
||||
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
ApiParameterError error = ApiParameterError.parameterError(
|
||||
|
||||
@@ -10,8 +10,10 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
import org.mifosng.data.EntityIdentifier;
|
||||
@@ -45,39 +47,31 @@ public class OfficeApiResource {
|
||||
@GET
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retrieveOffices(@QueryParam("fields") String fields,
|
||||
@QueryParam("pretty") String pretty) {
|
||||
public String retrieveOffices(@Context UriInfo uriInfo) {
|
||||
|
||||
Collection<OfficeData> offices = this.readPlatformService
|
||||
.retrieveAllOffices();
|
||||
|
||||
String filterType = "E";
|
||||
String fieldList = "allowedParents";
|
||||
if (this.jsonFormattingService.isPassed(fields)) {
|
||||
filterType = "I";
|
||||
fieldList = fields;
|
||||
}
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(offices,
|
||||
filterType, fieldList,
|
||||
this.jsonFormattingService.isTrue(pretty));
|
||||
String templateFields = "allowedParents";
|
||||
return this.jsonFormattingService.convertRequest(offices, filterType,
|
||||
templateFields, uriInfo.getQueryParameters());
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("template")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
@Produces({ MediaType.APPLICATION_JSON })
|
||||
public String retrieveOfficeTemplate(@QueryParam("pretty") String pretty) {
|
||||
public String retrieveOfficeTemplate(@Context UriInfo uriInfo) {
|
||||
|
||||
OfficeData officeData = this.readPlatformService
|
||||
.retrieveNewOfficeTemplate();
|
||||
|
||||
String filterType = "I";
|
||||
String fieldList = "openingDate,allowedParents";
|
||||
String templateFields = "openingDate,allowedParents";
|
||||
|
||||
return this.jsonFormattingService.convertDataObjectJSON(officeData,
|
||||
filterType, fieldList,
|
||||
this.jsonFormattingService.isTrue(pretty));
|
||||
return this.jsonFormattingService.convertRequest(officeData,
|
||||
filterType, templateFields, uriInfo.getQueryParameters());
|
||||
}
|
||||
|
||||
@POST
|
||||
|
||||
+4
-1
@@ -1,9 +1,12 @@
|
||||
package org.mifosng.platform.api.infrastructure;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
public interface ApiJSONFormattingService {
|
||||
|
||||
String convertRequest(Object dataObject, String filterType,
|
||||
String filterFields, MultivaluedMap<String, String> queryParams);
|
||||
|
||||
String convertDataObjectJSON(Object dataObject, String filterType,
|
||||
String fields, boolean prettyOutput);
|
||||
|
||||
|
||||
+19
@@ -5,6 +5,8 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import javax.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
@@ -18,6 +20,23 @@ import org.springframework.stereotype.Service;
|
||||
@Service
|
||||
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
|
||||
public String convertDataObjectJSON(Object dataObject, String filterType,
|
||||
String fields, boolean prettyOutput) {
|
||||
|
||||
Reference in New Issue
Block a user