Merge pull request 'Add hierarchical group count API' (#214) from ThilinaPremachandra/device-mgt-core:Bug#10300 into master

Reviewed-on: community/device-mgt-core#214
billing-delete-9713
Inosh Perara 1 year ago
commit 28b18941aa

@ -355,6 +355,52 @@ public interface GroupManagementService {
@QueryParam("limit")
int limit);
@Path("/hierarchy/count")
@GET
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Getting the Number of Hirarchical Device Groups",
notes = "Get the number of hierarchical device groups in the server that the current signed in user can access.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:count")
})
},
nickname = "getGroupCountNonAdmin"
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the hierarchical device group count.",
response = DeviceGroupList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the group count.",
response = ErrorResponse.class)
})
Response getHierarchicalGroupCount();
@Path("/count")
@GET
@ApiOperation(

@ -256,6 +256,58 @@ public interface GroupManagementAdminService {
@QueryParam("limit")
int limit);
@Path("/hierarchy/count")
@GET
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Get the count of all hierarchical groups belongs to current admin user.",
notes = "Returns count of all permitted hierarchical groups enrolled with the system.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:admin-groups:count")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the hierarchical device group count.",
response = Integer.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 404,
message = "No groups found.",
response = ErrorResponse.class),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the group count.",
response = ErrorResponse.class)
})
Response getHierarchicalGroupCount(@ApiParam(
name = "status",
value = "status of hierarchical groups of which count should be retrieved")
@QueryParam("status")
String status);
@Path("/count")
@GET
@ApiOperation(

@ -140,6 +140,19 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}
}
@Override
public Response getHierarchicalGroupCount() {
try {
String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getHierarchicalGroupCount(currentUser, null);
return Response.status(Response.Status.OK).entity(count).build();
} catch (GroupManagementException e) {
String msg = "Error occurred while retrieving hierarchical group count.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@Override
public Response getGroupCount() {
try {

@ -133,6 +133,23 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
}
}
@Override
public Response getHierarchicalGroupCount(String status) {
try {
int count;
if (status == null || status.isEmpty()) {
count = DeviceMgtAPIUtils.getGroupManagementProviderService().getHierarchicalGroupCount();
} else {
count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCountByStatus(status);
}
return Response.status(Response.Status.OK).entity(count).build();
} catch (GroupManagementException e) {
String msg = "ErrorResponse occurred while retrieving hierarchical group count.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@Override
public Response getGroupCount(String status) {
try {

@ -189,6 +189,14 @@ public interface GroupManagementProviderService {
PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request,
boolean requireGroupProps) throws GroupManagementException;
/**
* Get all hierarchical device groups count in tenant
*
* @return hierarchical group count
* @throws GroupManagementException
*/
int getHierarchicalGroupCount() throws GroupManagementException;
/**
* Get all device group count in tenant
*
@ -215,6 +223,16 @@ public interface GroupManagementProviderService {
*/
int getGroupCount(String username, String parentPath) throws GroupManagementException;
/**
* Get hierarchical device groups count of user
*
* @param username of the user
* @param parentPath of the group
* @return hierarchical group count
* @throws GroupManagementException
*/
int getHierarchicalGroupCount(String username, String parentPath) throws GroupManagementException;
/**
* Manage device group sharing with user with list of roles.
*

@ -854,6 +854,28 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return groupResult;
}
@Override
public int getHierarchicalGroupCount() throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups count");
}
try {
GroupManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
return groupDAO.getGroupCount(tenantId, null);
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
@Override
public int getGroupCount() throws GroupManagementException {
if (log.isDebugEnabled()) {
@ -962,6 +984,52 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
}
/**
* {@inheritDoc}
*/
@Override
public int getHierarchicalGroupCount(String username, String parentPath) throws GroupManagementException {
if (username == null || username.isEmpty()) {
String msg = "Received empty user name for getHierarchicalGroupCount";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups count of '" + username + "'");
}
UserStoreManager userStoreManager;
int count;
try {
GroupManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)
.getUserStoreManager();
if (isAdminUser(username, userStoreManager)) {
count = groupDAO.getGroupCount(tenantId, null);
return count;
} else {
String[] roleList = userStoreManager.getRoleListOfUser(username);
count = groupDAO.getOwnGroupsCount(username, tenantId, parentPath);
count += groupDAO.getGroupsCount(roleList, tenantId, parentPath);
return count;
}
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving role list of user '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening db connection to get group count.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) {
String msg = "Error occurred while retrieving group count of user '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
/**
* {@inheritDoc}
*/

Loading…
Cancel
Save