Fixing the conflicts

revert-70aa11f8
geethkokila 9 years ago
commit dc205aa081

@ -92,4 +92,23 @@ public class EnrolmentInfo {
this.device = device; this.device = device;
} }
@Override
public boolean equals(Object obj) {
if (obj instanceof EnrolmentInfo) {
EnrolmentInfo tempInfo = (EnrolmentInfo) obj;
if (owner != null && ownership != null
&& tempInfo.getOwner() != null && tempInfo.getOwnership() != null) {
if (owner.equals(tempInfo.getOwner()) && ownership.equals(tempInfo.getOwnership())) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
} }

@ -62,5 +62,8 @@ public interface DeviceDAO {
List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException; List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException;
int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status,
int tenantId) throws DeviceManagementDAOException;
} }

@ -435,6 +435,37 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
} }
public int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getConnection();
String sql =
"SELECT ID AS ENROLMENT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID FROM DM_DEVICE d, " +
"DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? " +
"AND d.TENANT_ID = ?) AND STATUS = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getId());
stmt.setString(2, deviceId.getType());
stmt.setInt(3, tenantId);
stmt.setString(4, status.toString());
stmt.setInt(5, tenantId);
rs = stmt.executeQuery();
if (rs.next()) {
return rs.getInt("ENROLMENT_ID");
} else {
return -1; // if no results found
}
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " +
"id of device '" + deviceId + "'", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
private Device loadDevice(ResultSet rs) throws SQLException { private Device loadDevice(ResultSet rs) throws SQLException {
Device device = new Device(); Device device = new Device();
device.setId(rs.getInt("DEVICE_ID")); device.setId(rs.getInt("DEVICE_ID"));

@ -77,7 +77,7 @@ public class EnrolmentDAOImpl implements EnrolmentDAO {
stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(1, enrolmentInfo.getOwnership().toString());
stmt.setString(2, enrolmentInfo.getStatus().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString());
stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment())); stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment()));
stmt.setTimestamp(4, new Timestamp(enrolmentInfo.getDateOfLastUpdate())); stmt.setTimestamp(4, new Timestamp(new Date().getTime()));
stmt.setInt(5, deviceId); stmt.setInt(5, deviceId);
stmt.setString(6, enrolmentInfo.getOwner()); stmt.setString(6, enrolmentInfo.getOwner());
stmt.setInt(7, tenantId); stmt.setInt(7, tenantId);
@ -156,9 +156,9 @@ public class EnrolmentDAOImpl implements EnrolmentDAO {
conn = this.getConnection(); conn = this.getConnection();
String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(2, deviceId); stmt.setInt(1, deviceId);
stmt.setString(3, currentOwner); stmt.setString(2, currentOwner);
stmt.setInt(4, tenantId); stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS"));

@ -21,7 +21,6 @@ package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
@ -32,7 +31,6 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -81,7 +79,7 @@ public class OperationManagerImpl implements OperationManager {
log.debug("operation:[" + operation.toString() + "]"); log.debug("operation:[" + operation.toString() + "]");
for (DeviceIdentifier deviceIdentifier : deviceIds) { for (DeviceIdentifier deviceIdentifier : deviceIds) {
log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType() log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType()
+ "]"); + "]");
} }
} }
try { try {
@ -91,17 +89,17 @@ public class OperationManagerImpl implements OperationManager {
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
Device device; int enrolmentId;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {
device = deviceDAO.getDevice(deviceId, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (device == null) { if (enrolmentId < 0) {
String errorMsg = "The operation not added for device.The device not found for " + String errorMsg = "The operation not added for device.The device not found for " +
"device Identifier type -'" + deviceId.getType() + "' and device Id '" + "device Identifier type -'" + deviceId.getType() + "' and device Id '" +
deviceId.getId(); deviceId.getId();
log.info(errorMsg); log.error(errorMsg);
} else { } else {
operationMappingDAO.addOperationMapping(operationId, device.getId()); operationMappingDAO.addOperationMapping(operationId, enrolmentId);
} }
} }
OperationManagementDAOFactory.commitTransaction(); OperationManagementDAOFactory.commitTransaction();
@ -126,21 +124,25 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException { public List<? extends Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException {
List<Operation> operations = new ArrayList<Operation>();
int enrolmentId = -1;
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (device == null) { if (enrolmentId < 0) {
throw new OperationManagementException("Device not found for given device " + throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType());
} }
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList = List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> operationList = operationDAO
operationDAO.getOperationsForDevice(device.getId()); .getOperationsForDevice(enrolmentId);
Operation operation; Operation operation;
List<Operation> operations = new ArrayList<Operation>();
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) {
operation = OperationDAOUtil.convertOperation(dtoOperation); operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation); operations.add(operation);
@ -148,11 +150,11 @@ public class OperationManagerImpl implements OperationManager {
return operations; return operations;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId()
+ "'", e); + "'", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving metadata of '" + throw new OperationManagementException("Error occurred while retrieving metadata of '" +
deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'"); deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'");
} finally { } finally {
try { try {
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
@ -160,6 +162,7 @@ public class OperationManagerImpl implements OperationManager {
log.warn("Error occurred while closing data source connection", e); log.warn("Error occurred while closing data source connection", e);
} }
} }
} }
@Override @Override
@ -168,10 +171,10 @@ public class OperationManagerImpl implements OperationManager {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType()
+ "]"); + "]");
} }
Device device; int enrolmentId = -1;
List<Operation> operations = new ArrayList<Operation>(); List<Operation> operations = new ArrayList<Operation>();
List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = List<org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
@ -181,30 +184,24 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (device == null) { if (enrolmentId < 0) {
throw new OperationManagementException("Device not found for given device " + throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType());
}
if (device.getEnrolmentInfo().getStatus() != null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)) {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device, EnrolmentInfo.Status.ACTIVE);
} }
dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
Operation operation; Operation operation;
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) {
@ -215,17 +212,14 @@ public class OperationManagerImpl implements OperationManager {
return operations; return operations;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"pending operations assigned for '" + deviceId.getType() + "' device '" + "pending operations assigned for '" + deviceId.getType() + "' device '" +
deviceId.getId() + "'", e); deviceId.getId() + "'", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the device " + String errorMsg = "Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" "for device Identifier type -'" + deviceId.getType() + "' and device Id '"
+ deviceId.getId(); + deviceId.getId();
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e); throw new OperationManagementException(errorMsg, e);
} catch (DeviceManagementException e) {
throw new OperationManagementException("Error occurred while update enrol status: " +
deviceId.toString(), e);
} finally { } finally {
try { try {
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
@ -239,34 +233,22 @@ public class OperationManagerImpl implements OperationManager {
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() log.debug("device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType()
+ "]"); + "]");
} }
Operation operation = null; Operation operation = null;
Device device; int enrolmentId = -1;
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (device == null) { if (enrolmentId < 0) {
throw new OperationManagementException("Device not found for given device " + throw new OperationManagementException("Device not found for given device " +
"Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType());
} }
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO
.getNextOperation(device.getId()); .getNextOperation(enrolmentId);
if (device.getEnrolmentInfo().getStatus() != null && !device.getEnrolmentInfo().getStatus().equals(
EnrolmentInfo.Status.ACTIVE)) {
try {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.updateDeviceEnrolmentInfo(device,
EnrolmentInfo.Status.ACTIVE);
} catch (DeviceManagementException e) {
throw new OperationManagementException("Error occurred while update enrol status: '" +
deviceId.toString() + "'", e);
}
}
if (dtoOperation != null) { if (dtoOperation != null) {
if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND
@ -293,7 +275,7 @@ public class OperationManagerImpl implements OperationManager {
throw new OperationManagementException("Error occurred while retrieving next pending operation", e); throw new OperationManagementException("Error occurred while retrieving next pending operation", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e);
} finally { } finally {
try { try {
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
@ -315,19 +297,21 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (operation.getStatus() != null) { if (operation.getStatus() != null) {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
operationDAO.updateOperationStatus(device.getId(), operationId, operationDAO.updateOperationStatus(enrolmentId, operationId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status
.valueOf(operation.getStatus().toString())); .valueOf(operation.getStatus().toString()));
} }
if (operation.getOperationResponse() != null) { if (operation.getOperationResponse() != null) {
operationDAO.addOperationResponse(device.getId(), operationId, operation.getOperationResponse()); OperationManagementDAOFactory.beginTransaction();
operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse());
OperationManagementDAOFactory.commitTransaction();
} }
OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
try { try {
OperationManagementDAOFactory.rollbackTransaction(); OperationManagementDAOFactory.rollbackTransaction();
@ -335,7 +319,7 @@ public class OperationManagerImpl implements OperationManager {
log.warn("Error occurred while roll-backing the update operation transaction", e1); log.warn("Error occurred while roll-backing the update operation transaction", e1);
} }
throw new OperationManagementException("Error occurred while updating the operation: " + operationId + throw new OperationManagementException("Error occurred while updating the operation: " + operationId +
" status:" + operation.getStatus(), e); " status:" + operation.getStatus(), e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
try { try {
OperationManagementDAOFactory.rollbackTransaction(); OperationManagementDAOFactory.rollbackTransaction();
@ -343,7 +327,7 @@ public class OperationManagerImpl implements OperationManager {
log.warn("Error occurred while roll-backing the update operation transaction", e1); log.warn("Error occurred while roll-backing the update operation transaction", e1);
} }
throw new OperationManagementException("Error occurred while fetching the device for device identifier: " + throw new OperationManagementException("Error occurred while fetching the device for device identifier: " +
deviceId.getId() + "type:" + deviceId.getType(), e); deviceId.getId() + "type:" + deviceId.getType(), e);
} finally { } finally {
try { try {
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
@ -381,25 +365,25 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException { throws OperationManagementException {
Device device; int enrolmentId = -1;
Operation operation; Operation operation;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" + log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" +
deviceId.getId()); deviceId.getId());
} }
try { try {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
device = deviceDAO.getDevice(deviceId, tenantId); enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (device == null) { if (enrolmentId < 0) {
throw new OperationManagementException("Device not found for given device identifier:" + throw new OperationManagementException("Device not found for given device identifier:" +
deviceId.getId() + " type:" + deviceId.getType()); deviceId.getId() + " type:" + deviceId.getType());
} }
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO
.getOperationByDeviceAndId(device.getId(), operationId); .getOperationByDeviceAndId(enrolmentId, operationId);
if (dtoOperation.getType() if (dtoOperation.getType()
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
@ -412,27 +396,27 @@ public class OperationManagerImpl implements OperationManager {
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type
.PROFILE)) { .PROFILE)) {
dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type
.POLICY)) { .POLICY)) {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
} }
if (dtoOperation == null) { if (dtoOperation == null) {
throw new OperationManagementException("Operation not found for operation Id:" + operationId + throw new OperationManagementException("Operation not found for operation Id:" + operationId +
" device" + " Id:" + device.getId()); " device id:" + deviceId.getId());
} }
operation = OperationDAOUtil.convertOperation(dtoOperation); operation = OperationDAOUtil.convertOperation(dtoOperation);
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId()
+ "'", e); + "'", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the device " + String errorMsg = "Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" "for device Identifier type -'" + deviceId.getType() + "' and device Id '"
+ deviceId.getId(); + deviceId.getId();
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e); throw new OperationManagementException(errorMsg, e);
} finally { } finally {
@ -455,25 +439,28 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.getConnection(); OperationManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
Device device = deviceDAO.getDevice(deviceId, tenantId); int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId);
if (device == null) { if (enrolmentId < 0) {
throw new OperationManagementException("Device not found for device id:" + deviceId.getId() + " " + throw new OperationManagementException("Device not found for device id:" + deviceId.getId() + " " +
"type:" + deviceId.getType()); "type:" + deviceId.getType());
} }
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = org.wso2.carbon.device org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = org.wso2.carbon.device
.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); .mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString());
dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOpStatus)); dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus));
dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOperationList.addAll(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId,
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING));
Operation operation; Operation operation;
@ -484,11 +471,11 @@ public class OperationManagerImpl implements OperationManager {
return operations; return operations;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "' device '" + "operations assigned for '" + deviceId.getType() + "' device '" +
deviceId.getId() + "' and status:" + status.toString(), e); deviceId.getId() + "' and status:" + status.toString(), e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the device " + throw new OperationManagementException("Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e);
} finally { } finally {
try { try {
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
@ -521,10 +508,10 @@ public class OperationManagerImpl implements OperationManager {
.equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = configOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type
.PROFILE)) { .PROFILE)) {
dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId());
} else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type
.POLICY)) { .POLICY)) {
dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId());
} }

@ -22,15 +22,15 @@ import java.util.List;
public class PolicyOperation extends Operation { public class PolicyOperation extends Operation {
public static final String POLICY_OPERATION_CODE = "POLICY_BUNDLE";
private List<ProfileOperation> profileOperations;
public List<ProfileOperation> getProfileOperations() { public List<ProfileOperation> getProfileOperations() {
return profileOperations; return profileOperations;
} }
public static final String POLICY_OPERATION_CODE = "POLICY_BUNDLE";
public void setProfileOperations(List<ProfileOperation> profileOperations) { public void setProfileOperations(List<ProfileOperation> profileOperations) {
this.profileOperations = profileOperations; this.profileOperations = profileOperations;
} }
private List<ProfileOperation> profileOperations;
} }

@ -18,7 +18,6 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import java.util.List; import java.util.List;
@ -33,18 +32,18 @@ public interface OperationDAO {
Operation getOperation(int operationId) throws OperationManagementDAOException; Operation getOperation(int operationId) throws OperationManagementDAOException;
Operation getOperationByDeviceAndId(int deviceId, int operationId) throws OperationManagementDAOException; Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException;
List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, Operation.Status status) List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status)
throws OperationManagementDAOException; throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int deviceId) throws OperationManagementDAOException; List<? extends Operation> getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException;
Operation getNextOperation(int deviceId) throws OperationManagementDAOException; Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException;
void updateOperationStatus(int deviceId, int operationId,Operation.Status status) void updateOperationStatus(int enrolmentId, int operationId,Operation.Status status)
throws OperationManagementDAOException; throws OperationManagementDAOException;
void addOperationResponse(int deviceId, int operationId, Object operationResponse) void addOperationResponse(int enrolmentId, int operationId, Object operationResponse)
throws OperationManagementDAOException; throws OperationManagementDAOException;
} }

@ -130,7 +130,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -146,11 +146,11 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select co.OPERATION_ID,ENABLED from DM_COMMAND_OPERATION co " + String sql = "Select co.OPERATION_ID,ENABLED from DM_COMMAND_OPERATION co " +
"INNER JOIN " + "INNER JOIN " +
"(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
"AND STATUS=? ) dm ON dm.OPERATION_ID = co.OPERATION_ID"; "AND STATUS=? ) dm ON dm.OPERATION_ID = co.OPERATION_ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -169,7 +169,7 @@ public class CommandOperationDAOImpl extends OperationDAOImpl {
} }
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString(); "' with status '" + status.toString();
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);

@ -168,7 +168,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -184,11 +184,11 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select co.OPERATION_ID, co.OPERATION_CONFIG from DM_CONFIG_OPERATION co " + String sql = "Select co.OPERATION_ID, co.OPERATION_CONFIG from DM_CONFIG_OPERATION co " +
"INNER JOIN " + "INNER JOIN " +
"(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
"AND STATUS=?) dm ON dm.OPERATION_ID = co.OPERATION_ID"; "AND STATUS=?) dm ON dm.OPERATION_ID = co.OPERATION_ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -211,7 +211,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl {
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString(); "' with status '" + status.toString();
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);

@ -86,17 +86,17 @@ public class OperationDAOImpl implements OperationDAO {
} }
} }
public void updateOperationStatus(int deviceId, int operationId, Operation.Status status) public void updateOperationStatus(int enrolmentId, int operationId, Operation.Status status)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("UPDATE DM_DEVICE_OPERATION_MAPPING O SET O.STATUS=? " + stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OPERATION_MAPPING O SET O.STATUS=? " +
"WHERE O.DEVICE_ID=? and O.OPERATION_ID=?"); "WHERE O.ENROLMENT_ID=? and O.OPERATION_ID=?");
stmt.setString(1, status.toString()); stmt.setString(1, status.toString());
stmt.setInt(2, deviceId); stmt.setInt(2, enrolmentId);
stmt.setInt(3, operationId); stmt.setInt(3, operationId);
stmt.executeUpdate(); stmt.executeUpdate();
@ -111,7 +111,7 @@ public class OperationDAOImpl implements OperationDAO {
} }
@Override @Override
public void addOperationResponse(int deviceId, int operationId, Object operationResponse) public void addOperationResponse(int enrolmentId, int operationId, Object operationResponse)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -129,7 +129,7 @@ public class OperationDAOImpl implements OperationDAO {
oos.writeObject(operationResponse); oos.writeObject(operationResponse);
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setInt(2, deviceId); stmt.setInt(2, enrolmentId);
stmt.setBytes(3, bao.toByteArray()); stmt.setBytes(3, bao.toByteArray());
stmt.executeUpdate(); stmt.executeUpdate();
@ -214,7 +214,7 @@ public class OperationDAOImpl implements OperationDAO {
} }
@Override @Override
public Operation getOperationByDeviceAndId(int deviceId, int operationId) throws OperationManagementDAOException { public Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -225,13 +225,13 @@ public class OperationDAOImpl implements OperationDAO {
String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE " + String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE " +
" From (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," + " From (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," +
"OPERATION_CODE FROM DM_OPERATION WHERE id=?) o INNER JOIN (Select * from " + "OPERATION_CODE FROM DM_OPERATION WHERE id=?) o INNER JOIN (Select * from " +
"DM_DEVICE_OPERATION_MAPPING dm where dm.OPERATION_ID=? AND dm.DEVICE_ID=?) om " + "DM_ENROLMENT_OPERATION_MAPPING dm where dm.OPERATION_ID=? AND dm.ENROLMENT_ID=?) om " +
"ON o.ID = om.OPERATION_ID "; "ON o.ID = om.OPERATION_ID ";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setInt(2, operationId); stmt.setInt(2, operationId);
stmt.setInt(3, deviceId); stmt.setInt(3, enrolmentId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
@ -247,7 +247,7 @@ public class OperationDAOImpl implements OperationDAO {
operation.setCode(rs.getString("OPERATION_CODE")); operation.setCode(rs.getString("OPERATION_CODE"));
} }
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with id '" + operationId; "' with id '" + operationId;
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
@ -259,7 +259,7 @@ public class OperationDAOImpl implements OperationDAO {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -272,11 +272,11 @@ public class OperationDAOImpl implements OperationDAO {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o " + "FROM DM_OPERATION o " +
"INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.DEVICE_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; "where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -296,7 +296,7 @@ public class OperationDAOImpl implements OperationDAO {
operationList.add(operation); operationList.add(operation);
} }
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString(); "' with status '" + status.toString();
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
@ -308,7 +308,7 @@ public class OperationDAOImpl implements OperationDAO {
} }
@Override @Override
public List<? extends Operation> getOperationsForDevice(int deviceId) public List<? extends Operation> getOperationsForDevice(int enrolmentId)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -321,11 +321,11 @@ public class OperationDAOImpl implements OperationDAO {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE,dm.STATUS FROM DM_OPERATION o " + "OPERATION_CODE,dm.STATUS FROM DM_OPERATION o " +
"INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.DEVICE_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; "where dm.ENROLMENT_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -344,7 +344,7 @@ public class OperationDAOImpl implements OperationDAO {
operationList.add(operation); operationList.add(operation);
} }
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '"; "' with status '";
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
@ -356,7 +356,7 @@ public class OperationDAOImpl implements OperationDAO {
} }
@Override @Override
public Operation getNextOperation(int deviceId) throws OperationManagementDAOException { public Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -365,11 +365,11 @@ public class OperationDAOImpl implements OperationDAO {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement("SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + stmt = connection.prepareStatement("SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " +
"OPERATION_CODE FROM DM_OPERATION o " + "OPERATION_CODE FROM DM_OPERATION o " +
"INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.DEVICE_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " + "where dm.ENROLMENT_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " +
"ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); "ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1");
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
stmt.setString(2, Operation.Status.PENDING.toString()); stmt.setString(2, Operation.Status.PENDING.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -398,7 +398,7 @@ public class OperationDAOImpl implements OperationDAO {
} }
public List<? extends Operation> getOperationsByDeviceStatusAndType(int deviceId, public List<? extends Operation> getOperationsByDeviceStatusAndType(int enrolmentId,
Operation.Status status, Operation.Type type) throws OperationManagementDAOException { Operation.Status status, Operation.Type type) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -412,12 +412,12 @@ public class OperationDAOImpl implements OperationDAO {
String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM " + String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM " +
"(SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + "(SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " +
"FROM DM_OPERATION o WHERE o.TYPE=?) o " + "FROM DM_OPERATION o WHERE o.TYPE=?) o " +
"INNER JOIN (Select * from DM_DEVICE_OPERATION_MAPPING dm " + "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " +
"where dm.DEVICE_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; "where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, type.toString()); stmt.setString(1, type.toString());
stmt.setInt(2, deviceId); stmt.setInt(2, enrolmentId);
stmt.setString(3, status.toString()); stmt.setString(3, status.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -436,7 +436,7 @@ public class OperationDAOImpl implements OperationDAO {
operationList.add(operation); operationList.add(operation);
} }
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString(); "' with status '" + status.toString();
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);

@ -35,7 +35,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID,STATUS) VALUES(?, ?,?)"; String sql = "INSERT INTO DM_ENROLMENT_OPERATION_MAPPING(ENROLMENT_ID, OPERATION_ID, STATUS) VALUES (?, ?,?)";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, operationId); stmt.setInt(2, operationId);
@ -54,7 +54,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "DELETE FROM DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?"; String sql = "DELETE FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? AND OPERATION_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0); stmt.setInt(1, 0);
stmt.setInt(2, operationId); stmt.setInt(2, operationId);

@ -171,7 +171,7 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -187,11 +187,11 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " + String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " +
"INNER JOIN " + "INNER JOIN " +
"(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
"AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -214,7 +214,7 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString(); "' with status '" + status.toString();
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);

@ -167,7 +167,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
} }
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId,
Operation.Status status) throws OperationManagementDAOException { Operation.Status status) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -183,11 +183,11 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_PROFILE_OPERATION po " + String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_PROFILE_OPERATION po " +
"INNER JOIN " + "INNER JOIN " +
"(Select * From DM_DEVICE_OPERATION_MAPPING WHERE DEVICE_ID=? " + "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " +
"AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString()); stmt.setString(2, status.toString());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -210,7 +210,7 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + deviceId + String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId +
"' with status '" + status.toString(); "' with status '" + status.toString();
log.error(errorMsg); log.error(errorMsg);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);

@ -48,7 +48,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService,
PluginInitializationListener { PluginInitializationListener {
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
@ -67,7 +67,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementServiceComponent.registerPluginInitializationListener(this); DeviceManagementServiceComponent.registerPluginInitializationListener(this);
} }
DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test){ /**
* This constructor calls from unit tests
*
* @param pluginRepo
*/
DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test) {
this.pluginRepository = pluginRepo; this.pluginRepository = pluginRepo;
initDataAccessObjects(); initDataAccessObjects();
} }
@ -106,31 +111,60 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
boolean status = false;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager();
boolean status = dms.enrollDevice(device); dms.enrollDevice(device);
try { try {
if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { if (dms.isClaimable(deviceIdentifier)) {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE);
} else { } else {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE);
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDAOFactory.beginTransaction(); Device existingDevice = this.getDevice(deviceIdentifier);
DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); if (existingDevice != null) {
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); EnrolmentInfo existingEnrolmentInfo = existingDevice.getEnrolmentInfo();
int enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo();
if (existingEnrolmentInfo != null && newEnrolmentInfo != null) {
if (log.isDebugEnabled()) { if (existingEnrolmentInfo.equals(newEnrolmentInfo)) {
log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " + device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment());
"the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " + this.modifyEnrollment(device);
"platform '" + device.getType() + " upon the user '" + status = true;
device.getEnrolmentInfo().getOwner() + "'"); } else {
this.setStatus(deviceIdentifier, existingEnrolmentInfo.getOwner(), EnrolmentInfo.Status.INACTIVE);
DeviceManagementDAOFactory.beginTransaction();
int enrolmentId = enrolmentDAO.addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId);
DeviceManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("An enrolment is successfully updated with the id '" + enrolmentId +
"' associated with " + "the device identified by key '" + device.getDeviceIdentifier() +
"', which belongs to " + "platform '" + device.getType() + " upon the user '" +
device.getEnrolmentInfo().getOwner() + "'");
}
status = true;
}
}
} else {
DeviceManagementDAOFactory.beginTransaction();
DeviceType type = deviceTypeDAO.getDeviceType(device.getType());
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId);
int enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId);
DeviceManagementDAOFactory.commitTransaction();
if (log.isDebugEnabled()) {
log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " +
"the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " +
"platform '" + device.getType() + " upon the user '" +
device.getEnrolmentInfo().getOwner() + "'");
}
status = true;
} }
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
try { try {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
@ -138,7 +172,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
log.warn("Error occurred while roll-backing the current transaction", e); log.warn("Error occurred while roll-backing the current transaction", e);
} }
throw new DeviceManagementException("Error occurred while enrolling the device " + throw new DeviceManagementException("Error occurred while enrolling the device " +
"'" + device.getId() + "'", e); "'" + device.getId() + "'", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -150,7 +184,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
DeviceManager dms = DeviceManager dms =
@ -171,7 +204,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
log.warn("Error occurred while roll-backing the current transaction", e); log.warn("Error occurred while roll-backing the current transaction", e);
} }
throw new DeviceManagementException("Error occurred while modifying the device " + throw new DeviceManagementException("Error occurred while modifying the device " +
"'" + device.getId() + "'", e); "'" + device.getId() + "'", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -189,16 +222,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager();
try { try {
Device device = deviceDAO.getDevice(deviceId,tenantId); Device device = deviceDAO.getDevice(deviceId, tenantId);
DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType());
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED);
enrolmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId);
deviceDAO.updateDevice(deviceType.getId(), device, tenantId); deviceDAO.updateDevice(deviceType.getId(), device, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while fetch device for device Identifier:"; String errorMsg = "Error occurred while fetch device for device Identifier:";
log.error(errorMsg + deviceId.toString(),e); log.error(errorMsg + deviceId.toString(), e);
throw new DeviceManagementException(errorMsg, e); throw new DeviceManagementException(errorMsg, e);
} }
@ -236,7 +270,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
allDevices = deviceDAO.getDevices(tenantId); allDevices = deviceDAO.getDevices(tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " +
"the current tenant", e); "the current tenant", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -270,7 +304,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
allDevices = deviceDAO.getDevices(type, tenantId); allDevices = deviceDAO.getDevices(type, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + throw new DeviceManagementException("Error occurred while retrieving all devices of type '" +
type + "' that are being managed within the scope of current tenant", e); type + "' that are being managed within the scope of current tenant", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -329,13 +363,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}",
URLEncoder.encode(emailMessageProperties.getFirstName(), URLEncoder.encode(emailMessageProperties.getFirstName(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBody = messageBody.trim() + System.getProperty("line.separator") + messageBody = messageBody.trim() + System.getProperty("line.separator") +
System.getProperty("line.separator") + url.replaceAll("\\{" System.getProperty("line.separator") + url.replaceAll("\\{"
+ EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}",
URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) messageBuilder.append(messageHeader).append(System.getProperty("line.separator"))
.append(System.getProperty("line.separator")); .append(System.getProperty("line.separator"));
@ -343,12 +377,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
messageBuilder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator")); messageBuilder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator"));
messageBuilder.append(messageFooter1.trim()) messageBuilder.append(messageFooter1.trim())
.append(System.getProperty("line.separator")).append(messageFooter2.trim()).append(System .append(System.getProperty("line.separator")).append(messageFooter2.trim()).append(System
.getProperty("line.separator")).append(messageFooter3.trim()); .getProperty("line.separator")).append(messageFooter3.trim());
} catch (IOException e) { } catch (IOException e) {
log.error("IO error in processing enrol email message " + emailMessageProperties); log.error("IO error in processing enrol email message " + emailMessageProperties);
throw new DeviceManagementException("Error replacing tags in email template '" + throw new DeviceManagementException("Error replacing tags in email template '" +
emailMessageProperties.getSubject() + "'", e); emailMessageProperties.getSubject() + "'", e);
} }
emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setMessageBody(messageBuilder.toString());
emailMessageProperties.setSubject(subject); emailMessageProperties.setSubject(subject);
@ -386,23 +420,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try { try {
messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}",
URLEncoder.encode(emailMessageProperties.getFirstName(), URLEncoder.encode(emailMessageProperties.getFirstName(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants
.USERNAME .USERNAME
+ "\\}", + "\\}",
URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants
.ENCODED_SCHEME)); .ENCODED_SCHEME));
messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}", messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}",
URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants
.ENCODED_SCHEME)); .ENCODED_SCHEME));
messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{"
+ EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}",
URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(),
EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME));
messageBuilder.append(messageHeader).append(System.getProperty("line.separator")); messageBuilder.append(messageHeader).append(System.getProperty("line.separator"));
messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append(
@ -413,7 +447,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} catch (IOException e) { } catch (IOException e) {
log.error("IO error in processing enrol email message " + emailMessageProperties); log.error("IO error in processing enrol email message " + emailMessageProperties);
throw new DeviceManagementException("Error replacing tags in email template '" + throw new DeviceManagementException("Error replacing tags in email template '" +
emailMessageProperties.getSubject() + "'", e); emailMessageProperties.getSubject() + "'", e);
} }
emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setMessageBody(messageBuilder.toString());
emailMessageProperties.setSubject(subject); emailMessageProperties.setSubject(subject);
@ -428,7 +462,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
device = deviceDAO.getDevice(deviceId, tenantId); device = deviceDAO.getDevice(deviceId, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for id " + throw new DeviceManagementException("Error occurred while obtaining the device for id " +
"'" + deviceId.getId() + "'", e); "'" + deviceId.getId() + "'", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -531,7 +565,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public int addOperation(Operation operation, List<DeviceIdentifier> devices) throws public int addOperation(Operation operation, List<DeviceIdentifier> devices) throws
OperationManagementException { OperationManagementException {
return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices); return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices);
} }
@ -591,7 +625,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
userDevices = deviceDAO.getDevicesOfUser(username, tenantId); userDevices = deviceDAO.getDevicesOfUser(username, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + throw new DeviceManagementException("Error occurred while retrieving the list of devices that " +
"belong to the user '" + username + "'", e); "belong to the user '" + username + "'", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -622,11 +656,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
String[] users; String[] users;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)
.getUserStoreManager().getUserListOfRole(role); .getUserStoreManager().getUserListOfRole(role);
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " +
"with the role '" + role + "'", e); "with the role '" + role + "'", e);
} }
List<Device> userDevices; List<Device> userDevices;
@ -686,7 +720,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
allDevices = deviceDAO.getDevicesByName(deviceName, tenantId); allDevices = deviceDAO.getDevicesByName(deviceName, tenantId);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '"
+ deviceName + "'", e); + deviceName + "'", e);
} finally { } finally {
try { try {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
@ -718,8 +752,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
device.getEnrolmentInfo().setStatus(status); device.getEnrolmentInfo().setStatus(status);
deviceDAO.updateDevice(deviceType.getId(), device, tenantId); deviceDAO.updateDevice(deviceType.getId(), device, tenantId);
}catch (DeviceManagementDAOException deviceDaoEx){ } catch (DeviceManagementDAOException deviceDaoEx) {
String errorMsg = "Error occured update device enrolment status : "+device.getId(); String errorMsg = "Error occured update device enrolment status : " + device.getId();
log.error(errorMsg, deviceDaoEx); log.error(errorMsg, deviceDaoEx);
throw new DeviceManagementException(errorMsg, deviceDaoEx); throw new DeviceManagementException(errorMsg, deviceDaoEx);
} }
@ -731,7 +765,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
pluginRepository.addDeviceManagementProvider(deviceManagementService); pluginRepository.addDeviceManagementProvider(deviceManagementService);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Error occurred while registering device management plugin '" + log.error("Error occurred while registering device management plugin '" +
deviceManagementService.getType() + "'", e); deviceManagementService.getType() + "'", e);
} }
} }
@ -741,7 +775,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
pluginRepository.removeDeviceManagementProvider(deviceManagementService); pluginRepository.removeDeviceManagementProvider(deviceManagementService);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Error occurred while un-registering device management plugin '" + log.error("Error occurred while un-registering device management plugin '" +
deviceManagementService.getType() + "'", e); deviceManagementService.getType() + "'", e);
} }
} }
@ -777,6 +811,4 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
return devices; return devices;
} }
} }

@ -50,18 +50,6 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION (
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
@ -75,6 +63,20 @@ CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
ENROLMENT_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES
DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_APPLICATION ( CREATE TABLE IF NOT EXISTS DM_APPLICATION (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL,

@ -24,10 +24,11 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.List; import java.util.List;
@XmlRootElement @XmlRootElement
public class Profile { public class Profile implements Serializable {
private int profileId; private int profileId;
private String profileName; private String profileName;

@ -90,12 +90,12 @@ public interface PolicyDAO {
List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException;
void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) void addEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; void setPolicyApplied(int deviceId) throws PolicyManagerDAOException;
void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) void updateEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException;

@ -190,7 +190,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
for (ComplianceFeature feature : complianceFeatures) { for (ComplianceFeature feature : complianceFeatures) {
stmt.setInt(1, policyComplianceStatusId); stmt.setInt(1, policyComplianceStatusId);
stmt.setString(2, feature.getFeatureCode()); stmt.setString(2, feature.getFeatureCode());
stmt.setString(3, String.valueOf(feature.isCompliance())); if (feature.isCompliance() == true) {
stmt.setInt(3, 1);
} else {
stmt.setInt(3, 0);
}
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();

@ -413,7 +413,7 @@ public class PolicyDAOImpl implements PolicyDAO {
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while inserting the criterion to policy (" + policy.getPolicyName() + ") " + String msg = "Error occurred while inserting the criterion to policy (" + policy.getPolicyName() + ") " +
"to database."; "to database.";
log.error(msg, e); log.error(msg, e);
throw new PolicyManagerDAOException(msg, e); throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
@ -433,7 +433,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " +
"CONTENT) VALUES (?, ?, ?, ?)"; "CONTENT) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
for (PolicyCriterion criterion : policyCriteria) { for (PolicyCriterion criterion : policyCriteria) {
@ -474,9 +474,9 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " + String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " +
"DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " +
"ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " +
"ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId); stmt.setInt(1, policyId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -522,7 +522,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" +
" WHERE ID = ?"; " WHERE ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setString(1, policy.getPolicyName()); stmt.setString(1, policy.getPolicyName());
stmt.setInt(2, policy.getTenantId()); stmt.setInt(2, policy.getTenantId());
@ -746,7 +746,7 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) public void addEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
Connection conn; Connection conn;
@ -759,8 +759,8 @@ public class PolicyDAOImpl implements PolicyDAO {
"CREATED_TIME, UPDATED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)"; "CREATED_TIME, UPDATED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, policyId); stmt.setInt(2, policy.getId());
stmt.setObject(3, profileFeatures); stmt.setObject(3, policy);
stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp); stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId); stmt.setInt(6, tenantId);
@ -804,7 +804,7 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) public void updateEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
Connection conn; Connection conn;
@ -813,10 +813,10 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " +
"APPLIED = ? WHERE DEVICE_ID = ?"; "APPLIED = ? WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId); stmt.setInt(1, policy.getId());
stmt.setObject(2, profileFeatures); stmt.setObject(2, policy);
stmt.setTimestamp(3, currentTimestamp); stmt.setTimestamp(3, currentTimestamp);
stmt.setBoolean(4, false); stmt.setBoolean(4, false);
stmt.setInt(5, deviceId); stmt.setInt(5, deviceId);
@ -1067,7 +1067,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE)" + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE)" +
" VALUES (?, ?, ?, ?, ?, ?)"; " VALUES (?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setString(1, policy.getPolicyName()); stmt.setString(1, policy.getPolicyName());
@ -1244,11 +1244,11 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
ByteArrayInputStream bais = null; ByteArrayInputStream bais = null;
ObjectInputStream ois = null; ObjectInputStream ois = null;
byte[] contentBytes; byte[] contentBytes;
try { try {
contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT");
bais = new ByteArrayInputStream(contentBytes); bais = new ByteArrayInputStream(contentBytes);

@ -38,7 +38,7 @@ public interface PolicyManager {
boolean deletePolicy(int policyId) throws PolicyManagementException; boolean deletePolicy(int policyId) throws PolicyManagementException;
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws
PolicyManagementException; PolicyManagementException;
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException; Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException;
@ -60,10 +60,10 @@ public interface PolicyManager {
List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException;
void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, List<ProfileFeature> void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
profileFeatures) throws PolicyManagementException; throws PolicyManagementException;
void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;
boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;

@ -708,8 +708,8 @@ public class PolicyManagerImpl implements PolicyManager {
} }
@Override @Override
public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
List<ProfileFeature> profileFeatures) throws PolicyManagementException { throws PolicyManagementException {
int deviceId = -1; int deviceId = -1;
try { try {
@ -719,9 +719,9 @@ public class PolicyManagerImpl implements PolicyManager {
boolean exist = policyDAO.checkPolicyAvailable(deviceId); boolean exist = policyDAO.checkPolicyAvailable(deviceId);
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (exist) { if (exist) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policyId, profileFeatures); policyDAO.updateEffectivePolicyToDevice(deviceId, policy);
} else { } else {
policyDAO.addEffectivePolicyToDevice(deviceId, policyId, profileFeatures); policyDAO.addEffectivePolicyToDevice(deviceId, policy);
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -731,7 +731,7 @@ public class PolicyManagerImpl implements PolicyManager {
log.warn("Error occurred while roll backing the transaction."); log.warn("Error occurred while roll backing the transaction.");
} }
String msg = "Error occurred while adding the evaluated policy to device (" + String msg = "Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policyId + ")"; deviceId + " - " + policy.getId() + ")";
log.error(msg, e); log.error(msg, e);
throw new PolicyManagementException(msg, e); throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -756,12 +756,10 @@ public class PolicyManagerImpl implements PolicyManager {
if (exist) { if (exist) {
Policy policySaved = policyDAO.getAppliedPolicy(deviceId); Policy policySaved = policyDAO.getAppliedPolicy(deviceId);
if (!policy.equals(policySaved)) { if (!policy.equals(policySaved)) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). policyDAO.updateEffectivePolicyToDevice(deviceId, policy);
getProfileFeaturesList());
} }
} else { } else {
policyDAO.addEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). policyDAO.addEffectivePolicyToDevice(deviceId, policy);
getProfileFeaturesList());
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -847,8 +845,8 @@ public class PolicyManagerImpl implements PolicyManager {
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
int policyId = policyDAO.getAppliedPolicyId(device.getId()); //int policyId = policyDAO.getAppliedPolicyId(device.getId());
policy = policyDAO.getPolicy(policyId); policy = policyDAO.getAppliedPolicy(device.getId());
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while getting device id."; String msg = "Error occurred while getting device id.";

@ -59,18 +59,18 @@ public class PolicyMonitoringServiceTest implements PolicyMonitoringService {
List<ComplianceFeature> complianceFeatures = new ArrayList<>(); List<ComplianceFeature> complianceFeatures = new ArrayList<>();
// List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList(); List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList();
// for (ProfileFeature pf : profileFeatures) { for (ProfileFeature pf : profileFeatures) {
// log.debug(pf.getFeatureCode()); log.debug(pf.getFeatureCode());
// ComplianceFeature comf = new ComplianceFeature(); ComplianceFeature comf = new ComplianceFeature();
//
// comf.setFeatureCode(pf.getFeatureCode()); comf.setFeatureCode(pf.getFeatureCode());
// comf.setCompliance(false); comf.setCompliance(false);
// comf.setMessage("This is a test...."); comf.setMessage("This is a test....");
//
// complianceFeatures.add(comf); complianceFeatures.add(comf);
// } }
data.setComplianceFeatures(complianceFeatures); data.setComplianceFeatures(complianceFeatures);
data.setStatus(false); data.setStatus(false);

@ -79,14 +79,14 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION (
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL,
STATUS VARCHAR(50) NULL, STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
@ -331,7 +331,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
COMPLIANCE_STATUS_ID INT NOT NULL, COMPLIANCE_STATUS_ID INT NOT NULL,
FEATURE_CODE INT NOT NULL, FEATURE_CODE VARCHAR(15) NOT NULL,
STATUS INT NULL, STATUS INT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS

@ -64,14 +64,28 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION (
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL, DEVICE_ID INTEGER NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) NULL DEFAULT NULL,
STATUS VARCHAR(50) NULL,
DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL,
TENANT_ID INT NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
ENROLMENT_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL,
STATUS VARCHAR(50) NULL, STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
@ -303,7 +317,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
COMPLIANCE_STATUS_ID INT NOT NULL, COMPLIANCE_STATUS_ID INT NOT NULL,
FEATURE_CODE INT NOT NULL, FEATURE_CODE VARCHAR(15) NOT NULL,
STATUS INT NULL, STATUS INT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS
@ -354,6 +368,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING (
DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
-- POLICY RELATED TABLES FINISHED -- -- POLICY RELATED TABLES FINISHED --

@ -62,14 +62,14 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION (
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL, ENROLMENT_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL, OPERATION_ID INTEGER NOT NULL,
STATUS VARCHAR(50) NULL, STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );

Loading…
Cancel
Save