From 20122b3f1e2d498df0bb591656a5fc5c083d07bc Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 30 Oct 2015 17:48:30 +0530 Subject: [PATCH] Fixing the roles and users adding issue --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 6 + .../mgt/core/dao/impl/PolicyDAOImpl.java | 150 ++++++++++++++++-- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 8 +- 3 files changed, 151 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 9fa1259b313..d1065e17a56 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,8 @@ public interface PolicyDAO { */ Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException; + Policy updateRolesOfPolicy(List rolesToAdd, 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 @@ -50,6 +52,8 @@ public interface PolicyDAO { */ Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException; + Policy updateUserOfPolicy(List usersToAdd, Policy policy) throws PolicyManagerDAOException; + Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException; boolean updatePolicyPriorities(List policies) throws PolicyManagerDAOException; @@ -114,6 +118,8 @@ public interface PolicyDAO { boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException; + boolean deleteCriteriaAndDeviceRelatedConfigs(int policyId) throws PolicyManagerDAOException; + List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException; List getPolicyAppliedUsers(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 322b57918c0..9cc11640d66 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 @@ -71,8 +71,52 @@ public class PolicyDAOImpl implements PolicyDAO { public Policy addPolicyToRole(List rolesToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement insertStmt = null; +// PreparedStatement deleteStmt = null; +// final List currentRoles = this.getPolicy(policy.getId()).getRoles(); +// +// SetReferenceTransformer transformer = new SetReferenceTransformer(); +// +// transformer.transform(currentRoles, rolesToAdd); +// rolesToAdd = transformer.getObjectsToAdd(); +// List rolesToDelete = transformer.getObjectsToRemove(); + try { + conn = this.getConnection(); + 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 (rolesToDelete.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(); +// } + } catch (SQLException e) { + throw new PolicyManagerDAOException("Error occurred while adding the role name with policy to database", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(insertStmt, null); + } + return policy; + } + + + @Override + public Policy updateRolesOfPolicy(List rolesToAdd, Policy previousPolicy) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement insertStmt = null; PreparedStatement deleteStmt = null; - final List currentRoles = policy.getRoles(); + + final List currentRoles = previousPolicy.getRoles(); SetReferenceTransformer transformer = new SetReferenceTransformer(); @@ -81,22 +125,22 @@ public class PolicyDAOImpl implements PolicyDAO { List rolesToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - if (rolesToAdd.size() > 0){ + 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.setInt(2, previousPolicy.getId()); insertStmt.addBatch(); } insertStmt.executeBatch(); } - if (rolesToAdd.size() > 0){ + if (rolesToDelete.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.setInt(2, previousPolicy.getId()); deleteStmt.addBatch(); } deleteStmt.executeBatch(); @@ -105,14 +149,60 @@ public class PolicyDAOImpl implements PolicyDAO { throw new PolicyManagerDAOException("Error occurred while adding the role name with policy to database", e); } finally { PolicyManagementDAOUtil.cleanupResources(insertStmt, null); + PolicyManagementDAOUtil.cleanupResources(deleteStmt, null); } - return policy; + return previousPolicy; } @Override public Policy addPolicyToUser(List usersToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement insertStmt = null; +// PreparedStatement deleteStmt = null; +// final List currentUsers = this.getPolicy(policy.getId()).getUsers(); +// +// SetReferenceTransformer transformer = new SetReferenceTransformer(); +// +// transformer.transform(currentUsers, usersToAdd); +// usersToAdd = transformer.getObjectsToAdd(); +// List usersToDelete = transformer.getObjectsToRemove(); + try { + conn = this.getConnection(); + 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(); + } +// 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(insertStmt, null); +// PolicyManagementDAOUtil.cleanupResources(deleteStmt, null); + } + return policy; + } + + + @Override + public Policy updateUserOfPolicy(List usersToAdd, Policy policy) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement insertStmt = null; PreparedStatement deleteStmt = null; final List currentUsers = policy.getUsers(); @@ -123,7 +213,7 @@ public class PolicyDAOImpl implements PolicyDAO { List usersToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - if (usersToAdd.size() > 0){ + if (usersToAdd.size() > 0) { String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; insertStmt = conn.prepareStatement(query); for (String username : usersToAdd) { @@ -133,7 +223,7 @@ public class PolicyDAOImpl implements PolicyDAO { } insertStmt.executeBatch(); } - if (usersToDelete.size() > 0){ + if (usersToDelete.size() > 0) { String deleteQuery = "DELETE FROM DM_USER_POLICY WHERE USERNAME=? AND POLICY_ID=?"; deleteStmt = conn.prepareStatement(deleteQuery); for (String username : usersToDelete) { @@ -153,6 +243,7 @@ public class PolicyDAOImpl implements PolicyDAO { return policy; } + @Override public Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException { Connection conn; @@ -1202,6 +1293,46 @@ public class PolicyDAOImpl implements PolicyDAO { } } + + @Override + public boolean deleteCriteriaAndDeviceRelatedConfigs(int policyId) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + +// String userPolicy = "DELETE FROM DM_USER_POLICY WHERE POLICY_ID = ?"; +// stmt = conn.prepareStatement(userPolicy); +// stmt.setInt(1, policyId); +// stmt.executeUpdate(); +// +// String rolePolicy = "DELETE FROM DM_ROLE_POLICY WHERE POLICY_ID = ?"; +// stmt = conn.prepareStatement(rolePolicy); +// stmt.setInt(1, policyId); +// stmt.executeUpdate(); + + String devicePolicy = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(devicePolicy); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + String deleteCriteria = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(deleteCriteria); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + if (log.isDebugEnabled()) { + log.debug("Policy (" + policyId + ") related configs deleted from database."); + } + return true; + } catch (SQLException e) { + throw new PolicyManagerDAOException("Unable to delete the policy (" + policyId + + ") related configs from database", e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + private Connection getConnection() throws PolicyManagerDAOException { return PolicyManagementDAOFactory.getConnection(); } @@ -1345,7 +1476,8 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? AND " + + "ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); 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 e563d8c631e..06b71d071a2 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 @@ -180,15 +180,15 @@ public class PolicyManagerImpl implements PolicyManager { if (!newFeaturesList.isEmpty()) { featureDAO.addProfileFeatures(newFeaturesList, profileId); } - policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); + policyDAO.deleteCriteriaAndDeviceRelatedConfigs(policy.getId()); if (policy.getUsers() != null) { - policyDAO.addPolicyToUser(policy.getUsers(), previousPolicy); + policyDAO.updateUserOfPolicy(policy.getUsers(), previousPolicy); } if (policy.getRoles() != null) { - policyDAO.addPolicyToRole(policy.getRoles(), previousPolicy); + policyDAO.updateRolesOfPolicy(policy.getRoles(), previousPolicy); } if (policy.getDevices() != null) { @@ -539,7 +539,7 @@ public class PolicyManagerImpl implements PolicyManager { policy.setDevices(deviceList); try { - // PolicyManagementDAOFactory.openConnection(); + // PolicyManagementDAOFactory.openConnection(); Profile profile = profileManager.getProfile(policy.getProfileId()); policy.setProfile(profile); } catch (ProfileManagementException e) {