Device Organization getChildrenOf Modifications

backup
Isuri Mendis 1 year ago
parent 1e93dcce2b
commit ea0657e221

@ -18,22 +18,79 @@
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import java.sql.Date;
import java.util.List;
/**
* This is responsible for DeviceOrganization related DAO operations.
*/
public interface DeviceOrganizationDAO {
List<Device> getChildDevices(int parentId) throws DeviceOrganizationMgtDAOException;
/**
* retrieve child devices per particular device ID
* @param node
* @param maxDepth
* @param includeDevice
* @return
* @throws DeviceOrganizationMgtDAOException
*/
List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException;
List<Device> getParentDevices(Integer deviceID) throws DeviceOrganizationMgtDAOException;
/**
* retrieve parent devices per particular device ID
* @param deviceID
* @return
* @throws DeviceOrganizationMgtDAOException
*/
// List<Device> getParentDevices(DeviceNode node) throws DeviceOrganizationMgtDAOException;
/**
* add a new reocrd to device organization table
* @param deviceOrganization
* @return
* @throws DeviceOrganizationMgtDAOException
*/
boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException;
boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status, int organizationId)
/**
* update a record in device organization table
* @param deviceID
* @param parentDeviceID
* @param timestamp
* @param organizationId
* @return
* @throws DeviceOrganizationMgtDAOException
*/
boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, int organizationId)
throws DeviceOrganizationMgtDAOException;
/**
*
* @param organizationId
* @return
* @throws DeviceOrganizationMgtDAOException
*/
DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException;
/**
* delete a record from device organization table
* @param organizationId
* @throws DeviceOrganizationMgtDAOException
*/
boolean deleteDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException;
/**
* delete a record associated with a particular device ID from device organization table
* delete a record if the param ID is either device_ID OR parent_device_ID in the device organization table
* @param deviceId
* @return
* @throws DeviceOrganizationMgtDAOException
*/
boolean deleteDeviceAssociations(int deviceId) throws DeviceOrganizationMgtDAOException;
boolean doesDeviceIdExist(int deviceId) throws DeviceOrganizationMgtDAOException;
}

@ -23,10 +23,20 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.u
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so
* that the business objection handling layer doesn't need to be aware of the same providing seamless plug-ability of
* different data sources, connection acquisition mechanisms as well as different forms of DAO implementations to the
* high-level implementations that require Device Organization related metadata persistence.
*/
public class DeviceOrganizationDAOFactory {
private static final Log log = LogFactory.getLog(DeviceOrganizationDAOFactory.class);
private static String databaseEngine;
/**
*
* @param dataSourceConfiguration
*/
public static void init(DataSourceConfig dataSourceConfiguration) {
if (log.isDebugEnabled()) {
log.debug("Initializing Device Organization Data Source");
@ -35,6 +45,10 @@ public class DeviceOrganizationDAOFactory {
databaseEngine = ConnectionManagerUtil.getDatabaseType();
}
/**
*
* @return
*/
public static DeviceOrganizationDAO getDeviceOrganizationDAO() {
if (databaseEngine != null) {
//noinspection SwitchStatementWithTooFewBranches

@ -17,19 +17,25 @@
*/
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.*;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.DeviceOrganizationDaoUtil.getDeviceFromResultSet;
@ -38,84 +44,128 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
private static final Log log = LogFactory.getLog(DeviceOrganizationDAOImpl.class);
@Override
public List<Device> getChildDevices(int parentId) throws DeviceOrganizationMgtDAOException {
List<Device> childDevices = null;
public List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtDAOException {
List<DeviceNode> childNodes = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
String sql = "SELECT d.ID, d.DESCRIPTION, d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.ID = ? AND d.DEVICE_TYPE_ID = t.ID";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, parentId);
try (ResultSet rs = stmt.executeQuery()) {
childDevices = new ArrayList<>();
while (rs.next()) {
childDevices.add(getDeviceFromResultSet(rs));
}
return childDevices;
}
}
getChildrenRecursive(node, maxDepth, visited, conn, childNodes, includeDevice);
return childNodes;
// Prepare SQL query to retrieve children of the node using DM_DEVICE_ORGANIZATION
// String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
// "FROM DM_DEVICE D " +
// "JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.DEVICE_ID " +
// "JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " +
// "WHERE DO.PARENT_DEVICE_ID = ?";
//
// try (PreparedStatement stmt = conn.prepareStatement(sql)) {
// stmt.setInt(1, node.getDeviceId());
// try (ResultSet rs = stmt.executeQuery()) {
// while (rs.next()) {
// DeviceNode child = getDeviceFromResultSet(rs);
// child.setChildren(childNodes);
// childNodes.add(child);
//
// if (maxDepth > 0) {
// List<DeviceNode> deviceChildren = getChildrenOf(child, maxDepth - 1, true);
// childNodes.addAll(deviceChildren);
// }
// }
// return childNodes;
// }
// }
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve all child devices for " +
"parent device ID " +
parentId;
node.getDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to retrieve all child devices for " +
"parent device ID" + parentId;
"parent device ID" + node.getDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
@Override
public List<Device> getParentDevices(Integer deviceID) throws DeviceOrganizationMgtDAOException {
List<Device> parentDevices = null;
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
String sql = "SELECT d.ID, d.DESCRIPTION,d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.ID IN (SELECT ID FROM DM_DEVICE WHERE ID = ?) " +
"AND d.DEVICE_TYPE_ID = t.ID";
private void getChildrenRecursive(DeviceNode node, int maxDepth, Set<Integer> visited, Connection conn,
List<DeviceNode> childNodes, boolean includeDevice) throws SQLException {
if (maxDepth <= 0 || visited.contains(node.getDeviceId())) {
return;
}
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceID);
try (ResultSet rs = stmt.executeQuery()) {
parentDevices = new ArrayList<>();
while (rs.next()) {
parentDevices.add(getDeviceFromResultSet(rs));
}
return parentDevices;
visited.add(node.getDeviceId());
String sql = "SELECT D.ID, D.NAME, D.DESCRIPTION, D.DEVICE_IDENTIFICATION, DT.NAME AS DEVICE_TYPE_NAME " +
"FROM DM_DEVICE D " +
"JOIN DM_DEVICE_ORGANIZATION DO ON D.ID = DO.DEVICE_ID " +
"JOIN DM_DEVICE_TYPE DT ON D.DEVICE_TYPE_ID = DT.ID " +
"WHERE DO.PARENT_DEVICE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, node.getDeviceId());
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceNode child = getDeviceFromResultSet(rs);
child.setChildren(childNodes);
// if (includeDevice) {
// childNodes.add(node); // Add the parent device if includeDevice is true.
// }
childNodes.add(child);
getChildrenRecursive(child, maxDepth - 1, visited, conn, childNodes, includeDevice);
}
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to retrieve all parent devices for " +
"child device ID " +
deviceID;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to retrieve all parent devices for " +
"child device ID" + deviceID;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
// @Override
// public List<DeviceNode> getParentDevices(DeviceNode node) throws DeviceOrganizationMgtDAOException {
// List<DeviceNode> parentDevices = null;
// try {
// Connection conn = ConnectionManagerUtil.getDBConnection();
// String sql = "SELECT do.PARENT_DEVICE_ID,d.ID, d.DESCRIPTION,d.DEVICE_IDENTIFICATION, d.NAME, t.NAME AS DEVICE_TYPE_NAME " +
// "FROM DM_DEVICE d, DM_DEVICE_TYPE t, DM_DEVICE_ORGANIZATION do " +
// "WHERE d.ID IN (SELECT ID FROM DM_DEVICE WHERE ID = ?) " +
// "AND d.ID = do.DEVICE_ID" +
// "AND d.DEVICE_TYPE_ID = t.ID";
//
// try (PreparedStatement stmt = conn.prepareStatement(sql)) {
// stmt.setInt(1, node.getDeviceId());
// try (ResultSet rs = stmt.executeQuery()) {
// parentDevices = new ArrayList<>();
// while (rs.next()) {
// parentDevices.add(getDeviceFromResultSet(rs));
// }
// return parentDevices;
// }
// }
// } catch (DBConnectionException e) {
// String msg = "Error occurred while obtaining DB connection to retrieve all parent devices for " +
// "child device ID " +
// deviceID;
// log.error(msg);
// throw new DeviceOrganizationMgtDAOException(msg, e);
// } catch (SQLException e) {
// String msg = "Error occurred while processing SQL to retrieve all parent devices for " +
// "child device ID" + deviceID;
// log.error(msg);
// throw new DeviceOrganizationMgtDAOException(msg, e);
// }
// }
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtDAOException {
try {
String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP, STATUS)" +
" VALUES (?, ?, ?, ?)";
String sql = "INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP)" +
" VALUES (?, ?, ?)";
Connection conn = ConnectionManagerUtil.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceOrganization.getDeviceId());
stmt.setInt(2, deviceOrganization.getParentDeviceId());
stmt.setDate(3, deviceOrganization.getUpdateTime());
stmt.setString(4, deviceOrganization.getStatus().toString());
return stmt.executeUpdate() > 0;
}
@ -133,19 +183,18 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
@Override
public boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status, int organizationId)
public boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, int organizationId)
throws DeviceOrganizationMgtDAOException {
try {
String sql = "UPDATE DM_DEVICE_ORGANIZATION SET DEVICE_ID = ? , PARENT_DEVICE_ID = ? , " +
"LAST_UPDATED_TIMESTAMP = ? , STATUS = ? WHERE ID = ? ";
"LAST_UPDATED_TIMESTAMP = ? WHERE ID = ? ";
Connection conn = ConnectionManagerUtil.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceID);
stmt.setInt(2, parentDeviceID);
stmt.setDate(3, timestamp);
stmt.setString(4, status);
stmt.setInt(5, organizationId);
stmt.setInt(4, organizationId);
return stmt.executeUpdate() > 0;
}
@ -165,7 +214,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
@Override
public DeviceOrganization getDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException {
try {
String sql = "SELECT do.ID,do.DEVICE_ID, do.PARENT_DEVICE_ID, do.LAST_UPDATED_TIMESTAMP, do.STATUS " +
String sql = "SELECT do.ID,do.DEVICE_ID, do.PARENT_DEVICE_ID, do.LAST_UPDATED_TIMESTAMP " +
"FROM DM_DEVICE_ORGANIZATION do WHERE do.ID = ? ";
Connection conn = ConnectionManagerUtil.getDBConnection();
@ -175,6 +224,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
if (rs.next()) {
return DeviceOrganizationDaoUtil.loadDeviceOrganization(rs);
}
log.info("No Device Organization found");
return null;
}
}
@ -192,4 +242,92 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO {
}
}
@Override
public boolean deleteDeviceOrganizationByID(int organizationId) throws DeviceOrganizationMgtDAOException {
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
String deleteOrganizationSql = "DELETE FROM DM_DEVICE_ORGANIZATION WHERE ID = ?";
try (PreparedStatement deleteOrgStmt = conn.prepareStatement(deleteOrganizationSql)) {
// Delete the organization
deleteOrgStmt.setInt(1, organizationId);
return deleteOrgStmt.executeUpdate() > 0;
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to delete device organization for " +
organizationId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to delete device organization details for " +
organizationId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
@Override
public boolean deleteDeviceAssociations(int deviceId) 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 = ?";
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);
// Delete device organizations where the device is the parent_device_id
deleteByParentDeviceIdStmt.setInt(1, deviceId);
return deleteByDeviceIdStmt.executeUpdate() > 0 | deleteByParentDeviceIdStmt.executeUpdate() > 0;
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to delete device organization for device ID" +
deviceId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to delete device organization details for device ID " +
deviceId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
@Override
public boolean doesDeviceIdExist(int deviceId) throws DeviceOrganizationMgtDAOException {
try {
Connection conn = ConnectionManagerUtil.getDBConnection();
String sql = "SELECT 1 " +
"FROM DM_DEVICE_ORGANIZATION " +
"WHERE device_id = ? OR parent_device_id = ? " +
"LIMIT 1";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId);
stmt.setInt(2, deviceId);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next(); // Returns true if a match is found, false otherwise
}
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining DB connection to query device organization for device ID" +
deviceId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while processing SQL to query device organization details for device ID " +
deviceId;
log.error(msg);
throw new DeviceOrganizationMgtDAOException(msg, e);
}
}
}

@ -17,12 +17,12 @@
*/
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.IllegalTransactionStateException;
import io.entgra.device.mgt.core.device.mgt.core.config.datasource.DataSourceConfig;
import io.entgra.device.mgt.core.device.mgt.core.config.datasource.JNDILookupDefinition;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.naming.InitialContext;
import javax.sql.DataSource;
@ -37,6 +37,10 @@ public class ConnectionManagerUtil {
private static final ThreadLocal<Connection> currentConnection = new ThreadLocal<>();
private static DataSource dataSource;
/**
*
* @throws DBConnectionException
*/
public static void openDBConnection() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn != null) {
@ -50,6 +54,11 @@ public class ConnectionManagerUtil {
currentConnection.set(conn);
}
/**
*
* @return
* @throws DBConnectionException
*/
public static Connection getDBConnection() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
@ -63,6 +72,10 @@ public class ConnectionManagerUtil {
return conn;
}
/**
*
* @throws DBConnectionException
*/
public static void beginDBTransaction() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
@ -78,6 +91,10 @@ public class ConnectionManagerUtil {
}
}
/**
*
* @throws DBConnectionException
*/
public static void endDBTransaction() throws DBConnectionException {
Connection conn = currentConnection.get();
if (conn == null) {
@ -95,6 +112,9 @@ public class ConnectionManagerUtil {
}
}
/**
*
*/
public static void commitDBTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
@ -112,6 +132,9 @@ public class ConnectionManagerUtil {
}
}
/**
*
*/
public static void rollbackDBTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
@ -129,6 +152,9 @@ public class ConnectionManagerUtil {
}
}
/**
*
*/
public static void closeDBConnection() {
Connection conn = currentConnection.get();
if (conn == null) {
@ -142,6 +168,11 @@ public class ConnectionManagerUtil {
currentConnection.remove();
}
/**
*
* @param conn
* @return
*/
private static boolean inTransaction(Connection conn) {
boolean inTransaction = true;
try {
@ -154,6 +185,11 @@ public class ConnectionManagerUtil {
return inTransaction;
}
/**
*
* @return
* @throws DBConnectionException
*/
public static boolean isTransactionStarted() throws DBConnectionException {
Connection connection = getDBConnection();
return inTransaction(connection);
@ -191,6 +227,12 @@ public class ConnectionManagerUtil {
return dataSource;
}
/**
*
* @param dataSourceName
* @param jndiProperties
* @return
*/
public static DataSource lookupDataSource(String dataSourceName,
final Hashtable<Object, Object> jndiProperties) {
@ -207,6 +249,10 @@ public class ConnectionManagerUtil {
}
}
/**
*
* @return
*/
public static String getDatabaseType() {
try {
return dataSource.getConnection().getMetaData().getDatabaseProductName();

@ -2,6 +2,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -16,6 +17,12 @@ public class DeviceOrganizationDaoUtil {
private static final Log log = LogFactory.getLog(DeviceOrganizationDaoUtil.class);
/**
* Helper method to create a Device Organization object from a ResultSet
* @param rs
* @return
* @throws SQLException
*/
public static DeviceOrganization loadDeviceOrganization(ResultSet rs) throws SQLException {
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
@ -23,19 +30,26 @@ public class DeviceOrganizationDaoUtil {
deviceOrganization.setDeviceId(rs.getInt("DEVICE_ID"));
deviceOrganization.setParentDeviceId(rs.getInt("PARENT_DEVICE_ID"));
deviceOrganization.setUpdateTime(rs.getDate("LAST_UPDATED_TIMESTAMP"));
deviceOrganization.setStatus(DeviceOrganization.DeviceOrganizationStatus.valueOf(rs.getString("STATUS")));
return deviceOrganization;
}
// Helper method to create a Device object from a ResultSet
public static Device getDeviceFromResultSet(ResultSet rs) throws SQLException {
/**
* Helper method to create a Device object from a ResultSet
* @param rs
* @return
* @throws SQLException
*/
public static DeviceNode getDeviceFromResultSet(ResultSet rs) throws SQLException {
DeviceNode node = new DeviceNode();
node.setDeviceId(rs.getInt("ID"));
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"));
return device;
node.setDevice(device);
return node;
}
}

@ -17,9 +17,14 @@
*/
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.ArrayList;
import java.util.List;
import io.entgra.device.mgt.core.device.mgt.common.Device;
/**
* This class is used in DeviceOrganizationService
*/
public class DeviceNode {
private int deviceId;

@ -21,13 +21,15 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto;
import java.sql.Date;
/**
* This class is used in DeviceOrganizationService
*/
public abstract class DeviceOrganization {
private int organizationId;
private int deviceId;
private int parentDeviceId;
private Date updateTime;
private DeviceOrganizationStatus status;
public int getOrganizationId() {
return organizationId;
@ -61,15 +63,4 @@ public abstract class DeviceOrganization {
this.updateTime = updateTime;
}
public DeviceOrganizationStatus getStatus() {
return status;
}
public void setStatus(DeviceOrganizationStatus status) {
this.status = status;
}
public enum DeviceOrganizationStatus {
ACT, INA
}
}

@ -18,13 +18,20 @@
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception;
/**
* Exception thrown during the DeviceOrganization Management DAO operations.
*/
public class DeviceOrganizationMgtDAOException extends Exception {
private static final long serialVersionUID = 2412162605436684110L;
private String errorMessage;
public DeviceOrganizationMgtDAOException() { super(); }
public DeviceOrganizationMgtDAOException() {
super();
}
public DeviceOrganizationMgtDAOException(Throwable cause) { super(cause); }
public DeviceOrganizationMgtDAOException(Throwable cause) {
super(cause);
}
public DeviceOrganizationMgtDAOException(String msg, Exception nestedEx) {
super(msg, nestedEx);

@ -18,6 +18,9 @@
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception;
/**
* Exception thrown during the DeviceOrganization Management operations.
*/
public class DeviceOrganizationMgtPluginException extends Exception {
private static final long serialVersionUID = -7686100672332447443L;

@ -23,7 +23,6 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.D
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.BadRequestException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException;
@ -53,10 +52,10 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
ConnectionManagerUtil.openDBConnection();
List<DeviceNode> children = new ArrayList<>();
if (includeDevice) {
children.add(node);
if (maxDepth <= 0) {
return children;
}
retrieveChildren(node, children, 1, maxDepth);
children = deviceOrganizationDao.getChildrenOf(node, maxDepth, includeDevice);
return children;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve child devices";
@ -72,146 +71,230 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
}
// @Override
// public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
// throws DeviceOrganizationMgtPluginException {
// try {
// // Open a database connection
// ConnectionManagerUtil.openDBConnection();
//
// List<DeviceNode> parents = new ArrayList<>();
// if (includeDevice) {
// parents.add(node);
// }
// retrieveParents(node, parents, 1, maxDepth);
// return parents;
// } catch (DBConnectionException e) {
// String msg = "Error occurred while obtaining the database connection to retrieve parent devices";
// log.error(msg);
// throw new DeviceOrganizationMgtPluginException(msg, e);
// } catch (DeviceOrganizationMgtDAOException e) {
// String msg = "Error occurred in the database level while retrieving parent devices";
// log.error(msg);
// throw new DeviceOrganizationMgtPluginException(msg, e);
// } finally {
// // Close the database connection
// ConnectionManagerUtil.closeDBConnection();
// }
// }
// private void retrieveParents(DeviceNode node, List<DeviceNode> result, int currentDepth, int maxDepth)
// throws DeviceOrganizationMgtDAOException {
// if (currentDepth > maxDepth) {
// return;
// }
//
// List<Device> parentDevices = deviceOrganizationDao.getParentDevices(node);
//
// for (Device parentDevice : parentDevices) {
// DeviceNode parentNode = new DeviceNode();
// parentNode.setDeviceId(parentDevice.getId());
// parentNode.setDevice(parentDevice);
//
// result.add(parentNode);
//
// if (currentDepth < maxDepth) {
// retrieveParents(parentNode, result, currentDepth + 1, maxDepth);
// }
// }
// }
@Override
public List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtPluginException {
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
String msg = "";
List<DeviceNode> parents = new ArrayList<>();
if (includeDevice) {
parents.add(node);
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization);
if (result) {
msg = "Device organization added successfully,for " + deviceOrganization.getDeviceId();
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to add,for " + deviceOrganization.getDeviceId();
throw new DeviceOrganizationMgtPluginException(msg);
}
retrieveParents(node, parents, 1, maxDepth);
return parents;
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve parent devices";
msg = "Error occurred while obtaining the database connection to add device organization for " +
deviceOrganization.getDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving parent devices";
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while adding device organization for " +
deviceOrganization.getDeviceId();
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
// Close the database connection
ConnectionManagerUtil.closeDBConnection();
}
}
private void retrieveParents(DeviceNode node, List<DeviceNode> result, int currentDepth, int maxDepth)
throws DeviceOrganizationMgtDAOException {
if (currentDepth > maxDepth) {
return;
@Override
public boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp,
int organizationId) throws DeviceOrganizationMgtPluginException {
String msg = "";
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organizationId);
if (deviceOrganization == null) {
String errorMsg = "Cannot find device organization for organization ID" + organizationId;
log.error(errorMsg);
return false;
}
List<Device> parentDevices = deviceOrganizationDao.getParentDevices(node.getDeviceId());
for (Device parentDevice : parentDevices) {
DeviceNode parentNode = new DeviceNode();
parentNode.setDeviceId(parentDevice.getId());
parentNode.setDevice(parentDevice);
result.add(parentNode);
if (currentDepth < maxDepth) {
retrieveParents(parentNode, result, currentDepth + 1, maxDepth);
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.updateDeviceOrganization(deviceID, parentDeviceID, timestamp,
organizationId);
if (result) {
msg = "Device organization updated successfully,for " + organizationId;
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to update,for " + organizationId;
throw new DeviceOrganizationMgtPluginException(msg);
}
}
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to update device organization for " + organizationId;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while updating device organization for " + organizationId;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
private void retrieveChildren(DeviceNode node, List<DeviceNode> result, int currentDepth, int maxDepth)
throws DeviceOrganizationMgtDAOException {
if (currentDepth > maxDepth) {
return;
}
}
List<Device> childDevices = deviceOrganizationDao.getChildDevices(node.getDeviceId());
for (Device childDevice : childDevices) {
DeviceNode childNode = new DeviceNode();
childNode.setDeviceId(childDevice.getId());
childNode.setDevice(childDevice);
result.add(childNode);
if (currentDepth < maxDepth) {
retrieveChildren(childNode, result, currentDepth + 1, maxDepth);
}
@Override
public DeviceOrganization getDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException {
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
DeviceOrganization deviceOrganization = deviceOrganizationDao.getDeviceOrganizationByID(organizationId);
return deviceOrganization;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve child devices";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving child devices";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
// Close the database connection
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
public boolean deleteDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException {
String msg = "";
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organizationId);
if (deviceOrganization == null) {
msg = "Cannot find device organization for organization ID " + organizationId;
log.error(msg);
return false;
}
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.addDeviceOrganization(deviceOrganization);
boolean result = deviceOrganizationDao.deleteDeviceOrganizationByID(organizationId);
if (result) {
msg = "Device organization added successfully,for " + deviceOrganization.getDeviceId();
msg = "Device organization record deleted successfully,for " + organizationId;
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to add,for " + deviceOrganization.getDeviceId();
msg = "Device organization failed to delete,for " + organizationId;
throw new DeviceOrganizationMgtPluginException(msg);
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to add device organization for " +
deviceOrganization.getDeviceId();
msg = "Error occurred while obtaining the database connection to delete device organization for " + organizationId;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while adding device organization for " +
deviceOrganization.getDeviceId();
msg = "Error occurred in the database level while deleting device organization for " + organizationId;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status,
int organizationId) throws DeviceOrganizationMgtPluginException {
public boolean deleteDeviceAssociations(int deviceId)
throws DeviceOrganizationMgtPluginException {
String msg = "";
DeviceOrganization deviceOrganization = getDeviceOrganizationByID(organizationId);
if (deviceOrganization == null) {
String errorMsg = "Cannot find device organization for organization ID" + organizationId;
log.error(errorMsg);
throw new NullPointerException(errorMsg);
// throw new BadRequestException(errorMsg);
boolean deviceIdExist = doesDeviceIdExist(deviceId);
if (!deviceIdExist) {
msg = "Cannot find device organization associated with device ID " + deviceId;
log.error(msg);
return false;
}
try {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDao.updateDeviceOrganization(deviceID, parentDeviceID, timestamp, status,
organizationId);
boolean result = deviceOrganizationDao.deleteDeviceAssociations(deviceId);
if (result) {
msg = "Device organization updated successfully,for " + deviceID;
msg = "Device organization records deleted successfully,for " + deviceId;
if (log.isDebugEnabled()) {
log.debug(msg);
}
} else {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Device organization failed to update,for " + deviceID;
msg = "Device organization failed to delete,for " + deviceId;
throw new DeviceOrganizationMgtPluginException(msg);
}
ConnectionManagerUtil.commitDBTransaction();
return true;
} catch (DBConnectionException e) {
msg = "Error occurred while obtaining the database connection to update device organization for " + deviceID;
msg = "Error occurred while obtaining the database connection to delete device organization for " + deviceId;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
msg = "Error occurred in the database level while updating device organization for " + deviceID;
msg = "Error occurred in the database level while deleting device organization for " + deviceId;
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {
@ -221,19 +304,19 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService
}
@Override
public DeviceOrganization getDeviceOrganizationByID(int organizationId)
public boolean doesDeviceIdExist(int deviceId)
throws DeviceOrganizationMgtPluginException {
try {
// Open a database connection
ConnectionManagerUtil.openDBConnection();
DeviceOrganization deviceOrganization = deviceOrganizationDao.getDeviceOrganizationByID(organizationId);
return deviceOrganization;
boolean deviceIdExist = deviceOrganizationDao.doesDeviceIdExist(deviceId);
return deviceIdExist;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve child devices";
String msg = "Error occurred while obtaining the database connection to check deviceID exists";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} catch (DeviceOrganizationMgtDAOException e) {
String msg = "Error occurred in the database level while retrieving child devices";
String msg = "Error occurred in the database level while checking the existence of deviceID";
log.error(msg);
throw new DeviceOrganizationMgtPluginException(msg, e);
} finally {

@ -17,7 +17,6 @@
*/
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.internal;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService;
import org.wso2.carbon.registry.core.service.RegistryService;
@ -30,6 +29,7 @@ public class DeviceOrganizationMgtDataHolder {
private DeviceOrganizationMgtDataHolder() {
}
public static DeviceOrganizationMgtDataHolder getInstance() {
return thisInstance;
}

@ -32,7 +32,7 @@ import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.registry.core.service.RegistryService;
/**
* @scr.component name="io.entgra.device.mgt.core.subtype.mgt.internal.DeviceSubTypeMgtServiceComponent" immediate="true"
* @scr.component name="io.entgra.device.mgt.core.subtype.mgt.internal.DeviceOrganizationMgtServiceComponent" immediate="true"
* @scr.reference name="org.wso2.carbon.device.manager"
* interface="io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService"
* cardinality="1..1"

@ -26,15 +26,28 @@ import java.util.List;
public interface DeviceOrganizationService {
List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException;
List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice) throws DeviceOrganizationMgtPluginException;
boolean addDeviceOrganization(DeviceOrganization deviceOrganization)
throws DeviceOrganizationMgtPluginException;
boolean addDeviceOrganization(DeviceOrganization deviceOrganization) throws DeviceOrganizationMgtPluginException;
List<DeviceNode> getChildrenOf(DeviceNode node, int maxDepth, boolean includeDevice)
throws DeviceOrganizationMgtPluginException;
boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, String status,
int organizationId) throws DeviceOrganizationMgtPluginException;
// List<DeviceNode> getParentsOf(DeviceNode node, int maxDepth, boolean includeDevice)
// throws DeviceOrganizationMgtPluginException;
DeviceOrganization getDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException;
boolean doesDeviceIdExist(int deviceId)
throws DeviceOrganizationMgtPluginException;
boolean updateDeviceOrganization(int deviceID, int parentDeviceID, Date timestamp, int organizationId)
throws DeviceOrganizationMgtPluginException;
boolean deleteDeviceOrganizationByID(int organizationId)
throws DeviceOrganizationMgtPluginException;
boolean deleteDeviceAssociations(int deviceId)
throws DeviceOrganizationMgtPluginException;
}

@ -4,6 +4,7 @@ import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNode;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException;
import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException;
@ -32,42 +33,54 @@ public class DAOTest extends BaseDeviceOrganizationTest {
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetChildrenOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection();
List<Device> childrenList = deviceOrganizationDAO.getChildDevices(3);
DeviceNode node = new DeviceNode();
node.setDeviceId(2);
int maxDepth = 4;
boolean includeDevice = true;
List<DeviceNode> childrenList = deviceOrganizationDAO.getChildrenOf(node, maxDepth, includeDevice);
ConnectionManagerUtil.closeDBConnection();
log.info(childrenList.size());
Assert.assertNotNull(childrenList, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.openDBConnection();
List<Device> parentList = deviceOrganizationDAO.getParentDevices(4);
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(parentList, "Cannot be null");
}
// @Test(dependsOnMethods = "testAddDeviceOrganization")
// public void testGetParentsOf() throws DBConnectionException, DeviceOrganizationMgtDAOException {
// ConnectionManagerUtil.openDBConnection();
// List<Device> parentList = deviceOrganizationDAO.getParentDevices(4);
// ConnectionManagerUtil.closeDBConnection();
// Assert.assertNotNull(parentList, "Cannot be null");
// }
@Test
public void testAddDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException {
DeviceOrganization.DeviceOrganizationStatus status = DeviceOrganization.DeviceOrganizationStatus.ACT;
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
deviceOrganization.setStatus(status);
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
DeviceOrganization deviceOrganization1 = new DeviceOrganization() {
};
deviceOrganization1.setDeviceId(3);
deviceOrganization1.setParentDeviceId(2);
deviceOrganization1.setUpdateTime(new Date(System.currentTimeMillis()));
ConnectionManagerUtil.beginDBTransaction();
boolean result1 = deviceOrganizationDAO.addDeviceOrganization(deviceOrganization1);
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null");
Assert.assertNotNull(result1, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "ACTIVE", 1);
boolean result = deviceOrganizationDAO.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), 1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
@ -75,22 +88,42 @@ public class DAOTest extends BaseDeviceOrganizationTest {
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganizationInactivate() throws DBConnectionException, DeviceOrganizationMgtDAOException {
public void testGetDeviceOrganizationByID() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "INACTIVE", 1);
DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
if(deviceOrganization != null){
log.info("Device Organization device ID : " + deviceOrganization.getDeviceId()+
" ,Device Organization Parent Device ID : " + deviceOrganization.getParentDeviceId());
}
}
Assert.assertNotNull(result, "Cannot be null");
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDoesDeviceIdExist() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean doesDeviceIdExist = deviceOrganizationDAO.doesDeviceIdExist(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(doesDeviceIdExist, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganization() throws DBConnectionException, DeviceOrganizationMgtDAOException {
public void testDeleteDeviceOrganizationByID() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
DeviceOrganization deviceOrganization = deviceOrganizationDAO.getDeviceOrganizationByID(1);
boolean result = deviceOrganizationDAO.deleteDeviceOrganizationByID(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null");
}
Assert.assertNotNull(deviceOrganization, "Cannot be null");
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void deleteDeviceOrganizationsByDeviceId() throws DBConnectionException, DeviceOrganizationMgtDAOException {
ConnectionManagerUtil.beginDBTransaction();
boolean result = deviceOrganizationDAO.deleteDeviceAssociations(1);
ConnectionManagerUtil.commitDBTransaction();
ConnectionManagerUtil.closeDBConnection();
Assert.assertNotNull(result, "Cannot be null");
}
}

@ -7,7 +7,6 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.
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.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@ -30,7 +29,8 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = new DeviceOrganization(){};
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
}
@ -40,7 +40,7 @@ public class ServiceNegativeTest extends BaseDeviceOrganizationTest {
public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.updateDeviceOrganization(2,3,
new Date(System.currentTimeMillis()), "INACTIVE", 5);
boolean result = deviceOrganizationService.updateDeviceOrganization(2, 3,
new Date(System.currentTimeMillis()), 5);
}
}

@ -37,27 +37,25 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
Assert.assertNotNull(childrenList, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
DeviceNode deviceNode = new DeviceNode();
deviceNode.setDeviceId(4);
List<DeviceNode> parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, true);
Assert.assertNotNull(parentList, "Cannot be null");
}
// @Test(dependsOnMethods = "testAddDeviceOrganization")
// public void testGetParentsOf() throws DeviceOrganizationMgtPluginException {
//
// DeviceNode deviceNode = new DeviceNode();
// deviceNode.setDeviceId(4);
// List<DeviceNode> parentList = deviceOrganizationService.getParentsOf(deviceNode, 2, false);
//
// Assert.assertNotNull(parentList, "Cannot be null");
// }
@Test
public void testAddDeviceOrganization() throws DeviceOrganizationMgtPluginException {
DeviceOrganization.DeviceOrganizationStatus status = DeviceOrganization.DeviceOrganizationStatus.ACT;
DeviceOrganization deviceOrganization = new DeviceOrganization() {
};
deviceOrganization.setDeviceId(4);
deviceOrganization.setParentDeviceId(3);
deviceOrganization.setUpdateTime(new Date(System.currentTimeMillis()));
deviceOrganization.setStatus(status);
boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization);
Assert.assertNotNull(result, "Cannot be null");
@ -67,24 +65,34 @@ public class ServiceTest extends BaseDeviceOrganizationTest {
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganization() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "ACTIVE", 1);
boolean result = deviceOrganizationService.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), 1);
Assert.assertNotNull(result, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testUpdateDeviceOrganizationInactivate() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.updateDeviceOrganization(4, 2, new Date(System.currentTimeMillis()), "INACTIVE", 1);
public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
Assert.assertNotNull(result, "Cannot be null");
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(5);
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testGetDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException {
DeviceOrganization deviceOrganization = deviceOrganizationService.getDeviceOrganizationByID(1);
boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(1);
Assert.assertNotNull(deviceOrganization, "Cannot be null");
Assert.assertNotNull(deviceIdExist, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void testDeleteDeviceOrganizationByID() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.deleteDeviceOrganizationByID(5);
Assert.assertNotNull(result, "Cannot be null");
}
@Test(dependsOnMethods = "testAddDeviceOrganization")
public void deleteDeviceAssociations() throws DeviceOrganizationMgtPluginException {
boolean result = deviceOrganizationService.deleteDeviceAssociations(1);
Assert.assertNotNull(result, "Cannot be null");
}
}

@ -17,7 +17,21 @@
*/
package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock;
import java.sql.*;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.NClob;
import java.sql.PreparedStatement;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Savepoint;
import java.sql.Statement;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@ -20,7 +20,11 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
import java.sql.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.RowIdLifetime;
import java.sql.SQLException;
public class MockDatabaseMetaData implements DatabaseMetaData {
private final String url;

@ -22,7 +22,21 @@ import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@ -22,7 +22,23 @@ import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@ -807,10 +807,9 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE (
-- DM_DEVICE_ORGANIZATION TABLE--
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INT(11) DEFAULT NULL,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,

@ -20,10 +20,9 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION (
ID INTEGER auto_increment NOT NULL,
DEVICE_ID INT(11) DEFAULT NULL,
DEVICE_ID INT(11) NOT NULL,
PARENT_DEVICE_ID INT(11) DEFAULT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL,
STATUS VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID)
REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,

Loading…
Cancel
Save