|
|
@ -26,18 +26,8 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.excep
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.*;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.util.*;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
|
|
|
import java.sql.Timestamp;
|
|
|
|
|
|
|
|
import java.sql.Types;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet;
|
|
|
|
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet;
|
|
|
|
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.loadDeviceOrganization;
|
|
|
|
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.loadDeviceOrganization;
|
|
|
@ -56,15 +46,21 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
public List<DeviceNode> getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice)
|
|
|
|
public List<DeviceNode> getChildrenOfDeviceNode(DeviceNode node, int maxDepth, boolean includeDevice)
|
|
|
|
throws DeviceOrganizationMgtDAOException {
|
|
|
|
throws DeviceOrganizationMgtDAOException {
|
|
|
|
List<DeviceNode> childNodes = new ArrayList<>();
|
|
|
|
List<DeviceNode> childNodes = new ArrayList<>();
|
|
|
|
// Set<Integer> visited = new HashSet<>();
|
|
|
|
Set<Integer> visited = new HashSet<>();
|
|
|
|
|
|
|
|
Set<Integer> twiseVisited = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
boolean parentAdded = false; // Flag to track whether the parent device has been added
|
|
|
|
boolean parentAdded = false; // Flag to track whether the parent device has been added
|
|
|
|
getChildrenRecursive(node, maxDepth,
|
|
|
|
getChildrenRecursive(node, maxDepth,
|
|
|
|
// visited,
|
|
|
|
visited,
|
|
|
|
conn, childNodes, includeDevice, parentAdded);
|
|
|
|
twiseVisited,
|
|
|
|
if (!includeDevice && !parentAdded) {
|
|
|
|
conn, childNodes, includeDevice
|
|
|
|
|
|
|
|
, parentAdded
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!includeDevice
|
|
|
|
|
|
|
|
&& !parentAdded
|
|
|
|
|
|
|
|
) {
|
|
|
|
childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false.
|
|
|
|
childNodes.add(node); // Add the parent device if it hasn't been added and includeDevice is false.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return childNodes;
|
|
|
|
return childNodes;
|
|
|
@ -82,17 +78,25 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void getChildrenRecursive(DeviceNode node, int maxDepth,
|
|
|
|
private void getChildrenRecursive(DeviceNode node, int maxDepth,
|
|
|
|
// Set<Integer> visited,
|
|
|
|
Set<Integer> visited,
|
|
|
|
|
|
|
|
Set<Integer> twiseVisited,
|
|
|
|
Connection conn,
|
|
|
|
Connection conn,
|
|
|
|
List<DeviceNode> childNodes, boolean includeDevice, boolean parentAdded)
|
|
|
|
List<DeviceNode> childNodes, boolean includeDevice
|
|
|
|
|
|
|
|
, boolean parentAdded
|
|
|
|
|
|
|
|
)
|
|
|
|
throws SQLException {
|
|
|
|
throws SQLException {
|
|
|
|
if (maxDepth <= 0
|
|
|
|
if (maxDepth <= 0) {
|
|
|
|
// || visited.contains(node.getDeviceId())
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (twiseVisited.contains(node.getDeviceId())) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (visited.contains(node.getDeviceId())) {
|
|
|
|
|
|
|
|
twiseVisited.add(node.getDeviceId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// visited.add(node.getDeviceId());
|
|
|
|
visited.add(node.getDeviceId());
|
|
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
|
|
|
|
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
|
|
|
|
"FROM DM_DEVICE D " +
|
|
|
|
"FROM DM_DEVICE D " +
|
|
|
@ -107,14 +111,19 @@ 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 && !parentAdded) {
|
|
|
|
if (includeDevice
|
|
|
|
|
|
|
|
&& !parentAdded
|
|
|
|
|
|
|
|
) {
|
|
|
|
childNodes.add(node); // Add the parent device only if includeDevice is true and it hasn't been added.
|
|
|
|
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.
|
|
|
|
parentAdded = true; // Set the flag to true after adding the parent device.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getChildrenRecursive(child, maxDepth - 1,
|
|
|
|
getChildrenRecursive(child, maxDepth - 1,
|
|
|
|
// visited,
|
|
|
|
visited,
|
|
|
|
conn, childNodes, includeDevice, parentAdded);
|
|
|
|
twiseVisited,
|
|
|
|
|
|
|
|
conn, childNodes, includeDevice
|
|
|
|
|
|
|
|
, parentAdded
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -129,11 +138,13 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
|
|
|
|
|
|
|
|
List<DeviceNode> parentNodes = new ArrayList<>();
|
|
|
|
List<DeviceNode> parentNodes = new ArrayList<>();
|
|
|
|
Set<Integer> visited = new HashSet<>();
|
|
|
|
Set<Integer> visited = new HashSet<>();
|
|
|
|
|
|
|
|
Set<Integer> twiseVisited = new HashSet<>();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
Connection conn = ConnectionManagerUtil.getDBConnection();
|
|
|
|
boolean childAdded = false;
|
|
|
|
boolean childAdded = false;
|
|
|
|
getParentsRecursive(node, maxDepth,
|
|
|
|
getParentsRecursive(node, maxDepth,
|
|
|
|
// visited,
|
|
|
|
visited,
|
|
|
|
|
|
|
|
twiseVisited,
|
|
|
|
conn, parentNodes, includeDevice, childAdded);
|
|
|
|
conn, parentNodes, includeDevice, childAdded);
|
|
|
|
if (!includeDevice && !childAdded) {
|
|
|
|
if (!includeDevice && !childAdded) {
|
|
|
|
parentNodes.add(node);
|
|
|
|
parentNodes.add(node);
|
|
|
@ -153,16 +164,22 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void getParentsRecursive(DeviceNode node, int maxDepth,
|
|
|
|
private void getParentsRecursive(DeviceNode node, int maxDepth,
|
|
|
|
// Set<Integer> visited,
|
|
|
|
Set<Integer> visited,
|
|
|
|
|
|
|
|
Set<Integer> twiseVisited,
|
|
|
|
Connection conn,
|
|
|
|
Connection conn,
|
|
|
|
List<DeviceNode> parentNodes, boolean includeDevice, boolean childAdded) throws SQLException {
|
|
|
|
List<DeviceNode> parentNodes, boolean includeDevice, boolean childAdded) throws SQLException {
|
|
|
|
if (maxDepth <= 0
|
|
|
|
if (maxDepth <= 0) {
|
|
|
|
// || visited.contains(node.getDeviceId())
|
|
|
|
return;
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (twiseVisited.contains(node.getDeviceId())) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// visited.add(node.getDeviceId());
|
|
|
|
if (visited.contains(node.getDeviceId())) {
|
|
|
|
|
|
|
|
twiseVisited.add(node.getDeviceId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
visited.add(node.getDeviceId());
|
|
|
|
|
|
|
|
|
|
|
|
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
|
|
|
|
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
|
|
|
|
"FROM DM_DEVICE D " +
|
|
|
|
"FROM DM_DEVICE D " +
|
|
|
@ -181,7 +198,8 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
childAdded = true;
|
|
|
|
childAdded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getParentsRecursive(parent, maxDepth - 1,
|
|
|
|
getParentsRecursive(parent, maxDepth - 1,
|
|
|
|
// visited,
|
|
|
|
visited,
|
|
|
|
|
|
|
|
twiseVisited,
|
|
|
|
conn, parentNodes, includeDevice, childAdded);
|
|
|
|
conn, parentNodes, includeDevice, childAdded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|