From 1025e9a4259e8fbdaf6798796f01884b43aaad1a Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Fri, 16 Oct 2015 21:05:51 +0530 Subject: [PATCH] Fixes to user api --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 7 ++++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 42 ++++++++++++++----- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 5 ++- .../policy/mgt/core/PolicyDAOTestCase.java | 1 - 4 files changed, 42 insertions(+), 13 deletions(-) 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 d684525148..9fa1259b31 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 @@ -41,6 +41,13 @@ public interface PolicyDAO { */ Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException; + /** + * This method is used to add/update the users associated with the policy. + * @param usernameList - List of the users that needs to be applied + * @param policy - policy object with the current role list + * @return + * @throws PolicyManagerDAOException + */ Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToDevice(List devices, 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 b33b71289e..7df06e8fe6 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 @@ -110,23 +110,45 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException { + public Policy addPolicyToUser(List usersToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; - PreparedStatement stmt = null; + PreparedStatement insertStmt = null; + PreparedStatement deleteStmt = null; + final List currentUsers = policy.getUsers(); + + SetReferenceTransformer transformer = new SetReferenceTransformer(); + + transformer.transform(currentUsers, usersToAdd); + usersToAdd = transformer.getObjectsToAdd(); + List usersToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; - stmt = conn.prepareStatement(query); - for (String username : usernameList) { - stmt.setInt(1, policy.getId()); - stmt.setString(2, username); - stmt.addBatch(); + if (usersToAdd.size() > 0){ + String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; + insertStmt = conn.prepareStatement(query); + for (String username : usersToAdd) { + insertStmt.setInt(1, policy.getId()); + insertStmt.setString(2, username); + insertStmt.addBatch(); + } + insertStmt.executeBatch(); } - stmt.executeBatch(); + if (usersToDelete.size() > 0){ + String deleteQuery = "DELETE FROM DM_USER_POLICY WHERE USERNAME=? AND POLICY_ID=?"; + deleteStmt = conn.prepareStatement(deleteQuery); + for (String username : usersToDelete) { + deleteStmt.setString(1, username); + deleteStmt.setInt(2, policy.getId()); + deleteStmt.addBatch(); + } + deleteStmt.executeBatch(); + } + } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e); } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); + PolicyManagementDAOUtil.cleanupResources(insertStmt, null); + PolicyManagementDAOUtil.cleanupResources(deleteStmt, 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 7474651d0d..263817802b 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 @@ -471,17 +471,18 @@ public class PolicyManagerImpl implements PolicyManager { Policy policy; List deviceList; List roleNames; - + List userNames; try { PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getPolicy(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId); + userNames = policyDAO.getPolicyAppliedUsers(policyId); Profile profile = profileDAO.getProfile(policy.getProfileId()); policy.setProfile(profile); policy.setRoles(roleNames); - + policy.setUsers(userNames); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + 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 730e02071c..b17fda051c 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 @@ -311,7 +311,6 @@ 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);