From f22d392978dc2541fbaf5784600f8c01ff262b18 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 4 Oct 2016 19:03:07 +0530 Subject: [PATCH] Upgrade to comply with latest CDMF changes --- .../mgt/jaxrs/beans/BasePaginatedResult.java | 2 - .../mgt/jaxrs/beans/DeviceGroupList.java | 3 - .../mgt/jaxrs/beans/DeviceGroupUsersList.java | 2 - .../service/api/GroupManagementService.java | 107 +++++++++- .../admin/GroupManagementAdminService.java | 134 ++++++++----- .../impl/GroupManagementServiceImpl.java | 81 +++++--- .../GroupManagementAdminServiceImpl.java | 39 ++-- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 +- .../mgt/common/group/mgt/DeviceGroup.java | 24 +-- .../DeviceAccessAuthorizationServiceImpl.java | 2 +- .../core/group/mgt/DeviceGroupBuilder.java | 71 ------- .../mgt/core/group/mgt/dao/GroupDAO.java | 12 +- .../mgt/core/group/mgt/dao/GroupDAOImpl.java | 17 +- .../group/mgt/dao/GroupManagementDAOUtil.java | 5 +- .../GroupManagementProviderService.java | 42 +--- .../GroupManagementProviderServiceImpl.java | 184 +++++------------- .../mgt/core/common/TestDataHolder.java | 4 +- .../mgt/core/dao/GroupPersistTests.java | 5 +- .../policy/mgt/common/DeviceGroupWrapper.java | 2 +- .../mgt/core/util/PolicyManagerUtil.java | 2 +- 20 files changed, 343 insertions(+), 397 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/DeviceGroupBuilder.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java index b3a60032e1..92ec04424f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java @@ -24,8 +24,6 @@ import io.swagger.annotations.ApiModelProperty; public class BasePaginatedResult { private int count; - private String next; - private String previous; /** * Number of Resources returned. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java index 15a3e1698c..d1e472255a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java @@ -44,10 +44,7 @@ public class DeviceGroupList extends BasePaginatedResult { public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{\n"); - sb.append(" count: ").append(getCount()).append(",\n"); - sb.append(" next: ").append(getNext()).append(",\n"); - sb.append(" previous: ").append(getPrevious()).append(",\n"); sb.append(" groups: [").append(deviceGroups).append("\n"); sb.append("]}\n"); return sb.toString(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java index ed369ced60..21e72562ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java @@ -44,8 +44,6 @@ public class DeviceGroupUsersList extends BasePaginatedResult { sb.append("{\n"); sb.append(" count: ").append(getCount()).append(",\n"); - sb.append(" next: ").append(getNext()).append(",\n"); - sb.append(" previous: ").append(getPrevious()).append(",\n"); sb.append(" users: [").append(users).append("\n"); sb.append("]}\n"); return sb.toString(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java index c6c050fa71..4509547abd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java @@ -16,6 +16,7 @@ * under the License. * */ + package org.wso2.carbon.device.mgt.jaxrs.service.api; import io.swagger.annotations.Api; @@ -110,6 +111,49 @@ public interface GroupManagementService { value = "Maximum size of resource array to return.") @QueryParam("limit") int limit); + @Path("/count") + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Get the count of groups belongs to current user.", + notes = "Returns count of all permitted groups enrolled with the system.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device group count.", + response = DeviceGroupList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "No groups found.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the group count.", + response = ErrorResponse.class) + }) + @Permission(name = "View Groups", permission = "/device-mgt/groups/view") + Response getGroupCount(); + @POST @ApiOperation( consumes = MediaType.APPLICATION_JSON, @@ -170,7 +214,7 @@ public interface GroupManagementService { required = true) @Valid DeviceGroup group); - @Path("/{groupName}") + @Path("/name/{groupName}") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -217,7 +261,7 @@ public interface GroupManagementService { required = true) @PathParam("groupName") String groupName); - @Path("/{groupName}") + @Path("/name/{groupName}") @PUT @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -269,7 +313,7 @@ public interface GroupManagementService { required = true) @Valid DeviceGroup deviceGroup); - @Path("/{groupName}") + @Path("/name/{groupName}") @DELETE @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -316,7 +360,7 @@ public interface GroupManagementService { required = true) @PathParam("groupName") String groupName); - @Path("/{groupName}/share") + @Path("/name/{groupName}/share") @POST @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -368,7 +412,7 @@ public interface GroupManagementService { required = true) @Valid DeviceGroupShare deviceGroupShare); - @Path("/{groupName}/users") + @Path("/name/{groupName}/users") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -415,7 +459,7 @@ public interface GroupManagementService { required = true) @PathParam("groupName") String groupName); - @Path("/{groupName}/devices") + @Path("/name/{groupName}/devices") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -470,7 +514,54 @@ public interface GroupManagementService { value = "Maximum size of resource array to return.") @QueryParam("limit") int limit); - @Path("/{groupName}/devices") + @Path("/name/{groupName}/devices/count") + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "View list of device count in the device group.", + notes = "Returns device count in the device group.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device count.", + response = DeviceList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "No groups found.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching device count.", + response = ErrorResponse.class) + }) + @Permission(name = "View devices", permission = "/device-mgt/groups/devices/view") + Response getDeviceCountOfGroup(@ApiParam( + name = "groupName", + value = "Name of the group.", + required = true) + @PathParam("groupName") String groupName); + + @Path("/name/{groupName}/devices") @POST @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -521,7 +612,7 @@ public interface GroupManagementService { required = true) @Valid List deviceIdentifiers); - @Path("/{groupName}/devices") + @Path("/name/{groupName}/devices") @DELETE @ApiOperation( produces = MediaType.APPLICATION_JSON, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java index 885005944a..056bb743b0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java @@ -16,6 +16,7 @@ * under the License. * */ + package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; import io.swagger.annotations.Api; @@ -24,13 +25,14 @@ import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import io.swagger.annotations.ResponseHeader; +import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; -import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import javax.ws.rs.Consumes; import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; @@ -50,60 +52,92 @@ public interface GroupManagementAdminService { @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, - httpMethod = "GET", - value = "Get groups by the name.", - notes = "Get devices the name of device and tenant.", - response = DeviceGroupWrapper.class, - responseContainer = "List", - tags = "Group Management Administrative Service") + httpMethod = HTTPConstants.HEADER_GET, + value = "Get the list of groups.", + notes = "Returns all groups enrolled with the system.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of device groups.", + response = DeviceGroupList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "No groups found.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the groups list.", + response = ErrorResponse.class) + }) + @Permission(name = "View Groups", permission = "/device-mgt/admin/groups/view") + Response getGroups(@ApiParam( + name = "offset", + value = "Starting point within the complete list of items qualified.") + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Maximum size of resource array to return.") + @QueryParam("limit") int limit); + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Get the count of groups belongs to current user.", + notes = "Returns count of all permitted groups enrolled with the system.", + tags = "Device Group Management") @ApiResponses(value = { - @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of groups.", - response = DeviceGroupWrapper.class, - responseContainer = "List", - responseHeaders = { - @ResponseHeader( - name = "Content-Type", - description = "The content type of the body"), - @ResponseHeader( - name = "ETag", - description = "Entity Tag of the response resource.\n" + - "Used by caches, or in conditional requests."), - @ResponseHeader( - name = "Last-Modified", - description = "Date and time the resource has been modified the last time.\n" + - "Used by caches, or in conditional requests."), - }), + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device group count.", + response = DeviceGroupList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), @ApiResponse( code = 304, - message = "Not Modified. \n Empty body because the client has already the latest version of the " + - "requested resource."), + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "No groups found.", + response = ErrorResponse.class), @ApiResponse( code = 406, - message = "Not Acceptable.\n The requested media type is not supported"), + message = "Not Acceptable.\n The requested media type is not supported."), @ApiResponse( code = 500, - message = "Internal Server ErrorResponse. \n Server error occurred while fetching the group list.") + message = "Internal Server Error. \n Server error occurred while fetching the group count.", + response = ErrorResponse.class) }) - @Permission(name = "View All Groups", permission = "/permission/admin/device-mgt/user/groups/list") - Response getGroupsOfUser( - @ApiParam( - name = "username", - value = "Username of the user.", - required = true) - @QueryParam("username") String username, - @ApiParam( - name = "If-Modified-Since", - value = "Timestamp of the last modified date", - required = false) - @HeaderParam("If-Modified-Since") String timestamp, - @ApiParam( - name = "offset", - value = "Starting point within the complete list of items qualified.", - required = false) - @QueryParam("offset") int offset, - @ApiParam( - name = "limit", - value = "Maximum size of resource array to return.", - required = false) - @QueryParam("limit") int limit); + @Permission(name = "View Groups", permission = "/device-mgt/admin/groups/view") + Response getGroupCount(); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 02ec2514c4..e1e874ad1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -16,6 +16,7 @@ * under the License. * */ + package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.apache.commons.logging.Log; @@ -24,55 +25,77 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare; import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; -import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper; -import javax.ws.rs.Consumes; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; +import java.util.Date; import java.util.List; -@Path("/groups") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) public class GroupManagementServiceImpl implements GroupManagementService { private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); + private static final String DEFAULT_ADMIN_ROLE = "admin"; + private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", + "/permission/device-mgt/user/groups"}; + @Override public Response getGroups(int offset, int limit) { try { - List groupWrappers = new ArrayList<>(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - List deviceGroups = service.getGroups(currentUser); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - for (DeviceGroup dg : deviceGroups) { - DeviceGroupWrapper gw = new DeviceGroupWrapper(); - gw.setId(dg.getId()); - gw.setOwner(dg.getOwner()); - gw.setName(dg.getName()); - gw.setTenantId(tenantId); - groupWrappers.add(gw); - } - return Response.status(Response.Status.OK).entity(groupWrappers).build(); + List deviceGroups = service.getGroups(currentUser, offset, limit); + DeviceGroupList deviceGroupList = new DeviceGroupList(); + deviceGroupList.setList(deviceGroups); + deviceGroupList.setCount(service.getGroupCount(currentUser)); + return Response.status(Response.Status.OK).entity(deviceGroupList).build(); } catch (GroupManagementException e) { - String error = "ErrorResponse occurred while getting the groups related to users for policy."; + String error = "Error occurred while getting the groups related to users for policy."; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); } } + @Override + public Response getGroupCount() { + try { + String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); + int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser); + return Response.status(Response.Status.OK).entity(count).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while retrieving group count."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + @Override public Response createGroup(DeviceGroup group) { - return null; + String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + if (group == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + group.setOwner(owner); + group.setDateOfCreation(new Date().getTime()); + group.setDateOfLastUpdate(new Date().getTime()); + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + return Response.status(Response.Status.OK).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while adding new group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupAlreadyExistException e) { + String msg = "Group already exists with name '" + group.getName() + "'."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } } @Override @@ -106,12 +129,18 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response addDeviceToGroup(String groupName, DeviceIdentifier deviceIdentifier) { + public Response getDeviceCountOfGroup(String groupName) { return null; } @Override - public Response removeDevicesFromGroup(String groupName, String type, String id) { + public Response addDevicesToGroup(String groupName, List deviceIdentifiers) { return null; } -} \ No newline at end of file + + @Override + public Response removeDevicesFromGroup(String groupName, List deviceIdentifiers) { + return null; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index 9cf5253c2c..887be40c08 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -20,19 +20,21 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.GroupManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; @Path("/admin/groups") @Produces(MediaType.APPLICATION_JSON) @@ -41,26 +43,35 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); - @GET @Override - public Response getGroupsOfUser( - @QueryParam("username") String username, - @HeaderParam("If-Modified-Since") String timestamp, - @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { + public Response getGroups(int offset, int limit) { try { - PaginationResult result = - DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(username, offset, limit); - if (result != null && result.getRecordsTotal() > 0) { - return Response.status(Response.Status.OK).entity(result).build(); + GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); + List deviceGroups = service.getGroups(offset, limit); + DeviceGroupList deviceGroupList = new DeviceGroupList(); + deviceGroupList.setList(deviceGroups); + deviceGroupList.setCount(service.getGroupCount()); + if (deviceGroups != null && deviceGroups.size() > 0) { + return Response.status(Response.Status.OK).entity(deviceGroupList).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } } catch (GroupManagementException e) { - String msg = "ErrorResponse occurred while retrieving the groups of user '" + username + "'"; + String msg = "ErrorResponse occurred while retrieving all groups."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + @Override + public Response getGroupCount() { + try { + int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(); + return Response.status(Response.Status.OK).entity(count).build(); + } catch (GroupManagementException e) { + String msg = "ErrorResponse occurred while retrieving group count."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index a5f5ed308a..ac127d1500 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -34,7 +34,7 @@ - + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java index 4f54a37705..61d02b2e6b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java @@ -33,8 +33,7 @@ public class DeviceGroup implements Serializable { private static final long serialVersionUID = 1998121711L; - @ApiModelProperty(name = "id", value = "ID of the device group in the device group information database.", - required = true) + @ApiModelProperty(name = "id", value = "ID of the device group in the device group information database.") private int id; @ApiModelProperty(name = "description", value = "The device group description that can be set on the device group by the user.", @@ -51,11 +50,11 @@ public class DeviceGroup implements Serializable { private List users; private List roles; - public int getId() { + public int getGroupId() { return id; } - public void setId(int id) { + public void setGroupId(int id) { this.id = id; } @@ -103,7 +102,7 @@ public class DeviceGroup implements Serializable { return users; } - protected void setUsers(List users) { + public void setUsers(List users) { this.users = users; } @@ -111,21 +110,8 @@ public class DeviceGroup implements Serializable { return roles; } - protected void setRoles(List roles) { + public void setRoles(List roles) { this.roles = roles; } - protected DeviceGroup getGroup() { - DeviceGroup deviceGroup = new DeviceGroup(); - deviceGroup.setId(getId()); - deviceGroup.setDescription(getDescription()); - deviceGroup.setName(getName()); - deviceGroup.setDateOfCreation(getDateOfCreation()); - deviceGroup.setDateOfLastUpdate(getDateOfLastUpdate()); - deviceGroup.setOwner(getOwner()); - deviceGroup.setUsers(getUsers()); - deviceGroup.setRoles(getRoles()); - return deviceGroup; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java index 97b5a21c04..0703d4d39d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java @@ -203,7 +203,7 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori Iterator groupsWithDeviceIterator = groupsWithDevice.iterator(); while (groupsWithDeviceIterator.hasNext()) { DeviceGroup deviceGroup = groupsWithDeviceIterator.next(); - if (deviceGroup.getId() == group.getId()) { + if (deviceGroup.getGroupId() == group.getGroupId()) { return true; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/DeviceGroupBuilder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/DeviceGroupBuilder.java deleted file mode 100644 index c1e37bbd6d..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/DeviceGroupBuilder.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.device.mgt.core.group.mgt; - -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; - -import java.util.List; - -/** - * This class is used to expose protected methods to the core. Use with internal access only. - */ -public class DeviceGroupBuilder extends DeviceGroup { - - private int groupId; - - /** - * Set device group to be decorated with the builder - * - * @param deviceGroup to decorate - */ - public DeviceGroupBuilder(DeviceGroup deviceGroup) { - this.setId(deviceGroup.getId()); - this.setDescription(deviceGroup.getDescription()); - this.setName(deviceGroup.getName()); - this.setDateOfCreation(deviceGroup.getDateOfCreation()); - this.setDateOfLastUpdate(deviceGroup.getDateOfLastUpdate()); - this.setOwner(deviceGroup.getOwner()); - this.setUsers(deviceGroup.getUsers()); - this.setRoles(deviceGroup.getRoles()); - } - - @Override - public void setUsers(List users) { - super.setUsers(users); - } - - @Override - public void setRoles(List roles) { - super.setRoles(roles); - } - - @Override - public DeviceGroup getGroup() { - return super.getGroup(); - } - - public int getGroupId() { - return groupId; - } - - public void setGroupId(int groupId) { - this.groupId = groupId; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java index 7e8132fedf..b164a4ba11 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java @@ -20,7 +20,6 @@ package org.wso2.carbon.device.mgt.core.group.mgt.dao; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import java.util.List; @@ -68,8 +67,7 @@ public interface GroupDAO { * @return Device Group in tenant with specified name. * @throws GroupManagementDAOException */ - DeviceGroupBuilder getGroup(int groupId, int tenantId) throws GroupManagementDAOException; - + DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException; /** * Get device group by name. @@ -80,7 +78,7 @@ public interface GroupDAO { * @return Device Group in tenant with specified name. * @throws GroupManagementDAOException */ - DeviceGroupBuilder getGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException; + DeviceGroup getGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException; /** * Get the groups of device with device id provided @@ -88,7 +86,7 @@ public interface GroupDAO { * @return * @throws GroupManagementDAOException */ - List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException; + List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException; /** * Get the list of Device Groups in tenant. @@ -99,7 +97,7 @@ public interface GroupDAO { * @return List of all Device Groups in tenant. * @throws GroupManagementDAOException */ - List getGroups(int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException; + List getGroups(int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException; /** @@ -119,7 +117,7 @@ public interface GroupDAO { * @return List of DeviceGroup that matches with the given DeviceGroup name. * @throws GroupManagementDAOException */ - List findInGroups(String groupName, int tenantId) throws GroupManagementDAOException; + List findInGroups(String groupName, int tenantId) throws GroupManagementDAOException; /** * Check group already existed with given name. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java index df70f43502..4b2134d6b9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java @@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.core.group.mgt.dao; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import java.sql.Connection; import java.sql.PreparedStatement; @@ -127,7 +126,7 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public DeviceGroupBuilder getGroup(int groupId, int tenantId) throws GroupManagementDAOException { + public DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; try { @@ -152,7 +151,7 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public DeviceGroupBuilder getGroup(String groupName, String owner, int tenantId) + public DeviceGroup getGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; @@ -179,11 +178,11 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException { + public List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; - List deviceGroupBuilders = new ArrayList<>(); + List deviceGroupBuilders = new ArrayList<>(); try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.DATE_OF_CREATE, G.DATE_OF_LAST_UPDATE, \n" + @@ -205,11 +204,11 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public List getGroups(int startIndex, int rowCount, int tenantId) + public List getGroups(int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; - List deviceGroupList = null; + List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER " @@ -256,11 +255,11 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public List findInGroups(String groupName, int tenantId) + public List findInGroups(String groupName, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; - List deviceGroups = new ArrayList<>(); + List deviceGroups = new ArrayList<>(); try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER " diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupManagementDAOUtil.java index 9a206c7b71..2352ef149e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupManagementDAOUtil.java @@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.core.group.mgt.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -80,8 +79,8 @@ public final class GroupManagementDAOUtil { } } - public static DeviceGroupBuilder loadGroup(ResultSet resultSet) throws SQLException { - DeviceGroupBuilder group = new DeviceGroupBuilder(new DeviceGroup()); + public static DeviceGroup loadGroup(ResultSet resultSet) throws SQLException { + DeviceGroup group = new DeviceGroup(); group.setGroupId(resultSet.getInt("ID")); group.setDescription(resultSet.getString("DESCRIPTION")); group.setName(resultSet.getString("GROUP_NAME")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 1b16cc0507..b1c9a8468c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -60,6 +60,7 @@ public interface GroupManagementProviderService { * * @param groupName to be deleted. * @param owner of the group. + * @return status of the delete operation. * @throws GroupManagementException */ boolean deleteGroup(String groupName, String owner) throws GroupManagementException; @@ -84,25 +85,15 @@ public interface GroupManagementProviderService { */ DeviceGroup getGroup(int groupId) throws GroupManagementException; - /** - * Get list of device groups matched with %groupName% - * - * @param groupName of the groups. - * @param username of user - * @return List of Groups that matches with the given DeviceGroup name. - * @throws GroupManagementException - */ - List findInGroups(String groupName, String username) throws GroupManagementException; - /** * Get paginated device groups in tenant * * @param startIndex for pagination. * @param rowCount for pagination. - * @return paginated list of groups + * @return list of groups. * @throws GroupManagementException */ - PaginationResult getGroups(int startIndex, int rowCount) throws GroupManagementException; + List getGroups(int startIndex, int rowCount) throws GroupManagementException; /** * Get paginated device groups in tenant @@ -110,10 +101,10 @@ public interface GroupManagementProviderService { * @param username of user. * @param startIndex for pagination. * @param rowCount for pagination. - * @return paginated list of groups + * @return list of groups * @throws GroupManagementException */ - PaginationResult getGroups(String username, int startIndex, int rowCount) throws GroupManagementException; + List getGroups(String username, int startIndex, int rowCount) throws GroupManagementException; /** * Get all device group count in tenant @@ -123,15 +114,6 @@ public interface GroupManagementProviderService { */ int getGroupCount() throws GroupManagementException; - /** - * Get device groups of user - * - * @param username of the user - * @return list of groups - * @throws GroupManagementException - */ - List getGroups(String username) throws GroupManagementException; - /** * Get device group count of user * @@ -224,16 +206,6 @@ public interface GroupManagementProviderService { */ List getUsers(String groupName, String owner) throws GroupManagementException; - /** - * Get all devices in device group. - * - * @param groupName of the group. - * @param owner of the group. - * @return list of group devices. - * @throws GroupManagementException - */ - List getDevices(String groupName, String owner) throws GroupManagementException; - /** * Get all devices in device group as paginated result. * @@ -241,10 +213,10 @@ public interface GroupManagementProviderService { * @param owner of the group. * @param startIndex for pagination. * @param rowCount for pagination. - * @return Paginated list of devices. + * @return list of devices in group. * @throws GroupManagementException */ - PaginationResult getDevices(String groupName, String owner, int startIndex, int rowCount) + List getDevices(String groupName, String owner, int startIndex, int rowCount) throws GroupManagementException; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index e9beade43e..762478c592 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -28,7 +28,6 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupUser; -import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import org.wso2.carbon.device.mgt.core.group.mgt.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.group.mgt.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.group.mgt.dao.GroupManagementDAOFactory; @@ -65,14 +64,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); } - DeviceGroupBuilder groupBroker = new DeviceGroupBuilder(deviceGroup); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int groupId = -1; try { GroupManagementDAOFactory.beginTransaction(); boolean nameIsExists = this.groupDAO.isGroupExist(deviceGroup.getName(), deviceGroup.getOwner(), tenantId); if (!nameIsExists) { - groupId = this.groupDAO.addGroup(groupBroker, tenantId); + groupId = this.groupDAO.addGroup(deviceGroup, tenantId); GroupManagementDAOFactory.commitTransaction(); } else { throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); @@ -87,9 +85,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.closeConnection(); } - addGroupSharingRole(groupBroker.getOwner(), groupId, defaultRole, defaultPermissions); + addGroupSharingRole(deviceGroup.getOwner(), groupId, defaultRole, defaultPermissions); if (log.isDebugEnabled()) { - log.debug("DeviceGroup added: " + groupBroker.getName()); + log.debug("DeviceGroup added: " + deviceGroup.getName()); } } @@ -125,7 +123,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public boolean deleteGroup(String groupName, String owner) throws GroupManagementException { String roleName; - DeviceGroupBuilder deviceGroup = getGroupBuilder(groupName, owner); + DeviceGroup deviceGroup = buildDeviceGroup(groupName, owner); if (deviceGroup == null) { return false; } @@ -160,16 +158,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(String groupName, String owner) throws GroupManagementException { - return getGroupBuilder(groupName, owner).getGroup(); + return buildDeviceGroup(groupName, owner); } - @SuppressWarnings("Duplicates") - private DeviceGroupBuilder getGroupBuilder(String groupName, String owner) throws GroupManagementException { - DeviceGroupBuilder deviceGroupBuilder; + private DeviceGroup buildDeviceGroup(String groupName, String owner) throws GroupManagementException { + DeviceGroup deviceGroup; try { GroupManagementDAOFactory.openConnection(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceGroupBuilder = this.groupDAO.getGroup(groupName, owner, tenantId); + deviceGroup = this.groupDAO.getGroup(groupName, owner, tenantId); } catch (GroupManagementDAOException e) { throw new GroupManagementException("Error occurred while obtaining group '" + groupName + "'", e); } catch (SQLException e) { @@ -177,19 +174,19 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } finally { GroupManagementDAOFactory.closeConnection(); } - if (deviceGroupBuilder != null) { - deviceGroupBuilder.setUsers(this.getUsers(deviceGroupBuilder.getGroupId())); - deviceGroupBuilder.setRoles(this.getRoles(deviceGroupBuilder.getGroupId())); + if (deviceGroup != null) { + deviceGroup.setUsers(this.getUsers(deviceGroup.getGroupId())); + deviceGroup.setRoles(this.getRoles(deviceGroup.getGroupId())); } - return deviceGroupBuilder; + return deviceGroup; } - private DeviceGroupBuilder getGroupBuilder(int groupId) throws GroupManagementException { - DeviceGroupBuilder groupBroker; + private DeviceGroup buildDeviceGroup(int groupId) throws GroupManagementException { + DeviceGroup deviceGroup; try { GroupManagementDAOFactory.openConnection(); - groupBroker = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { throw new GroupManagementException("Error occurred while obtaining group '" + groupId + "'", e); } catch (SQLException e) { @@ -197,11 +194,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } finally { GroupManagementDAOFactory.closeConnection(); } - if (groupBroker != null) { - groupBroker.setUsers(this.getUsers(groupBroker.getGroupId())); - groupBroker.setRoles(this.getRoles(groupBroker.getGroupId())); + if (deviceGroup != null) { + deviceGroup.setUsers(this.getUsers(groupId)); + deviceGroup.setRoles(this.getRoles(groupId)); } - return groupBroker; + return deviceGroup; } /** @@ -209,43 +206,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(int groupId) throws GroupManagementException { - DeviceGroupBuilder groupBroker = this.getGroupBuilder(groupId); - if (groupBroker != null) { - groupBroker.setUsers(this.getUsers(groupBroker.getGroupId())); - groupBroker.setRoles(this.getRoles(groupBroker.getGroupId())); + DeviceGroup deviceGroup = this.buildDeviceGroup(groupId); + if (deviceGroup != null) { + deviceGroup.setUsers(this.getUsers(groupId)); + deviceGroup.setRoles(this.getRoles(groupId)); } - return groupBroker.getGroup(); - } - - /** - * {@inheritDoc} - */ - @Override - public List findInGroups(String groupName, String owner) throws GroupManagementException { - List deviceGroups = new ArrayList<>(); - try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - GroupManagementDAOFactory.openConnection(); - deviceGroups = this.groupDAO.findInGroups(groupName, tenantId); - } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while finding group " + groupName, e); - } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); - } finally { - GroupManagementDAOFactory.closeConnection(); - } - List groupsWithData = new ArrayList<>(); - for (DeviceGroupBuilder groupBroker : deviceGroups) { - groupBroker.setUsers(this.getUsers(groupBroker.getGroupId())); - groupBroker.setRoles(this.getRoles(groupBroker.getGroupId())); - groupsWithData.add(groupBroker.getGroup()); - } - return groupsWithData; + return deviceGroup; } @Override - public PaginationResult getGroups(int startIndex, int rowCount) throws GroupManagementException { - List deviceGroups = new ArrayList<>(); + public List getGroups(int startIndex, int rowCount) throws GroupManagementException { + List deviceGroups = new ArrayList<>(); try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); @@ -257,21 +228,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } finally { GroupManagementDAOFactory.closeConnection(); } - List groupsWithData = new ArrayList<>(); - for (DeviceGroupBuilder groupBroker : deviceGroups) { - groupBroker.setUsers(this.getUsers(groupBroker.getGroupId())); - groupBroker.setRoles(this.getRoles(groupBroker.getGroupId())); - groupsWithData.add(groupBroker.getGroup()); + for (DeviceGroup group : deviceGroups) { + group.setUsers(this.getUsers(group.getGroupId())); + group.setRoles(this.getRoles(group.getGroupId())); } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setRecordsTotal(getGroupCount()); - paginationResult.setData(groupsWithData); - paginationResult.setRecordsFiltered(groupsWithData.size()); - return paginationResult; + return deviceGroups; } @Override - public PaginationResult getGroups(String username, int startIndex, int rowCount) throws GroupManagementException { + public List getGroups(String username, int startIndex, int rowCount) throws GroupManagementException { Map groups = new HashMap<>(); UserStoreManager userStoreManager; try { @@ -282,20 +247,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int index = 0; for (String role : roleList) { if (role != null && role.contains("Internal/group-")) { - DeviceGroupBuilder deviceGroupBuilder = extractNewGroupFromRole(groups, role); + DeviceGroup deviceGroupBuilder = extractNewGroupFromRole(groups, role); if (deviceGroupBuilder != null && startIndex <= index++ && index <= rowCount) { - groups.put(deviceGroupBuilder.getGroupId(), deviceGroupBuilder.getGroup()); + groups.put(deviceGroupBuilder.getGroupId(), deviceGroupBuilder); } } } } catch (UserStoreException e) { throw new GroupManagementException("Error occurred while getting user store manager.", e); } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setRecordsTotal(getGroupCount()); - paginationResult.setData(new ArrayList<>(groups.values())); - paginationResult.setRecordsFiltered(groups.size()); - return paginationResult; + return new ArrayList<>(groups.values()); } @Override @@ -313,32 +274,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } - /** - * {@inheritDoc} - */ - @Override - public List getGroups(String username) throws GroupManagementException { - UserStoreManager userStoreManager; - try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) - .getUserStoreManager(); - String[] roleList = userStoreManager.getRoleListOfUser(username); - Map groups = new HashMap<>(); - for (String role : roleList) { - if (role != null && role.contains("Internal/group-")) { - DeviceGroupBuilder deviceGroupBuilder = extractNewGroupFromRole(groups, role); - if (deviceGroupBuilder != null) { - groups.put(deviceGroupBuilder.getGroupId(), deviceGroupBuilder.getGroup()); - } - } - } - return new ArrayList<>(groups.values()); - } catch (UserStoreException e) { - throw new GroupManagementException("Error occurred while getting user store manager.", e); - } - } - /** * {@inheritDoc} */ @@ -421,11 +356,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private int getGroupId(String groupName, String owner) throws GroupManagementException { - DeviceGroupBuilder deviceGroupBuilder = getGroupBuilder(groupName, owner); - if (deviceGroupBuilder == null) { + DeviceGroup deviceGroup = buildDeviceGroup(groupName, owner); + if (deviceGroup == null) { return -1; } - return deviceGroupBuilder.getGroupId(); + return deviceGroup.getGroupId(); } /** @@ -610,25 +545,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public List getDevices(String groupName, String owner) throws GroupManagementException { - try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - GroupManagementDAOFactory.openConnection(); - return this.groupDAO.getDevices(groupName, owner, tenantId); - } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while getting devices in group.", e); - } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); - } finally { - GroupManagementDAOFactory.closeConnection(); - } - } - - /** - * {@inheritDoc} - */ - @Override - public PaginationResult getDevices(String groupName, String owner, int startIndex, int rowCount) + public List getDevices(String groupName, String owner, int startIndex, int rowCount) throws GroupManagementException { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices; @@ -642,11 +559,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } finally { GroupManagementDAOFactory.closeConnection(); } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setRecordsTotal(getDeviceCount(groupName, owner)); - paginationResult.setData(devices); - paginationResult.setRecordsFiltered(devices.size()); - return paginationResult; + return devices; } /** @@ -774,9 +687,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid for (String role : roles) { if (role != null && role.contains("Internal/group-") && userRealm.getAuthorizationManager() .isRoleAuthorized(role, permission, CarbonConstants.UI_PERMISSION_ACTION)) { - DeviceGroupBuilder deviceGroupBuilder = extractNewGroupFromRole(groups, role); - if (deviceGroupBuilder != null) { - groups.put(deviceGroupBuilder.getGroupId(), deviceGroupBuilder.getGroup()); + DeviceGroup group = extractNewGroupFromRole(groups, role); + if (group != null) { + groups.put(group.getGroupId(), group); } } } @@ -789,15 +702,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl(); - List deviceGroups = new ArrayList<>(); try { Device device = managementProviderService.getDevice(deviceIdentifier); GroupManagementDAOFactory.openConnection(); - List builders = groupDAO.getGroups(device.getId(), - PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); - for (DeviceGroupBuilder d : builders){ - deviceGroups.add(d.getGroup()); - } + return groupDAO.getGroups(device.getId(), + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (DeviceManagementException e) { throw new GroupManagementException("Error occurred while retrieving the device details.", e); } catch (GroupManagementDAOException e) { @@ -807,15 +716,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } finally { GroupManagementDAOFactory.closeConnection(); } - return deviceGroups; } - private DeviceGroupBuilder extractNewGroupFromRole(Map groups, String role) + private DeviceGroup extractNewGroupFromRole(Map groups, String role) throws GroupManagementException { try { int groupId = Integer.parseInt(role.split("-")[1]); if (!groups.containsKey(groupId)) { - return getGroupBuilder(groupId); + return buildDeviceGroup(groupId); } } catch (NumberFormatException e) { log.error("Unable to extract groupId from role " + role, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java index bb467761e7..895deab36c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDataHolder.java @@ -20,7 +20,6 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import java.util.Date; import java.util.Properties; @@ -82,7 +81,6 @@ public class TestDataHolder { deviceGroup.setDateOfCreation(new Date().getTime()); deviceGroup.setDateOfLastUpdate(new Date().getTime()); deviceGroup.setOwner(OWNER); - DeviceGroupBuilder broker = new DeviceGroupBuilder(deviceGroup); - return broker.getGroup(); + return deviceGroup; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java index fa733444bb..d6e435a7c6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java @@ -28,7 +28,6 @@ import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; -import org.wso2.carbon.device.mgt.core.group.mgt.DeviceGroupBuilder; import org.wso2.carbon.device.mgt.core.group.mgt.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.group.mgt.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.group.mgt.dao.GroupManagementDAOFactory; @@ -80,7 +79,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { public void findGroupTest() { try { GroupManagementDAOFactory.openConnection(); - List groups = groupDAO.findInGroups("Test", TestDataHolder.SUPER_TENANT_ID); + List groups = groupDAO.findInGroups("Test", TestDataHolder.SUPER_TENANT_ID); Assert.assertNotEquals(groups.size(), 0, "No groups found"); Assert.assertNotNull(groups.get(0), "Group is null"); log.debug("Group found: " + groups.get(0).getName()); @@ -101,7 +100,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { public void getGroupTest() { try { GroupManagementDAOFactory.openConnection(); - List groups = groupDAO.getGroups(0, 100, TestDataHolder.SUPER_TENANT_ID); + List groups = groupDAO.getGroups(0, 100, TestDataHolder.SUPER_TENANT_ID); Assert.assertNotEquals(groups.size(), 0, "No groups found"); Assert.assertNotNull(groups.get(0), "Group is null"); log.debug("No of Groups found: " + groups.size()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/DeviceGroupWrapper.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/DeviceGroupWrapper.java index aee0576a4c..3457873b08 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/DeviceGroupWrapper.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/DeviceGroupWrapper.java @@ -22,7 +22,7 @@ package org.wso2.carbon.policy.mgt.common; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -@ApiModel(value = "DeviceGroupWrapper", description = "This class carries all information related to device groups") +@ApiModel(value = "DeviceGroupWrapper", description = "This class carries information related to device groups expect users and devices.") public class DeviceGroupWrapper { @ApiModelProperty(name = "id", value = "Id of the group", required = true) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index e5c983ed6f..7f96729c7e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -231,7 +231,7 @@ public class PolicyManagerUtil { public static Map convertDeviceGroupMap(List deviceGroups) { Map groupMap = new HashMap<>(); for (DeviceGroup dg: deviceGroups){ - groupMap.put(dg.getId(), dg); + groupMap.put(dg.getGroupId(), dg); } return groupMap; }