Exposed group id to API level

4.x.x
charitha 8 years ago
parent f22d392978
commit 06176898d0

@ -214,12 +214,12 @@ public interface GroupManagementService {
required = true) required = true)
@Valid DeviceGroup group); @Valid DeviceGroup group);
@Path("/name/{groupName}") @Path("/id/{groupId}")
@GET @GET
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET, httpMethod = HTTPConstants.HEADER_GET,
value = "View group specified with name.", value = "View group specified.",
notes = "Returns details of group enrolled with the system.", notes = "Returns details of group enrolled with the system.",
tags = "Device Group Management") tags = "Device Group Management")
@ApiResponses(value = { @ApiResponses(value = {
@ -256,12 +256,12 @@ public interface GroupManagementService {
}) })
@Permission(name = "View Groups", permission = "/device-mgt/groups/view") @Permission(name = "View Groups", permission = "/device-mgt/groups/view")
Response getGroup(@ApiParam( Response getGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group to view.", value = "ID of the group to view.",
required = true) required = true)
@PathParam("groupName") String groupName); @PathParam("groupId") int groupId);
@Path("/name/{groupName}") @Path("/id/{groupId}")
@PUT @PUT
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -303,17 +303,17 @@ public interface GroupManagementService {
}) })
@Permission(name = "Update Group", permission = "/device-mgt/groups/update") @Permission(name = "Update Group", permission = "/device-mgt/groups/update")
Response updateGroup(@ApiParam( Response updateGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group to be updated.", value = "ID of the group to be updated.",
required = true) required = true)
@PathParam("groupName") String groupName, @PathParam("groupId") int groupId,
@ApiParam( @ApiParam(
name = "group", name = "group",
value = "Group object with data.", value = "Group object with data.",
required = true) required = true)
@Valid DeviceGroup deviceGroup); @Valid DeviceGroup deviceGroup);
@Path("/name/{groupName}") @Path("/id/{groupId}")
@DELETE @DELETE
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -355,12 +355,12 @@ public interface GroupManagementService {
}) })
@Permission(name = "Remove Group", permission = "/device-mgt/groups/remove") @Permission(name = "Remove Group", permission = "/device-mgt/groups/remove")
Response deleteGroup(@ApiParam( Response deleteGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group to be deleted.", value = "ID of the group to be deleted.",
required = true) required = true)
@PathParam("groupName") String groupName); @PathParam("groupId") int groupId);
@Path("/name/{groupName}/share") @Path("/id/{groupId}/share")
@POST @POST
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -405,14 +405,14 @@ public interface GroupManagementService {
name = "groupName", name = "groupName",
value = "Name of the group to be shared or unshared.", value = "Name of the group to be shared or unshared.",
required = true) required = true)
@PathParam("groupName") String groupName, @PathParam("groupId") int groupId,
@ApiParam( @ApiParam(
name = "deviceGroupShare", name = "deviceGroupShare",
value = "User name and the assigned roles for the share.", value = "User name and the assigned roles for the share.",
required = true) required = true)
@Valid DeviceGroupShare deviceGroupShare); @Valid DeviceGroupShare deviceGroupShare);
@Path("/name/{groupName}/users") @Path("/id/{groupId}/users")
@GET @GET
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -454,12 +454,12 @@ public interface GroupManagementService {
}) })
@Permission(name = "View users", permission = "/device-mgt/groups/users/view") @Permission(name = "View users", permission = "/device-mgt/groups/users/view")
Response getUsersOfGroup(@ApiParam( Response getUsersOfGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group.", value = "ID of the group.",
required = true) required = true)
@PathParam("groupName") String groupName); @PathParam("groupId") int groupId);
@Path("/name/{groupName}/devices") @Path("/id/{groupId}/devices")
@GET @GET
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -501,10 +501,10 @@ public interface GroupManagementService {
}) })
@Permission(name = "View devices", permission = "/device-mgt/groups/devices/view") @Permission(name = "View devices", permission = "/device-mgt/groups/devices/view")
Response getDevicesOfGroup(@ApiParam( Response getDevicesOfGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group.", value = "ID of the group.",
required = true) required = true)
@PathParam("groupName") String groupName, @PathParam("groupId") int groupId,
@ApiParam( @ApiParam(
name = "offset", name = "offset",
value = "Starting point within the complete list of items qualified.") 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.") value = "Maximum size of resource array to return.")
@QueryParam("limit") int limit); @QueryParam("limit") int limit);
@Path("/name/{groupName}/devices/count") @Path("/id/{groupId}/devices/count")
@GET @GET
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -556,12 +556,12 @@ public interface GroupManagementService {
}) })
@Permission(name = "View devices", permission = "/device-mgt/groups/devices/view") @Permission(name = "View devices", permission = "/device-mgt/groups/devices/view")
Response getDeviceCountOfGroup(@ApiParam( Response getDeviceCountOfGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group.", value = "ID of the group.",
required = true) required = true)
@PathParam("groupName") String groupName); @PathParam("groupId") int groupId);
@Path("/name/{groupName}/devices") @Path("/id/{groupId}/devices")
@POST @POST
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -602,17 +602,17 @@ public interface GroupManagementService {
}) })
@Permission(name = "Add devices", permission = "/device-mgt/groups/devices/add") @Permission(name = "Add devices", permission = "/device-mgt/groups/devices/add")
Response addDevicesToGroup(@ApiParam( Response addDevicesToGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group.", value = "ID of the group.",
required = true) required = true)
@PathParam("groupName") String groupName, @PathParam("groupId") int groupId,
@ApiParam( @ApiParam(
name = "deviceIdentifiers", name = "deviceIdentifiers",
value = "Device identifiers of the devices which needed be added.", value = "Device identifiers of the devices which needed be added.",
required = true) required = true)
@Valid List<DeviceIdentifier> deviceIdentifiers); @Valid List<DeviceIdentifier> deviceIdentifiers);
@Path("/name/{groupName}/devices") @Path("/id/{groupId}/devices")
@DELETE @DELETE
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -653,10 +653,10 @@ public interface GroupManagementService {
}) })
@Permission(name = "Remove devices", permission = "/device-mgt/groups/devices/remove") @Permission(name = "Remove devices", permission = "/device-mgt/groups/devices/remove")
Response removeDevicesFromGroup(@ApiParam( Response removeDevicesFromGroup(@ApiParam(
name = "groupName", name = "groupId",
value = "Name of the group.", value = "ID of the group.",
required = true) required = true)
@PathParam("groupName") String groupName, @PathParam("groupId") int groupId,
@ApiParam( @ApiParam(
name = "deviceIdentifiers", name = "deviceIdentifiers",
value = "Device identifiers of the devices which needed to be removed.", value = "Device identifiers of the devices which needed to be removed.",

@ -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.DeviceGroupList;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare; 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.api.GroupManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -48,15 +49,20 @@ public class GroupManagementServiceImpl implements GroupManagementService {
@Override @Override
public Response getGroups(int offset, int limit) { public Response getGroups(int offset, int limit) {
try { try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
List<DeviceGroup> deviceGroups = service.getGroups(currentUser, offset, limit); List<DeviceGroup> deviceGroups = service.getGroups(currentUser, offset, limit);
DeviceGroupList deviceGroupList = new DeviceGroupList(); if (deviceGroups != null && deviceGroups.size() > 0) {
deviceGroupList.setList(deviceGroups); DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setCount(service.getGroupCount(currentUser)); deviceGroupList.setList(deviceGroups);
return Response.status(Response.Status.OK).entity(deviceGroupList).build(); 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) { } 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); log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
} }
@ -94,52 +100,75 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} catch (GroupAlreadyExistException e) { } catch (GroupAlreadyExistException e) {
String msg = "Group already exists with name '" + group.getName() + "'."; String msg = "Group already exists with name '" + group.getName() + "'.";
log.error(msg, e); 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 @Override
public Response getGroup(String groupName) { public Response getGroup(int groupId) {
return null; 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 @Override
public Response updateGroup(String groupName, DeviceGroup deviceGroup) { public Response updateGroup(int groupId, DeviceGroup deviceGroup) {
return null; 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 @Override
public Response deleteGroup(String groupName) { public Response deleteGroup(int groupId) {
return null; return null;
} }
@Override @Override
public Response manageGroupSharing(String groupName, DeviceGroupShare deviceGroupShare) { public Response manageGroupSharing(int groupId, DeviceGroupShare deviceGroupShare) {
return null; return null;
} }
@Override @Override
public Response getUsersOfGroup(String groupName) { public Response getUsersOfGroup(int groupId) {
return null; return null;
} }
@Override @Override
public Response getDevicesOfGroup(String groupName, int offset, int limit) { public Response getDevicesOfGroup(int groupId, int offset, int limit) {
return null; return null;
} }
@Override @Override
public Response getDeviceCountOfGroup(String groupName) { public Response getDeviceCountOfGroup(int groupId) {
return null; return null;
} }
@Override @Override
public Response addDevicesToGroup(String groupName, List<DeviceIdentifier> deviceIdentifiers) { public Response addDevicesToGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
return null; return null;
} }
@Override @Override
public Response removeDevicesFromGroup(String groupName, List<DeviceIdentifier> deviceIdentifiers) { public Response removeDevicesFromGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
return null; return null;
} }

@ -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.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; 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.api.admin.GroupManagementAdminService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@ -36,9 +37,6 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.List; import java.util.List;
@Path("/admin/groups")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class GroupManagementAdminServiceImpl implements GroupManagementAdminService { public class GroupManagementAdminServiceImpl implements GroupManagementAdminService {
private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class);
@ -46,12 +44,13 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
@Override @Override
public Response getGroups(int offset, int limit) { public Response getGroups(int offset, int limit) {
try { try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
List<DeviceGroup> deviceGroups = service.getGroups(offset, limit); List<DeviceGroup> deviceGroups = service.getGroups(offset, limit);
DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroups);
deviceGroupList.setCount(service.getGroupCount());
if (deviceGroups != null && deviceGroups.size() > 0) { 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(); return Response.status(Response.Status.OK).entity(deviceGroupList).build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).build();

@ -40,24 +40,22 @@ public interface GroupDAO {
/** /**
* Update an existing Device Group. * Update an existing Device Group.
* *
* @param deviceGroup group to update. * @param deviceGroup group to update.
* @param oldGroupName of the group. * @param groupId of Device Group.
* @param oldOwner of the group. * @param tenantId of the group.
* @param tenantId of the group.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner, int tenantId) void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId)
throws GroupManagementDAOException; throws GroupManagementDAOException;
/** /**
* Delete an existing Device Group. * Delete an existing Device Group.
* *
* @param groupName to be deleted. * @param groupId of Device Group.
* @param owner of the group.
* @param tenantId of the group. * @param tenantId of the group.
* @throws GroupManagementDAOException * @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. * Get device group by id.
@ -69,21 +67,10 @@ public interface GroupDAO {
*/ */
DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException; 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 * Get the groups of device with device id provided
* @param deviceId * @param deviceId
* @return * @return groups which has the device.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
List<DeviceGroup> getGroups(int deviceId, int tenantId) throws GroupManagementDAOException; List<DeviceGroup> getGroups(int deviceId, int tenantId) throws GroupManagementDAOException;
@ -123,81 +110,64 @@ public interface GroupDAO {
* Check group already existed with given name. * Check group already existed with given name.
* *
* @param groupName of the Device Group. * @param groupName of the Device Group.
* @param owner of the Device Group.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @return existence of group with name * @return existence of group with name
* @throws GroupManagementDAOException * @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. * Add device to a given Device Group.
* *
* @param groupName of the Device Group. * @param groupId of Device Group.
* @param owner of the Device Group.
* @param deviceId of the device. * @param deviceId of the device.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @throws GroupManagementDAOException * @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. * Remove device from the Device Group.
* *
* @param groupName of the Device Group. * @param groupId of Device Group.
* @param owner of the Device Group.
* @param deviceId of the device. * @param deviceId of the device.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @throws GroupManagementDAOException * @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. * Check device is belonging to a Device Group.
* *
* @param groupName of the Device Group. * @param groupId of Device Group.
* @param owner of the Device Group.
* @param deviceId of the device. * @param deviceId of the device.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
boolean isDeviceMappedToGroup(String groupName, String owner, int deviceId, int tenantId) boolean isDeviceMappedToGroup(int groupId, int deviceId, int tenantId)
throws GroupManagementDAOException; throws GroupManagementDAOException;
/** /**
* Get count of devices in a Device Group. * Get count of devices in a Device Group.
* *
* @param groupName of the Device Group. * @param groupId of Device Group.
* @param owner of the Device Group.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @return device count. * @return device count.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
int getDeviceCount(String groupName, String owner, int tenantId) throws GroupManagementDAOException; int getDeviceCount(int groupId, 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<Device> getDevices(String groupName, String owner, int tenantId) throws GroupManagementDAOException;
/** /**
* Get paginated result of devices of a given tenant and device group. * Get paginated result of devices of a given tenant and device group.
* *
* @param groupName of the group. * @param groupId of Device Group.
* @param owner of the Device Group.
* @param startIndex for pagination. * @param startIndex for pagination.
* @param rowCount for pagination. * @param rowCount for pagination.
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @return list of device in group * @return list of device in group
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
List<Device> getDevices(String groupName, String owner, int startIndex, int rowCount, int tenantId) List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId)
throws GroupManagementDAOException; throws GroupManagementDAOException;
} }

@ -66,21 +66,20 @@ public class GroupDAOImpl implements GroupDAO {
} }
@Override @Override
public void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner, int tenantId) public void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, DATE_OF_LAST_UPDATE = ?, OWNER = ? " 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 = conn.prepareStatement(sql);
stmt.setString(1, deviceGroup.getDescription()); stmt.setString(1, deviceGroup.getDescription());
stmt.setString(2, deviceGroup.getName()); stmt.setString(2, deviceGroup.getName());
stmt.setLong(3, deviceGroup.getDateOfLastUpdate()); stmt.setLong(3, deviceGroup.getDateOfLastUpdate());
stmt.setString(4, deviceGroup.getOwner()); stmt.setString(4, deviceGroup.getOwner());
stmt.setString(5, oldGroupName); stmt.setInt(5, groupId);
stmt.setString(6, oldOwner); stmt.setInt(6, tenantId);
stmt.setInt(7, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" +
@ -91,35 +90,31 @@ public class GroupDAOImpl implements GroupDAO {
} }
@Override @Override
public void deleteGroup(String groupName, String owner, int tenantId) throws GroupManagementDAOException { public void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = GroupManagementDAOFactory.getConnection(); 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 = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setInt(1, groupId);
stmt.setString(2, owner); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while removing mappings for group '" + groupName + throw new GroupManagementDAOException("Error occurred while removing mappings for group.'", e);
"'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, null); GroupManagementDAOUtil.cleanupResources(stmt, null);
} }
try { try {
conn = GroupManagementDAOFactory.getConnection(); 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 = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setInt(1, groupId);
stmt.setString(2, owner); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while deleting group '" + groupName + "'", e); throw new GroupManagementDAOException("Error occurred while deleting group.'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, null); 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 @Override
public List<DeviceGroup> getGroups(int deviceId, int tenantId) throws GroupManagementDAOException { public List<DeviceGroup> getGroups(int deviceId, int tenantId) throws GroupManagementDAOException {
@ -281,114 +249,95 @@ public class GroupDAOImpl implements GroupDAO {
} }
@Override @Override
public boolean isGroupExist(String groupName, String owner, int tenantId) throws GroupManagementDAOException { public boolean isGroupExist(String groupName, int tenantId) throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); 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 = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setString(1, groupName);
stmt.setString(2, owner); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
return resultSet.next(); return resultSet.next();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" + throw new GroupManagementDAOException("Error occurred while group Id listing by group name.'", e);
groupName + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
} }
@Override @Override
public void addDevice(String groupName, String owner, int deviceId, int tenantId) public void addDevice(int groupId, int deviceId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_DEVICE_GROUP_MAP(DEVICE_ID, GROUP_ID, TENANT_ID) " + String sql = "INSERT INTO DM_DEVICE_GROUP_MAP(DEVICE_ID, GROUP_ID, TENANT_ID) VALUES (?, ?, ?)";
"VALUES (?, (SELECT ID as GROUP_ID FROM DM_GROUP " +
"WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?), ?)";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setString(2, groupName); stmt.setInt(2, groupId);
stmt.setString(3, owner); stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
stmt.setInt(5, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
stmt.getGeneratedKeys(); stmt.getGeneratedKeys();
} catch (SQLException e) { } 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 { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, null); GroupManagementDAOUtil.cleanupResources(stmt, null);
} }
} }
@Override @Override
public void removeDevice(String groupName, String owner, int deviceId, int tenantId) public void removeDevice(int groupId, int deviceId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ? AND GROUP_ID = (SELECT ID as GROUP_ID " + String sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE DEVICE_ID = ? AND GROUP_ID = ? AND TENANT_ID = ?";
"FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setString(2, groupName); stmt.setInt(2, groupId);
stmt.setString(3, owner); stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
stmt.setInt(5, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
stmt.getGeneratedKeys(); stmt.getGeneratedKeys();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while removing device from Group '" + throw new GroupManagementDAOException("Error occurred while removing device from Group.", e);
groupName + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, null); GroupManagementDAOUtil.cleanupResources(stmt, null);
} }
} }
@Override @Override
public boolean isDeviceMappedToGroup(String groupName, String owner, int deviceId, int tenantId) public boolean isDeviceMappedToGroup(int groupId, int deviceId, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT dm.ID FROM DM_DEVICE_GROUP_MAP dm, (SELECT ID as GROUP_ID FROM DM_GROUP " + String sql = "SELECT ID FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND DEVICE_ID = ? AND TENANT_ID = ?";
"WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " +
"WHERE dm.GROUP_ID = dg.GROUP_ID AND dm.ID = ? AND dm.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setInt(1, groupId);
stmt.setString(2, owner); stmt.setInt(2, deviceId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, deviceId);
stmt.setInt(5, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
return resultSet.next(); return resultSet.next();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" + throw new GroupManagementDAOException("Error occurred while checking device mapping with group.", e);
groupName + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
} }
@Override @Override
public int getDeviceCount(String groupName, String owner, int tenantId) throws GroupManagementDAOException { public int getDeviceCount(int groupId, int tenantId) throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(gm.ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP gm, (SELECT ID " + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?";
"FROM DM_GROUP WHERE GROUP_NAME = ? AND OWNER = ? AND TENANT_ID = ?) dg " +
"WHERE gm.GROUP_ID = dg.ID AND gm.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setInt(1, groupId);
stmt.setString(2, owner); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
return resultSet.getInt("DEVICE_COUNT"); return resultSet.getInt("DEVICE_COUNT");
@ -396,54 +345,14 @@ public class GroupDAOImpl implements GroupDAO {
return 0; return 0;
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while group Id listing by group name '" + throw new GroupManagementDAOException("Error occurred while getting device count from the group.", e);
groupName + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
} }
@Override @Override
public List<Device> getDevices(String groupName, String owner, int tenantId) throws GroupManagementDAOException { public List<Device> getDevices(int groupId, int startIndex, int rowCount, int tenantId)
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Device> 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<Device> getDevices(String groupName, String owner, int startIndex, int rowCount, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; 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, " + 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, " + "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, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " +
"(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, " + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " +
"t.NAME AS DEVICE_TYPE FROM (SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, " + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (" +
"d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (SELECT dgm.DEVICE_ID " + "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " +
"FROM DM_DEVICE_GROUP_MAP dgm, DM_GROUP dg WHERE dgm.GROUP_ID = dg.ID AND dg.GROUP_NAME = ? " + "WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " +
"AND dg.OWNER = ? AND dg.TENANT_ID = ?) dgm1 WHERE d.ID = dgm1.DEVICE_ID " + "WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?";
"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 = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setInt(1, groupId);
stmt.setString(2, owner); stmt.setInt(2, tenantId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setInt(4, tenantId);
stmt.setInt(5, tenantId);
//noinspection JpaQueryApiInspection //noinspection JpaQueryApiInspection
stmt.setInt(6, startIndex); stmt.setInt(4, startIndex);
//noinspection JpaQueryApiInspection //noinspection JpaQueryApiInspection
stmt.setInt(7, rowCount); stmt.setInt(5, rowCount);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();
while (rs.next()) { while (rs.next()) {

@ -49,44 +49,31 @@ public interface GroupManagementProviderService {
* Update existing device group. * Update existing device group.
* *
* @param deviceGroup to update. * @param deviceGroup to update.
* @param oldGroupName of the group. * @param groupId of the group.
* @param oldOwner of the group.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner) throws GroupManagementException; void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException;
/** /**
* Delete existing device group. * Delete existing device group.
* *
* @param groupName to be deleted. * @param groupId to be deleted.
* @param owner of the group.
* @return status of the delete operation. * @return status of the delete operation.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
boolean deleteGroup(String groupName, String owner) throws GroupManagementException; boolean deleteGroup(int groupId) 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;
/** /**
* Get the device group provided the device group id. * Get the device group provided the device group id.
* *
* @param groupId * @param groupId of the group.
* @return * @return group with details.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
DeviceGroup getGroup(int groupId) 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 startIndex for pagination.
* @param rowCount for pagination. * @param rowCount for pagination.
@ -96,9 +83,9 @@ public interface GroupManagementProviderService {
List<DeviceGroup> getGroups(int startIndex, int rowCount) throws GroupManagementException; List<DeviceGroup> 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 startIndex for pagination.
* @param rowCount for pagination. * @param rowCount for pagination.
* @return list of groups * @return list of groups
@ -127,141 +114,126 @@ public interface GroupManagementProviderService {
* Share device group with user specified by role * Share device group with user specified by role
* *
* @param username of the user * @param username of the user
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @param sharingRole to be shared * @param sharingRole to be shared
* @return is group shared * @return is group shared
* @throws GroupManagementException UserDoesNotExistException * @throws GroupManagementException UserDoesNotExistException
*/ */
boolean shareGroup(String username, String groupName, String owner, String sharingRole) boolean shareGroup(String username, int groupId, String sharingRole)
throws GroupManagementException, UserDoesNotExistException; throws GroupManagementException, UserDoesNotExistException;
/** /**
* Un share existing group sharing with user specified by role * Un share existing group sharing with user specified by role
* *
* @param userName of the user * @param userName of the user
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @param sharingRole to be un shared * @param sharingRole to be un shared
* @return is group un shared * @return is group un shared
* @throws GroupManagementException UserDoesNotExistException * @throws GroupManagementException UserDoesNotExistException
*/ */
boolean unshareGroup(String userName, String groupName, String owner, String sharingRole) boolean unshareGroup(String userName, int groupId, String sharingRole)
throws GroupManagementException, UserDoesNotExistException; throws GroupManagementException, UserDoesNotExistException;
/** /**
* Add new sharing role for device group * Add new sharing role for device group
* *
* @param userName of the user * @param userName of the user
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @param roleName to add * @param roleName to add
* @param permissions to bind with role * @param permissions to bind with role
* @return is role added * @return is role added
* @throws GroupManagementException * @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; throws GroupManagementException;
/** /**
* Remove existing sharing role for device group * Remove existing sharing role for device group
* *
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @param roleName to remove * @param roleName to remove
* @return is role removed * @return is role removed
* @throws GroupManagementException * @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 * Get all sharing roles for device group
* *
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @return list of roles * @return list of roles
* @throws GroupManagementException * @throws GroupManagementException
*/ */
List<String> getRoles(String groupName, String owner) throws GroupManagementException; List<String> getRoles(int groupId) throws GroupManagementException;
/** /**
* Get specific device group sharing roles for user * Get specific device group sharing roles for user
* *
* @param userName of the user * @param userName of the user
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @return list of roles * @return list of roles
* @throws GroupManagementException UserDoesNotExistException * @throws GroupManagementException UserDoesNotExistException
*/ */
List<String> getRoles(String userName, String groupName, String owner) List<String> getRoles(String userName, int groupId) throws GroupManagementException, UserDoesNotExistException;
throws GroupManagementException, UserDoesNotExistException;
/** /**
* Get device group users * Get device group users
* *
* @param groupName of the group * @param groupId of the group
* @param owner of the group
* @return list of group users * @return list of group users
* @throws GroupManagementException * @throws GroupManagementException
*/ */
List<GroupUser> getUsers(String groupName, String owner) throws GroupManagementException; List<GroupUser> getUsers(int groupId) throws GroupManagementException;
/** /**
* Get all devices in device group as paginated result. * Get all devices in device group as paginated result.
* *
* @param groupName of the group. * @param groupId of the group
* @param owner of the group.
* @param startIndex for pagination. * @param startIndex for pagination.
* @param rowCount for pagination. * @param rowCount for pagination.
* @return list of devices in group. * @return list of devices in group.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
List<Device> getDevices(String groupName, String owner, int startIndex, int rowCount) List<Device> getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException;
throws GroupManagementException;
/** /**
* This method is used to retrieve the device count of a given group. * This method is used to retrieve the device count of a given group.
* *
* @param groupName of the group. * @param groupId of the group
* @param owner of the group.
* @return returns the device count. * @return returns the device count.
* @throws GroupManagementException * @throws GroupManagementException
*/ */
int getDeviceCount(String groupName, String owner) throws GroupManagementException; int getDeviceCount(int groupId) throws GroupManagementException;
/** /**
* Add device to device group. * Add device to device group.
* *
* @param deviceId of the device. * @param deviceId of the device.
* @param groupName of the group. * @param groupId of the group
* @param owner of the group.
* @return is device added. * @return is device added.
* @throws GroupManagementException * @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. * Remove device from device group.
* *
* @param deviceId of the device. * @param deviceId of the device.
* @param groupName of the group. * @param groupId of the group
* @param owner of the group.
* @return is device removed. * @return is device removed.
* @throws GroupManagementException * @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. * Get device group permissions of user.
* *
* @param username of the user. * @param username of the user.
* @param groupName of the group. * @param groupId of the group
* @param owner of the group.
* @return array of permissions. * @return array of permissions.
* @throws GroupManagementException UserDoesNotExistException * @throws GroupManagementException UserDoesNotExistException
*/ */
String[] getPermissions(String username, String groupName, String owner) String[] getPermissions(String username, int groupId) throws GroupManagementException, UserDoesNotExistException;
throws GroupManagementException, UserDoesNotExistException;
/** /**
* Get device groups of user with permission. * Get device groups of user with permission.

@ -23,7 +23,10 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; 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.GroupManagementException;
@ -40,7 +43,11 @@ import org.wso2.carbon.user.core.multiplecredentials.UserDoesNotExistException;
import org.wso2.carbon.user.core.util.UserCoreUtil; import org.wso2.carbon.user.core.util.UserCoreUtil;
import java.sql.SQLException; 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 { public class GroupManagementProviderServiceImpl implements GroupManagementProviderService {
@ -68,7 +75,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
int groupId = -1; int groupId = -1;
try { try {
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
boolean nameIsExists = this.groupDAO.isGroupExist(deviceGroup.getName(), deviceGroup.getOwner(), tenantId); boolean nameIsExists = this.groupDAO.isGroupExist(deviceGroup.getName(), tenantId);
if (!nameIsExists) { if (!nameIsExists) {
groupId = this.groupDAO.addGroup(deviceGroup, tenantId); groupId = this.groupDAO.addGroup(deviceGroup, tenantId);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
@ -95,7 +102,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void updateGroup(DeviceGroup deviceGroup, String oldGroupName, String oldOwner) public void updateGroup(DeviceGroup deviceGroup, int groupId)
throws GroupManagementException { throws GroupManagementException {
if (deviceGroup == null) { if (deviceGroup == null) {
throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException());
@ -104,7 +111,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
deviceGroup.setDateOfLastUpdate(new Date().getTime()); deviceGroup.setDateOfLastUpdate(new Date().getTime());
this.groupDAO.updateGroup(deviceGroup, oldGroupName, oldOwner, tenantId); this.groupDAO.updateGroup(deviceGroup, groupId, tenantId);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
@ -121,13 +128,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean deleteGroup(String groupName, String owner) throws GroupManagementException { public boolean deleteGroup(int groupId) throws GroupManagementException {
String roleName; String roleName;
DeviceGroup deviceGroup = buildDeviceGroup(groupName, owner); DeviceGroup deviceGroup = buildDeviceGroup(groupId);
if (deviceGroup == null) { if (deviceGroup == null) {
return false; return false;
} }
List<String> groupRoles = getRoles(groupName, owner); List<String> groupRoles = getRoles(groupId);
for (String role : groupRoles) { for (String role : groupRoles) {
if (role != null) { if (role != null) {
roleName = role.replace("Internal/group-" + deviceGroup.getGroupId() + "-", ""); roleName = role.replace("Internal/group-" + deviceGroup.getGroupId() + "-", "");
@ -136,7 +143,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
try { try {
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
this.groupDAO.deleteGroup(groupName, owner, CarbonContext.getThreadLocalCarbonContext().getTenantId()); this.groupDAO.deleteGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("DeviceGroup " + deviceGroup.getName() + " removed."); log.debug("DeviceGroup " + deviceGroup.getName() + " removed.");
@ -144,8 +151,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return true; return true;
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while removing group " + throw new GroupManagementException("Error occurred while removing group data.", e);
"'" + groupName + "' data.", e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); throw new GroupManagementException("Error occurred while initiating transaction.", e);
} finally { } 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 { private DeviceGroup buildDeviceGroup(int groupId) throws GroupManagementException {
DeviceGroup deviceGroup; DeviceGroup deviceGroup;
try { try {
@ -306,9 +283,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean shareGroup(String username, String groupName, String owner, String sharingRole) public boolean shareGroup(String username, int groupId, String sharingRole)
throws GroupManagementException, UserDoesNotExistException { throws GroupManagementException, UserDoesNotExistException {
int groupId = getGroupId(groupName, owner);
return modifyGroupShare(username, groupId, sharingRole, true); return modifyGroupShare(username, groupId, sharingRole, true);
} }
@ -316,9 +292,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean unshareGroup(String username, String groupName, String owner, String sharingRole) public boolean unshareGroup(String username, int groupId, String sharingRole)
throws GroupManagementException, UserDoesNotExistException { throws GroupManagementException, UserDoesNotExistException {
int groupId = getGroupId(groupName, owner);
return modifyGroupShare(username, groupId, sharingRole, false); 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} * {@inheritDoc}
*/ */
@Override @Override
public boolean addGroupSharingRole(String userName, String groupName, String owner, String roleName, public boolean addGroupSharingRole(String username, int groupId, String roleName, String[] permissions)
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)
throws GroupManagementException { throws GroupManagementException {
if (groupId == -1) { if (groupId == -1) {
return false; return false;
@ -404,14 +364,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean removeGroupSharingRole(String groupName, String owner, String roleName) public boolean removeGroupSharingRole(int groupId, String roleName) throws GroupManagementException {
throws GroupManagementException {
int groupId = getGroupId(groupName, owner);
return removeGroupSharingRole(groupId, roleName);
}
private boolean removeGroupSharingRole(int groupId, String roleName)
throws GroupManagementException {
if (groupId == -1) { if (groupId == -1) {
return false; return false;
} }
@ -435,12 +388,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<String> getRoles(String groupName, String owner) throws GroupManagementException { public List<String> getRoles(int groupId) throws GroupManagementException {
int groupId = getGroupId(groupName, owner);
return getRoles(groupId);
}
private List<String> getRoles(int groupId) throws GroupManagementException {
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
String[] roles; String[] roles;
List<String> groupRoles; List<String> groupRoles;
@ -467,13 +415,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<String> getRoles(String username, String groupName, String owner) public List<String> getRoles(String username, int groupId)
throws GroupManagementException, UserDoesNotExistException {
int groupId = getGroupId(groupName, owner);
return getRoles(username, groupId);
}
private List<String> getRoles(String username, int groupId)
throws GroupManagementException, UserDoesNotExistException { throws GroupManagementException, UserDoesNotExistException {
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
List<String> groupRoleList = new ArrayList<>(); List<String> groupRoleList = new ArrayList<>();
@ -504,11 +446,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<GroupUser> getUsers(String groupName, String owner) throws GroupManagementException {
int groupId = getGroupId(groupName, owner);
return getUsers(groupId);
}
public List<GroupUser> getUsers(int groupId) throws GroupManagementException { public List<GroupUser> getUsers(int groupId) throws GroupManagementException {
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
Map<String, GroupUser> groupUserHashMap = new HashMap<>(); Map<String, GroupUser> groupUserHashMap = new HashMap<>();
@ -545,13 +482,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public List<Device> getDevices(String groupName, String owner, int startIndex, int rowCount) public List<Device> getDevices(int groupId, int startIndex, int rowCount)
throws GroupManagementException { throws GroupManagementException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
List<Device> devices; List<Device> devices;
try { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
devices = this.groupDAO.getDevices(groupName, owner, startIndex, rowCount, tenantId); devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while getting devices in group.", e); throw new GroupManagementException("Error occurred while getting devices in group.", e);
} catch (SQLException e) { } catch (SQLException e) {
@ -566,13 +503,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public int getDeviceCount(String groupName, String owner) throws GroupManagementException { public int getDeviceCount(int groupId) throws GroupManagementException {
try { try {
int count;
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
count = groupDAO.getDeviceCount(groupName, owner, return groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
CarbonContext.getThreadLocalCarbonContext().getTenantId());
return count;
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e);
} catch (SQLException e) { } catch (SQLException e) {
@ -586,7 +520,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean addDevice(DeviceIdentifier deviceIdentifier, String groupName, String owner) public boolean addDevice(DeviceIdentifier deviceIdentifier, int groupId)
throws GroupManagementException { throws GroupManagementException {
Device device; Device device;
try { try {
@ -596,13 +530,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
this.groupDAO.addDevice(groupName, owner, device.getId(), tenantId); this.groupDAO.addDevice(groupId, device.getId(), tenantId);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", e); throw new GroupManagementException("Error occurred while retrieving device.", e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); 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) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); throw new GroupManagementException("Error occurred while initiating transaction.", e);
} finally { } finally {
@ -615,8 +549,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public boolean removeDevice(DeviceIdentifier deviceIdentifier, String groupName, String owner) public boolean removeDevice(DeviceIdentifier deviceIdentifier, int groupId) throws GroupManagementException {
throws GroupManagementException {
Device device; Device device;
try { try {
device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier); device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier);
@ -625,7 +558,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
this.groupDAO.removeDevice(groupName, owner, device.getId(), tenantId); this.groupDAO.removeDevice(groupId, device.getId(), tenantId);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", 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); throw new GroupManagementException("Error occurred while initiating transaction.", e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); 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 { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -644,10 +577,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public String[] getPermissions(String username, String groupName, String owner) public String[] getPermissions(String username, int groupId)
throws GroupManagementException, UserDoesNotExistException { throws GroupManagementException, UserDoesNotExistException {
UserRealm userRealm; UserRealm userRealm;
int groupId = getGroupId(groupName, owner);
List<String> roles = getRoles(username, groupId); List<String> roles = getRoles(username, groupId);
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {

@ -56,7 +56,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
groupId = groupDAO.addGroup(deviceGroup, TestDataHolder.SUPER_TENANT_ID); groupId = groupDAO.addGroup(deviceGroup, TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
log.debug("Group added to database."); log.debug("Group added to database. ID: " + groupId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while adding device type '" + deviceGroup.getName() + "'."; String msg = "Error occurred while adding device type '" + deviceGroup.getName() + "'.";
@ -123,8 +123,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
DeviceGroup deviceGroup = getGroupById(groupId); DeviceGroup deviceGroup = getGroupById(groupId);
try { try {
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
groupDAO.addDevice(deviceGroup.getName(), deviceGroup.getOwner(), initialTestDevice.getId(), groupDAO.addDevice(deviceGroup.getGroupId(), initialTestDevice.getId(), TestDataHolder.SUPER_TENANT_ID);
TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
log.debug("Device added to group."); log.debug("Device added to group.");
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
@ -142,8 +141,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
try { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
List<Device> groupedDevices = groupDAO.getDevices(deviceGroup.getName(), deviceGroup.getOwner(), List<Device> groupedDevices = groupDAO.getDevices(deviceGroup.getGroupId(), 0, 10, TestDataHolder.SUPER_TENANT_ID);
TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotEquals(groupedDevices.size(), 0, "No device found"); Assert.assertNotEquals(groupedDevices.size(), 0, "No device found");
Assert.assertNotNull(groupedDevices.get(0), "Device is null"); Assert.assertNotNull(groupedDevices.get(0), "Device is null");
Assert.assertEquals(groupedDevices.get(0).getId(), initialTestDevice.getId(), "Device ids not matched"); 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); DeviceGroup deviceGroup = getGroupById(groupId);
try { try {
GroupManagementDAOFactory.beginTransaction(); 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(); GroupManagementDAOFactory.commitTransaction();
log.debug("Device added to group."); log.debug("Device added to group.");
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
@ -195,8 +193,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
group.setDescription(desc); group.setDescription(desc);
try { try {
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
groupDAO.updateGroup(group, TestDataHolder.generateDummyGroupData().getName(), groupDAO.updateGroup(group, groupId, TestDataHolder.SUPER_TENANT_ID);
TestDataHolder.generateDummyGroupData().getOwner(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
log.debug("Group updated"); log.debug("Group updated");
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
@ -225,7 +222,7 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
try { try {
Assert.assertNotNull(group, "Group is null"); Assert.assertNotNull(group, "Group is null");
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.beginTransaction();
groupDAO.deleteGroup(group.getName(), group.getOwner(), TestDataHolder.SUPER_TENANT_ID); groupDAO.deleteGroup(group.getGroupId(), TestDataHolder.SUPER_TENANT_ID);
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
log.debug("Group deleted"); log.debug("Group deleted");
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {

Loading…
Cancel
Save