|
|
@ -58,27 +58,27 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice);
|
|
|
|
boolean parentAdded = false; // Flag to track whether the parent device has been added
|
|
|
|
if (!includeDevice) {
|
|
|
|
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice, parentAdded);
|
|
|
|
childNodes.add(node);
|
|
|
|
if (!includeDevice && !parentAdded) {
|
|
|
|
|
|
|
|
childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return childNodes;
|
|
|
|
return childNodes;
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " +
|
|
|
|
String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " +
|
|
|
|
"parent device ID " +
|
|
|
|
"parent device ID " + node.getDeviceId();
|
|
|
|
node.getDeviceId();
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
throw new DeviceOrganizationMgtDAOException(msg, e);
|
|
|
|
throw new DeviceOrganizationMgtDAOException(msg, e);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException e) {
|
|
|
|
String msg = "Error occurred while processing SQL to retrieve all child devices for " +
|
|
|
|
String msg = "Error occurred while processing SQL to retrieve all child devices for " +
|
|
|
|
"parent device ID" + node.getDeviceId();
|
|
|
|
"parent device ID " + node.getDeviceId();
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
throw new DeviceOrganizationMgtDAOException(msg, e);
|
|
|
|
throw new DeviceOrganizationMgtDAOException(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void getChildrenRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
|
|
|
|
private void getChildrenRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
|
|
|
|
List<DeviceNode> childNodes, boolean includeDevice) throws SQLException {
|
|
|
|
List<DeviceNode> childNodes, boolean includeDevice, boolean parentAdded) throws SQLException {
|
|
|
|
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
|
|
|
|
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -98,11 +98,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
while (rs.next()) {
|
|
|
|
while (rs.next()) {
|
|
|
|
DeviceNode child = getDeviceFromResultSet(rs);
|
|
|
|
DeviceNode child = getDeviceFromResultSet(rs);
|
|
|
|
node.getChildren().add(child);
|
|
|
|
node.getChildren().add(child);
|
|
|
|
if (includeDevice) {
|
|
|
|
if (includeDevice && !parentAdded) {
|
|
|
|
childNodes.add(node); // Add the parent device if includeDevice is true.
|
|
|
|
childNodes.add(node); // Add the parent device only if includeDevice is true and it hasn't been added.
|
|
|
|
|
|
|
|
parentAdded = true; // Set the flag to true after adding the parent device.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice);
|
|
|
|
getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice, parentAdded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -118,10 +119,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
Set<Integer> visited = new HashSet<>();
|
|
|
|
Set<Integer> visited = new HashSet<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice);
|
|
|
|
if (includeDevice) {
|
|
|
|
if (!includeDevice) {
|
|
|
|
parentNodes.add(node); // Add the current node to the parent nodes list when includeDevice is true
|
|
|
|
parentNodes.add(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice);
|
|
|
|
return parentNodes;
|
|
|
|
return parentNodes;
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " +
|
|
|
|
String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " +
|
|
|
@ -136,7 +137,6 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void getParentsRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
|
|
|
|
private void getParentsRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
|
|
|
|
List<DeviceNode> parentNodes, boolean includeDevice) throws SQLException {
|
|
|
|
List<DeviceNode> parentNodes, boolean includeDevice) throws SQLException {
|
|
|
|
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
|
|
|
|
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
|
|
|
@ -157,15 +157,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
|
|
try (ResultSet rs = stmt.executeQuery()) {
|
|
|
|
while (rs.next()) {
|
|
|
|
while (rs.next()) {
|
|
|
|
DeviceNode parent = getDeviceFromResultSet(rs);
|
|
|
|
DeviceNode parent = getDeviceFromResultSet(rs);
|
|
|
|
if (!includeDevice && parent.getDeviceId() == node.getDeviceId()) {
|
|
|
|
if (includeDevice || parent.getDeviceId() != node.getDeviceId()) {
|
|
|
|
// Skip adding the current node as a parent when includeDevice is false
|
|
|
|
node.getParents().add(parent);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
node.getParents().add(parent);
|
|
|
|
if (!parentNodes.contains(parent) && (includeDevice || parent.getDeviceId() != node.getDeviceId())) {
|
|
|
|
|
|
|
|
parentNodes.add(parent);
|
|
|
|
if (!parentNodes.contains(parent)) {
|
|
|
|
|
|
|
|
parentNodes.add(parent); // Add the parent device if it hasn't been added already.
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getParentsRecursive(parent, maxDepth - 1, visited, conn, parentNodes, includeDevice);
|
|
|
|
getParentsRecursive(parent, maxDepth - 1, visited, conn, parentNodes, includeDevice);
|
|
|
|