deviceOrg: tenant management addition

pull/238/head
Isuri Mendis 11 months ago
parent c993051a07
commit a6af15e3ee

@ -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<DeviceOrganization> getDeviceOrganizationRoots(PaginationRequest request) throws DeviceOrganizationMgtDAOException;
public List<DeviceOrganization> 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<DeviceOrganization> getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException;
public List<DeviceOrganization> 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;
}

@ -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<DeviceNode> childNodes = new ArrayList<>();
Set<DeviceOrganization> 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<DeviceNode> childNodes,
boolean includeDevice,
boolean parentAdded,
int tenantID,
Set<DeviceOrganization> 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<DeviceNode> 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<DeviceNode> parentNodes,
boolean includeDevice,
boolean childAdded,
int tenantID,
Set<DeviceOrganization> 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<DeviceOrganization> getDeviceOrganizationRoots(PaginationRequest request)
public List<DeviceOrganization> getDeviceOrganizationRoots(PaginationRequest request, int tenantID)
throws DeviceOrganizationMgtDAOException {
List<DeviceOrganization> 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<DeviceOrganization> getDeviceOrganizationLeafs(PaginationRequest request) throws DeviceOrganizationMgtDAOException {
public List<DeviceOrganization> getDeviceOrganizationLeafs(PaginationRequest request, int tenantID) throws DeviceOrganizationMgtDAOException {
List<DeviceOrganization> 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

@ -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"));

@ -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);
}
}

@ -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;

@ -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();
}

@ -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,

@ -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,

@ -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,

@ -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),

@ -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,

@ -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,

@ -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,

Loading…
Cancel
Save