tidy up of json deserialization for funds,offices and permissions.

This commit is contained in:
Keith Woodlock
2012-07-19 15:05:31 +01:00
parent 0bfeecad7b
commit 5d07e70d59
10 changed files with 222 additions and 86 deletions
@@ -1,8 +1,6 @@
package org.mifosng.platform.api;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.Consumes;
@@ -14,7 +12,6 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@@ -22,6 +19,7 @@ import org.mifosng.platform.api.commands.FundCommand;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiParameterHelper;
import org.mifosng.platform.fund.service.FundReadPlatformService;
import org.mifosng.platform.fund.service.FundWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -47,23 +45,12 @@ public class FundsApiResource {
@Produces({MediaType.APPLICATION_JSON})
public String retrieveFunds(@Context final UriInfo uriInfo) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String commaSerperatedParameters = "";
if (queryParams.getFirst("fields") != null) {
commaSerperatedParameters = queryParams.getFirst("fields");
}
boolean prettyPrint = false;
if (queryParams.getFirst("pretty") != null) {
String prettyPrintValue = queryParams.getFirst("pretty");
prettyPrint = "true".equalsIgnoreCase(prettyPrintValue);
}
Set<String> responseParameters = new HashSet<String>(Arrays.asList(commaSerperatedParameters.split("\\s*,\\s*")));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<FundData> funds = this.readPlatformService.retrieveAllFunds();
return this.apiDataConversionService.covertFundDataToJson(prettyPrint, responseParameters, funds.toArray(new FundData[funds.size()]));
return this.apiDataConversionService.convertFundDataToJson(prettyPrint, responseParameters, funds.toArray(new FundData[funds.size()]));
}
@POST
@@ -83,10 +70,13 @@ public class FundsApiResource {
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String retreiveOffice(@PathParam("fundId") final Long fundId, @Context UriInfo uriInfo) {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
FundData fund = this.readPlatformService.retrieveFund(fundId);
return this.apiDataConversionService.covertFundDataToJson(fund);
return this.apiDataConversionService.convertFundDataToJson(prettyPrint, responseParameters, fund);
}
@PUT
@@ -1,6 +1,9 @@
package org.mifosng.platform.api;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -9,7 +12,6 @@ import javax.ws.rs.PUT;
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;
@@ -19,7 +21,7 @@ import org.mifosng.platform.api.commands.OfficeCommand;
import org.mifosng.platform.api.data.EntityIdentifier;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
import org.mifosng.platform.api.infrastructure.ApiParameterHelper;
import org.mifosng.platform.organisation.service.OfficeReadPlatformService;
import org.mifosng.platform.organisation.service.OfficeWritePlatformService;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,10 +33,6 @@ import org.springframework.stereotype.Component;
@Scope("singleton")
public class OfficesApiResource {
private String defaultFieldList = "openingDate";
private String allowedFieldList = "allowedParents";
private String filterName = "myFilter";
@Autowired
private OfficeReadPlatformService readPlatformService;
@@ -44,33 +42,41 @@ public class OfficesApiResource {
@Autowired
private ApiDataConversionService apiDataConversionService;
@Autowired
private ApiJSONFormattingService jsonFormattingService;
@GET
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String retrieveOffices(@Context UriInfo uriInfo) {
public String retrieveOffices(@Context final UriInfo uriInfo) {
Set<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "name", "nameDecorated", "externalId", "openingDate",
"hierarchy", "parentId", "parentName"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<OfficeData> offices = this.readPlatformService.retrieveAllOffices();
String selectedFields = "";
return this.jsonFormattingService.convertRequest(offices, filterName,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
return this.apiDataConversionService.convertOfficeDataToJson(prettyPrint, responseParameters, offices.toArray(new OfficeData[offices.size()]));
}
@GET
@Path("template")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String retrieveOfficeTemplate(@Context UriInfo uriInfo) {
public String retrieveOfficeTemplate(@Context final UriInfo uriInfo) {
OfficeData officeData = this.readPlatformService
.retrieveNewOfficeTemplate();
Set<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "name", "nameDecorated", "externalId", "openingDate",
"hierarchy", "parentId", "parentName", "allowedParents"));
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
String selectedFields = defaultFieldList + "," + allowedFieldList;
return this.jsonFormattingService.convertRequest(officeData,
filterName, allowedFieldList, selectedFields,
uriInfo.getQueryParameters());
OfficeData officeData = this.readPlatformService.retrieveNewOfficeTemplate();
return this.apiDataConversionService.convertOfficeDataToJson(prettyPrint, typicalResponseParameters, officeData);
}
@POST
@@ -89,18 +95,26 @@ public class OfficesApiResource {
@Path("{officeId}")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String retreiveOffice(@PathParam("officeId") final Long officeId,
@QueryParam("template") String template, @Context UriInfo uriInfo) {
public String retreiveOffice(@PathParam("officeId") final Long officeId, @Context final UriInfo uriInfo) {
Set<String> typicalResponseParameters = new HashSet<String>(
Arrays.asList("id", "name", "nameDecorated", "externalId", "openingDate",
"hierarchy", "parentId", "parentName"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters());
OfficeData office = this.readPlatformService.retrieveOffice(officeId);
if (template != null && template.equalsIgnoreCase("true")) {
office.setAllowedParents(this.readPlatformService
.retrieveAllowedParents(officeId));
if (template) {
office.setAllowedParents(this.readPlatformService.retrieveAllowedParents(officeId));
responseParameters.add("allowedParents");
}
String selectedFields = "";
return this.jsonFormattingService.convertRequest(office, filterName,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
return this.apiDataConversionService.convertOfficeDataToJson(prettyPrint, responseParameters, office);
}
@PUT
@@ -1,6 +1,7 @@
package org.mifosng.platform.api;
import java.util.Collection;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -11,7 +12,8 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.mifosng.platform.api.data.PermissionData;
import org.mifosng.platform.api.infrastructure.ApiJSONFormattingService;
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
import org.mifosng.platform.api.infrastructure.ApiParameterHelper;
import org.mifosng.platform.user.service.PermissionReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
@@ -21,25 +23,23 @@ import org.springframework.stereotype.Component;
@Component
@Scope("singleton")
public class PermissionsApiResource {
private String allowedFieldList = "";
private String filterName = "permissionFilter";
@Autowired
private PermissionReadPlatformService permissionReadPlatformService;
@Autowired
private ApiJSONFormattingService jsonFormattingService;
private ApiDataConversionService apiDataConversionService;
@GET
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public String retrieveAllPermissions(@Context final UriInfo uriInfo) {
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<PermissionData> permissions = this.permissionReadPlatformService.retrieveAllPermissions();
String selectedFields = "";
return this.jsonFormattingService.convertRequest(permissions,
filterName, allowedFieldList, selectedFields,
uriInfo.getQueryParameters());
return this.apiDataConversionService.convertPermissionDataToJson(prettyPrint, responseParameters, permissions.toArray(new PermissionData[permissions.size()]));
}
}
@@ -2,29 +2,22 @@ package org.mifosng.platform.api.data;
import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonFilter;
@JsonIgnoreProperties({ "orgId", "groupType" })
@JsonFilter("permissionFilter")
public class PermissionData implements Serializable {
private Long id;
private String name;
private String description;
private String code;
private int groupType;
public PermissionData() {
//
}
public PermissionData(final Long id, final String name, final String description, final String code, int groupType) {
public PermissionData(final Long id, final String name, final String description, final String code) {
this.id = id;
this.name = name;
this.description = description;
this.code = code;
this.groupType = groupType;
}
@Override
@@ -69,12 +62,4 @@ public class PermissionData implements Serializable {
public void setCode(final String code) {
this.code = code;
}
public int getGroupType() {
return groupType;
}
public void setGroupType(int groupType) {
this.groupType = groupType;
}
}
@@ -16,14 +16,22 @@ import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.PermissionData;
public interface ApiDataConversionService {
//
String convertPermissionDataToJson(boolean prettyPrint, Set<String> responseParameters, PermissionData... permissions);
//
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
String covertFundDataToJson(boolean prettyPrint, Set<String> responseParameters, FundData... funds);
String convertFundDataToJson(boolean prettyPrint, Set<String> responseParameters, FundData... funds);
OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json);
String convertOfficeDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeData... offices);
RoleCommand convertJsonToRoleCommand(Long resourceIdentifier, String json);
@@ -47,6 +55,4 @@ public interface ApiDataConversionService {
OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(String jsonRequestBody);
NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String jsonRequestBody);
}
@@ -32,6 +32,8 @@ import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
import org.mifosng.platform.api.commands.UserCommand;
import org.mifosng.platform.api.data.ApiParameterError;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.PermissionData;
import org.mifosng.platform.api.errorhandling.InvalidJsonException;
import org.mifosng.platform.api.errorhandling.UnsupportedParameterException;
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
@@ -58,20 +60,85 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
}
@Override
public String covertFundDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final FundData... funds) {
public String convertPermissionDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final PermissionData... permissions) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "nameDecorated", "externalId", "openingDate",
"hierarchy", "parentId", "parentName", "allowedParents"));
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "externalId"));
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
final Set<String> parameterNamesToSkip = new HashSet<String>(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
Gson gsonDeserializer = new GsonBuilder().addSerializationExclusionStrategy(strategy).create();
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
return gsonDeserializer.toJson(permissions);
}
@Override
public String convertOfficeDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final OfficeData... offices) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "nameDecorated", "externalId", "openingDate",
"hierarchy", "parentId", "parentName", "allowedParents"));
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter());
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
return gsonDeserializer.toJson(offices);
}
@Override
public String convertFundDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final FundData... funds) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "externalId"));
final Set<String> parameterNamesToSkip = new HashSet<String>();
if (!responseParameters.isEmpty()) {
if (!supportedParameters.containsAll(responseParameters)) {
throw new UnsupportedParameterException(new ArrayList<String>(responseParameters));
}
parameterNamesToSkip.addAll(supportedParameters);
parameterNamesToSkip.removeAll(responseParameters);
}
ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip);
GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy);
if (prettyPrint) {
builder.setPrettyPrinting();
}
Gson gsonDeserializer = builder.create();
return gsonDeserializer.toJson(funds);
}
@@ -0,0 +1,42 @@
package org.mifosng.platform.api.infrastructure;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.MultivaluedMap;
import org.apache.commons.lang.StringUtils;
public class ApiParameterHelper {
public static Set<String> extractFieldsForResponseIfProvided(final MultivaluedMap<String, String> queryParams) {
Set<String> fields = new HashSet<String>();
String commaSerperatedParameters = "";
if (queryParams.getFirst("fields") != null) {
commaSerperatedParameters = queryParams.getFirst("fields");
if (StringUtils.isNotBlank(commaSerperatedParameters)) {
fields = new HashSet<String>(Arrays.asList(commaSerperatedParameters.split("\\s*,\\s*")));
}
}
return fields;
}
public static boolean prettyPrint(final MultivaluedMap<String, String> queryParams) {
boolean prettyPrint = false;
if (queryParams.getFirst("pretty") != null) {
String prettyPrintValue = queryParams.getFirst("pretty");
prettyPrint = "true".equalsIgnoreCase(prettyPrintValue);
}
return prettyPrint;
}
public static boolean template(final MultivaluedMap<String, String> queryParams) {
boolean template = false;
if (queryParams.getFirst("template") != null) {
String prettyPrintValue = queryParams.getFirst("template");
template = "true".equalsIgnoreCase(prettyPrintValue);
}
return template;
}
}
@@ -0,0 +1,32 @@
package org.mifosng.platform.api.infrastructure;
import java.lang.reflect.Type;
import org.joda.time.LocalDate;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
* Serializer for joda time {@link LocalDate} that returns date in array format to match previous jackson functionality.
*/
public class JodaLocalDateAdapter implements JsonSerializer<LocalDate> {
@Override
public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) {
JsonArray array = null;
if (src != null) {
array = new JsonArray();
array.add(new JsonPrimitive(src.getYearOfEra()));
array.add(new JsonPrimitive(src.getMonthOfYear()));
array.add(new JsonPrimitive(src.getDayOfMonth()));
}
return array;
}
}
@@ -49,6 +49,6 @@ public class Permission extends AbstractPersistable<Long> {
}
public PermissionData toData() {
return new PermissionData(this.getId(), this.defaultName, this.defaultDescription, this.code, this.permissionGroup.ordinal());
return new PermissionData(this.getId(), this.defaultName, this.defaultDescription, this.code);
}
}
@@ -45,9 +45,9 @@ public class PermissionReadPlatformServiceImpl implements PermissionReadPlatform
String name = rs.getString("name");
String description = rs.getString("description");
String code = rs.getString("code");
Integer groupType = JdbcSupport.getInteger(rs, "groupType");
// Integer groupType = JdbcSupport.getInteger(rs, "groupType");
return new PermissionData(id, name, description, code, groupType);
return new PermissionData(id, name, description, code);
}
public String schema() {