diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 052199f4e0..e6091c1690 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -21,7 +21,6 @@ package org.wso2.carbon.policy.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; @@ -130,7 +129,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException { - policyManager.addAppliedPolicyToDevice(deviceIdentifier, policy.getId(), policy.getProfile().getProfileFeaturesList()); + policyManager.addAppliedPolicyToDevice(deviceIdentifier, policy); } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index 0aa8c11ac7..f398658df9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -60,8 +60,10 @@ public interface PolicyManager { List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; - void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List profileFeatures) throws - PolicyManagementException; + void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, List + profileFeatures) throws PolicyManagementException; + + void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 821b6ee187..f80f6964fb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -653,14 +653,15 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List profileFeatures) throws + public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, + List profileFeatures) throws PolicyManagementException { int deviceId = -1; try { int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - deviceId = device.getId(); + deviceId = device.getId(); boolean exist = policyDAO.checkPolicyAvailable(deviceId); PolicyManagementDAOFactory.beginTransaction(); if (exist) { @@ -687,6 +688,45 @@ public class PolicyManagerImpl implements PolicyManager { } + @Override + public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws + PolicyManagementException { + + int deviceId = -1; + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + deviceId = device.getId(); + boolean exist = policyDAO.checkPolicyAvailable(deviceId); + PolicyManagementDAOFactory.beginTransaction(); + if (exist) { + Policy policySaved = policyDAO.getAppliedPolicy(deviceId); + if (!policy.equals(policySaved)) { + policyDAO.updateEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). + getProfileFeaturesList()); + } + } else { + policyDAO.addEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). + getProfileFeaturesList()); + } + PolicyManagementDAOFactory.commitTransaction(); + } catch (PolicyManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Error occurred while adding the evaluated policy to device (" + + deviceId + " - " + policy.getId() + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + } + @Override public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {