From dd92e5dc4c8f62478fce635e8f6ff4b57b88d6cb Mon Sep 17 00:00:00 2001 From: isuri Date: Wed, 6 Dec 2023 19:41:15 +0530 Subject: [PATCH] device id detail add for root organizations --- .../dao/impl/DeviceOrganizationDAOImpl.java | 12 ++++++---- .../dao/util/DeviceOrganizationDaoUtil.java | 23 +++++++++++++++++-- .../organization/dto/DeviceOrganization.java | 10 ++++++++ .../device/organization/ServiceTest.java | 2 ++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java index a90e1447fe..b73facaa92 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java @@ -42,6 +42,7 @@ 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.loadDeviceOrganization; +import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.loadDeviceOrganizationWithDeviceDetails; /** * Implementation of the DeviceOrganizationDAO interface. @@ -323,9 +324,12 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { List deviceOrganizations = new ArrayList<>(); try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION " + - "WHERE (PARENT_DEVICE_ID IS NULL AND " + - "DEVICE_ID NOT IN " + + String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME, " + + "DO.ORGANIZATION_ID, DO.DEVICE_ID, DO.PARENT_DEVICE_ID, DO.DEVICE_ORGANIZATION_META ," + + "DO.LAST_UPDATED_TIMESTAMP FROM DM_DEVICE_ORGANIZATION DO JOIN DM_DEVICE D ON D.ID = DO.DEVICE_ID " + + "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " + + "WHERE (DO.PARENT_DEVICE_ID IS NULL AND " + + "DO.DEVICE_ID NOT IN " + "(SELECT DEVICE_ID FROM DM_DEVICE_ORGANIZATION " + "WHERE PARENT_DEVICE_ID IS NOT NULL)) " + "LIMIT ? OFFSET ?"; @@ -335,7 +339,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { stmt.setInt(2, request.getOffSet()); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { - DeviceOrganization deviceOrganization = loadDeviceOrganization(rs); + DeviceOrganization deviceOrganization = loadDeviceOrganizationWithDeviceDetails(rs); deviceOrganizations.add(deviceOrganization); } } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/util/DeviceOrganizationDaoUtil.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/util/DeviceOrganizationDaoUtil.java index c17bf50336..04e1394300 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/util/DeviceOrganizationDaoUtil.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/util/DeviceOrganizationDaoUtil.java @@ -38,6 +38,21 @@ public class DeviceOrganizationDaoUtil { return deviceOrganization; } + public static DeviceOrganization loadDeviceOrganizationWithDeviceDetails(ResultSet rs) throws SQLException { + DeviceOrganization deviceOrganization = new DeviceOrganization(); + deviceOrganization.setOrganizationId(rs.getInt("ORGANIZATION_ID")); + deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID")); + if (rs.getInt("PARENT_DEVICE_ID") != 0) { + deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID")); + } else { + deviceOrganization.setParentDeviceId(null); + } + deviceOrganization.setDeviceOrganizationMeta(rs.getString("DEVICE_ORGANIZATION_META")); + deviceOrganization.setUpdateTime(rs.getDate("LAST_UPDATED_TIMESTAMP")); + deviceOrganization.setDevice(getDeviceDetails(rs)); + return deviceOrganization; + } + /** * Helper method to create a DeviceNode object from a ResultSet * @@ -48,14 +63,18 @@ public class DeviceOrganizationDaoUtil { public static DeviceNode getDeviceFromResultSet(ResultSet rs) throws SQLException { DeviceNode node = new DeviceNode(); node.setDeviceId(rs.getInt("ID")); + node.setDevice(getDeviceDetails(rs)); + return node; + } + + public static Device getDeviceDetails(ResultSet rs) throws SQLException { Device device = new Device(); device.setId(rs.getInt("ID")); device.setDescription(rs.getString("DESCRIPTION")); device.setName(rs.getString("NAME")); device.setType(rs.getString("DEVICE_TYPE_NAME")); device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); - node.setDevice(device); - return node; + return device; } } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/DeviceOrganization.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/DeviceOrganization.java index 7a875b91aa..f92bb93687 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/DeviceOrganization.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/DeviceOrganization.java @@ -19,6 +19,8 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto; +import io.entgra.device.mgt.core.device.mgt.common.Device; + import java.util.Date; import java.util.Objects; @@ -30,6 +32,7 @@ public class DeviceOrganization { private int organizationId; private int deviceId; + private Device device; private Integer parentDeviceId; private String deviceOrganizationMeta; private Date updateTime; @@ -50,6 +53,13 @@ public class DeviceOrganization { this.deviceId = deviceId; } + public Device getDevice() { + return device; + } + public void setDevice(Device device) { + this.device = device; + } + public Integer getParentDeviceId() { return parentDeviceId; } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java index 3987b9b227..1f2a052454 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java @@ -275,6 +275,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { log.info("deviceID = " + organization.getDeviceId()); log.info("parentDeviceID = " + organization.getParentDeviceId()); log.info("updateTime = " + organization.getUpdateTime()); + log.info("deviceSerial = " + organization.getDevice().getDeviceIdentifier()); log.info("----------------------------------------------"); } Assert.assertNotNull(organizations, "List of organizations cannot be null"); @@ -292,6 +293,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { log.info("deviceID = " + organization.getDeviceId()); log.info("parentDeviceID = " + organization.getParentDeviceId()); log.info("updateTime = " + organization.getUpdateTime()); + log.info("deviceSerial = " + organization.getDevice().getDeviceIdentifier()); log.info("----------------------------------------------"); } Assert.assertNotNull(organizations, "List of organizations cannot be null");