|
|
@ -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;
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
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 " +
|
|
|
@ -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;
|
|
|
|
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 " +
|
|
|
@ -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -240,9 +258,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
stmt.setNull(2, Types.INTEGER);
|
|
|
|
stmt.setNull(2, Types.INTEGER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (deviceOrganization.getDeviceOrganizationMeta() != null) {
|
|
|
|
if (deviceOrganization.getDeviceOrganizationMeta() != null) {
|
|
|
|
stmt.setString(3,deviceOrganization.getDeviceOrganizationMeta());
|
|
|
|
stmt.setString(3, deviceOrganization.getDeviceOrganizationMeta());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
stmt.setString(3,"");
|
|
|
|
stmt.setString(3, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stmt.setTimestamp(4, timestamp);
|
|
|
|
stmt.setTimestamp(4, timestamp);
|
|
|
@ -359,14 +377,14 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String sql = "UPDATE DM_DEVICE_ORGANIZATION SET ";
|
|
|
|
String sql = "UPDATE DM_DEVICE_ORGANIZATION SET ";
|
|
|
|
if((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0){
|
|
|
|
if ((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0) {
|
|
|
|
sql += "DEVICE_ID = ? , ";
|
|
|
|
sql += "DEVICE_ID = ? , ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((deviceOrganization.getParentDeviceId() == null || deviceOrganization.getParentDeviceId() > 0) &&
|
|
|
|
if ((deviceOrganization.getParentDeviceId() == null || deviceOrganization.getParentDeviceId() > 0) &&
|
|
|
|
!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())){
|
|
|
|
!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())) {
|
|
|
|
sql += "PARENT_DEVICE_ID = ? ,";
|
|
|
|
sql += "PARENT_DEVICE_ID = ? ,";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())){
|
|
|
|
if (!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())) {
|
|
|
|
sql += "DEVICE_ORGANIZATION_META = ? ,";
|
|
|
|
sql += "DEVICE_ORGANIZATION_META = ? ,";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sql += "LAST_UPDATED_TIMESTAMP = ? WHERE ID = ? ";
|
|
|
|
sql += "LAST_UPDATED_TIMESTAMP = ? WHERE ID = ? ";
|
|
|
@ -377,13 +395,13 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
|
|
|
int x = 0;
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0){
|
|
|
|
if ((organization.getDeviceId() != deviceOrganization.getDeviceId()) && deviceOrganization.getDeviceId() > 0) {
|
|
|
|
stmt.setInt(++x, deviceOrganization.getDeviceId());
|
|
|
|
stmt.setInt(++x, deviceOrganization.getDeviceId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())) {
|
|
|
|
if (!Objects.equals(organization.getParentDeviceId(), deviceOrganization.getParentDeviceId())) {
|
|
|
|
stmt.setInt(++x, deviceOrganization.getParentDeviceId());
|
|
|
|
stmt.setInt(++x, deviceOrganization.getParentDeviceId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())){
|
|
|
|
if (!Objects.equals(organization.getDeviceOrganizationMeta(), deviceOrganization.getDeviceOrganizationMeta())) {
|
|
|
|
stmt.setString(++x, deviceOrganization.getDeviceOrganizationMeta());
|
|
|
|
stmt.setString(++x, deviceOrganization.getDeviceOrganizationMeta());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stmt.setTimestamp(++x, timestamp);
|
|
|
|
stmt.setTimestamp(++x, timestamp);
|
|
|
|