From 35ce4be1a1220db9c20bf28219006fb6e53b7934 Mon Sep 17 00:00:00 2001 From: milanperera Date: Thu, 23 Jul 2015 15:55:32 +0530 Subject: [PATCH 1/3] Partial Commit --- .../device/mgt/common/EnrolmentInfo.java | 19 +++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 3 + .../mgt/core/dao/impl/DeviceDAOImpl.java | 31 ++++ .../operation/mgt/OperationManagerImpl.java | 108 +++++-------- .../DeviceManagementProviderServiceImpl.java | 143 +++++++++++------- 5 files changed, 178 insertions(+), 126 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index b3a594a95c3..2c599474fc9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -92,4 +92,23 @@ public class EnrolmentInfo { 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; + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 75cddd8141c..5b5d0c66aa6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -62,5 +62,8 @@ public interface DeviceDAO { List getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException; + + int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, + int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index bd0cbe6038f..f82038ca3ef 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -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 { Device device = new Device(); device.setId(rs.getInt("DEVICE_ID")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 99116a47289..2dc6731ded5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -21,9 +21,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; @@ -32,7 +30,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.DeviceManagementDAOFactory; 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.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -91,17 +88,17 @@ public class OperationManagerImpl implements OperationManager { int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); - Device device; + int enrolmentId; int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); for (DeviceIdentifier deviceId : deviceIds) { - device = deviceDAO.getDevice(deviceId, tenantId); - if (device == null) { + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + if (enrolmentId < 0) { String errorMsg = "The operation not added for device.The device not found for " + "device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(); - log.info(errorMsg); + log.error(errorMsg); } else { - operationMappingDAO.addOperationMapping(operationId, device.getId()); + operationMappingDAO.addOperationMapping(operationId, enrolmentId); } } OperationManagementDAOFactory.commitTransaction(); @@ -128,20 +125,20 @@ public class OperationManagerImpl implements OperationManager { try { List operations = new ArrayList(); - Device device = null; + int enrolmentId = -1; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); } catch (DeviceManagementDAOException e) { e.printStackTrace(); } - if (device == null) { + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } List operationList = operationDAO - .getOperationsForDevice(device.getId()); + .getOperationsForDevice(enrolmentId); Operation operation; for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { operation = OperationDAOUtil.convertOperation(dtoOperation); @@ -164,7 +161,7 @@ public class OperationManagerImpl implements OperationManager { + "]"); } - Device device; + int enrolmentId = -1; List operations = new ArrayList(); List dtoOperationList = @@ -172,38 +169,23 @@ public class OperationManagerImpl implements OperationManager { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); - - if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( - EnrolmentInfo.Status.ACTIVE)){ - try { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() - .updateDeviceEnrolmentInfo(device, - EnrolmentInfo.Status.ACTIVE); - }catch (DeviceManagementException deviceMgtEx){ - String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); - log.error(errorMsg, deviceMgtEx); - throw new OperationManagementException(errorMsg, deviceMgtEx); - } - } - + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - - if (device == null) { + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); } - dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); Operation operation; @@ -234,30 +216,17 @@ public class OperationManagerImpl implements OperationManager { + "]"); } Operation operation = null; - Device device; + int enrolmentId = -1; try { 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:" + deviceId.getId() + " and given type" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO - .getNextOperation(device.getId()); - - if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( - EnrolmentInfo.Status.ACTIVE)){ - try { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() - .updateDeviceEnrolmentInfo(device, - EnrolmentInfo.Status.ACTIVE); - }catch (DeviceManagementException deviceMgtEx){ - String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); - log.error(errorMsg, deviceMgtEx); - throw new OperationManagementException(errorMsg, deviceMgtEx); - } - } + .getNextOperation(enrolmentId); if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND @@ -303,11 +272,11 @@ public class OperationManagerImpl implements OperationManager { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - Device device = deviceDAO.getDevice(deviceId, tenantId); + int enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); if (operation.getStatus() !=null) { OperationManagementDAOFactory.beginTransaction(); - operationDAO.updateOperationStatus(device.getId(), operationId, + operationDAO.updateOperationStatus(enrolmentId, operationId, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status .valueOf(operation.getStatus().toString())); OperationManagementDAOFactory.commitTransaction(); @@ -315,7 +284,7 @@ public class OperationManagerImpl implements OperationManager { if (operation.getOperationResponse() != null){ OperationManagementDAOFactory.beginTransaction(); - operationDAO.addOperationResponse(device.getId(), operationId, operation.getOperationResponse()); + operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); OperationManagementDAOFactory.commitTransaction(); } } catch (OperationManagementDAOException ex) { @@ -362,7 +331,7 @@ public class OperationManagerImpl implements OperationManager { @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { - Device device; + int enrolmentId = -1; Operation operation; if (log.isDebugEnabled()) { @@ -372,13 +341,13 @@ public class OperationManagerImpl implements OperationManager { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); - if (device == null) { + enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); + if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device identifier:" + deviceId.getId() + " type:" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO - .getOperationByDeviceAndId(device.getId(), operationId); + .getOperationByDeviceAndId(enrolmentId, operationId); if (dtoOperation.getType() .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { @@ -400,7 +369,7 @@ public class OperationManagerImpl implements OperationManager { if (dtoOperation == null) { throw new OperationManagementException("Operation not found for operation Id:" + operationId + - " device" + " Id:" + device.getId()); + " device id:" + deviceId.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { @@ -427,25 +396,28 @@ public class OperationManagerImpl implements OperationManager { new ArrayList(); 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() + " " + "type:" + deviceId.getType()); } 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()); - dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(device.getId(), dtoOpStatus)); + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); - dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll( + configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll( + profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); - dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(device.getId(), - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + dtoOperationList.addAll( + policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); Operation operation; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 0538923cdfd..289fd4c312f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -47,7 +47,7 @@ import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, - PluginInitializationListener { + PluginInitializationListener { private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; @@ -70,9 +70,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv /** * This constructor calls from unit tests + * * @param pluginRepo */ - DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test){ + DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test) { this.pluginRepository = pluginRepo; initDataAccessObjects(); isTest = test; @@ -98,31 +99,59 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - + boolean status = false; + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); - boolean status = dms.enrollDevice(device); + dms.enrollDevice(device); try { - if (dms.isClaimable(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()))) { + if (dms.isClaimable(deviceIdentifier)) { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); } else { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); } int tenantId = getTenantId(); - DeviceManagementDAOFactory.beginTransaction(); - - DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); - int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); - int enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); - - 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() + "'"); + Device existingDevice = this.getDevice(deviceIdentifier); + + if (existingDevice != null) { + EnrolmentInfo existingEnrolmentInfo = existingDevice.getEnrolmentInfo(); + EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); + if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { + if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { + this.modifyEnrollment(device); + status = true; + } 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) { try { DeviceManagementDAOFactory.rollbackTransaction(); @@ -130,7 +159,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.warn("Error occurred while roll-backing the current transaction", e); } throw new DeviceManagementException("Error occurred while enrolling the device " + - "'" + device.getId() + "'", e); + "'" + device.getId() + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -142,7 +171,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } - @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { DeviceManager dms = @@ -163,7 +191,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.warn("Error occurred while roll-backing the current transaction", e); } throw new DeviceManagementException("Error occurred while modifying the device " + - "'" + device.getId() + "'", e); + "'" + device.getId() + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -181,7 +209,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManager dms = this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); try { - Device device = deviceDAO.getDevice(deviceId,tenantId); + Device device = deviceDAO.getDevice(deviceId, tenantId); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); @@ -189,8 +217,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceDAO.updateDevice(deviceType.getId(), device, tenantId); } catch (DeviceManagementDAOException e) { - String errorMsg = "Error occurred while fetch device for device Identifier:"; - log.error(errorMsg + deviceId.toString(),e); + String errorMsg = "Error occurred while fetch device for device Identifier:"; + log.error(errorMsg + deviceId.toString(), e); throw new DeviceManagementException(errorMsg, e); } @@ -229,7 +257,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + "the current tenant", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -261,7 +289,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(type, tenantId); } catch (DeviceManagementDAOException e) { 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 { try { DeviceManagementDAOFactory.closeConnection(); @@ -317,13 +345,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", - URLEncoder.encode(emailMessageProperties.getFirstName(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim() + System.getProperty("line.separator") + - System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + System.getProperty("line.separator") + url.replaceAll("\\{" + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) .append(System.getProperty("line.separator")); @@ -331,12 +359,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv messageBuilder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator")); messageBuilder.append(messageFooter1.trim()) .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) { log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + - emailMessageProperties.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setSubject(subject); @@ -374,23 +402,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", - URLEncoder.encode(emailMessageProperties.getFirstName(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants - .USERNAME - + "\\}", - URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants - .ENCODED_SCHEME)); + .USERNAME + + "\\}", + URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}", - URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants - .ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")); messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( @@ -401,7 +429,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (IOException e) { log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + - emailMessageProperties.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setSubject(subject); @@ -416,7 +444,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device = deviceDAO.getDevice(deviceId, tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); + "'" + deviceId.getId() + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -502,7 +530,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int addOperation(Operation operation, List devices) throws - OperationManagementException { + OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices); } @@ -562,7 +590,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv userDevices = deviceDAO.getDevicesOfUser(username, tenantId); } catch (DeviceManagementDAOException e) { 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 { try { DeviceManagementDAOFactory.closeConnection(); @@ -593,11 +621,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String[] users; int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { - users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) + users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) .getUserStoreManager().getUserListOfRole(role); } catch (UserStoreException e) { throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + - "with the role '" + role + "'", e); + "with the role '" + role + "'", e); } List userDevices; @@ -657,7 +685,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevicesByName(deviceName, tenantId); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" - + deviceName + "'", e); + + deviceName + "'", e); } finally { try { DeviceManagementDAOFactory.closeConnection(); @@ -689,8 +717,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); - }catch (DeviceManagementDAOException deviceDaoEx){ - String errorMsg = "Error occured update device enrolment status : "+device.getId(); + } catch (DeviceManagementDAOException deviceDaoEx) { + String errorMsg = "Error occured update device enrolment status : " + device.getId(); log.error(errorMsg, deviceDaoEx); throw new DeviceManagementException(errorMsg, deviceDaoEx); } @@ -702,7 +730,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv pluginRepository.addDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while registering device management plugin '" + - deviceManagementService.getType() + "'", e); + deviceManagementService.getType() + "'", e); } } @@ -712,7 +740,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv pluginRepository.removeDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while un-registering device management plugin '" + - deviceManagementService.getType() + "'", e); + deviceManagementService.getType() + "'", e); } } @@ -754,15 +782,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } - private int getTenantId() { ThreadLocal tenantId = new ThreadLocal(); int tenant = 0; - if (isTest){ + if (isTest) { tenant = DeviceManagerUtil.currentTenant.get(); - }else{ + } else { tenant = CarbonContext.getThreadLocalCarbonContext().getTenantId(); } return tenant; From 7ec10d670cf5c5b363101a4c62a27640a638103a Mon Sep 17 00:00:00 2001 From: milanperera Date: Thu, 23 Jul 2015 17:32:05 +0530 Subject: [PATCH 2/3] Fixed bugs in device disenrolment --- .../mgt/core/service/DeviceManagementProviderServiceImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 4efceee978b..a3c58daddf9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -233,6 +233,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + enrolmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); } catch (DeviceManagementDAOException e) { From 5b2e0c898908d8a2034924efcf49cafe2eda85fb Mon Sep 17 00:00:00 2001 From: milanperera Date: Fri, 24 Jul 2015 12:32:26 +0530 Subject: [PATCH 3/3] Merged --- .../mgt/core/dao/impl/EnrolmentDAOImpl.java | 8 +-- .../DeviceManagementProviderServiceImpl.java | 1 + .../mgt/core/dao/impl/PolicyDAOImpl.java | 56 ++++++++++--------- .../src/main/resources/dbscripts/cdm/h2.sql | 31 ++++++++++ 4 files changed, 65 insertions(+), 31 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java index f6b22e07d62..b616202f828 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java @@ -77,7 +77,7 @@ public class EnrolmentDAOImpl implements EnrolmentDAO { stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString()); 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.setString(6, enrolmentInfo.getOwner()); stmt.setInt(7, tenantId); @@ -156,9 +156,9 @@ public class EnrolmentDAOImpl implements EnrolmentDAO { conn = this.getConnection(); String sql = "SELECT STATUS FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); - stmt.setInt(2, deviceId); - stmt.setString(3, currentOwner); - stmt.setInt(4, tenantId); + stmt.setInt(1, deviceId); + stmt.setString(2, currentOwner); + stmt.setInt(3, tenantId); rs = stmt.executeQuery(); if (rs.next()) { status = EnrolmentInfo.Status.valueOf(rs.getString("STATUS")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index a3c58daddf9..c2e352384e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -138,6 +138,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { + device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment()); this.modifyEnrollment(device); status = true; } else { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 08232ea7edc..586c9b31c1d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -416,7 +416,7 @@ public class PolicyDAOImpl implements PolicyDAO { } catch (SQLException e) { String msg = "Error occurred while inserting the criterion to policy (" + policy.getPolicyName() + ") " + - "to database."; + "to database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { @@ -436,7 +436,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + - "CONTENT) VALUES (?, ?, ?, ?)"; + "CONTENT) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(query); for (PolicyCriterion criterion : policyCriteria) { @@ -477,9 +477,9 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); 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 " + - "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + - "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; + "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + + "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + + "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); resultSet = stmt.executeQuery(); @@ -525,7 +525,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + - " WHERE ID = ?"; + " WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getTenantId()); @@ -758,7 +758,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED " + - "(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; + "(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); stmt.setInt(2, policyId); @@ -814,7 +814,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); 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.setInt(1, policyId); stmt.setObject(2, profileFeatures); @@ -1068,7 +1068,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); 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.setString(1, policy.getPolicyName()); @@ -1245,28 +1245,30 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, deviceId); resultSet = stmt.executeQuery(); - ByteArrayInputStream bais = null; ObjectInputStream ois = null; byte[] contentBytes; - try { - contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); - bais = new ByteArrayInputStream(contentBytes); - ois = new ObjectInputStream(bais); - policy = (Policy) ois.readObject(); - } finally { - if (bais != null) { - try { - bais.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ByteArrayOutputStream", e); + + while (resultSet.next()) { + try { + contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + policy = (Policy) ois.readObject(); + } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } } - } - if (ois != null) { - try { - ois.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ObjectOutputStream", e); + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } } } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index d417bfe573b..cf8bf647d9b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -322,6 +322,37 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + STATUS INT NULL, + LAST_SUCCESS_TIME TIMESTAMP NULL, + LAST_REQUESTED_TIME TIMESTAMP NULL, + LAST_FAILED_TIME TIMESTAMP NULL, + ATTEMPTS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + + +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( + ID INT NOT NULL AUTO_INCREMENT, + COMPLIANCE_STATUS_ID INT NOT NULL, + FEATURE_CODE INT NOT NULL, + STATUS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS + FOREIGN KEY (COMPLIANCE_STATUS_ID) + REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + -- POLICY RELATED TABLES FINISHED --