HierarchicalGrouping (#153)

Goals
Complete hierarchical grouping task
https://roadmap.entgra.net/issues/9528 and https://roadmap.entgra.net/issues/9529

Approach
Fix the encountered issues

Co-authored-by: ThilinaPremachandra <thilina@entgra.io>
Co-authored-by: Pahansith Gunathilake <pahansith@entgra.io>
Reviewed-on: community/device-mgt-core#153
Co-authored-by: Thilina Sandaruwan <thilina@entgra.io>
Co-committed-by: Thilina Sandaruwan <thilina@entgra.io>
master
Thilina Sandaruwan 1 year ago committed by pasindu
parent 729f1fd85f
commit 0ce7cd954c

@ -211,6 +211,19 @@ public interface GroupDAO {
*/ */
List<DeviceGroup> getGroups(GroupPaginationRequest paginationRequest, int tenantId) throws GroupManagementDAOException; List<DeviceGroup> 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<DeviceGroup> getGroups(GroupPaginationRequest paginationRequest, List<Integer> deviceGroupIds,
int tenantId, boolean isWithParentPath) throws GroupManagementDAOException;
/** /**
* Get paginated list of Device Groups in tenant with specified device group ids. * Get paginated list of Device Groups in tenant with specified device group ids.
* *

@ -104,7 +104,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
@Override @Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds, public List<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds,
int tenantId) throws GroupManagementDAOException { int tenantId) throws GroupManagementDAOException {
int deviceGroupIdsCount = deviceGroupIds.size(); int deviceGroupIdsCount = deviceGroupIds.size();
if (deviceGroupIdsCount == 0) { if (deviceGroupIdsCount == 0) {
return new ArrayList<>(); return new ArrayList<>();
@ -165,6 +165,73 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
throw new GroupManagementDAOException(msg, e); throw new GroupManagementDAOException(msg, e);
} }
} }
@Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> 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<DeviceGroup> 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 @Override
public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException { public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException {
@ -373,7 +440,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ?, " 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)){ try (PreparedStatement stmt = conn.prepareStatement(sql)){
for (DeviceGroup deviceGroup : deviceGroups) { for (DeviceGroup deviceGroup : deviceGroups) {
stmt.setString(1, deviceGroup.getDescription()); stmt.setString(1, deviceGroup.getDescription());
@ -381,8 +448,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(3, deviceGroup.getOwner()); stmt.setString(3, deviceGroup.getOwner());
stmt.setString(4, deviceGroup.getStatus()); stmt.setString(4, deviceGroup.getStatus());
stmt.setString(5, deviceGroup.getParentPath()); stmt.setString(5, deviceGroup.getParentPath());
stmt.setInt(6, deviceGroup.getGroupId()); stmt.setInt(6, deviceGroup.getParentGroupId());
stmt.setInt(7, tenantId); stmt.setInt(7, deviceGroup.getGroupId());
stmt.setInt(8, tenantId);
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
@ -1198,7 +1266,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
} }
@Override @Override
public List<Device> getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException { public List<Device> getAllDevicesOfGroup(String groupName, int tenantId) throws GroupManagementDAOException {
Connection conn; Connection conn;
List<Device> devices; List<Device> devices;

@ -312,6 +312,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
newParentPath = DeviceGroupConstants.HierarchicalGroup.SEPERATOR; newParentPath = DeviceGroupConstants.HierarchicalGroup.SEPERATOR;
} }
childrenGroup.setParentPath(newParentPath); 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 @Override
public PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request, public PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request,
boolean requireGroupProps) throws GroupManagementException { boolean requireGroupProps) throws GroupManagementException {
if (request == null) { if (request == null) {
String msg = "Received incomplete data for retrieve groups with hierarchy"; String msg = "Received incomplete data for retrieve groups with hierarchy";
log.error(msg); log.error(msg);
@ -527,6 +534,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get groups with hierarchy " + request.toString()); log.debug("Get groups with hierarchy " + request.toString());
} }
boolean isWithParentPath = false;
DeviceManagerUtil.validateGroupListPageSize(request); DeviceManagerUtil.validateGroupListPageSize(request);
List<DeviceGroup> rootGroups; List<DeviceGroup> rootGroups;
try { try {
@ -538,7 +546,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} else { } else {
List<Integer> allDeviceGroupIdsOfUser = getGroupIds(username); List<Integer> allDeviceGroupIdsOfUser = getGroupIds(username);
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId, isWithParentPath);
} }
String parentPath; String parentPath;
List<DeviceGroup> childrenGroups; List<DeviceGroup> childrenGroups;
@ -1359,7 +1367,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
* @throws GroupManagementDAOException on error during population of group properties. * @throws GroupManagementDAOException on error during population of group properties.
*/ */
private void createGroupWithChildren(DeviceGroup parentGroup, List<DeviceGroup> childrenGroups, private void createGroupWithChildren(DeviceGroup parentGroup, List<DeviceGroup> 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) { if (childrenGroups.isEmpty() || depth == counter) {
return; return;
} }

Loading…
Cancel
Save