From 45ce4dbe4e11ac25318087c9013967cee1e6e7f9 Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Mon, 12 Oct 2015 12:09:21 +0530 Subject: [PATCH] Policy Updating * Fixed issues regarding roles updating for policy * Fixed the relevent test cases JIRA:- https://wso2.org/jira/browse/EMM-812 --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 9 +++- .../mgt/core/dao/impl/PolicyDAOImpl.java | 44 +++++++++++++------ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 13 +++--- .../mgt/core/util/PolicyManagerUtil.java | 5 +-- .../core/util/SetReferenceTransformer.java | 42 ++++++++++++++++++ .../policy/mgt/core/PolicyDAOTestCase.java | 8 ++-- 6 files changed, 93 insertions(+), 28 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/SetReferenceTransformer.java 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 9f5ba5b255..d684525148 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 @@ -19,11 +19,9 @@ package org.wso2.carbon.policy.mgt.core.dao; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyCriterion; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; import java.util.HashMap; import java.util.List; @@ -34,6 +32,13 @@ public interface PolicyDAO { Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException; + /** + * This method is used to add/update the roles associated with the policy. + * @param roleNames - List of the roles that needs to be applied + * @param policy - policy object with the current role list + * @return + * @throws PolicyManagerDAOException + */ Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToUser(List usernameList, Policy policy) 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 e1921f1aac..b33b71289e 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 @@ -22,17 +22,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyCriterion; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; -import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; +import org.wso2.carbon.policy.mgt.core.util.SetReferenceTransformer; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -70,23 +68,43 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException { + public Policy addPolicyToRole(List rolesToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; - PreparedStatement stmt = null; + PreparedStatement insertStmt = null; + PreparedStatement deleteStmt = null; + final List currentRoles = policy.getRoles(); + + SetReferenceTransformer transformer = new SetReferenceTransformer(); + + transformer.transform(currentRoles, rolesToAdd); + rolesToAdd = transformer.getObjectsToAdd(); + List rolesToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; - stmt = conn.prepareStatement(query); - for (String role : roleNames) { - stmt.setString(1, role); - stmt.setInt(2, policy.getId()); - stmt.addBatch(); + if (rolesToAdd.size() > 0){ + String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; + insertStmt = conn.prepareStatement(query); + for (String role : rolesToAdd) { + insertStmt.setString(1, role); + insertStmt.setInt(2, policy.getId()); + insertStmt.addBatch(); + } + insertStmt.executeBatch(); + } + if (rolesToAdd.size() > 0){ + String deleteQuery = "DELETE FROM DM_ROLE_POLICY WHERE ROLE_NAME=? AND POLICY_ID=?"; + deleteStmt = conn.prepareStatement(deleteQuery); + for (String role : rolesToDelete) { + deleteStmt.setString(1, role); + deleteStmt.setInt(2, policy.getId()); + deleteStmt.addBatch(); + } + deleteStmt.executeBatch(); } - stmt.executeBatch(); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the role name with policy to database", e); } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); + PolicyManagementDAOUtil.cleanupResources(insertStmt, null); } return policy; } 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 a9f1a10fd8..7474651d0d 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 @@ -25,13 +25,11 @@ 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.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.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.*; -import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; @@ -136,6 +134,9 @@ public class PolicyManagerImpl implements PolicyManager { public Policy updatePolicy(Policy policy) throws PolicyManagementException { try { + // Previous policy needs to be obtained before begining the transaction + Policy previousPolicy = getPolicy(policy.getId()); + PolicyManagementDAOFactory.beginTransaction(); // This will keep track of the policies updated. policyDAO.recordUpdatedPolicy(policy); @@ -146,16 +147,18 @@ public class PolicyManagerImpl implements PolicyManager { .getProfileId()); policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); + + if (policy.getUsers() != null) { - policyDAO.addPolicyToUser(policy.getUsers(), policy); + policyDAO.addPolicyToUser(policy.getUsers(), previousPolicy); } if (policy.getRoles() != null) { - policyDAO.addPolicyToRole(policy.getRoles(), policy); + policyDAO.addPolicyToRole(policy.getRoles(), previousPolicy); } if (policy.getDevices() != null) { - policyDAO.addPolicyToDevice(policy.getDevices(), policy); + policyDAO.addPolicyToDevice(policy.getDevices(), previousPolicy); } if (policy.getPolicyCriterias() != null) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 982b3859a7..a9d292c732 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -41,10 +41,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.ObjectOutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Hashtable; -import java.util.List; +import java.util.*; public class PolicyManagerUtil { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/SetReferenceTransformer.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/SetReferenceTransformer.java new file mode 100644 index 0000000000..f9d78e05fc --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/SetReferenceTransformer.java @@ -0,0 +1,42 @@ +package org.wso2.carbon.policy.mgt.core.util; + +import java.util.ArrayList; +import java.util.List; +import java.util.TreeSet; + +public class SetReferenceTransformer{ + private List objectsToRemove; + private List objectsToAdd; + + /** + * Use the Set theory to find the objects to delete and objects to add + + The difference of objects in existingSet and newSet needed to be deleted + + new roles to add = newSet - The intersection of roles in existingSet and newSet + * @param currentList + * @param nextList + */ + public void transform(List currentList, List nextList){ + TreeSet existingSet = new TreeSet(currentList); + TreeSet newSet = new TreeSet(nextList);; + + existingSet.removeAll(newSet); + + objectsToRemove = new ArrayList<>(existingSet); + + // Clearing and re-initializing the set + existingSet = new TreeSet(currentList); + + newSet.removeAll(existingSet); + objectsToAdd = new ArrayList(newSet); + } + + public List getObjectsToRemove() { + return objectsToRemove; + } + + public List getObjectsToAdd() { + return objectsToAdd; + } +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index ffc592b335..730e02071c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -27,15 +27,12 @@ import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; -import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.util.*; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -163,6 +160,8 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { roles.add("Test_ROLE_02"); roles.add("Test_ROLE_03"); + policy = pap.getPolicy(policy.getId()); + pap.addPolicyToRole(roles, policy); } @@ -312,6 +311,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { policy = pap.addPolicy(policy); pap.activatePolicy(policy.getId()); List users = new ArrayList<>(); + log.debug(policy.getRoles().size()); users.add("Udara"); users.add("Dileesha"); policy.setUsers(users);