From a6af15e3ee7330cb0e8d1c1bf7efdbe387271306 Mon Sep 17 00:00:00 2001 From: isuri Date: Wed, 3 Jan 2024 10:40:16 +0530 Subject: [PATCH] deviceOrg: tenant management addition --- .../dao/DeviceOrganizationDAO.java | 22 +-- .../dao/impl/DeviceOrganizationDAOImpl.java | 127 ++++++++++-------- .../dao/util/DeviceOrganizationDaoUtil.java | 2 + .../organization/dto/DeviceOrganization.java | 12 +- .../impl/DeviceOrganizationServiceImpl.java | 38 ++++-- .../device/organization/DAOTest.java | 24 ++-- .../carbon-home/dbscripts/dm-db-h2.sql | 1 + .../resources/carbon-home/dbscripts/h2.sql | 1 + .../src/main/resources/dbscripts/cdm/h2.sql | 1 + .../main/resources/dbscripts/cdm/mssql.sql | 7 +- .../main/resources/dbscripts/cdm/mysql.sql | 1 + .../main/resources/dbscripts/cdm/oracle.sql | 1 + .../resources/dbscripts/cdm/postgresql.sql | 1 + 13 files changed, 150 insertions(+), 88 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/DeviceOrganizationDAO.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/DeviceOrganizationDAO.java index 20e6242d09..0821fc9839 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/DeviceOrganizationDAO.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/DeviceOrganizationDAO.java @@ -38,7 +38,7 @@ public interface DeviceOrganizationDAO { * @return A list of child device nodes. * @throws DeviceOrganizationMgtDAOException If an error occurs while retrieving child devices. */ - DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; + DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Retrieves parent devices for a given device node. @@ -49,7 +49,7 @@ public interface DeviceOrganizationDAO { * @return A list of parent device nodes. * @throws DeviceOrganizationMgtDAOException If an error occurs while retrieving parent devices. */ - DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException; + DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Retrieves all device organization records. @@ -65,7 +65,7 @@ public interface DeviceOrganizationDAO { * @return A list of root device organization records. * @throws DeviceOrganizationMgtDAOException */ - public List getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtDAOException; + public List getDeviceOrganizationRoots(PaginationRequest request, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Retrieves device Organization Leafs @@ -73,7 +73,7 @@ public interface DeviceOrganizationDAO { * @return A list of leaf device organization records. * @throws DeviceOrganizationMgtDAOException */ - public List getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException; + public List getDeviceOrganizationLeafs(PaginationRequest request, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Adds a new record to the device organization table. @@ -92,7 +92,7 @@ public interface DeviceOrganizationDAO { * @return True if a record with the specified deviceId and parentDeviceId exists, false otherwise. * @throws DeviceOrganizationMgtDAOException If an error occurs while checking the existence of the record. */ - boolean isDeviceOrganizationExist(int deviceId, Integer parentDeviceId) throws DeviceOrganizationMgtDAOException; + boolean isDeviceOrganizationExist(int deviceId, Integer parentDeviceId, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Get a device organization by the CHILD_PARENT_COMP_KEY unique key. @@ -102,7 +102,7 @@ public interface DeviceOrganizationDAO { * @return The DeviceOrganization object if found, null otherwise. * @throws DeviceOrganizationMgtDAOException if an error occurs while accessing the database. */ - DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, Integer parentDeviceId) + DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, Integer parentDeviceId, int tenantID) throws DeviceOrganizationMgtDAOException; /** @@ -122,7 +122,7 @@ public interface DeviceOrganizationDAO { * @return The DeviceOrganization object representing the retrieved organization, or null if not found. * @throws DeviceOrganizationMgtDAOException If an error occurs while retrieving the organization record. */ - DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException; + DeviceOrganization getDeviceOrganizationByID(int organizationId, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Deletes a device organization record from the database based on the provided organization ID. @@ -131,7 +131,7 @@ public interface DeviceOrganizationDAO { * @return true if the organization record was successfully deleted, false otherwise. * @throws DeviceOrganizationMgtDAOException If an error occurs while deleting the organization record. */ - boolean deleteDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException; + boolean deleteDeviceOrganizationByID(int organizationId, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Deletes records associated with a particular device ID from the device organization table. @@ -142,7 +142,7 @@ public interface DeviceOrganizationDAO { * @return true if associated records were successfully deleted, false otherwise. * @throws DeviceOrganizationMgtDAOException If an error occurs while deleting the associated records. */ - boolean deleteDeviceAssociations(int deviceId) throws DeviceOrganizationMgtDAOException; + boolean deleteDeviceAssociations(int deviceId, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Checks whether a record with the specified device ID exists either in the deviceID column or @@ -152,7 +152,7 @@ public interface DeviceOrganizationDAO { * @return true if a record with the given device ID exists, false otherwise. * @throws DeviceOrganizationMgtDAOException If an error occurs while querying the database. */ - boolean isDeviceIdExist(int deviceId) throws DeviceOrganizationMgtDAOException; + boolean isDeviceIdExist(int deviceId, int tenantID) throws DeviceOrganizationMgtDAOException; /** * Checks if a child device with the given `deviceId` exists in the database. @@ -161,5 +161,5 @@ public interface DeviceOrganizationDAO { * @return `true` if the child device exists, `false` otherwise. * @throws DeviceOrganizationMgtDAOException If an error occurs while checking the existence. */ - boolean isChildDeviceIdExist(int deviceId) throws DeviceOrganizationMgtDAOException; + boolean isChildDeviceIdExist(int deviceId, int tenantID) throws DeviceOrganizationMgtDAOException; } 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 302c0351cf..ed897f264a 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 @@ -55,7 +55,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) + public DeviceNodeResult getChildrenOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice, int tenantID) throws DeviceOrganizationMgtDAOException { List childNodes = new ArrayList<>(); Set organizations = new HashSet<>(); @@ -75,6 +75,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { childNodes, includeDevice, parentAdded, + tenantID, organizations ); if (!includeDevice @@ -104,6 +105,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { List childNodes, boolean includeDevice, boolean parentAdded, + int tenantID, Set organizations ) throws SQLException { @@ -121,15 +123,16 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { visited.add(node.getDeviceId()); 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.ORGANIZATION_ID, DO.TENANT_ID, DO.DEVICE_ID, DO.PARENT_DEVICE_ID, DO.DEVICE_ORGANIZATION_META ," + "DO.LAST_UPDATED_TIMESTAMP 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 = ?"; + "WHERE DO.TENANT_ID = ? AND DO.PARENT_DEVICE_ID = ? "; boolean hasChildren = false; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, node.getDeviceId()); + stmt.setInt(1, tenantID); + stmt.setInt(2, node.getDeviceId()); try (ResultSet rs = stmt.executeQuery()) { DeviceNode child; @@ -157,6 +160,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { childNodes, includeDevice, parentAdded, + tenantID, organizations ); } @@ -174,7 +178,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice) + public DeviceNodeResult getParentsOfDeviceNode(int deviceId, int maxDepth, boolean includeDevice, int tenantID) throws DeviceOrganizationMgtDAOException { List parentNodes = new ArrayList<>(); @@ -194,6 +198,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { parentNodes, includeDevice, childAdded, + tenantID, organizations); if (!includeDevice && !childAdded) { parentNodes.add(deviceNode); @@ -221,6 +226,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { List parentNodes, boolean includeDevice, boolean childAdded, + int tenantID, Set organizations) throws SQLException { if (maxDepth <= 0) { @@ -237,14 +243,15 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { visited.add(node.getDeviceId()); 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.ORGANIZATION_ID, DO.TENANT_ID, DO.DEVICE_ID, DO.PARENT_DEVICE_ID, DO.DEVICE_ORGANIZATION_META ," + "DO.LAST_UPDATED_TIMESTAMP 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 = ?"; + "WHERE DO.TENANT_ID = ? AND DO.DEVICE_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, node.getDeviceId()); + stmt.setInt(1, tenantID); + stmt.setInt(2, node.getDeviceId()); try (ResultSet rs = stmt.executeQuery()) { DeviceNode parent; DeviceOrganization organization; @@ -268,6 +275,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { parentNodes, includeDevice, childAdded, + tenantID, organizations); } } @@ -325,24 +333,25 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public List getDeviceOrganizationRoots(PaginationRequest request) + public List getDeviceOrganizationRoots(PaginationRequest request, int tenantID) throws DeviceOrganizationMgtDAOException { List deviceOrganizations = new ArrayList<>(); try { Connection conn = ConnectionManagerUtil.getDBConnection(); 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.ORGANIZATION_ID, DO.TENANT_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 " + + "WHERE DO.TENANT_ID = ? AND (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 ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, request.getLimit()); - stmt.setInt(2, request.getOffSet()); + stmt.setInt(1, tenantID); + stmt.setInt(2, request.getLimit()); + stmt.setInt(3, request.getOffSet()); try (ResultSet rs = stmt.executeQuery()) { DeviceOrganization deviceOrganization; while (rs.next()) { @@ -368,16 +377,17 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public List getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException { + public List getDeviceOrganizationLeafs(PaginationRequest request, int tenantID) throws DeviceOrganizationMgtDAOException { List deviceOrganizations = new ArrayList<>(); try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID NOT IN " + + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND DEVICE_ID NOT IN " + "(SELECT DISTINCT PARENT_DEVICE_ID FROM DM_DEVICE_ORGANIZATION WHERE PARENT_DEVICE_ID IS NOT NULL ) " + "LIMIT ? OFFSET ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, request.getLimit()); - stmt.setInt(2, request.getOffSet()); + stmt.setInt(1, tenantID); + stmt.setInt(2, request.getLimit()); + stmt.setInt(3, request.getOffSet()); try (ResultSet rs = stmt.executeQuery()) { DeviceOrganization deviceOrganization; while (rs.next()) { @@ -407,27 +417,28 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { throws DeviceOrganizationMgtDAOException { try { - String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, " + - "DEVICE_ORGANIZATION_META,LAST_UPDATED_TIMESTAMP)" + - " VALUES (?, ?, ?, ?)"; + String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (TENANT_ID, DEVICE_ID, PARENT_DEVICE_ID, " + + "DEVICE_ORGANIZATION_META, LAST_UPDATED_TIMESTAMP)" + + " VALUES (?, ?, ?, ?, ?)"; Connection conn = ConnectionManagerUtil.getDBConnection(); Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceOrganization.getDeviceId()); + stmt.setInt(1, deviceOrganization.getTenantID()); + stmt.setInt(2, deviceOrganization.getDeviceId()); if (deviceOrganization.getParentDeviceId() != null) { - stmt.setInt(2, deviceOrganization.getParentDeviceId()); + stmt.setInt(3, deviceOrganization.getParentDeviceId()); } else { - stmt.setNull(2, java.sql.Types.INTEGER); + stmt.setNull(3, java.sql.Types.INTEGER); } if (deviceOrganization.getDeviceOrganizationMeta() != null) { - stmt.setString(3, deviceOrganization.getDeviceOrganizationMeta()); + stmt.setString(4, deviceOrganization.getDeviceOrganizationMeta()); } else { - stmt.setString(3, ""); + stmt.setString(4, ""); } - stmt.setTimestamp(4, timestamp); + stmt.setTimestamp(5, timestamp); return stmt.executeUpdate() > 0; } } catch (DBConnectionException e) { @@ -447,22 +458,23 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public boolean isDeviceOrganizationExist(int deviceId, Integer parentDeviceId) + public boolean isDeviceOrganizationExist(int deviceId, Integer parentDeviceId, int tenantID) throws DeviceOrganizationMgtDAOException { try { String sql; Connection conn = ConnectionManagerUtil.getDBConnection(); if (parentDeviceId != null) { - sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID = ?"; + sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND DEVICE_ID = ? AND PARENT_DEVICE_ID = ?"; } else { - sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID IS NULL"; + sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND DEVICE_ID = ? AND PARENT_DEVICE_ID IS NULL"; } try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceId); + stmt.setInt(1, tenantID); + stmt.setInt(2, deviceId); if (parentDeviceId != null) { - stmt.setInt(2, parentDeviceId); + stmt.setInt(3, parentDeviceId); } try (ResultSet rs = stmt.executeQuery()) { @@ -485,22 +497,23 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { /** * {@inheritDoc} */ - public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, Integer parentDeviceId) + public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, Integer parentDeviceId, int tenantID) throws DeviceOrganizationMgtDAOException { try { String sql; Connection conn = ConnectionManagerUtil.getDBConnection(); if (parentDeviceId != null) { - sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID = ?"; + sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND DEVICE_ID = ? AND PARENT_DEVICE_ID = ?"; } else { - sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID IS NULL"; + sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND DEVICE_ID = ? AND PARENT_DEVICE_ID IS NULL"; } try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceId); + stmt.setInt(1, tenantID); + stmt.setInt(2, deviceId); if (parentDeviceId != null) { - stmt.setInt(2, parentDeviceId); + stmt.setInt(3, parentDeviceId); } try (ResultSet rs = stmt.executeQuery()) { @@ -531,7 +544,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { public boolean updateDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException { String msg; - DeviceOrganization organization = getDeviceOrganizationByID(deviceOrganization.getOrganizationId()); + DeviceOrganization organization = getDeviceOrganizationByID(deviceOrganization.getOrganizationId(), deviceOrganization.getTenantID()); if (organization == null) { msg = "Device Organization does not exist for organization ID = " + deviceOrganization.getOrganizationId(); @@ -589,13 +602,14 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException { + public DeviceOrganization getDeviceOrganizationByID(int organizationId, int tenantID) throws DeviceOrganizationMgtDAOException { try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION do WHERE do.ORGANIZATION_ID = ? "; + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION DO WHERE DO.TENANT_ID = ? AND DO.ORGANIZATION_ID = ? "; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, organizationId); + stmt.setInt(1, tenantID); + stmt.setInt(2, organizationId); try (ResultSet rs = stmt.executeQuery()) { if (rs.next()) { return loadDeviceOrganization(rs); @@ -622,15 +636,16 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public boolean deleteDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException { + public boolean deleteDeviceOrganizationByID(int organizationId, int tenantID) throws DeviceOrganizationMgtDAOException { try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String deleteOrganizationSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE ORGANIZATION_ID = ?"; + String deleteOrganizationSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND ORGANIZATION_ID = ?"; try (PreparedStatement deleteOrgStmt = conn.prepareStatement(deleteOrganizationSql)) { // Delete the organization - deleteOrgStmt.setInt(1, organizationId); + deleteOrgStmt.setInt(1, tenantID); + deleteOrgStmt.setInt(2, organizationId); return deleteOrgStmt.executeUpdate() > 0; } @@ -651,20 +666,22 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public boolean deleteDeviceAssociations(int deviceId) throws DeviceOrganizationMgtDAOException { + public boolean deleteDeviceAssociations(int deviceId, int tenantID) throws DeviceOrganizationMgtDAOException { try { Connection conn = ConnectionManagerUtil.getDBConnection(); - String deleteByDeviceIdSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ?"; - String deleteByParentDeviceIdSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE PARENT_DEVICE_ID = ?"; + String deleteByDeviceIdSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND DEVICE_ID = ?"; + String deleteByParentDeviceIdSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE TENANT_ID = ? AND PARENT_DEVICE_ID = ?"; try (PreparedStatement deleteByDeviceIdStmt = conn.prepareStatement(deleteByDeviceIdSql); PreparedStatement deleteByParentDeviceIdStmt = conn.prepareStatement(deleteByParentDeviceIdSql)) { // Delete device organizations where the device is the device_id - deleteByDeviceIdStmt.setInt(1, deviceId); + deleteByDeviceIdStmt.setInt(1, tenantID); + deleteByDeviceIdStmt.setInt(2, deviceId); // Delete device organizations where the device is the parent_device_id - deleteByParentDeviceIdStmt.setInt(1, deviceId); + deleteByParentDeviceIdStmt.setInt(1, tenantID); + deleteByParentDeviceIdStmt.setInt(2, deviceId); return deleteByDeviceIdStmt.executeUpdate() > 0 | deleteByParentDeviceIdStmt.executeUpdate() > 0; @@ -686,17 +703,18 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public boolean isDeviceIdExist(int deviceId) throws DeviceOrganizationMgtDAOException { + public boolean isDeviceIdExist(int deviceId, int tenantID) throws DeviceOrganizationMgtDAOException { try { Connection conn = ConnectionManagerUtil.getDBConnection(); String sql = "SELECT 1 " + "FROM DM_DEVICE_ORGANIZATION " + - "WHERE device_id = ? OR parent_device_id = ? " + + "WHERE TENANT_ID = ? AND (device_id = ? OR parent_device_id = ?) " + "LIMIT 1"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceId); + stmt.setInt(1, tenantID); stmt.setInt(2, deviceId); + stmt.setInt(3, deviceId); try (ResultSet rs = stmt.executeQuery()) { return rs.next(); // Returns true if a match is found, false otherwise @@ -719,16 +737,17 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { * {@inheritDoc} */ @Override - public boolean isChildDeviceIdExist(int deviceId) throws DeviceOrganizationMgtDAOException { + public boolean isChildDeviceIdExist(int deviceId, int tenantID) throws DeviceOrganizationMgtDAOException { try { Connection conn = ConnectionManagerUtil.getDBConnection(); String sql = "SELECT 1 " + "FROM DM_DEVICE_ORGANIZATION " + - "WHERE device_id = ? " + + "WHERE TENANT_ID = ? AND device_id = ? " + "LIMIT 1"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(1, deviceId); + stmt.setInt(1, tenantID); + stmt.setInt(2, deviceId); try (ResultSet rs = stmt.executeQuery()) { return rs.next(); // Returns true if a match is found, false otherwise 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 5947e516e9..e1e972b226 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 @@ -45,6 +45,7 @@ public class DeviceOrganizationDaoUtil { public static DeviceOrganization loadDeviceOrganization(ResultSet rs) throws SQLException { DeviceOrganization deviceOrganization = new DeviceOrganization(); deviceOrganization.setOrganizationId(rs.getInt("ORGANIZATION_ID")); + deviceOrganization.setTenantID(rs.getInt("TENANT_ID")); deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID")); if (rs.getInt("PARENT_DEVICE_ID") != 0) { deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID")); @@ -59,6 +60,7 @@ public class DeviceOrganizationDaoUtil { public static DeviceOrganization loadDeviceOrganizationWithDeviceDetails(ResultSet rs) throws SQLException { DeviceOrganization deviceOrganization = new DeviceOrganization(); deviceOrganization.setOrganizationId(rs.getInt("ORGANIZATION_ID")); + deviceOrganization.setTenantID(rs.getInt("TENANT_ID")); deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID")); if (rs.getInt("PARENT_DEVICE_ID") != 0) { deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID")); 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 f92bb93687..1dae5fdeba 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 @@ -36,6 +36,7 @@ public class DeviceOrganization { private Integer parentDeviceId; private String deviceOrganizationMeta; private Date updateTime; + private int tenantID; public int getOrganizationId() { return organizationId; @@ -84,6 +85,14 @@ public class DeviceOrganization { this.updateTime = updateTime; } + public int getTenantID() { + return tenantID; + } + + public void setTenantID(int tenantID) { + this.tenantID = tenantID; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -91,6 +100,7 @@ public class DeviceOrganization { DeviceOrganization that = (DeviceOrganization) o; // Compare fields for equality return Objects.equals(organizationId, that.organizationId) + && Objects.equals(tenantID, that.tenantID) && Objects.equals(deviceId, that.deviceId) && Objects.equals(parentDeviceId, that.parentDeviceId) && Objects.equals(deviceOrganizationMeta, that.deviceOrganizationMeta); @@ -99,6 +109,6 @@ public class DeviceOrganization { @Override public int hashCode() { // Hash based on fields - return Objects.hash(organizationId, deviceId, parentDeviceId, deviceOrganizationMeta); + return Objects.hash(organizationId, tenantID, deviceId, parentDeviceId, deviceOrganizationMeta); } } 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/impl/DeviceOrganizationServiceImpl.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/impl/DeviceOrganizationServiceImpl.java index 5a743e21b9..65b567136b 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/impl/DeviceOrganizationServiceImpl.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/impl/DeviceOrganizationServiceImpl.java @@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.excep import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import java.util.List; @@ -59,7 +60,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService // Open a database connection ConnectionManagerUtil.openDBConnection(); //set device details - return deviceOrganizationDao.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve child devices : " + "deviceID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + @@ -93,7 +95,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getParentsOfDeviceNode(deviceId, maxDepth, includeDevice); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.getParentsOfDeviceNode(deviceId, maxDepth, includeDevice, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve parent devices for : " + "device ID = " + deviceId + ", maxDepth = " + maxDepth + ", includeDevice = " + @@ -143,7 +146,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getDeviceOrganizationRoots(request); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.getDeviceOrganizationRoots(request, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve all device organizations."; log.error(msg); @@ -167,7 +171,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getDeviceOrganizationLeafs(request); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.getDeviceOrganizationLeafs(request, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve all device organizations."; log.error(msg); @@ -206,6 +211,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { ConnectionManagerUtil.beginDBTransaction(); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + deviceOrganization.setTenantID(tenantID); boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization); if (result) { msg = "Device organization added successfully. Device Organization details : " + @@ -250,7 +257,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } try { ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.isDeviceOrganizationExist(deviceID, parentDeviceID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.isDeviceOrganizationExist(deviceID, parentDeviceID, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to check organization existence. " + "Params : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID; @@ -279,7 +287,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } try { ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve organization. " + "Params : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID; @@ -312,6 +321,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { ConnectionManagerUtil.beginDBTransaction(); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + deviceOrganization.setTenantID(tenantID); boolean result = deviceOrganizationDao.updateDeviceOrganization(deviceOrganization); if (result) { msg = "Device organization updated successfully for organizationID = " + deviceOrganization.getOrganizationId(); @@ -355,7 +366,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.getDeviceOrganizationByID(organizationID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.getDeviceOrganizationByID(organizationID, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to retrieve deviceOrganization for : " + "organizationID = " + organizationID; @@ -394,7 +406,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { ConnectionManagerUtil.beginDBTransaction(); - boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationID, tenantID); if (result) { msg = "Device organization record deleted successfully for organizationID = " + organizationID; if (log.isDebugEnabled()) { @@ -445,7 +458,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { ConnectionManagerUtil.beginDBTransaction(); - boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceID, tenantID); if (result) { msg = "Device organization records associated with deviceID = " + deviceID + " are deleted successfully."; if (log.isDebugEnabled()) { @@ -489,7 +503,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.isDeviceIdExist(deviceID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.isDeviceIdExist(deviceID, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to check deviceID existence " + "in deviceOrganization : deviceID = " + deviceID; @@ -520,7 +535,8 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService try { // Open a database connection ConnectionManagerUtil.openDBConnection(); - return deviceOrganizationDao.isChildDeviceIdExist(deviceID); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return deviceOrganizationDao.isChildDeviceIdExist(deviceID, tenantID); } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the database connection to check child deviceID existence " + "in deviceOrganization : deviceID = " + deviceID; 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/DAOTest.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/DAOTest.java index ee0f550adf..a1f7529071 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/DAOTest.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/DAOTest.java @@ -28,6 +28,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.excep import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -53,7 +54,8 @@ public class DAOTest extends BaseDeviceOrganizationTest { int deviceId = 2; int maxDepth = 4; boolean includeDevice = true; - DeviceNodeResult childrenList = deviceOrganizationDAO.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceNodeResult childrenList = deviceOrganizationDAO.getChildrenOfDeviceNode(deviceId, maxDepth, includeDevice, tenantID); ConnectionManagerUtil.closeDBConnection(); Assert.assertNotNull(childrenList, "Cannot be null"); } @@ -64,7 +66,8 @@ public class DAOTest extends BaseDeviceOrganizationTest { int deviceID = 4; int maxDepth = 4; boolean includeDevice = false; - DeviceNodeResult parentList = deviceOrganizationDAO.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceNodeResult parentList = deviceOrganizationDAO.getParentsOfDeviceNode(deviceID, maxDepth, includeDevice, tenantID); ConnectionManagerUtil.closeDBConnection(); Assert.assertNotNull(parentList, "Cannot be null"); } @@ -72,12 +75,13 @@ public class DAOTest extends BaseDeviceOrganizationTest { @Test public void testAddDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); ConnectionManagerUtil.beginDBTransaction(); - deviceOrganizationDAO.deleteDeviceAssociations(1); + deviceOrganizationDAO.deleteDeviceAssociations(1, tenantID); ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.beginDBTransaction(); - deviceOrganizationDAO.deleteDeviceAssociations(2); + deviceOrganizationDAO.deleteDeviceAssociations(2, tenantID); ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.closeDBConnection(); DeviceOrganization deviceOrganization = new DeviceOrganization(); @@ -110,8 +114,9 @@ public class DAOTest extends BaseDeviceOrganizationTest { @Test(dependsOnMethods = "testAddDeviceOrganizationDAO") public void testGetDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); ConnectionManagerUtil.beginDBTransaction(); - DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1); + DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1, tenantID); ConnectionManagerUtil.closeDBConnection(); if (deviceOrganization != null) { log.info("Device Organization device ID : " + deviceOrganization.getDeviceId() + @@ -121,24 +126,27 @@ public class DAOTest extends BaseDeviceOrganizationTest { @Test(dependsOnMethods = "testAddDeviceOrganizationDAO") public void testDoesDeviceIdExistDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); ConnectionManagerUtil.beginDBTransaction(); - boolean isDeviceIdExist = deviceOrganizationDAO.isDeviceIdExist(1); + boolean isDeviceIdExist = deviceOrganizationDAO.isDeviceIdExist(1, tenantID); ConnectionManagerUtil.closeDBConnection(); } @Test(dependsOnMethods = "testAddDeviceOrganizationDAO") public void testDeleteDeviceOrganizationByIDDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); ConnectionManagerUtil.beginDBTransaction(); - boolean result = deviceOrganizationDAO.deleteDeviceOrganizationByID(1); + boolean result = deviceOrganizationDAO.deleteDeviceOrganizationByID(1, tenantID); ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.closeDBConnection(); } @Test(dependsOnMethods = "testAddDeviceOrganizationDAO") public void deleteDeviceOrganizationsByDeviceIdDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); ConnectionManagerUtil.beginDBTransaction(); - boolean result = deviceOrganizationDAO.deleteDeviceAssociations(1); + boolean result = deviceOrganizationDAO.deleteDeviceAssociations(1, tenantID); ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index 507987e71c..158615a137 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -875,6 +875,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT DEFAULT 0, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql index a944be130a..d89dfd584b 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql @@ -21,6 +21,7 @@ -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT DEFAULT 0, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index dd5e1884fd..9a71137454 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -844,6 +844,7 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT DEFAULT 0, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 65eebccc88..e22686343e 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -917,9 +917,10 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ORGANIZATION_ID INT NOT NULL IDENTITY(1,1), - DEVICE_ID INT NOT NULL, - PARENT_DEVICE_ID INT DEFAULT NULL, + ORGANIZATION_ID INTEGER NOT NULL IDENTITY(1,1), + TENANT_ID INTEGER NOT NULL, + DEVICE_ID INTEGER NOT NULL, + PARENT_DEVICE_ID INTEGER DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, LAST_UPDATED_TIMESTAMP BIGINT NOT NULL, PRIMARY KEY (ORGANIZATION_ID), diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index e4ff1dc305..282647ca59 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -911,6 +911,7 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( ORGANIZATION_ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT DEFAULT 0, DEVICE_ID INT(11) NOT NULL, PARENT_DEVICE_ID INT(11) DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 828435236d..93444788a9 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1198,6 +1198,7 @@ CREATE SEQUENCE SUB_OPERATION_TEMPLATE_seq START WITH 1 INCREMENT BY 1; -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( ORGANIZATION_ID NUMBER(10) NOT NULL, + TENANT_ID NUMBER(10) NOT NULL, DEVICE_ID NUMBER(10) NOT NULL, PARENT_DEVICE_ID NUMBER(10) DEFAULT NULL, DEVICE_ORGANIZATION_META CLOB DEFAULT NULL, diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 52e20e467d..d3458c3211 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -851,6 +851,7 @@ CREATE SEQUENCE DM_DEVICE_ORGANIZATION_seq; CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( ORGANIZATION_ID INTEGER DEFAULT NEXTVAL ('DM_DEVICE_ORGANIZATION_seq') NOT NULL, + TENANT_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL, PARENT_DEVICE_ID INTEGER DEFAULT NULL, DEVICE_ORGANIZATION_META TEXT DEFAULT NULL,