From 06176898d09b2822cd4c3628f8704566222ba405 Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 5 Oct 2016 12:20:35 +0530 Subject: [PATCH] Exposed group id to API level --- .../service/api/GroupManagementService.java | 70 +++---- .../impl/GroupManagementServiceImpl.java | 63 ++++-- .../GroupManagementAdminServiceImpl.java | 11 +- .../mgt/core/group/mgt/dao/GroupDAO.java | 66 ++---- .../mgt/core/group/mgt/dao/GroupDAOImpl.java | 189 +++++------------- .../GroupManagementProviderService.java | 94 +++------ .../GroupManagementProviderServiceImpl.java | 136 ++++--------- .../mgt/core/dao/GroupPersistTests.java | 15 +- 8 files changed, 224 insertions(+), 420 deletions(-) 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 4509547abd..b8c8c461fe 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 @@ -214,12 +214,12 @@ public interface GroupManagementService { required = true) @Valid DeviceGroup group); - @Path("/name/{groupName}") + @Path("/id/{groupId}") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = HTTPConstants.HEADER_GET, - value = "View group specified with name.", + value = "View group specified.", notes = "Returns details of group enrolled with the system.", tags = "Device Group Management") @ApiResponses(value = { @@ -256,12 +256,12 @@ public interface GroupManagementService { }) @Permission(name = "View Groups", permission = "/device-mgt/groups/view") Response getGroup(@ApiParam( - name = "groupName", - value = "Name of the group to view.", + name = "groupId", + value = "ID of the group to view.", required = true) - @PathParam("groupName") String groupName); + @PathParam("groupId") int groupId); - @Path("/name/{groupName}") + @Path("/id/{groupId}") @PUT @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -303,17 +303,17 @@ public interface GroupManagementService { }) @Permission(name = "Update Group", permission = "/device-mgt/groups/update") Response updateGroup(@ApiParam( - name = "groupName", - value = "Name of the group to be updated.", + name = "groupId", + value = "ID of the group to be updated.", required = true) - @PathParam("groupName") String groupName, + @PathParam("groupId") int groupId, @ApiParam( name = "group", value = "Group object with data.", required = true) @Valid DeviceGroup deviceGroup); - @Path("/name/{groupName}") + @Path("/id/{groupId}") @DELETE @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -355,12 +355,12 @@ public interface GroupManagementService { }) @Permission(name = "Remove Group", permission = "/device-mgt/groups/remove") Response deleteGroup(@ApiParam( - name = "groupName", - value = "Name of the group to be deleted.", + name = "groupId", + value = "ID of the group to be deleted.", required = true) - @PathParam("groupName") String groupName); + @PathParam("groupId") int groupId); - @Path("/name/{groupName}/share") + @Path("/id/{groupId}/share") @POST @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -405,14 +405,14 @@ public interface GroupManagementService { name = "groupName", value = "Name of the group to be shared or unshared.", required = true) - @PathParam("groupName") String groupName, + @PathParam("groupId") int groupId, @ApiParam( name = "deviceGroupShare", value = "User name and the assigned roles for the share.", required = true) @Valid DeviceGroupShare deviceGroupShare); - @Path("/name/{groupName}/users") + @Path("/id/{groupId}/users") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -454,12 +454,12 @@ public interface GroupManagementService { }) @Permission(name = "View users", permission = "/device-mgt/groups/users/view") Response getUsersOfGroup(@ApiParam( - name = "groupName", - value = "Name of the group.", + name = "groupId", + value = "ID of the group.", required = true) - @PathParam("groupName") String groupName); + @PathParam("groupId") int groupId); - @Path("/name/{groupName}/devices") + @Path("/id/{groupId}/devices") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -501,10 +501,10 @@ public interface GroupManagementService { }) @Permission(name = "View devices", permission = "/device-mgt/groups/devices/view") Response getDevicesOfGroup(@ApiParam( - name = "groupName", - value = "Name of the group.", + name = "groupId", + value = "ID of the group.", required = true) - @PathParam("groupName") String groupName, + @PathParam("groupId") int groupId, @ApiParam( name = "offset", value = "Starting point within the complete list of items qualified.") @@ -514,7 +514,7 @@ public interface GroupManagementService { value = "Maximum size of resource array to return.") @QueryParam("limit") int limit); - @Path("/name/{groupName}/devices/count") + @Path("/id/{groupId}/devices/count") @GET @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -556,12 +556,12 @@ public interface GroupManagementService { }) @Permission(name = "View devices", permission = "/device-mgt/groups/devices/view") Response getDeviceCountOfGroup(@ApiParam( - name = "groupName", - value = "Name of the group.", + name = "groupId", + value = "ID of the group.", required = true) - @PathParam("groupName") String groupName); + @PathParam("groupId") int groupId); - @Path("/name/{groupName}/devices") + @Path("/id/{groupId}/devices") @POST @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -602,17 +602,17 @@ public interface GroupManagementService { }) @Permission(name = "Add devices", permission = "/device-mgt/groups/devices/add") Response addDevicesToGroup(@ApiParam( - name = "groupName", - value = "Name of the group.", + name = "groupId", + value = "ID of the group.", required = true) - @PathParam("groupName") String groupName, + @PathParam("groupId") int groupId, @ApiParam( name = "deviceIdentifiers", value = "Device identifiers of the devices which needed be added.", required = true) @Valid List deviceIdentifiers); - @Path("/name/{groupName}/devices") + @Path("/id/{groupId}/devices") @DELETE @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -653,10 +653,10 @@ public interface GroupManagementService { }) @Permission(name = "Remove devices", permission = "/device-mgt/groups/devices/remove") Response removeDevicesFromGroup(@ApiParam( - name = "groupName", - value = "Name of the group.", + name = "groupId", + value = "ID of the group.", required = true) - @PathParam("groupName") String groupName, + @PathParam("groupId") int groupId, @ApiParam( name = "deviceIdentifiers", value = "Device identifiers of the devices which needed to be removed.", 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 e1e874ad1f..93307aa361 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 @@ -31,6 +31,7 @@ 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.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.core.Response; @@ -48,15 +49,20 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Override public Response getGroups(int offset, int limit) { try { + RequestValidationUtil.validatePaginationParameters(offset, limit); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); 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(); + if (deviceGroups != null && deviceGroups.size() > 0) { + DeviceGroupList deviceGroupList = new DeviceGroupList(); + deviceGroupList.setList(deviceGroups); + deviceGroupList.setCount(service.getGroupCount(currentUser)); + return Response.status(Response.Status.OK).entity(deviceGroupList).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).build(); + } } catch (GroupManagementException e) { - String error = "Error occurred while getting the groups related to users for policy."; + String error = "Error occurred while getting the groups."; log.error(error, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); } @@ -94,52 +100,75 @@ public class GroupManagementServiceImpl implements GroupManagementService { } 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(); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); } } @Override - public Response getGroup(String groupName) { - return null; + public Response getGroup(int groupId) { + try { + GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); + DeviceGroup deviceGroup = service.getGroup(groupId); + if (deviceGroup != null) { + return Response.status(Response.Status.OK).entity(deviceGroup).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).build(); + } + } catch (GroupManagementException e) { + String error = "Error occurred while getting the group."; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } } @Override - public Response updateGroup(String groupName, DeviceGroup deviceGroup) { - return null; + public Response updateGroup(int groupId, DeviceGroup deviceGroup) { + if (deviceGroup == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + deviceGroup.setDateOfLastUpdate(new Date().getTime()); + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().updateGroup(deviceGroup, groupId); + 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(); + } } @Override - public Response deleteGroup(String groupName) { + public Response deleteGroup(int groupId) { return null; } @Override - public Response manageGroupSharing(String groupName, DeviceGroupShare deviceGroupShare) { + public Response manageGroupSharing(int groupId, DeviceGroupShare deviceGroupShare) { return null; } @Override - public Response getUsersOfGroup(String groupName) { + public Response getUsersOfGroup(int groupId) { return null; } @Override - public Response getDevicesOfGroup(String groupName, int offset, int limit) { + public Response getDevicesOfGroup(int groupId, int offset, int limit) { return null; } @Override - public Response getDeviceCountOfGroup(String groupName) { + public Response getDeviceCountOfGroup(int groupId) { return null; } @Override - public Response addDevicesToGroup(String groupName, List deviceIdentifiers) { + public Response addDevicesToGroup(int groupId, List deviceIdentifiers) { return null; } @Override - public Response removeDevicesFromGroup(String groupName, List deviceIdentifiers) { + public Response removeDevicesFromGroup(int groupId, 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 887be40c08..01415da80a 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 @@ -27,6 +27,7 @@ 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.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.Consumes; @@ -36,9 +37,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; -@Path("/admin/groups") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) public class GroupManagementAdminServiceImpl implements GroupManagementAdminService { private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); @@ -46,12 +44,13 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ @Override public Response getGroups(int offset, int limit) { try { + RequestValidationUtil.validatePaginationParameters(offset, limit); 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) { + DeviceGroupList deviceGroupList = new DeviceGroupList(); + deviceGroupList.setList(deviceGroups); + deviceGroupList.setCount(service.getGroupCount()); return Response.status(Response.Status.OK).entity(deviceGroupList).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); 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 b164a4ba11..8d91c08cff 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 @@ -40,24 +40,22 @@ public interface GroupDAO { /** * Update an existing Device Group. * - * @param deviceGroup group to update. - * @param oldGroupName of the group. - * @param oldOwner of the group. - * @param tenantId of the group. + * @param deviceGroup group to update. + * @param groupId of Device Group. + * @param tenantId of the group. * @throws GroupManagementDAOException */ - void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner, int tenantId) + void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException; /** * Delete an existing Device Group. * - * @param groupName to be deleted. - * @param owner of the group. + * @param groupId of Device Group. * @param tenantId of the group. * @throws GroupManagementDAOException */ - void deleteGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException; + void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException; /** * Get device group by id. @@ -69,21 +67,10 @@ public interface GroupDAO { */ DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException; - /** - * Get device group by name. - * - * @param groupName of Device Group. - * @param owner of the group. - * @param tenantId of the group. - * @return Device Group in tenant with specified name. - * @throws GroupManagementDAOException - */ - DeviceGroup getGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException; - /** * Get the groups of device with device id provided * @param deviceId - * @return + * @return groups which has the device. * @throws GroupManagementDAOException */ List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException; @@ -123,81 +110,64 @@ public interface GroupDAO { * Check group already existed with given name. * * @param groupName of the Device Group. - * @param owner of the Device Group. * @param tenantId of user's tenant. * @return existence of group with name * @throws GroupManagementDAOException */ - boolean isGroupExist(String groupName, String owner, int tenantId) throws GroupManagementDAOException; + boolean isGroupExist(String groupName, int tenantId) throws GroupManagementDAOException; /** * Add device to a given Device Group. * - * @param groupName of the Device Group. - * @param owner of the Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException */ - void addDevice(String groupName, String owner, int deviceId, int tenantId) throws GroupManagementDAOException; + void addDevice(int groupId, int deviceId, int tenantId) throws GroupManagementDAOException; /** * Remove device from the Device Group. * - * @param groupName of the Device Group. - * @param owner of the Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException */ - void removeDevice(String groupName, String owner, int deviceId, int tenantId) throws GroupManagementDAOException; + void removeDevice(int groupId, int deviceId, int tenantId) throws GroupManagementDAOException; /** * Check device is belonging to a Device Group. * - * @param groupName of the Device Group. - * @param owner of the Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException */ - boolean isDeviceMappedToGroup(String groupName, String owner, int deviceId, int tenantId) + boolean isDeviceMappedToGroup(int groupId, int deviceId, int tenantId) throws GroupManagementDAOException; /** * Get count of devices in a Device Group. * - * @param groupName of the Device Group. - * @param owner of the Device Group. + * @param groupId of Device Group. * @param tenantId of user's tenant. * @return device count. * @throws GroupManagementDAOException */ - int getDeviceCount(String groupName, String owner, int tenantId) throws GroupManagementDAOException; - - /** - * Get all devices of a given tenant and device group. - * - * @param groupName of the group. - * @param owner of the Device Group. - * @param tenantId of user's tenant. - * @return list of device in group - * @throws GroupManagementDAOException - */ - List getDevices(String groupName, String owner, int tenantId) throws GroupManagementDAOException; + int getDeviceCount(int groupId, int tenantId) throws GroupManagementDAOException; /** * Get paginated result of devices of a given tenant and device group. * - * @param groupName of the group. - * @param owner of the Device Group. + * @param groupId of Device Group. * @param startIndex for pagination. * @param rowCount for pagination. * @param tenantId of user's tenant. * @return list of device in group * @throws GroupManagementDAOException */ - List getDevices(String groupName, String owner, int startIndex, int rowCount, int tenantId) + List getDevices(int groupId, int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException; } 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 4b2134d6b9..43928cc821 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 @@ -66,21 +66,20 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner, int tenantId) + public void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, DATE_OF_LAST_UPDATE = ?, OWNER = ? " - + "WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?"; + + "WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceGroup.getDescription()); stmt.setString(2, deviceGroup.getName()); stmt.setLong(3, deviceGroup.getDateOfLastUpdate()); stmt.setString(4, deviceGroup.getOwner()); - stmt.setString(5, oldGroupName); - stmt.setString(6, oldOwner); - stmt.setInt(7, tenantId); + stmt.setInt(5, groupId); + stmt.setInt(6, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + @@ -91,35 +90,31 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public void deleteGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException { + public void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException { Connection conn; PreparedStatement stmt = null; try { conn = GroupManagementDAOFactory.getConnection(); - String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = (SELECT ID AS GROUP_ID FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) AND TENANT_ID = ?"; + String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); - stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); stmt.executeUpdate(); } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while removing mappings for group '" + groupName + - "'", e); + throw new GroupManagementDAOException("Error occurred while removing mappings for group.'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, null); } try { conn = GroupManagementDAOFactory.getConnection(); - String sql = "DELETE FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?"; + String sql = "DELETE FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); - stmt.setInt(3, tenantId); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); stmt.executeUpdate(); } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while deleting group '" + groupName + "'", e); + throw new GroupManagementDAOException("Error occurred while deleting group.'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, null); } @@ -150,33 +145,6 @@ public class GroupDAOImpl implements GroupDAO { } } - @Override - public DeviceGroup getGroup(String groupName, String owner, int tenantId) - throws GroupManagementDAOException { - PreparedStatement stmt = null; - ResultSet resultSet = null; - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER " - + "FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); - stmt.setInt(3, tenantId); - resultSet = stmt.executeQuery(); - if (resultSet.next()) { - return GroupManagementDAOUtil.loadGroup(resultSet); - } else { - return null; - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while obtaining information of Device Group '" + - groupName + "'", e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - } - @Override public List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException { @@ -281,114 +249,95 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public boolean isGroupExist(String groupName, String owner, int tenantId) throws GroupManagementDAOException { + public boolean isGroupExist(String groupName, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT GROUP_NAME FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?"; + String sql = "SELECT GROUP_NAME FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, groupName); - stmt.setString(2, owner); - stmt.setInt(3, tenantId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); return resultSet.next(); } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" + - groupName + "'", e); + throw new GroupManagementDAOException("Error occurred while group Id listing by group name.'", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } } @Override - public void addDevice(String groupName, String owner, int deviceId, int tenantId) + public void addDevice(int groupId, int deviceId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "INSERT INTO DM_DEVICE_GROUP_MAP(DEVICE_ID, GROUP_ID, TENANT_ID) " + - "VALUES (?, (SELECT ID as GROUP_ID FROM DM_GROUP " + - "WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?), ?)"; + String sql = "INSERT INTO DM_DEVICE_GROUP_MAP(DEVICE_ID, GROUP_ID, TENANT_ID) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); - stmt.setString(2, groupName); - stmt.setString(3, owner); - stmt.setInt(4, tenantId); - stmt.setInt(5, tenantId); + stmt.setInt(2, groupId); + stmt.setInt(3, tenantId); stmt.executeUpdate(); stmt.getGeneratedKeys(); } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while adding device to Group '" + groupName + "'", e); + throw new GroupManagementDAOException("Error occurred while adding device to Group.", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, null); } } @Override - public void removeDevice(String groupName, String owner, int deviceId, int tenantId) + public void removeDevice(int groupId, int deviceId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ? AND GROUP_ID = (SELECT ID as GROUP_ID " + - "FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) AND TENANT_ID = ?"; + String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ? AND GROUP_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); - stmt.setString(2, groupName); - stmt.setString(3, owner); - stmt.setInt(4, tenantId); - stmt.setInt(5, tenantId); + stmt.setInt(2, groupId); + stmt.setInt(3, tenantId); stmt.executeUpdate(); stmt.getGeneratedKeys(); } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while removing device from Group '" + - groupName + "'", e); + throw new GroupManagementDAOException("Error occurred while removing device from Group.", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, null); } } @Override - public boolean isDeviceMappedToGroup(String groupName, String owner, int deviceId, int tenantId) + public boolean isDeviceMappedToGroup(int groupId, int deviceId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT dm.ID FROM DM_DEVICE_GROUP_MAP dm, (SELECT ID as GROUP_ID FROM DM_GROUP " + - "WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " + - "WHERE dm.GROUP_ID = dg.GROUP_ID AND dm.ID = ? AND dm.TENANT_ID = ?"; + String sql = "SELECT ID FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); + stmt.setInt(1, groupId); + stmt.setInt(2, deviceId); stmt.setInt(3, tenantId); - stmt.setInt(4, deviceId); - stmt.setInt(5, tenantId); resultSet = stmt.executeQuery(); return resultSet.next(); } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" + - groupName + "'", e); + throw new GroupManagementDAOException("Error occurred while checking device mapping with group.", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } } @Override - public int getDeviceCount(String groupName, String owner, int tenantId) throws GroupManagementDAOException { + public int getDeviceCount(int groupId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT COUNT(gm.ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP gm, (SELECT ID " + - "FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " + - "WHERE gm.GROUP_ID = dg.ID AND gm.TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); - stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); if (resultSet.next()) { return resultSet.getInt("DEVICE_COUNT"); @@ -396,54 +345,14 @@ public class GroupDAOImpl implements GroupDAO { return 0; } } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" + - groupName + "'", e); + throw new GroupManagementDAOException("Error occurred while getting device count from the group.", e); } finally { GroupManagementDAOUtil.cleanupResources(stmt, resultSet); } } @Override - public List getDevices(String groupName, String owner, int tenantId) throws GroupManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - List devices = null; - try { - conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + - "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + - "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + - "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, " + - "d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (SELECT dgm.DEVICE_ID " + - "FROM DM_DEVICE_GROUP_MAP dgm, DM_GROUP dg WHERE dgm.GROUP_ID = dg.ID AND dg.GROUP_NAME = ? " + - "AND dg.OWNER = ? AND dg.TENANT_ID = ?) dgm1 WHERE d.ID = dgm1.DEVICE_ID " + - "AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " + - "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); - stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); - stmt.setInt(5, tenantId); - rs = stmt.executeQuery(); - devices = new ArrayList<>(); - while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while retrieving information of all " + - "registered devices", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return devices; - } - - @Override - public List getDevices(String groupName, String owner, int startIndex, int rowCount, int tenantId) + public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException { Connection conn; PreparedStatement stmt = null; @@ -454,24 +363,20 @@ public class GroupDAOImpl implements GroupDAO { String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + - "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, " + - "t.NAME AS DEVICE_TYPE FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, " + - "d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (SELECT dgm.DEVICE_ID " + - "FROM DM_DEVICE_GROUP_MAP dgm, DM_GROUP dg WHERE dgm.GROUP_ID = dg.ID AND dg.GROUP_NAME = ? " + - "AND dg.OWNER = ? AND dg.TENANT_ID = ?) dgm1 WHERE d.ID = dgm1.DEVICE_ID " + - "AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t WHERE gd.DEVICE_TYPE_ID = t.ID) d1 " + - "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?, ?"; + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " + + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (" + + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?"; stmt = conn.prepareStatement(sql); - stmt.setString(1, groupName); - stmt.setString(2, owner); + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); stmt.setInt(3, tenantId); - stmt.setInt(4, tenantId); - stmt.setInt(5, tenantId); //noinspection JpaQueryApiInspection - stmt.setInt(6, startIndex); + stmt.setInt(4, startIndex); //noinspection JpaQueryApiInspection - stmt.setInt(7, rowCount); + stmt.setInt(5, rowCount); rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { 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 b1c9a8468c..f04226943c 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 @@ -49,44 +49,31 @@ public interface GroupManagementProviderService { * Update existing device group. * * @param deviceGroup to update. - * @param oldGroupName of the group. - * @param oldOwner of the group. + * @param groupId of the group. * @throws GroupManagementException */ - void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner) throws GroupManagementException; + void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException; /** * Delete existing device group. * - * @param groupName to be deleted. - * @param owner of the group. + * @param groupId to be deleted. * @return status of the delete operation. * @throws GroupManagementException */ - boolean deleteGroup(String groupName, String owner) throws GroupManagementException; - - /** - * Get device group specified by group name. - * - * @param groupName of the group. - * @param owner of the group. - * @return group - * @throws GroupManagementException - */ - DeviceGroup getGroup(String groupName, String owner) throws GroupManagementException; - + boolean deleteGroup(int groupId) throws GroupManagementException; /** * Get the device group provided the device group id. * - * @param groupId - * @return + * @param groupId of the group. + * @return group with details. * @throws GroupManagementException */ DeviceGroup getGroup(int groupId) throws GroupManagementException; /** - * Get paginated device groups in tenant + * Get paginated device groups in tenant. * * @param startIndex for pagination. * @param rowCount for pagination. @@ -96,9 +83,9 @@ public interface GroupManagementProviderService { List getGroups(int startIndex, int rowCount) throws GroupManagementException; /** - * Get paginated device groups in tenant + * Get paginated device groups for user. * - * @param username of user. + * @param username of the user. * @param startIndex for pagination. * @param rowCount for pagination. * @return list of groups @@ -127,141 +114,126 @@ public interface GroupManagementProviderService { * Share device group with user specified by role * * @param username of the user - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @param sharingRole to be shared * @return is group shared * @throws GroupManagementException UserDoesNotExistException */ - boolean shareGroup(String username, String groupName, String owner, String sharingRole) + boolean shareGroup(String username, int groupId, String sharingRole) throws GroupManagementException, UserDoesNotExistException; /** * Un share existing group sharing with user specified by role * * @param userName of the user - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @param sharingRole to be un shared * @return is group un shared * @throws GroupManagementException UserDoesNotExistException */ - boolean unshareGroup(String userName, String groupName, String owner, String sharingRole) + boolean unshareGroup(String userName, int groupId, String sharingRole) throws GroupManagementException, UserDoesNotExistException; /** * Add new sharing role for device group * * @param userName of the user - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @param roleName to add * @param permissions to bind with role * @return is role added * @throws GroupManagementException */ - boolean addGroupSharingRole(String userName, String groupName, String owner, String roleName, String[] permissions) + boolean addGroupSharingRole(String userName, int groupId, String roleName, String[] permissions) throws GroupManagementException; /** * Remove existing sharing role for device group * - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @param roleName to remove * @return is role removed * @throws GroupManagementException */ - boolean removeGroupSharingRole(String groupName, String owner, String roleName) throws GroupManagementException; + boolean removeGroupSharingRole(int groupId, String roleName) throws GroupManagementException; /** * Get all sharing roles for device group * - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @return list of roles * @throws GroupManagementException */ - List getRoles(String groupName, String owner) throws GroupManagementException; + List getRoles(int groupId) throws GroupManagementException; /** * Get specific device group sharing roles for user * * @param userName of the user - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @return list of roles * @throws GroupManagementException UserDoesNotExistException */ - List getRoles(String userName, String groupName, String owner) - throws GroupManagementException, UserDoesNotExistException; + List getRoles(String userName, int groupId) throws GroupManagementException, UserDoesNotExistException; /** * Get device group users * - * @param groupName of the group - * @param owner of the group + * @param groupId of the group * @return list of group users * @throws GroupManagementException */ - List getUsers(String groupName, String owner) throws GroupManagementException; + List getUsers(int groupId) throws GroupManagementException; /** * Get all devices in device group as paginated result. * - * @param groupName of the group. - * @param owner of the group. + * @param groupId of the group * @param startIndex for pagination. * @param rowCount for pagination. * @return list of devices in group. * @throws GroupManagementException */ - List getDevices(String groupName, String owner, int startIndex, int rowCount) - throws GroupManagementException; + List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException; /** * This method is used to retrieve the device count of a given group. * - * @param groupName of the group. - * @param owner of the group. + * @param groupId of the group * @return returns the device count. * @throws GroupManagementException */ - int getDeviceCount(String groupName, String owner) throws GroupManagementException; + int getDeviceCount(int groupId) throws GroupManagementException; /** * Add device to device group. * * @param deviceId of the device. - * @param groupName of the group. - * @param owner of the group. + * @param groupId of the group * @return is device added. * @throws GroupManagementException */ - boolean addDevice(DeviceIdentifier deviceId, String groupName, String owner) throws GroupManagementException; + boolean addDevice(DeviceIdentifier deviceId, int groupId) throws GroupManagementException; /** * Remove device from device group. * * @param deviceId of the device. - * @param groupName of the group. - * @param owner of the group. + * @param groupId of the group * @return is device removed. * @throws GroupManagementException */ - boolean removeDevice(DeviceIdentifier deviceId, String groupName, String owner) throws GroupManagementException; + boolean removeDevice(DeviceIdentifier deviceId, int groupId) throws GroupManagementException; /** * Get device group permissions of user. * * @param username of the user. - * @param groupName of the group. - * @param owner of the group. + * @param groupId of the group * @return array of permissions. * @throws GroupManagementException UserDoesNotExistException */ - String[] getPermissions(String username, String groupName, String owner) - throws GroupManagementException, UserDoesNotExistException; + String[] getPermissions(String username, int groupId) throws GroupManagementException, UserDoesNotExistException; /** * Get device groups of user with permission. 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 762478c592..a76c956646 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 @@ -23,7 +23,10 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; 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; @@ -40,7 +43,11 @@ import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException; import org.wso2.carbon.user.core.util.UserCoreUtil; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class GroupManagementProviderServiceImpl implements GroupManagementProviderService { @@ -68,7 +75,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int groupId = -1; try { GroupManagementDAOFactory.beginTransaction(); - boolean nameIsExists = this.groupDAO.isGroupExist(deviceGroup.getName(), deviceGroup.getOwner(), tenantId); + boolean nameIsExists = this.groupDAO.isGroupExist(deviceGroup.getName(), tenantId); if (!nameIsExists) { groupId = this.groupDAO.addGroup(deviceGroup, tenantId); GroupManagementDAOFactory.commitTransaction(); @@ -95,7 +102,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner) + public void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException { if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); @@ -104,7 +111,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); deviceGroup.setDateOfLastUpdate(new Date().getTime()); - this.groupDAO.updateGroup(deviceGroup, oldGroupName, oldOwner, tenantId); + this.groupDAO.updateGroup(deviceGroup, groupId, tenantId); GroupManagementDAOFactory.commitTransaction(); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); @@ -121,13 +128,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean deleteGroup(String groupName, String owner) throws GroupManagementException { + public boolean deleteGroup(int groupId) throws GroupManagementException { String roleName; - DeviceGroup deviceGroup = buildDeviceGroup(groupName, owner); + DeviceGroup deviceGroup = buildDeviceGroup(groupId); if (deviceGroup == null) { return false; } - List groupRoles = getRoles(groupName, owner); + List groupRoles = getRoles(groupId); for (String role : groupRoles) { if (role != null) { roleName = role.replace("Internal/group-" + deviceGroup.getGroupId() + "-", ""); @@ -136,7 +143,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } try { GroupManagementDAOFactory.beginTransaction(); - this.groupDAO.deleteGroup(groupName, owner, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + this.groupDAO.deleteGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); GroupManagementDAOFactory.commitTransaction(); if (log.isDebugEnabled()) { log.debug("DeviceGroup " + deviceGroup.getName() + " removed."); @@ -144,8 +151,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return true; } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while removing group " + - "'" + groupName + "' data.", e); + throw new GroupManagementException("Error occurred while removing group data.", e); } catch (TransactionManagementException e) { throw new GroupManagementException("Error occurred while initiating transaction.", e); } finally { @@ -153,35 +159,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } - /** - * {@inheritDoc} - */ - @Override - public DeviceGroup getGroup(String groupName, String owner) throws GroupManagementException { - return buildDeviceGroup(groupName, owner); - } - - private DeviceGroup buildDeviceGroup(String groupName, String owner) throws GroupManagementException { - DeviceGroup deviceGroup; - try { - GroupManagementDAOFactory.openConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceGroup = this.groupDAO.getGroup(groupName, owner, tenantId); - } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while obtaining group '" + groupName + "'", e); - } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); - } finally { - GroupManagementDAOFactory.closeConnection(); - } - if (deviceGroup != null) { - deviceGroup.setUsers(this.getUsers(deviceGroup.getGroupId())); - deviceGroup.setRoles(this.getRoles(deviceGroup.getGroupId())); - } - return deviceGroup; - } - - private DeviceGroup buildDeviceGroup(int groupId) throws GroupManagementException { DeviceGroup deviceGroup; try { @@ -306,9 +283,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean shareGroup(String username, String groupName, String owner, String sharingRole) + public boolean shareGroup(String username, int groupId, String sharingRole) throws GroupManagementException, UserDoesNotExistException { - int groupId = getGroupId(groupName, owner); return modifyGroupShare(username, groupId, sharingRole, true); } @@ -316,9 +292,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean unshareGroup(String username, String groupName, String owner, String sharingRole) + public boolean unshareGroup(String username, int groupId, String sharingRole) throws GroupManagementException, UserDoesNotExistException { - int groupId = getGroupId(groupName, owner); return modifyGroupShare(username, groupId, sharingRole, false); } @@ -355,26 +330,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } - private int getGroupId(String groupName, String owner) throws GroupManagementException { - DeviceGroup deviceGroup = buildDeviceGroup(groupName, owner); - if (deviceGroup == null) { - return -1; - } - return deviceGroup.getGroupId(); - } - /** * {@inheritDoc} */ @Override - public boolean addGroupSharingRole(String userName, String groupName, String owner, String roleName, - String[] permissions) throws GroupManagementException { - int groupId = getGroupId(groupName, owner); - return addGroupSharingRole(userName, groupId, roleName, permissions); - } - - private boolean addGroupSharingRole(String username, int groupId, String roleName, - String[] permissions) + public boolean addGroupSharingRole(String username, int groupId, String roleName, String[] permissions) throws GroupManagementException { if (groupId == -1) { return false; @@ -404,14 +364,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean removeGroupSharingRole(String groupName, String owner, String roleName) - throws GroupManagementException { - int groupId = getGroupId(groupName, owner); - return removeGroupSharingRole(groupId, roleName); - } - - private boolean removeGroupSharingRole(int groupId, String roleName) - throws GroupManagementException { + public boolean removeGroupSharingRole(int groupId, String roleName) throws GroupManagementException { if (groupId == -1) { return false; } @@ -435,12 +388,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public List getRoles(String groupName, String owner) throws GroupManagementException { - int groupId = getGroupId(groupName, owner); - return getRoles(groupId); - } - - private List getRoles(int groupId) throws GroupManagementException { + public List getRoles(int groupId) throws GroupManagementException { UserStoreManager userStoreManager; String[] roles; List groupRoles; @@ -467,13 +415,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public List getRoles(String username, String groupName, String owner) - throws GroupManagementException, UserDoesNotExistException { - int groupId = getGroupId(groupName, owner); - return getRoles(username, groupId); - } - - private List getRoles(String username, int groupId) + public List getRoles(String username, int groupId) throws GroupManagementException, UserDoesNotExistException { UserStoreManager userStoreManager; List groupRoleList = new ArrayList<>(); @@ -504,11 +446,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public List getUsers(String groupName, String owner) throws GroupManagementException { - int groupId = getGroupId(groupName, owner); - return getUsers(groupId); - } - public List getUsers(int groupId) throws GroupManagementException { UserStoreManager userStoreManager; Map groupUserHashMap = new HashMap<>(); @@ -545,13 +482,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public List getDevices(String groupName, String owner, int startIndex, int rowCount) + public List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices; try { GroupManagementDAOFactory.openConnection(); - devices = this.groupDAO.getDevices(groupName, owner, startIndex, rowCount, tenantId); + devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId); } catch (GroupManagementDAOException e) { throw new GroupManagementException("Error occurred while getting devices in group.", e); } catch (SQLException e) { @@ -566,13 +503,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public int getDeviceCount(String groupName, String owner) throws GroupManagementException { + public int getDeviceCount(int groupId) throws GroupManagementException { try { - int count; GroupManagementDAOFactory.openConnection(); - count = groupDAO.getDeviceCount(groupName, owner, - CarbonContext.getThreadLocalCarbonContext().getTenantId()); - return count; + return groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); } catch (SQLException e) { @@ -586,7 +520,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean addDevice(DeviceIdentifier deviceIdentifier, String groupName, String owner) + public boolean addDevice(DeviceIdentifier deviceIdentifier, int groupId) throws GroupManagementException { Device device; try { @@ -596,13 +530,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); - this.groupDAO.addDevice(groupName, owner, device.getId(), tenantId); + this.groupDAO.addDevice(groupId, device.getId(), tenantId); GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { throw new GroupManagementException("Error occurred while retrieving device.", e); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while adding device to group '" + groupName + "'.", e); + throw new GroupManagementException("Error occurred while adding device to group.", e); } catch (TransactionManagementException e) { throw new GroupManagementException("Error occurred while initiating transaction.", e); } finally { @@ -615,8 +549,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean removeDevice(DeviceIdentifier deviceIdentifier, String groupName, String owner) - throws GroupManagementException { + public boolean removeDevice(DeviceIdentifier deviceIdentifier, int groupId) throws GroupManagementException { Device device; try { device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier); @@ -625,7 +558,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); - this.groupDAO.removeDevice(groupName, owner, device.getId(), tenantId); + this.groupDAO.removeDevice(groupId, device.getId(), tenantId); GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { throw new GroupManagementException("Error occurred while retrieving device.", e); @@ -633,7 +566,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid throw new GroupManagementException("Error occurred while initiating transaction.", e); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while adding device to group '" + groupName + "'.", e); + throw new GroupManagementException("Error occurred while adding device to group.", e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -644,10 +577,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public String[] getPermissions(String username, String groupName, String owner) + public String[] getPermissions(String username, int groupId) throws GroupManagementException, UserDoesNotExistException { UserRealm userRealm; - int groupId = getGroupId(groupName, owner); List roles = getRoles(username, groupId); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { 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 d6e435a7c6..6ef90d78c7 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 @@ -56,7 +56,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { GroupManagementDAOFactory.beginTransaction(); groupId = groupDAO.addGroup(deviceGroup, TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); - log.debug("Group added to database."); + log.debug("Group added to database. ID: " + groupId); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while adding device type '" + deviceGroup.getName() + "'."; @@ -123,8 +123,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { DeviceGroup deviceGroup = getGroupById(groupId); try { GroupManagementDAOFactory.beginTransaction(); - groupDAO.addDevice(deviceGroup.getName(), deviceGroup.getOwner(), initialTestDevice.getId(), - TestDataHolder.SUPER_TENANT_ID); + groupDAO.addDevice(deviceGroup.getGroupId(), initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); log.debug("Device added to group."); } catch (GroupManagementDAOException e) { @@ -142,8 +141,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { try { GroupManagementDAOFactory.openConnection(); - List groupedDevices = groupDAO.getDevices(deviceGroup.getName(), deviceGroup.getOwner(), - TestDataHolder.SUPER_TENANT_ID); + List groupedDevices = groupDAO.getDevices(deviceGroup.getGroupId(), 0, 10, TestDataHolder.SUPER_TENANT_ID); Assert.assertNotEquals(groupedDevices.size(), 0, "No device found"); Assert.assertNotNull(groupedDevices.get(0), "Device is null"); Assert.assertEquals(groupedDevices.get(0).getId(), initialTestDevice.getId(), "Device ids not matched"); @@ -166,7 +164,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { DeviceGroup deviceGroup = getGroupById(groupId); try { GroupManagementDAOFactory.beginTransaction(); - groupDAO.removeDevice(deviceGroup.getName(), deviceGroup.getOwner(), initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID); + groupDAO.removeDevice(deviceGroup.getGroupId(), initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); log.debug("Device added to group."); } catch (GroupManagementDAOException e) { @@ -195,8 +193,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { group.setDescription(desc); try { GroupManagementDAOFactory.beginTransaction(); - groupDAO.updateGroup(group, TestDataHolder.generateDummyGroupData().getName(), - TestDataHolder.generateDummyGroupData().getOwner(), TestDataHolder.SUPER_TENANT_ID); + groupDAO.updateGroup(group, groupId, TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); log.debug("Group updated"); } catch (GroupManagementDAOException e) { @@ -225,7 +222,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest { try { Assert.assertNotNull(group, "Group is null"); GroupManagementDAOFactory.beginTransaction(); - groupDAO.deleteGroup(group.getName(), group.getOwner(), TestDataHolder.SUPER_TENANT_ID); + groupDAO.deleteGroup(group.getGroupId(), TestDataHolder.SUPER_TENANT_ID); GroupManagementDAOFactory.commitTransaction(); log.debug("Group deleted"); } catch (GroupManagementDAOException e) {