From 3382686d52f32704e536e6dd2858ec01fac61313 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 21 May 2015 23:17:33 +0530 Subject: [PATCH] Fixing policies related issues, and adding the delete policy --- .../mgt/common/PolicyAdministratorPoint.java | 1 + .../policy/mgt/core/PolicyManagerService.java | 2 ++ .../mgt/core/PolicyManagerServiceImpl.java | 5 ++++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 ++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 25 +++++++++++++++++++ .../impl/PolicyAdministratorPointImpl.java | 5 ++++ .../mgt/core/impl/PolicyFilterImpl.java | 2 +- .../policy/mgt/core/mgt/PolicyManager.java | 2 ++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 23 +++++++++++++++++ .../core/service/PolicyManagementService.java | 5 ++++ 10 files changed, 71 insertions(+), 1 deletion(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 53f8411bbc..07c4e66f1d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -39,6 +39,7 @@ public interface PolicyAdministratorPoint { Policy updatePolicy(Policy policy) throws PolicyManagementException; boolean deletePolicy(Policy policy) throws PolicyManagementException; + boolean deletePolicy(int policyId) throws PolicyManagementException; /** * This method adds a policy per device which should be implemented by the related plugins. diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index 2cc06c0073..118566f188 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -48,6 +48,8 @@ public interface PolicyManagerService { boolean deletePolicy(Policy policy) throws PolicyManagementException; + boolean deletePolicy(int policyId) throws PolicyManagementException; + Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 338170cfba..b3c4644b9b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -78,6 +78,11 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { return policyAdministratorPoint.deletePolicy(policy); } + @Override + public boolean deletePolicy(int policyId) throws PolicyManagementException { + return policyAdministratorPoint.deletePolicy(policyId); + } + @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index a3d4331559..408ab11abf 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -82,6 +82,8 @@ public interface PolicyDAO { boolean deletePolicy(Policy policy) throws PolicyManagerDAOException; + boolean deletePolicy(int policyId) throws PolicyManagerDAOException; + boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException; List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException; 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 156bceeeb8..2a5b175755 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 @@ -1073,6 +1073,31 @@ public class PolicyDAOImpl implements PolicyDAO { } } + @Override + public boolean deletePolicy(int policyId) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY WHERE ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + if (log.isDebugEnabled()) { + log.debug("Policy (" + policyId+ ") delete from database."); + } + return true; + } catch (SQLException e) { + String msg = "Unable to delete the policy (" + policyId + ") from database."; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException { 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 c1c8f73d92..c61b9802a2 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 @@ -68,6 +68,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { return policyManager.deletePolicy(policy); } + @Override + public boolean deletePolicy(int policyId) throws PolicyManagementException { + return policyManager.deletePolicy(policyId); + } + @Override public Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws PolicyManagementException { return policyManager.addPolicyToDevice(deviceIdentifierList, policy); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index b9552e542e..0ebfe3daa2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -39,7 +39,7 @@ public class PolicyFilterImpl implements PolicyFilter { for (Policy policy : policies) { List tempRoles = policy.getRoles(); - if (tempRoles == null) { + if (tempRoles.isEmpty()) { continue; } if (PolicyManagementConstants.ANY.equalsIgnoreCase(tempRoles.get(0))) { 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 f7640093a3..dfe9ff7e9d 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 @@ -34,6 +34,8 @@ public interface PolicyManager { boolean deletePolicy(Policy policy) throws PolicyManagementException; + boolean deletePolicy(int policyId) throws PolicyManagementException; + Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) 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 be03d1e8df..d275eb3c9a 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 @@ -233,6 +233,29 @@ public class PolicyManagerImpl implements PolicyManager { return bool; } + @Override + public boolean deletePolicy(int policyId) throws PolicyManagementException { + boolean bool; + try { + PolicyManagementDAOFactory.beginTransaction(); + policyDAO.deleteAllPolicyRelatedConfigs(policyId); + bool = policyDAO.deletePolicy(policyId); + 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 deleting the policy (" + + policyId +")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return bool; + } + @Override public Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws PolicyManagementException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java index 2dcfbc99c5..a45bcbd0fe 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -77,6 +77,11 @@ public class PolicyManagementService implements PolicyManagerService { return policyManagerService.deletePolicy(policy); } + @Override + public boolean deletePolicy(int policyId) throws PolicyManagementException { + return policyManagerService.deletePolicy(policyId); + } + @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { return policyManagerService.getEffectivePolicy(deviceIdentifier);