From d7b06e9809ae2a586204cae9af287d506c40023d Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Fri, 20 Jul 2012 08:43:26 +0100 Subject: [PATCH] json serialization tidy up for user admin areas. --- .../api/OfficeTransactionsApiResource.java | 25 +++--- .../platform/api/RolesApiResource.java | 64 ++++++++++------ .../platform/api/UsersApiResource.java | 76 +++++++++++++------ .../platform/api/data/AppUserData.java | 5 -- .../mifosng/platform/api/data/RoleData.java | 5 -- .../ApiDataConversionService.java | 9 +++ .../ApiDataConversionServiceImpl.java | 65 ++++++++++++++++ .../AppUserReadPlatformServiceImpl.java | 8 +- 8 files changed, 185 insertions(+), 72 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/OfficeTransactionsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/OfficeTransactionsApiResource.java index 226da59b4..6cdd3f111 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/OfficeTransactionsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/OfficeTransactionsApiResource.java @@ -1,5 +1,9 @@ package org.mifosng.platform.api; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -11,10 +15,12 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.mifosng.platform.api.commands.BranchMoneyTransferCommand; +import org.mifosng.platform.api.data.AppUserData; import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.OfficeTransactionData; 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; @@ -44,16 +50,17 @@ public class OfficeTransactionsApiResource { @Produces({MediaType.APPLICATION_JSON}) public String newOfficeTransactionDetails(@Context final UriInfo uriInfo) { - OfficeTransactionData officeTransactionData = this.readPlatformService - .retrieveNewOfficeTransactionDetails(); + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "transactionDate", "allowedOffices", "currencyOptions")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + OfficeTransactionData officeTransactionData = this.readPlatformService.retrieveNewOfficeTransactionDetails(); - String filterName = "myFilter"; - String defaultFieldList = "transactionDate"; - String allowedFieldList = "currencyOptions,allowedOffices"; - String selectedFields = defaultFieldList + "," + allowedFieldList; - return this.jsonFormattingService.convertRequest(officeTransactionData, - filterName, allowedFieldList, selectedFields, - uriInfo.getQueryParameters()); + return this.apiDataConversionService.convertOfficeTransactionDataToJson(prettyPrint, responseParameters, officeTransactionData); } @POST diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/RolesApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/RolesApiResource.java index e691dff8c..523b56e02 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/RolesApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/RolesApiResource.java @@ -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; @@ -20,7 +22,7 @@ import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.api.data.PermissionData; import org.mifosng.platform.api.data.RoleData; 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.user.service.PermissionReadPlatformService; import org.mifosng.platform.user.service.RoleReadPlatformService; import org.mifosng.platform.user.service.RoleWritePlatformService; @@ -33,9 +35,6 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class RolesApiResource { - private String allowedFieldList = "availablePermissions"; - private String filterName = "roleFilter"; - @Autowired private RoleReadPlatformService roleReadPlatformService; @@ -45,9 +44,6 @@ public class RolesApiResource { @Autowired private RoleWritePlatformService roleWritePlatformService; - @Autowired - private ApiJSONFormattingService jsonFormattingService; - @Autowired private ApiDataConversionService apiDataConversionService; @@ -55,12 +51,18 @@ public class RolesApiResource { @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public String retrieveAllRoles(@Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "name", "description")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); Collection roles = this.roleReadPlatformService.retrieveAllRoles(); - - String selectedFields = "id,name,description"; - return this.jsonFormattingService.convertRequest(roles, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + + return this.apiDataConversionService.convertRoleDataToJson(prettyPrint, responseParameters, roles.toArray(new RoleData[roles.size()])); } @GET @@ -69,14 +71,20 @@ public class RolesApiResource { @Produces({MediaType.APPLICATION_JSON}) public String retrieveNewRoleDetails(@Context final UriInfo uriInfo) { + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "name", "description", "availablePermissions", "selectedPermissions")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + Collection allPermissions = this.permissionReadPlatformService.retrieveAllPermissions(); RoleData role = new RoleData(); role.setAvailablePermissions(allPermissions); - - String selectedFields = allowedFieldList; - return this.jsonFormattingService.convertRequest(role, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + + return this.apiDataConversionService.convertRoleDataToJson(prettyPrint, responseParameters, role); } @POST @@ -95,21 +103,29 @@ public class RolesApiResource { @Path("{roleId}") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public String retrieveRole(@PathParam("roleId") final Long roleId, - @QueryParam("template") final String template, @Context final UriInfo uriInfo) { + public String retrieveRole(@PathParam("roleId") final Long roleId, @Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "name", "description")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); RoleData role = this.roleReadPlatformService.retrieveRole(roleId); - if (template != null && template.equalsIgnoreCase("true")) { - Collection availablePermissions = this.permissionReadPlatformService - .retrieveAllPermissions(); + if (template) { + Collection availablePermissions = this.permissionReadPlatformService.retrieveAllPermissions(); availablePermissions.removeAll(role.getSelectedPermissions()); role.setAvailablePermissions(availablePermissions); + + responseParameters.add("availablePermissions"); + responseParameters.add("selectedPermissions"); } - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(role, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + return this.apiDataConversionService.convertRoleDataToJson(prettyPrint, responseParameters, role); } @PUT diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/UsersApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/UsersApiResource.java index 5ef6f8ddf..ac87c51f6 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/UsersApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/UsersApiResource.java @@ -1,6 +1,11 @@ package org.mifosng.platform.api; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -18,8 +23,10 @@ import javax.ws.rs.core.UriInfo; import org.mifosng.platform.api.commands.UserCommand; import org.mifosng.platform.api.data.AppUserData; import org.mifosng.platform.api.data.EntityIdentifier; +import org.mifosng.platform.api.data.OfficeLookup; 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.user.service.AppUserReadPlatformService; import org.mifosng.platform.user.service.AppUserWritePlatformService; import org.springframework.beans.factory.annotation.Autowired; @@ -31,31 +38,34 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class UsersApiResource { - private String allowedFieldList = "allowedOffices,availableRoles"; - private String filterName = "userFilter"; - @Autowired private AppUserReadPlatformService appUserReadPlatformService; - + + @Autowired + private OfficeReadPlatformService officeReadPlatformService; + @Autowired private AppUserWritePlatformService appUserWritePlatformService; @Autowired private ApiDataConversionService apiDataConversionService; - @Autowired - private ApiJSONFormattingService jsonFormattingService; - @GET @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public String retrieveUsers(@Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); Collection users = this.appUserReadPlatformService.retrieveAllUsers(); - - String selectedFields = "id,officeId,officeName,username,firstname,lastname,email"; - return this.jsonFormattingService.convertRequest(users, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + + return this.apiDataConversionService.convertAppUserDataToJson(prettyPrint, responseParameters, users.toArray(new AppUserData[users.size()])); } @GET @@ -63,12 +73,26 @@ public class UsersApiResource { @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public String retrieveUser(@PathParam("userId") final Long userId, @Context final UriInfo uriInfo) { - + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); + AppUserData user = this.appUserReadPlatformService.retrieveUser(userId); - - String selectedFields = ""; - return this.jsonFormattingService.convertRequest(user, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + if (template) { + List offices = new ArrayList(this.officeReadPlatformService.retrieveAllOfficesForLookup()); + user.setAllowedOffices(offices); + responseParameters.add("allowedOffices"); + responseParameters.add("availableRoles"); + responseParameters.add("selectedRoles"); + } + + return this.apiDataConversionService.convertAppUserDataToJson(prettyPrint, responseParameters, user); } @GET @@ -76,13 +100,19 @@ public class UsersApiResource { @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) public String newUserDetails(@Context final UriInfo uriInfo) { + + Set typicalResponseParameters = new HashSet(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email", + "allowedOffices", "availableRoles", "selectedRoles")); + + Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + if (responseParameters.isEmpty()) { + responseParameters.addAll(typicalResponseParameters); + } + boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + + AppUserData user = this.appUserReadPlatformService.retrieveNewUserDetails(); - AppUserData newUser = this.appUserReadPlatformService - .retrieveNewUserDetails(); - - String selectedFields = allowedFieldList; - return this.jsonFormattingService.convertRequest(newUser, filterName, - allowedFieldList, selectedFields, uriInfo.getQueryParameters()); + return this.apiDataConversionService.convertAppUserDataToJson(prettyPrint, responseParameters, user); } @POST diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AppUserData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AppUserData.java index 4bc34c37b..ecbab89cf 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AppUserData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/AppUserData.java @@ -3,11 +3,6 @@ package org.mifosng.platform.api.data; import java.util.ArrayList; import java.util.List; -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.map.annotate.JsonFilter; - -@JsonIgnoreProperties({ "orgId" }) -@JsonFilter("userFilter") public class AppUserData { private Long id; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/RoleData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/RoleData.java index 764b4838b..f2109e637 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/RoleData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/RoleData.java @@ -3,11 +3,6 @@ package org.mifosng.platform.api.data; import java.util.ArrayList; import java.util.Collection; -import org.codehaus.jackson.annotate.JsonIgnoreProperties; -import org.codehaus.jackson.map.annotate.JsonFilter; - -@JsonIgnoreProperties({ "orgId" }) -@JsonFilter("roleFilter") public class RoleData { private Long id; diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java index b39af7861..e4209e8ad 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionService.java @@ -15,14 +15,23 @@ import org.mifosng.platform.api.commands.OrganisationCurrencyCommand; 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.AppUserData; import org.mifosng.platform.api.data.FundData; import org.mifosng.platform.api.data.OfficeData; +import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.PermissionData; +import org.mifosng.platform.api.data.RoleData; public interface ApiDataConversionService { // String convertPermissionDataToJson(boolean prettyPrint, Set responseParameters, PermissionData... permissions); + + String convertRoleDataToJson(boolean prettyPrint, Set responseParameters, RoleData... roles); + + String convertAppUserDataToJson(boolean prettyPrint, Set responseParameters, AppUserData... users); + + String convertOfficeTransactionDataToJson(boolean prettyPrint, Set responseParameters, OfficeTransactionData... officeTransactions); // FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json); diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java index 883f9e4ad..bd143597b 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/ApiDataConversionServiceImpl.java @@ -31,9 +31,12 @@ 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.ApiParameterError; +import org.mifosng.platform.api.data.AppUserData; import org.mifosng.platform.api.data.FundData; import org.mifosng.platform.api.data.OfficeData; +import org.mifosng.platform.api.data.OfficeTransactionData; import org.mifosng.platform.api.data.PermissionData; +import org.mifosng.platform.api.data.RoleData; import org.mifosng.platform.api.errorhandling.InvalidJsonException; import org.mifosng.platform.api.errorhandling.UnsupportedParameterException; import org.mifosng.platform.exceptions.PlatformApiDataValidationException; @@ -59,6 +62,68 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService { gsonConverter = new Gson(); } + @Override + public String convertOfficeTransactionDataToJson(final boolean prettyPrint, final Set responseParameters, final OfficeTransactionData... officeTransactions) { + return null; + } + + @Override + public String convertAppUserDataToJson(final boolean prettyPrint, final Set responseParameters, final AppUserData... users) { + + Set supportedParameters = new HashSet(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email", + "allowedOffices", "availableRoles", "selectedRoles")); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + if (!supportedParameters.containsAll(responseParameters)) { + throw new UnsupportedParameterException(new ArrayList(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(users); + + } + + @Override + public String convertRoleDataToJson(final boolean prettyPrint, final Set responseParameters, final RoleData... roles) { + Set supportedParameters = new HashSet(Arrays.asList("id", "name", "description", "availablePermissions", "selectedPermissions")); + + final Set parameterNamesToSkip = new HashSet(); + + if (!responseParameters.isEmpty()) { + if (!supportedParameters.containsAll(responseParameters)) { + throw new UnsupportedParameterException(new ArrayList(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(roles); + } + @Override public String convertPermissionDataToJson(final boolean prettyPrint, final Set responseParameters, final PermissionData... permissions) { Set supportedParameters = new HashSet(Arrays.asList("id", "name", "nameDecorated", "externalId", "openingDate", diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/AppUserReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/AppUserReadPlatformServiceImpl.java index 2ffbd1a3f..b0c75dadc 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/AppUserReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/AppUserReadPlatformServiceImpl.java @@ -91,15 +91,13 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic context.authenticatedUser(); - List offices = new ArrayList(officeReadPlatformService.retrieveAllOfficesForLookup()); - - List availableRoles = new ArrayList(this.roleReadPlatformService.retrieveAllRoles()); - AppUser user = this.appUserRepository.findOne(userId); if (user == null) { throw new UserNotFoundException(userId); } + List availableRoles = new ArrayList(this.roleReadPlatformService.retrieveAllRoles()); + List userRoleData = new ArrayList(); Set userRoles = user.getRoles(); for (Role role : userRoles) { @@ -112,8 +110,6 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic userData.setFirstname(user.getFirstname()); userData.setLastname(user.getLastname()); - userData.setAllowedOffices(offices); - availableRoles.removeAll(userRoleData); userData.setAvailableRoles(availableRoles); userData.setSelectedRoles(userRoleData);