Device Organization getChildrenOf and getParentsOf modifications

pull/238/head
Isuri Mendis 1 year ago
parent ea0657e221
commit d7dc2c4791

@ -41,12 +41,14 @@ public interface DeviceOrganizationDAO {
List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException;
/**
* retrieve parent devices per particular device ID
* @param deviceID
*
* @param node
* @param maxDepth
* @param includeDevice
* @return
* @throws DeviceOrganizationMgtDAOException
*/
// List<Device> getParentDevices(DeviceNode node) throws DeviceOrganizationMgtDAOException;
List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException;
/**
* add a new reocrd to device organization table

@ -50,30 +50,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice);
if(!includeDevice){
childNodes.add(node);
}
return childNodes;
// Prepare SQL query to retrieve children of the node using DM_DEVICE_ORGANIZATION
// String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
// "FROM DM_DEVICE D " +
// "JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.DEVICE_ID " +
// "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " +
// "WHERE DO.PARENT_DEVICE_ID = ?";
//
// try (PreparedStatement stmt = conn.prepareStatement(sql)) {
// stmt.setInt(1, node.getDeviceId());
// try (ResultSet rs = stmt.executeQuery()) {
// while (rs.next()) {
// DeviceNode child = getDeviceFromResultSet(rs);
// child.setChildren(childNodes);
// childNodes.add(child);
//
// if (maxDepth > 0) {
// List<DeviceNode> deviceChildren = getChildrenOf(child, maxDepth - 1, true);
// childNodes.addAll(deviceChildren);
// }
// }
// return childNodes;
// }
// }
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " +
"parent device ID " +
@ -108,11 +88,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceNode child = getDeviceFromResultSet(rs);
child.setChildren(childNodes);
// if (includeDevice) {
// childNodes.add(node); // Add the parent device if includeDevice is true.
// }
childNodes.add(child);
node.getChildren().add(child);
if (includeDevice) {
childNodes.add(node); // Add the parent device if includeDevice is true.
}
getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice);
}
@ -120,40 +99,66 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
}
// @Override
// public List<DeviceNode> getParentDevices(DeviceNode node) throws DeviceOrganizationMgtDAOException {
// List<DeviceNode> parentDevices = null;
// try {
// Connection conn = ConnectionManagerUtil.getDBConnection();
// String sql = "SELECT do.PARENT_DEVICE_ID,d.ID, d.DESCRIPTION,d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " +
// "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_ORGANIZATION do " +
// "WHERE d.ID IN (SELECT ID FROM DM_DEVICE WHERE ID = ?) " +
// "AND d.ID = do.DEVICE_ID" +
// "AND d.DEVICE_TYPE_ID = t.ID";
//
// try (PreparedStatement stmt = conn.prepareStatement(sql)) {
// stmt.setInt(1, node.getDeviceId());
// try (ResultSet rs = stmt.executeQuery()) {
// parentDevices = new ArrayList<>();
// while (rs.next()) {
// parentDevices.add(getDeviceFromResultSet(rs));
// }
// return parentDevices;
// }
// }
// } catch (DBConnectionException e) {
// String msg = "Error occurred while obtaining DB connection to retrieve all parent devices for " +
// "child device ID " +
// deviceID;
// log.error(msg);
// throw new DeviceOrganizationMgtDAOException(msg, e);
// } catch (SQLException e) {
// String msg = "Error occurred while processing SQL to retrieve all parent devices for " +
// "child device ID" + deviceID;
// log.error(msg);
// throw new DeviceOrganizationMgtDAOException(msg, e);
// }
// }
@Override
public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException {
List<DeviceNode> parentNodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
getParentsRecursive(node, maxDepth, visited, conn, parentNodes, includeDevice);
if (!includeDevice) {
parentNodes.add(node);
}
return parentNodes;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve parent devices for " +
"device ID " + node.getDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to retrieve parent devices for " +
"device ID " + node.getDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
private void getParentsRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
List<DeviceNode> parentNodes, boolean includeDevice) throws SQLException {
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
return;
}
visited.add(node.getDeviceId());
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE D " +
"JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.PARENT_DEVICE_ID " +
"JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " +
"WHERE DO.DEVICE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, node.getDeviceId());
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceNode parent = getDeviceFromResultSet(rs);
if (!includeDevice && parent.getDeviceId() == node.getDeviceId()) {
// Skip adding the current node as a parent when includeDevice is false
continue;
}
node.getParents().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);
}
}
}
}
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException {

@ -50,13 +50,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
List<DeviceNode> children = new ArrayList<>();
if (maxDepth <= 0) {
return children;
}
children = deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice);
return children;
return deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve child devices";
log.error(msg);
@ -71,53 +65,26 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
}
// @Override
// public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
// throws DeviceOrganizationMgtPluginException {
// try {
// // Open a database connection
// ConnectionManagerUtil.openDBConnection();
//
// List<DeviceNode> parents = new ArrayList<>();
// if (includeDevice) {
// parents.add(node);
// }
// retrieveParents(node, parents, 1, maxDepth);
// return parents;
// } catch (DBConnectionException e) {
// String msg = "Error occurred while obtaining the database connection to retrieve parent devices";
// log.error(msg);
// throw new DeviceOrganizationMgtPluginException(msg, e);
// } catch (DeviceOrganizationMgtDAOException e) {
// String msg = "Error occurred in the database level while retrieving parent devices";
// log.error(msg);
// throw new DeviceOrganizationMgtPluginException(msg, e);
// } finally {
// // Close the database connection
// ConnectionManagerUtil.closeDBConnection();
// }
// }
// private void retrieveParents(DeviceNode node, List<DeviceNode> result, int currentDepth, int maxDepth)
// throws DeviceOrganizationMgtDAOException {
// if (currentDepth > maxDepth) {
// return;
// }
//
// List<Device> parentDevices = deviceOrganizationDao.getParentDevices(node);
//
// for (Device parentDevice : parentDevices) {
// DeviceNode parentNode = new DeviceNode();
// parentNode.setDeviceId(parentDevice.getId());
// parentNode.setDevice(parentDevice);
//
// result.add(parentNode);
//
// if (currentDepth < maxDepth) {
// retrieveParents(parentNode, result, currentDepth + 1, maxDepth);
// }
// }
// }
@Override
public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException {
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
return deviceOrganizationDao.getParentsOf(node, maxDepth, includeDevice);
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve parent devices";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving parent devices";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
// Close the database connection
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)

@ -32,8 +32,8 @@ public interface DeviceOrganizationService {
List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException;
// List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
// throws DeviceOrganizationMgtPluginException;
List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException;
DeviceOrganization getDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException;

@ -43,13 +43,20 @@ public class DAOTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(childrenList, "Cannot be null");
}
// @Test(dependsOnMethods = "testAddDeviceOrganization")
// public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
// ConnectionManagerUtil.openDBConnection();
// List<Device> parentList = deviceOrganizationDAO.getParentDevices(4);
// ConnectionManagerUtil.closeDBConnection();
// Assert.assertNotNull(parentList, "Cannot be null");
// }
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection();
DeviceNode node = new DeviceNode();
node.setDeviceId(4);
int maxDepth = 4;
boolean includeDevice = true;
List<DeviceNode> childrenList = deviceOrganizationDAO.getParentsOf(node, maxDepth, includeDevice);
ConnectionManagerUtil.closeDBConnection();
log.info(childrenList.size());
Assert.assertNotNull(childrenList, "Cannot be null");
}
@Test
public void testAddDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException {
@ -72,6 +79,15 @@ public class DAOTest extends BaseDeviceOrganizationTest {
boolean result1 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization2 = new DeviceOrganization() {
};
deviceOrganization1.setDeviceId(4);
deviceOrganization1.setParentDeviceId(2);
deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction();
boolean result2 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null");
Assert.assertNotNull(result1, "Cannot be null");

@ -31,21 +31,21 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
public void testGetChildrenOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(3);
deviceNode.setDeviceId(2);
List<DeviceNode> childrenList = deviceOrganizationService.getChildrenOf(deviceNode, 2, true);
Assert.assertNotNull(childrenList, "Cannot be null");
}
// @Test(dependsOnMethods = "testAddDeviceOrganization")
// public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
//
// DeviceNode deviceNode = new DeviceNode();
// deviceNode.setDeviceId(4);
// List<DeviceNode> parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, false);
//
// Assert.assertNotNull(parentList, "Cannot be null");
// }
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(4);
List<DeviceNode> parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, true);
Assert.assertNotNull(parentList, "Cannot be null");
}
@Test
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
@ -57,6 +57,18 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
DeviceOrganization deviceOrganization1 = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(3);
deviceOrganization.setParentDeviceId(2);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
DeviceOrganization deviceOrganization2 = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(2);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
Assert.assertNotNull(result, "Cannot be null");

Loading…
Cancel
Save