Add DEVICE_TYPE and DEVICE_IDENTIFICATION fields to DM_ENROLLMENT table

apps-11010^2
Charitha Goonetilleke 7 months ago
parent 3abe68788c
commit fb79d61b60

@ -125,6 +125,8 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT
(
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(255) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -149,6 +149,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION (
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -477,6 +477,7 @@ public interface DeviceDAO {
* @return returns list of device types.
* @throws DeviceManagementDAOException
*/
@Deprecated
List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException;
/**

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.dao;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo.Status;
@ -26,7 +27,8 @@ import java.util.List;
public interface EnrollmentDAO {
EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException;
EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier,
EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException;
int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException;

@ -17,6 +17,7 @@
*/
package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
@ -39,24 +40,29 @@ import java.util.List;
public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
@Override
public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
public EnrolmentInfo addEnrollment(int deviceId, DeviceIdentifier deviceIdentifier, EnrolmentInfo enrolmentInfo,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " +
"DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, new String[] {"id"});
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, DEVICE_TYPE, DEVICE_IDENTIFICATION, OWNER, OWNERSHIP, " +
"STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
Timestamp enrollmentTime = new Timestamp(new Date().getTime());
stmt = conn.prepareStatement(sql, new String[]{"id"});
stmt.setInt(1, deviceId);
stmt.setString(2, enrolmentInfo.getOwner());
stmt.setString(3, enrolmentInfo.getOwnership().toString());
stmt.setString(4, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(5, enrollmentTime);
stmt.setTimestamp(6, enrollmentTime);
stmt.setInt(7, tenantId);
stmt.setString(2, deviceIdentifier.getType());
stmt.setString(3, deviceIdentifier.getId());
stmt.setString(4, enrolmentInfo.getOwner());
stmt.setString(5, enrolmentInfo.getOwnership().toString());
stmt.setString(6, enrolmentInfo.getStatus().toString());
stmt.setBoolean(7, enrolmentInfo.isTransferred());
stmt.setTimestamp(8, enrollmentTime);
stmt.setTimestamp(9, enrollmentTime);
stmt.setInt(10, tenantId);
stmt.execute();
rs = stmt.getGeneratedKeys();
@ -70,7 +76,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
return null;
} catch (SQLException e) {
e.printStackTrace();
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
@ -81,7 +86,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " +
@ -92,12 +96,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
stmt.setInt(4, enrolmentInfo.getId());
stmt.setInt(5, tenantId);
int updatedCount = stmt.executeUpdate();
return updatedCount;
return stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
}
@ -105,7 +108,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
public boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
boolean status = false;
int updateStatus = -1;
try {
@ -132,7 +134,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return status;
}
@ -147,7 +149,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
try {
conn = this.getConnection();
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt = conn.prepareStatement(sql, new String[]{"id"});
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
@ -177,12 +179,12 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
stmt.setString(1, owner);
stmt.setInt(2, tenantID);
rs = stmt.executeQuery();
if(rs.next()){
if (rs.next()) {
count = rs.getInt("COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while trying to get device " +
"count of Owner : "+owner, e);
"count of Owner : " + owner, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
@ -197,11 +199,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
@Override
public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId)
throws DeviceManagementDAOException{
throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp updateTime = new Timestamp(new Date().getTime());
if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){
if (getCountOfDevicesOfOwner(currentOwner, tenantId) > 0) {
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?";
@ -236,8 +238,8 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
stmt.setInt(3, enrolmentID);
stmt.setInt(4, tenantId);
int updatedRowCount = stmt.executeUpdate();
if (updatedRowCount != 1){
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+
if (updatedRowCount != 1) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: " +
updatedRowCount + " rows were updated instead of one row!!!");
}
// save the device status history
@ -254,10 +256,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
return addDeviceStatus(config.getId(), config.getStatus());
}
public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (changedBy == null){
if (changedBy == null) {
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
}
PreparedStatement stmt = null;
@ -281,7 +284,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
if (conn.getMetaData().supportsBatchUpdates()) {
for(int[] info: enrolmentInfoList){
for (int[] info : enrolmentInfoList) {
ps.setInt(1, info[0]);
ps.setInt(2, info[1]);
ps.setString(3, status.toString());
@ -296,7 +299,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
}
} else {
for(int[] info: enrolmentInfoList){
for (int[] info : enrolmentInfoList) {
ps.setInt(1, info[0]);
ps.setInt(2, info[1]);
ps.setString(3, status.toString());
@ -320,14 +323,15 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException {
Connection conn;
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (changedBy == null){
if (changedBy == null) {
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
}
PreparedStatement stmt = null;
try {
conn = this.getConnection();
// get the device id and last udpated status from the device status table
String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1";
// get the device id and last updated status from the device status table
String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS " +
"WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
ResultSet rs = stmt.executeQuery();
@ -353,12 +357,14 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
} else {
// if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment
// id is invalid
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId);
throw new DeviceManagementDAOException("Error occurred while setting the status of " +
"device enrolment: no record for enrolment id " + enrolmentId);
}
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) " +
"VALUES(?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
Timestamp updateTime = new Timestamp(new Date().getTime());
stmt.setInt(1, enrolmentId);
@ -377,6 +383,7 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
return true;
}
@Override
public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
@ -534,11 +541,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
}
private Connection getConnection() throws SQLException {
protected Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
protected EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));

@ -21,7 +21,6 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.device.mgt.common.event.config.EventConfig;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.dao.EventConfigDAO;
import io.entgra.device.mgt.core.device.mgt.core.dao.EventManagementDAOException;
import io.entgra.device.mgt.core.device.mgt.core.dao.EventManagementDAOFactory;
@ -30,12 +29,9 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class AbstractEventConfigDAO implements EventConfigDAO {
private static final Log log = LogFactory.getLog(AbstractEventConfigDAO.class);

@ -17,532 +17,8 @@
*/
package io.entgra.device.mgt.core.device.mgt.core.dao.impl.enrolment;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.dao.impl.AbstractEnrollmentDAOImpl;
import io.entgra.device.mgt.core.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class GenericEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl {
@Override
public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " +
"DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, new String[] {"id"});
Timestamp enrollmentTime = new Timestamp(new Date().getTime());
stmt.setInt(1, deviceId);
stmt.setString(2, enrolmentInfo.getOwner());
stmt.setString(3, enrolmentInfo.getOwnership().toString());
stmt.setString(4, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(5, enrollmentTime);
stmt.setTimestamp(6, enrollmentTime);
stmt.setInt(7, tenantId);
stmt.execute();
rs = stmt.getGeneratedKeys();
if (rs.next()) {
int enrolmentId = rs.getInt(1);
enrolmentInfo.setId(enrolmentId);
enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime());
enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime());
addDeviceStatus(enrolmentId, enrolmentInfo.getStatus());
return enrolmentInfo;
}
return null;
} catch (SQLException e) {
e.printStackTrace();
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " +
"WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, enrolmentInfo.getOwnership().toString());
stmt.setString(2, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
stmt.setInt(4, enrolmentInfo.getId());
stmt.setInt(5, tenantId);
int updatedCount = stmt.executeUpdate();
return updatedCount;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
boolean status = false;
int updateStatus = -1;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?";
stmt = conn.prepareStatement(sql);
if (conn.getMetaData().supportsBatchUpdates()) {
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
stmt.setString(1, enrolmentInfo.getStatus().toString());
stmt.setInt(2, enrolmentInfo.getId());
stmt.addBatch();
}
updateStatus = stmt.executeBatch().length;
} else {
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
stmt.setString(1, enrolmentInfo.getStatus().toString());
stmt.setInt(2, enrolmentInfo.getId());
updateStatus = stmt.executeUpdate();
}
}
if (updateStatus > 0) {
status = true;
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return status;
}
@Override
public int removeEnrollment(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int status = -1;
try {
conn = this.getConnection();
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
if (rs.next()) {
status = 1;
}
return status;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int count = 0;
try {
conn = this.getConnection();
String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(checkQuery);
stmt.setString(1, owner);
stmt.setInt(2, tenantID);
rs = stmt.executeQuery();
if(rs.next()){
count = rs.getInt("COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while trying to get device " +
"count of Owner : "+owner, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return count;
}
@Override
public boolean setStatus(String currentOwner, EnrolmentInfo.Status status,
int tenantId) throws DeviceManagementDAOException {
return setStatusAllDevices(currentOwner, status, tenantId);
}
@Override
public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId)
throws DeviceManagementDAOException{
Connection conn;
PreparedStatement stmt = null;
Timestamp updateTime = new Timestamp(new Date().getTime());
if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString());
stmt.setTimestamp(2, updateTime);
stmt.setString(3, currentOwner);
stmt.setInt(4, tenantId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return addDeviceStatus(currentOwner, status, tenantId);
} else {
return false;
}
}
@Override
public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp updateTime = new Timestamp(new Date().getTime());
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString());
stmt.setTimestamp(2, updateTime);
stmt.setInt(3, enrolmentID);
stmt.setInt(4, tenantId);
int updatedRowCount = stmt.executeUpdate();
if (updatedRowCount != 1){
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+
updatedRowCount + " rows were updated instead of one row!!!");
}
// save the device status history
addDeviceStatus(enrolmentID, status);
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return true;
}
public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException {
return addDeviceStatus(config.getId(), config.getStatus());
}
public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (changedBy == null){
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
}
PreparedStatement stmt = null;
ResultSet rs = null;
List<int[]> enrolmentInfoList = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, currentOwner);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
while (rs.next()) {
int enrolmentId = rs.getInt("ID");
int deviceId = rs.getInt("DEVICE_ID");
enrolmentInfoList.add(new int[]{enrolmentId, deviceId});
}
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
Timestamp updateTime = new Timestamp(new Date().getTime());
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
if (conn.getMetaData().supportsBatchUpdates()) {
for(int[] info: enrolmentInfoList){
ps.setInt(1, info[0]);
ps.setInt(2, info[1]);
ps.setString(3, status.toString());
ps.setTimestamp(4, updateTime);
ps.setString(5, changedBy);
ps.addBatch();
}
int[] batchResult = ps.executeBatch();
for (int i : batchResult) {
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
return false;
}
}
} else {
for(int[] info: enrolmentInfoList){
ps.setInt(1, info[0]);
ps.setInt(2, info[1]);
ps.setString(3, status.toString());
ps.setTimestamp(4, updateTime);
ps.setString(5, changedBy);
ps.execute();
}
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
"information of owner '" + currentOwner + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return true;
}
public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException {
Connection conn;
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (changedBy == null){
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
}
PreparedStatement stmt = null;
try {
conn = this.getConnection();
// get the device id and last udpated status from the device status table
String sql = "SELECT DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC LIMIT 1";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
ResultSet rs = stmt.executeQuery();
int deviceId = -1;
EnrolmentInfo.Status previousStatus = null;
if (rs.next()) {
// if there is a record corresponding to the enrolment we save the status and the device id
previousStatus = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
deviceId = rs.getInt("DEVICE_ID");
}
DeviceManagementDAOUtil.cleanupResources(stmt, null);
// if there was no record for the enrolment or the previous status is not the same as the current status
// we'll add a record
if (previousStatus == null || previousStatus != status){
if (deviceId == -1) {
// we need the device id in order to add a new record, therefore we get it from the enrolment table
sql = "SELECT DEVICE_ID FROM DM_ENROLMENT WHERE ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
rs = stmt.executeQuery();
if (rs.next()) {
deviceId = rs.getInt("DEVICE_ID");
} else {
// if there were no records corresponding to the enrolment id this is a problem. i.e. enrolment
// id is invalid
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: no record for enrolment id " + enrolmentId);
}
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
Timestamp updateTime = new Timestamp(new Date().getTime());
stmt.setInt(1, enrolmentId);
stmt.setInt(2, deviceId);
stmt.setString(3, status.toString());
stmt.setTimestamp(4, updateTime);
stmt.setString(5, changedBy);
stmt.execute();
} else {
// no need to update status since the last recorded status is the same as the current status
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return true;
}
@Override
public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo.Status status = null;
try {
conn = this.getConnection();
String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
}
return status;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public EnrolmentInfo getEnrollment(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
}
return enrolmentInfo;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"information of user '" + currentOwner + "' upon device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " +
"ORDER BY DATE_OF_LAST_UPDATE DESC";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
}
return enrolmentInfo;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"information of device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<EnrolmentInfo> getEnrollmentsOfUser(int deviceId, String user, int tenantId)
throws DeviceManagementDAOException {
List<EnrolmentInfo> enrolmentInfos = new ArrayList<>();
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setString(2, user);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
while (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
enrolmentInfos.add(enrolmentInfo);
}
return enrolmentInfos;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
"information of user '" + user + "' upon device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public boolean updateOwnerOfEnrollment(List<Device> devices, String owner, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = this.getConnection();
boolean updateStatus = true;
String sql = "UPDATE DM_ENROLMENT "
+ "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? "
+ "WHERE ID = ? AND TENANT_ID = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
if (conn.getMetaData().supportsBatchUpdates()) {
for (Device device : devices) {
ps.setString(1, owner);
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
ps.setTimestamp(3, new Timestamp(new Date().getTime()));
ps.setInt(4, device.getEnrolmentInfo().getId());
ps.setInt(5, tenantId);
ps.addBatch();
}
int[] batchResult = ps.executeBatch();
for (int i : batchResult) {
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
updateStatus = false;
break;
}
}
} else {
for (Device device : devices) {
ps.setString(1, owner);
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
ps.setInt(3, device.getId());
ps.setInt(4, tenantId);
if (ps.executeUpdate() == 0) {
updateStatus = false;
break;
}
}
}
}
return updateStatus;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the "
+ "owner of the device enrollment.", e);
}
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED"));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
enrolmentInfo.setId(rs.getInt("ID"));
return enrolmentInfo;
}
}

@ -17,6 +17,7 @@
*/
package io.entgra.device.mgt.core.device.mgt.core.dao.impl.enrolment;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
@ -32,285 +33,6 @@ import java.util.List;
public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl {
@Override
public EnrolmentInfo addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " +
"DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql, new String[] {"id"});
Timestamp enrollmentTime = new Timestamp(new Date().getTime());
stmt.setInt(1, deviceId);
stmt.setString(2, enrolmentInfo.getOwner());
stmt.setString(3, enrolmentInfo.getOwnership().toString());
stmt.setString(4, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(5, enrollmentTime);
stmt.setTimestamp(6, enrollmentTime);
stmt.setInt(7, tenantId);
stmt.execute();
rs = stmt.getGeneratedKeys();
if (rs.next()) {
int enrolmentId = rs.getInt(1);
enrolmentInfo.setId(enrolmentId);
enrolmentInfo.setDateOfEnrolment(enrollmentTime.getTime());
enrolmentInfo.setDateOfLastUpdate(enrollmentTime.getTime());
addDeviceStatus(enrolmentId, enrolmentInfo.getStatus());
return enrolmentInfo;
}
return null;
} catch (SQLException e) {
e.printStackTrace();
throw new DeviceManagementDAOException("Error occurred while adding enrolment configuration", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public int updateEnrollment(EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, DATE_OF_LAST_UPDATE = ? " +
"WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, enrolmentInfo.getOwnership().toString());
stmt.setString(2, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(3, new Timestamp(new Date().getTime()));
stmt.setInt(4, enrolmentInfo.getId());
stmt.setInt(5, tenantId);
int updatedCount = stmt.executeUpdate();
return updatedCount;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment configuration", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public boolean updateEnrollmentStatus(List<EnrolmentInfo> enrolmentInfos) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
boolean status = false;
int updateStatus = -1;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ?";
stmt = conn.prepareStatement(sql);
if (conn.getMetaData().supportsBatchUpdates()) {
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
stmt.setString(1, enrolmentInfo.getStatus().toString());
stmt.setInt(2, enrolmentInfo.getId());
stmt.addBatch();
}
updateStatus = stmt.executeBatch().length;
} else {
for (EnrolmentInfo enrolmentInfo : enrolmentInfos) {
stmt.setString(1, enrolmentInfo.getStatus().toString());
stmt.setInt(2, enrolmentInfo.getId());
updateStatus = stmt.executeUpdate();
}
}
if (updateStatus > 0) {
status = true;
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while updating enrolment status of given device-list.", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return status;
}
@Override
public int removeEnrollment(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int status = -1;
try {
conn = this.getConnection();
String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql, new String[] {"id"});
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
if (rs.next()) {
status = 1;
}
return status;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while removing device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private int getCountOfDevicesOfOwner(String owner, int tenantID) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
int count = 0;
try {
conn = this.getConnection();
String checkQuery = "SELECT COUNT(ID) AS COUNT FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(checkQuery);
stmt.setString(1, owner);
stmt.setInt(2, tenantID);
rs = stmt.executeQuery();
if(rs.next()){
count = rs.getInt("COUNT");
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while trying to get device " +
"count of Owner : "+owner, e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return count;
}
@Override
public boolean setStatus(String currentOwner, EnrolmentInfo.Status status,
int tenantId) throws DeviceManagementDAOException {
return setStatusAllDevices(currentOwner, status, tenantId);
}
@Override
public boolean setStatusAllDevices(String currentOwner, EnrolmentInfo.Status status, int tenantId)
throws DeviceManagementDAOException{
Connection conn;
PreparedStatement stmt = null;
Timestamp updateTime = new Timestamp(new Date().getTime());
if(getCountOfDevicesOfOwner(currentOwner, tenantId) > 0){
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString());
stmt.setTimestamp(2, updateTime);
stmt.setString(3, currentOwner);
stmt.setInt(4, tenantId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return addDeviceStatus(currentOwner, status, tenantId);
} else {
return false;
}
}
@Override
public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp updateTime = new Timestamp(new Date().getTime());
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString());
stmt.setTimestamp(2, updateTime);
stmt.setInt(3, enrolmentID);
stmt.setInt(4, tenantId);
int updatedRowCount = stmt.executeUpdate();
if (updatedRowCount != 1){
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment: "+
updatedRowCount + " rows were updated instead of one row!!!");
}
// save the device status history
addDeviceStatus(enrolmentID, status);
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return true;
}
public boolean addDeviceStatus(EnrolmentInfo config) throws DeviceManagementDAOException {
return addDeviceStatus(config.getId(), config.getStatus());
}
public boolean addDeviceStatus(String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
if (changedBy == null){
changedBy = DeviceManagementConstants.MaintenanceProperties.MAINTENANCE_USER;
}
PreparedStatement stmt = null;
ResultSet rs = null;
List<int[]> enrolmentInfoList = new ArrayList<>();
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, currentOwner);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
while (rs.next()) {
int enrolmentId = rs.getInt("ID");
int deviceId = rs.getInt("DEVICE_ID");
enrolmentInfoList.add(new int[]{enrolmentId, deviceId});
}
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
Timestamp updateTime = new Timestamp(new Date().getTime());
sql = "INSERT INTO DM_DEVICE_STATUS (ENROLMENT_ID, DEVICE_ID, STATUS, UPDATE_TIME, CHANGED_BY) VALUES(?, ?, ?, ?, ?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
if (conn.getMetaData().supportsBatchUpdates()) {
for(int[] info: enrolmentInfoList){
ps.setInt(1, info[0]);
ps.setInt(2, info[1]);
ps.setString(3, status.toString());
ps.setTimestamp(4, updateTime);
ps.setString(5, changedBy);
ps.addBatch();
}
int[] batchResult = ps.executeBatch();
for (int i : batchResult) {
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
return false;
}
}
} else {
for(int[] info: enrolmentInfoList){
ps.setInt(1, info[0]);
ps.setInt(2, info[1]);
ps.setString(3, status.toString());
ps.setTimestamp(4, updateTime);
ps.setString(5, changedBy);
ps.execute();
}
}
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
"information of owner '" + currentOwner + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
return true;
}
public boolean addDeviceStatus(int enrolmentId, EnrolmentInfo.Status status) throws DeviceManagementDAOException {
Connection conn;
String changedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -319,8 +41,8 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl {
}
PreparedStatement stmt = null;
try {
conn = this.getConnection();
// get the device id and last udpated status from the device status table
conn = getConnection();
// get the device id and last updated status from the device status table
String sql = "SELECT TOP 1 DEVICE_ID, STATUS FROM DM_DEVICE_STATUS WHERE ENROLMENT_ID = ? ORDER BY UPDATE_TIME DESC";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, enrolmentId);
@ -371,177 +93,5 @@ public class SQLServerEnrollmentDAOImpl extends AbstractEnrollmentDAOImpl {
}
return true;
}
@Override
public EnrolmentInfo.Status getStatus(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo.Status status = null;
try {
conn = this.getConnection();
String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));
}
return status;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public EnrolmentInfo getEnrollment(int deviceId, String currentOwner,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
}
return enrolmentInfo;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"information of user '" + currentOwner + "' upon device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public EnrolmentInfo getEnrollment(int deviceId, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? " +
"ORDER BY DATE_OF_LAST_UPDATE DESC";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
}
return enrolmentInfo;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"information of device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public List<EnrolmentInfo> getEnrollmentsOfUser(int deviceId, String user, int tenantId)
throws DeviceManagementDAOException {
List<EnrolmentInfo> enrolmentInfos = new ArrayList<>();
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
EnrolmentInfo enrolmentInfo = null;
try {
conn = this.getConnection();
String sql = "SELECT ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, IS_TRANSFERRED, DATE_OF_ENROLMENT, " +
"DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
stmt.setString(2, user);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
while (rs.next()) {
enrolmentInfo = this.loadEnrolment(rs);
enrolmentInfos.add(enrolmentInfo);
}
return enrolmentInfos;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolments " +
"information of user '" + user + "' upon device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public boolean updateOwnerOfEnrollment(List<Device> devices, String owner, int tenantId)
throws DeviceManagementDAOException {
try {
Connection conn = this.getConnection();
boolean updateStatus = true;
String sql = "UPDATE DM_ENROLMENT "
+ "SET OWNER = ?, IS_TRANSFERRED = ?, DATE_OF_LAST_UPDATE = ? "
+ "WHERE ID = ? AND TENANT_ID = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
if (conn.getMetaData().supportsBatchUpdates()) {
for (Device device : devices) {
ps.setString(1, owner);
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
ps.setTimestamp(3, new Timestamp(new Date().getTime()));
ps.setInt(4, device.getEnrolmentInfo().getId());
ps.setInt(5, tenantId);
ps.addBatch();
}
int[] batchResult = ps.executeBatch();
for (int i : batchResult) {
if (i == 0 || i == Statement.SUCCESS_NO_INFO || i == Statement.EXECUTE_FAILED) {
updateStatus = false;
break;
}
}
} else {
for (Device device : devices) {
ps.setString(1, owner);
ps.setBoolean(2, device.getEnrolmentInfo().isTransferred());
ps.setInt(3, device.getId());
ps.setInt(4, tenantId);
if (ps.executeUpdate() == 0) {
updateStatus = false;
break;
}
}
}
}
return updateStatus;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while obtaining the DB connection to update the "
+ "owner of the device enrollment.", e);
}
}
private Connection getConnection() throws SQLException {
return DeviceManagementDAOFactory.getConnection();
}
private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setOwner(rs.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP")));
enrolmentInfo.setTransferred(rs.getBoolean("IS_TRANSFERRED"));
enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime());
enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS")));
enrolmentInfo.setId(rs.getInt("ID"));
return enrolmentInfo;
}
}

@ -17,7 +17,6 @@
*/
package io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.MDMAppConstants;
import io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.ProfileOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -44,8 +43,6 @@ import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.dao.util.Operatio
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -59,7 +56,6 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -575,14 +571,12 @@ public class GenericOperationDAOImpl implements OperationDAO {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql =
"SELECT eom.ENROLMENT_ID, eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, "
+ "dor.ID AS OP_RES_ID, de.DEVICE_ID, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID, "
+ "dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, "
+ "dor.ID AS OP_RES_ID, de.DEVICE_ID, de.DEVICE_IDENTIFICATION, de.DEVICE_TYPE, "
+ "eom.STATUS, eom.CREATED_TIMESTAMP, "
+ "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, "
+ "dor.OPERATION_RESPONSE, op.INITIATED_BY, dor.RECEIVED_TIMESTAMP, dor.IS_LARGE_RESPONSE FROM "
+ "DM_ENROLMENT_OP_MAPPING eom INNER JOIN DM_OPERATION op "
+ "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de "
+ "ON de.ID=eom.ENROLMENT_ID INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID "
+ "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID "
+ "ON op.ID=eom.OPERATION_ID INNER JOIN DM_ENROLMENT de ON de.ID=eom.ENROLMENT_ID "
+ "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id "
+ "AND dor.OPERATION_ID = eom.OPERATION_ID WHERE eom.OPERATION_ID "
+ "IN (SELECT * FROM TABLE(x INT = ?)) AND de.TENANT_ID = ?";
@ -617,7 +611,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
@ -655,7 +649,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE_NAME"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
@ -715,8 +709,8 @@ public class GenericOperationDAOImpl implements OperationDAO {
+ "eom.OPERATION_ID, eom.ID AS EOM_MAPPING_ID, "
+ "dor.ID AS OP_RES_ID, "
+ "de.DEVICE_ID, "
+ "d.DEVICE_IDENTIFICATION, "
+ "d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, "
+ "de.DEVICE_IDENTIFICATION, "
+ "de.DEVICE_TYPE, "
+ "eom.STATUS, eom.CREATED_TIMESTAMP, "
+ "eom.UPDATED_TIMESTAMP, "
+ "op.OPERATION_CODE, "
@ -727,11 +721,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
+ "op.INITIATED_BY FROM DM_ENROLMENT_OP_MAPPING AS eom "
+ "INNER JOIN DM_OPERATION AS op ON op.ID=eom.OPERATION_ID "
+ "INNER JOIN DM_ENROLMENT AS de ON de.ID=eom.ENROLMENT_ID "
+ "INNER JOIN DM_DEVICE AS d ON d.ID=de.DEVICE_ID "
+ "INNER JOIN DM_DEVICE_TYPE AS dt ON dt.ID=d.DEVICE_TYPE_ID "
+ "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE AS dor ON dor.ENROLMENT_ID=de.id "
+ "AND dor.OPERATION_ID = eom.OPERATION_ID "
+ "WHERE eom.OPERATION_ID = ? AND de.device_id = ? AND de.TENANT_ID = ?";
+ "WHERE eom.OPERATION_ID = ? AND de.DEVICE_ID = ? AND de.TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, operationId);
@ -756,7 +748,7 @@ public class GenericOperationDAOImpl implements OperationDAO {
if (enrolmentId != rs.getInt("ENROLMENT_ID")) {
activityStatus = new ActivityStatus();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION"),
rs.getString("DEVICE_TYPE_NAME"));
rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
@ -850,15 +842,13 @@ public class GenericOperationDAOImpl implements OperationDAO {
" op.TYPE AS OPERATION_TYPE, " +
" opm.STATUS, " +
" en.DEVICE_ID, " +
" de.DEVICE_IDENTIFICATION, " +
" dt.NAME AS DEVICE_TYPE, " +
" en.DEVICE_IDENTIFICATION, " +
" en.DEVICE_TYPE, " +
" de.TENANT_ID " +
" FROM" +
" DM_ENROLMENT_OP_MAPPING opm " +
" INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " +
" INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " +
" INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " +
" INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " +
" WHERE " +
" op.OPERATION_CODE = ? " +
" AND de.TENANT_ID = ? " +

@ -329,7 +329,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED.
equals(existingEnrolmentInfo.getStatus())) {
enrollment = enrollmentDAO
.addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId);
.addEnrollment(existingDevice.getId(), deviceIdentifier,
newEnrolmentInfo, tenantId);
if (enrollment == null) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException(
@ -375,7 +376,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (type != null) {
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId);
device.setId(deviceId);
enrollment = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId);
enrollment = enrollmentDAO.addEnrollment(deviceId, deviceIdentifier, device.getEnrolmentInfo(), tenantId);
if (enrollment == null) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException(

@ -17,6 +17,7 @@
*/
package io.entgra.device.mgt.core.device.mgt.core.dao;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
@ -215,7 +216,8 @@ public class DeviceStatusPersistenceTests extends BaseDeviceManagementTest {
EnrolmentInfo source = new EnrolmentInfo(owner, EnrolmentInfo.OwnerShip.BYOD, initialStatus);
try {
DeviceManagementDAOFactory.openConnection();
EnrolmentInfo config = enrollmentDAO.addEnrollment(deviceId, source, tenantId);
EnrolmentInfo config = enrollmentDAO.addEnrollment(deviceId,
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()), source, tenantId);
device.setEnrolmentInfo(config);
return config.getId();
} catch (DeviceManagementDAOException | SQLException e) {

@ -17,6 +17,7 @@
*/
package io.entgra.device.mgt.core.device.mgt.core.dao;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
@ -58,7 +59,8 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest {
DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
deviceId = deviceDAO.addDevice(TestDataHolder.initialTestDeviceType.getId(), device, TestDataHolder.SUPER_TENANT_ID);
device.setId(deviceId);
enrollmentDAO.addEnrollment(deviceId, source, TestDataHolder.SUPER_TENANT_ID);
enrollmentDAO.addEnrollment(deviceId, new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()),
source, TestDataHolder.SUPER_TENANT_ID);
} catch (DeviceManagementDAOException | SQLException e) {
log.error("Error occurred while adding enrollment", e);
} finally {

@ -149,6 +149,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION (
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -79,6 +79,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION (
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -102,7 +102,8 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
DeviceManagementDAOFactory.beginTransaction();
for (Device device : devices) {
int id = deviceDAO.addDevice(type.getId(), device, -1234);
enrollmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234);
enrollmentDAO.addEnrollment(id, new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()),
device.getEnrolmentInfo(), -1234);
}
} catch (TransactionManagementException e) {
log.error("Error occurred while adding device enrolment", e);

@ -99,6 +99,8 @@ DROP TABLE IF EXISTS DM_ENROLMENT;
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -97,6 +97,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION (
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(255) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -100,6 +100,8 @@ CREATE TABLE IF NOT EXISTS DM_OPERATION (
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(255) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -142,6 +142,8 @@ IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[D
CREATE TABLE DM_ENROLMENT (
ID INTEGER IDENTITY(1,1) NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(255) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

@ -52,8 +52,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE (
ID INTEGER AUTO_INCREMENT NOT NULL,
DESCRIPTION TEXT DEFAULT NULL,
NAME VARCHAR(100) DEFAULT NULL,
DEVICE_TYPE_ID INT(11) DEFAULT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL,
DEVICE_TYPE_ID INT(11) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL,
TENANT_ID INTEGER DEFAULT 0,
PRIMARY KEY (ID),
@ -119,6 +119,8 @@ CREATE INDEX IDX_OP_INITIATED_BY ON DM_OPERATION (INITIATED_BY ASC);
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(255) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,
@ -130,6 +132,11 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB;
CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID);
CREATE INDEX IDX_ENROLMENT_DEVICE_TYPE_ID ON DM_ENROLMENT(DEVICE_TYPE_ID);
CREATE INDEX IDX_ENROLMENT_DEVICE_IDENTIFICATION ON DM_ENROLMENT(DEVICE_IDENTIFICATION);
CREATE INDEX IDX_ENROLMENT_STATUS ON DM_ENROLMENT(STATUS);
CREATE TABLE IF NOT EXISTS DM_DEVICE_STATUS (
ID INTEGER AUTO_INCREMENT NOT NULL,
ENROLMENT_ID INTEGER NOT NULL,
@ -143,9 +150,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_STATUS (
CONSTRAINT FK_DM_DEVICE_STATUS_ENROLMENT FOREIGN KEY (ENROLMENT_ID) REFERENCES
DM_ENROLMENT (ID) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE = InnoDB;
CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID);
CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID ON DM_ENROLMENT(DEVICE_ID, TENANT_ID);
CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID_STATUS ON DM_ENROLMENT(DEVICE_ID, TENANT_ID, STATUS);
CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,

@ -205,6 +205,8 @@ WHEN (NEW.ID IS NULL)
CREATE TABLE DM_ENROLMENT (
ID NUMBER(10) NOT NULL,
DEVICE_ID NUMBER(10) NOT NULL,
DEVICE_TYPE VARCHAR2(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR2(300) NOT NULL,
OWNER VARCHAR2(255) NOT NULL,
OWNERSHIP VARCHAR2(45) DEFAULT NULL,
STATUS VARCHAR2(50) NULL,

@ -114,6 +114,8 @@ CREATE SEQUENCE DM_ENROLMENT_seq;
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER DEFAULT NEXTVAL ('DM_ENROLMENT_seq') NOT NULL,
DEVICE_ID INTEGER NOT NULL,
DEVICE_TYPE VARCHAR(300) NOT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) DEFAULT NULL,
STATUS VARCHAR(50) NULL,

Loading…
Cancel
Save