diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index e339437cb75..b3c4321df33 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -246,6 +246,15 @@ public interface GroupDAO { List getGroups(GroupPaginationRequest paginationRequest, List deviceGroupIds, int tenantId) throws GroupManagementDAOException; + /** + * Get the list of Device Groups in tenant. + * + * @param tenantId of user's tenant. + * @return List of all Device Groups in tenant. + * @throws GroupManagementDAOException + */ + List getGroups(List deviceGroupIds, int tenantId) throws GroupManagementDAOException; + /** * Get the list of Device Groups in tenant. * diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 1dddaa093c2..f7d8e533ea7 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -169,6 +169,46 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { throw new GroupManagementDAOException(msg, e); } } + + @Override + public List getGroups(List deviceGroupIds, int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP WHERE TENANT_ID = ?"; + + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, tenantId); + + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + List deviceGroupList = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } + return deviceGroupList; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds + + " in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + + @Override public List getGroups(GroupPaginationRequest request, List deviceGroupIds, int tenantId, boolean isWithParentPath) throws GroupManagementDAOException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index ee489dedc36..d5ef09b14e9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -57,11 +57,7 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @@ -564,33 +560,38 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid throw new GroupManagementException(msg); } if (log.isDebugEnabled()) { - log.debug("Get groups with hierarchy " + request.toString()); + log.debug("Get groups with hierarchy " + request); } - boolean isWithParentPath = false; DeviceManagerUtil.validateGroupListPageSize(request); List rootGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); + String parentPath; + List childrenGroups; if (StringUtils.isBlank(username)) { GroupManagementDAOFactory.openConnection(); rootGroups = groupDAO.getGroups(request, tenantId); + for (DeviceGroup rootGroup : rootGroups) { + parentPath = DeviceManagerUtil.createParentPath(rootGroup); + childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); + createGroupWithChildren( + rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0); + if (requireGroupProps) { + populateGroupProperties(rootGroup, tenantId); + } + } } else { List allDeviceGroupIdsOfUser = getGroupIds(username); GroupManagementDAOFactory.openConnection(); - rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId, isWithParentPath); - } - String parentPath; - List childrenGroups; - for (DeviceGroup rootGroup : rootGroups) { - parentPath = DeviceManagerUtil.createParentPath(rootGroup); - childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); - createGroupWithChildren( - rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0); + rootGroups = this.getGroups(allDeviceGroupIdsOfUser, tenantId); if (requireGroupProps) { - populateGroupProperties(rootGroup, tenantId); + for (DeviceGroup rootGroup : rootGroups) { + populateGroupProperties(rootGroup, tenantId); + } } } + } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups with hierarchy"; log.error(msg, e); @@ -613,6 +614,49 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupResult; } + private List getGroups(List groupIds, int tenantId) throws GroupManagementException { + try { + Listgroups = groupDAO.getGroups(groupIds, tenantId); + if (groups == null) { + String msg = "Retrieved null when getting groups for group ids " + groupIds.toString(); + log.error(msg); + throw new GroupManagementException(msg); + } + if (groups.isEmpty()) return groups; + groups.sort(Comparator.comparing(DeviceGroup::getGroupId)); + return getTree(groups); + } catch (GroupManagementDAOException ex) { + String msg = "Error occurred while getting groups for group ids " + groupIds.toString(); + log.error(msg, ex); + throw new GroupManagementException(msg, ex); + } + } + + private List getTree(List groups) { + List tree = new ArrayList<>(); + for (DeviceGroup deviceGroup : groups) { + DeviceGroup treeNode = tree.stream(). + filter(node -> deviceGroup.getParentPath(). + contains(Integer.toString(node.getGroupId()))). + findFirst().orElse(null); + if (treeNode != null) { + if (Objects.equals(treeNode.getParentPath(), deviceGroup.getParentPath())) { + tree.add(deviceGroup); + } else { + List tempGroups = treeNode.getChildrenGroups(); + if (tempGroups == null) { + tempGroups = new ArrayList<>(); + } + tempGroups.add(deviceGroup); + treeNode.setChildrenGroups(getTree(tempGroups)); + } + } else { + tree.add(deviceGroup); + } + } + return tree; + } + @Override public List getGroups(String username, boolean requireGroupProps) throws GroupManagementException { if (username == null || username.isEmpty()) {