diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/PermissionsApiResource.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/PermissionsApiResource.java index 51b32338f..1d17ff2c1 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/PermissionsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/PermissionsApiResource.java @@ -35,10 +35,10 @@ public class PermissionsApiResource { @Produces({MediaType.APPLICATION_JSON}) public String retrieveAllPermissions(@Context final UriInfo uriInfo) { - Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - Collection permissions = this.permissionReadPlatformService.retrieveAllPermissions(); + final Collection permissions = this.permissionReadPlatformService.retrieveAllPermissions(); return this.apiJsonSerializerService.serializePermissionDataToJson(prettyPrint, responseParameters, permissions); } 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 335aeeb4f..952590c29 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,9 +1,7 @@ package org.mifosng.platform.api; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; @@ -13,10 +11,8 @@ 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; import javax.ws.rs.core.UriInfo; import org.mifosng.platform.api.commands.RoleCommand; @@ -58,15 +54,10 @@ public class RolesApiResource { @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()); + final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - Collection roles = this.roleReadPlatformService.retrieveAllRoles(); + final Collection roles = this.roleReadPlatformService.retrieveAllRoles(); return this.apiJsonSerializerService.serializeRoleDataToJson(prettyPrint, responseParameters, roles); } @@ -77,17 +68,12 @@ 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")); + final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); - if (responseParameters.isEmpty()) { - responseParameters.addAll(typicalResponseParameters); - } - boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); - - Collection allPermissions = this.permissionReadPlatformService.retrieveAllPermissions(); + final Collection allPermissions = this.permissionReadPlatformService.retrieveAllPermissions(); - RoleData role = new RoleData(allPermissions, new ArrayList()); + final RoleData role = new RoleData(allPermissions, new ArrayList()); return this.apiJsonSerializerService.serializeRoleDataToJson(prettyPrint, responseParameters, role); } @@ -95,40 +81,36 @@ public class RolesApiResource { @POST @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public Response createRole(final String jsonRequestBody) { + public String createRole(final String jsonRequestBody) { - RoleCommand command = this.apiDataConversionService.convertJsonToRoleCommand(null, jsonRequestBody); + final RoleCommand command = this.apiDataConversionService.convertJsonToRoleCommand(null, jsonRequestBody); - Long roleId = this.roleWritePlatformService.createRole(command); + final Long roleId = this.roleWritePlatformService.createRole(command); - return Response.ok().entity(new EntityIdentifier(roleId)).build(); + return this.apiJsonSerializerService.serializeEntityIdentifier(new EntityIdentifier(roleId)); } + // TODO - General thing to fix about REST API is if use partial response approach of fields=id,name,selectedPermissions + // It will return just id,name parameters of RoleData and ignore description, however as PermissionData used in selectedPermissions collection + // also has a field called description it gets ignored also. This is because of the implementation of ParameterListExclusionStrategy which doesnt take + // into account the Object its looking at. @GET @Path("{roleId}") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) 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()); + final Set responseParameters = ApiParameterHelper.extractFieldsForResponseIfProvided(uriInfo.getQueryParameters()); + final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo.getQueryParameters()); + final boolean template = ApiParameterHelper.template(uriInfo.getQueryParameters()); RoleData role = this.roleReadPlatformService.retrieveRole(roleId); if (template) { - Collection availablePermissions = this.permissionReadPlatformService.retrieveAllPermissions(); - availablePermissions.removeAll(role.getSelectedPermissions()); + final Collection availablePermissions = this.permissionReadPlatformService.retrieveAllPermissions(); + availablePermissions.removeAll(role.selectedPermissions()); role = new RoleData(role, availablePermissions); - - responseParameters.add("availablePermissions"); - responseParameters.add("selectedPermissions"); } return this.apiJsonSerializerService.serializeRoleDataToJson(prettyPrint, responseParameters, role); } @@ -137,13 +119,13 @@ public class RolesApiResource { @Path("{roleId}") @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON}) - public Response updateRole(@PathParam("roleId") final Long roleId, final String jsonRequestBody) { + public String updateRole(@PathParam("roleId") final Long roleId, final String jsonRequestBody) { - RoleCommand command = this.apiDataConversionService.convertJsonToRoleCommand(roleId, jsonRequestBody); + final RoleCommand command = this.apiDataConversionService.convertJsonToRoleCommand(roleId, jsonRequestBody); this.roleWritePlatformService.updateRole(command); - return Response.ok().entity(new EntityIdentifier(roleId)).build(); + return this.apiJsonSerializerService.serializeEntityIdentifier(new EntityIdentifier(roleId)); } /* diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/PermissionData.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/PermissionData.java index 11b8d1d1b..83a684b26 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/data/PermissionData.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/data/PermissionData.java @@ -5,8 +5,11 @@ package org.mifosng.platform.api.data; */ public class PermissionData { + @SuppressWarnings("unused") private final Long id; + @SuppressWarnings("unused") private final String name; + @SuppressWarnings("unused") private final String description; private final String code; @@ -27,20 +30,4 @@ public class PermissionData { public int hashCode() { return this.code.hashCode(); } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public String getDescription() { - return description; - } - - public String getCode() { - return code; - } } \ No newline at end of file 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 a33bf4bd3..fd3fd4010 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 @@ -12,17 +12,25 @@ public class RoleData { private final String name; private final String description; + @SuppressWarnings("unused") private final Collection availablePermissions; private final Collection selectedPermissions; + public RoleData(final Long id, final String name, final String description) { + this.id = id; + this.name = name; + this.description = description; + this.availablePermissions = null; + this.selectedPermissions = null; + } + public RoleData(final Long id, final String name, final String description, final Collection selectedPermissions) { this.id = id; this.name = name; this.description = description; - this.availablePermissions = new ArrayList(); - this.selectedPermissions = new ArrayList(selectedPermissions); + this.availablePermissions = null; + this.selectedPermissions = selectedPermissions; } - public RoleData(final Collection availablePermissions, final Collection selectedPermissions) { this.id = null; this.name = null; @@ -31,18 +39,18 @@ public class RoleData { this.selectedPermissions = new ArrayList(selectedPermissions); } - public RoleData(RoleData role, final Collection availablePermissions) { - this.id = role.getId(); - this.name = role.getName(); - this.description = role.getDescription(); - this.availablePermissions = new ArrayList(availablePermissions); - this.selectedPermissions = role.getSelectedPermissions(); + public RoleData(final RoleData role, final Collection availablePermissions) { + this.id = role.id; + this.name = role.name; + this.description = role.description; + this.availablePermissions = availablePermissions; + this.selectedPermissions = role.selectedPermissions; } @Override public boolean equals(final Object obj) { - RoleData role = (RoleData) obj; - return this.id.equals(role.getId()); + final RoleData role = (RoleData) obj; + return this.id.equals(role.id); } @Override @@ -50,23 +58,7 @@ public class RoleData { return this.id.hashCode(); } - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public String getDescription() { - return description; - } - - public Collection getAvailablePermissions() { - return availablePermissions; - } - - public Collection getSelectedPermissions() { - return selectedPermissions; + public Collection selectedPermissions() { + return this.selectedPermissions; } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/infrastructure/api/ParameterListExclusionStrategy.java b/mifosng-provider/src/main/java/org/mifosng/platform/infrastructure/api/ParameterListExclusionStrategy.java index 6c5e3027a..d4c4e1fb3 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/infrastructure/api/ParameterListExclusionStrategy.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/infrastructure/api/ParameterListExclusionStrategy.java @@ -5,6 +5,10 @@ import java.util.Set; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; +// FIXME - KW - General thing to fix about REST API is if use partial response approach of fields=id,name,selectedPermissions +// It will return just id,name parameters of RoleData and ignore description, however as PermissionData used in selectedPermissions collection +// also has a field called description it gets ignored also. This is because of the implementation of ParameterListExclusionStrategy which doesnt take +// into account the Object its looking at. public class ParameterListExclusionStrategy implements ExclusionStrategy { private final Set parameterNamesToSkip; @@ -23,5 +27,4 @@ public class ParameterListExclusionStrategy implements ExclusionStrategy { public boolean shouldSkipClass(Class clazz) { return false; } - -} +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/PermissionReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/PermissionReadPlatformServiceImpl.java index 86f46aaf5..2844beb59 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/PermissionReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/PermissionReadPlatformServiceImpl.java @@ -30,8 +30,8 @@ public class PermissionReadPlatformServiceImpl implements PermissionReadPlatform context.authenticatedUser(); - PermissionMapper mapper = new PermissionMapper(); - String sql = "select " + mapper.schema() + " order by p.id"; + final PermissionMapper mapper = new PermissionMapper(); + final String sql = "select " + mapper.schema() + " order by p.id"; return this.jdbcTemplate.query(sql, mapper, new Object[] {}); } @@ -41,10 +41,10 @@ public class PermissionReadPlatformServiceImpl implements PermissionReadPlatform @Override public PermissionData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { - Long id = JdbcSupport.getLong(rs, "id"); - String name = rs.getString("name"); - String description = rs.getString("description"); - String code = rs.getString("code"); + final Long id = JdbcSupport.getLong(rs, "id"); + final String name = rs.getString("name"); + final String description = rs.getString("description"); + final String code = rs.getString("code"); return new PermissionData(id, name, description, code); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/RoleReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/RoleReadPlatformServiceImpl.java index 11d18d514..f39267924 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/user/service/RoleReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/user/service/RoleReadPlatformServiceImpl.java @@ -2,10 +2,8 @@ package org.mifosng.platform.user.service; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Collection; -import org.mifosng.platform.api.data.PermissionData; import org.mifosng.platform.api.data.RoleData; import org.mifosng.platform.exceptions.RoleNotFoundException; import org.mifosng.platform.infrastructure.JdbcSupport; @@ -36,8 +34,8 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService { public Collection retrieveAllRoles() { context.authenticatedUser(); - RoleMapper mapper = new RoleMapper(); - String sql = "select " + mapper.schema() + " order by r.id"; + final RoleMapper mapper = new RoleMapper(); + final String sql = "select " + mapper.schema() + " order by r.id"; return this.jdbcTemplate.query(sql, mapper, new Object[] {}); } @@ -46,7 +44,7 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService { public RoleData retrieveRole(final Long id) { context.authenticatedUser(); - Role role = this.roleRepository.findOne(id); + final Role role = this.roleRepository.findOne(id); if (role == null) { throw new RoleNotFoundException(id); } @@ -59,12 +57,11 @@ public class RoleReadPlatformServiceImpl implements RoleReadPlatformService { public RoleData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { - Long id = JdbcSupport.getLong(rs, "id"); - String name = rs.getString("name"); - String description = rs.getString("description"); + final Long id = JdbcSupport.getLong(rs, "id"); + final String name = rs.getString("name"); + final String description = rs.getString("description"); - Collection selectedPermissions = new ArrayList(0); - return new RoleData(id, name, description, selectedPermissions); + return new RoleData(id, name, description); } public String schema() {