json serialization tidy up for user admin areas.

This commit is contained in:
Keith Woodlock
2012-07-20 09:16:43 +01:00
parent b1a41f63b7
commit d7b06e9809
8 changed files with 185 additions and 72 deletions
@@ -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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "transactionDate", "allowedOffices", "currencyOptions"));
Set<String> 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
@@ -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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "name", "description"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<RoleData> 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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "name", "description", "availablePermissions", "selectedPermissions"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<PermissionData> 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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "name", "description"));
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());
RoleData role = this.roleReadPlatformService.retrieveRole(roleId);
if (template != null && template.equalsIgnoreCase("true")) {
Collection<PermissionData> availablePermissions = this.permissionReadPlatformService
.retrieveAllPermissions();
if (template) {
Collection<PermissionData> 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
@@ -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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email"));
Set<String> responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters());
if (responseParameters.isEmpty()) {
responseParameters.addAll(typicalResponseParameters);
}
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters());
Collection<AppUserData> 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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email"));
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());
AppUserData user = this.appUserReadPlatformService.retrieveUser(userId);
String selectedFields = "";
return this.jsonFormattingService.convertRequest(user, filterName,
allowedFieldList, selectedFields, uriInfo.getQueryParameters());
if (template) {
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(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<String> typicalResponseParameters = new HashSet<String>(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email",
"allowedOffices", "availableRoles", "selectedRoles"));
Set<String> 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
@@ -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;
@@ -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;
@@ -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<String> responseParameters, PermissionData... permissions);
String convertRoleDataToJson(boolean prettyPrint, Set<String> responseParameters, RoleData... roles);
String convertAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, AppUserData... users);
String convertOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData... officeTransactions);
//
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
@@ -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<String> responseParameters, final OfficeTransactionData... officeTransactions) {
return null;
}
@Override
public String convertAppUserDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final AppUserData... users) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "officeId", "officeName", "username", "firstname", "lastname", "email",
"allowedOffices", "availableRoles", "selectedRoles"));
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(users);
}
@Override
public String convertRoleDataToJson(final boolean prettyPrint, final Set<String> responseParameters, final RoleData... roles) {
Set<String> supportedParameters = new HashSet<String>(Arrays.asList("id", "name", "description", "availablePermissions", "selectedPermissions"));
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(roles);
}
@Override
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",
@@ -91,15 +91,13 @@ public class AppUserReadPlatformServiceImpl implements AppUserReadPlatformServic
context.authenticatedUser();
List<OfficeLookup> offices = new ArrayList<OfficeLookup>(officeReadPlatformService.retrieveAllOfficesForLookup());
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
AppUser user = this.appUserRepository.findOne(userId);
if (user == null) {
throw new UserNotFoundException(userId);
}
List<RoleData> availableRoles = new ArrayList<RoleData>(this.roleReadPlatformService.retrieveAllRoles());
List<RoleData> userRoleData = new ArrayList<RoleData>();
Set<Role> 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);