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 e1639e6262..fe639e63e1 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 @@ -212,6 +212,19 @@ public interface GroupDAO { */ List getGroups(GroupPaginationRequest paginationRequest, int tenantId) throws GroupManagementDAOException; + /** + * Get paginated list of Device Groups in tenant with specified device group ids. + * + * @param paginationRequest to filter results. + * @param deviceGroupIds of groups required. + * @param tenantId of user's tenant. + * @param isWithParentPath of user's ParentPath. + * @return List of all Device Groups in tenant. + * @throws GroupManagementDAOException + */ + List getGroups(GroupPaginationRequest paginationRequest, List deviceGroupIds, + int tenantId, boolean isWithParentPath) throws GroupManagementDAOException; + /** * Get paginated list of Device Groups in tenant with specified device group ids. * 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 937b975259..480bc735b0 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 @@ -108,7 +108,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { @Override public List getGroups(GroupPaginationRequest request, List deviceGroupIds, - int tenantId) throws GroupManagementDAOException { + int tenantId) throws GroupManagementDAOException { int deviceGroupIdsCount = deviceGroupIds.size(); if (deviceGroupIdsCount == 0) { return new ArrayList<>(); @@ -169,6 +169,73 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { throw new GroupManagementDAOException(msg, e); } } + @Override + public List getGroups(GroupPaginationRequest request, List deviceGroupIds, + int tenantId, boolean isWithParentPath) 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 = ?"; + if (StringUtils.isNotBlank(request.getGroupName())) { + sql += " AND GROUP_NAME LIKE ?"; + } + if (StringUtils.isNotBlank(request.getOwner())) { + sql += " AND OWNER LIKE ?"; + } + if (StringUtils.isNotBlank(request.getParentPath())) { + if(isWithParentPath){ + sql += " AND PARENT_PATH LIKE ?"; + } + } + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + if (request.getRowCount() != 0) { + sql += " LIMIT ? OFFSET ?"; + } + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, tenantId); + if (StringUtils.isNotBlank(request.getGroupName())) { + stmt.setString(paramIndex++, request.getGroupName() + "%"); + } + if (StringUtils.isNotBlank(request.getOwner())) { + stmt.setString(paramIndex++, request.getOwner() + "%"); + } + if (StringUtils.isNotBlank(request.getParentPath())) { + if(isWithParentPath){ + stmt.setString(paramIndex++, request.getParentPath()); + } + } + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + if (request.getRowCount() != 0) { + stmt.setInt(paramIndex++, request.getRowCount()); + stmt.setInt(paramIndex, request.getStartIndex()); + } + 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.toString() + + " in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } @Override public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException { @@ -376,7 +443,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ?, " - + "PARENT_PATH = ? WHERE ID = ? AND TENANT_ID = ?"; + + "PARENT_PATH = ?, PARENT_GROUP_ID = ? WHERE ID = ? AND TENANT_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)){ for (DeviceGroup deviceGroup : deviceGroups) { stmt.setString(1, deviceGroup.getDescription()); @@ -384,8 +451,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(3, deviceGroup.getOwner()); stmt.setString(4, deviceGroup.getStatus()); stmt.setString(5, deviceGroup.getParentPath()); - stmt.setInt(6, deviceGroup.getGroupId()); - stmt.setInt(7, tenantId); + stmt.setInt(6, deviceGroup.getParentGroupId()); + stmt.setInt(7, deviceGroup.getGroupId()); + stmt.setInt(8, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -1201,7 +1269,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } - @Override + @Override public List getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException { Connection conn; List devices; 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 be1ad0d545..3b83d49c01 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 @@ -312,6 +312,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid newParentPath = DeviceGroupConstants.HierarchicalGroup.SEPERATOR; } childrenGroup.setParentPath(newParentPath); + if (!newParentPath.equals(DeviceGroupConstants.HierarchicalGroup.SEPERATOR)) { + String[] groupIds = newParentPath.split(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); + int latestGroupId = Integer.parseInt(groupIds[groupIds.length - 1]); + childrenGroup.setParentGroupId(latestGroupId); + } else { + childrenGroup.setParentGroupId(0); + } } } } @@ -518,7 +525,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request, - boolean requireGroupProps) throws GroupManagementException { + boolean requireGroupProps) throws GroupManagementException { if (request == null) { String msg = "Received incomplete data for retrieve groups with hierarchy"; log.error(msg); @@ -527,6 +534,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("Get groups with hierarchy " + request.toString()); } + boolean isWithParentPath = false; DeviceManagerUtil.validateGroupListPageSize(request); List rootGroups; try { @@ -538,7 +546,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } else { List allDeviceGroupIdsOfUser = getGroupIds(username); GroupManagementDAOFactory.openConnection(); - rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); + rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId, isWithParentPath); } String parentPath; List childrenGroups; @@ -1359,7 +1367,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * @throws GroupManagementDAOException on error during population of group properties. */ private void createGroupWithChildren(DeviceGroup parentGroup, List childrenGroups, - boolean requireGroupProps, int tenantId, int depth, int counter) throws GroupManagementDAOException { + boolean requireGroupProps, int tenantId, int depth, int counter) throws GroupManagementDAOException { if (childrenGroups.isEmpty() || depth == counter) { return; }