Policy Updating

* Fixed issues regarding roles updating for policy
* Fixed the relevent test cases

JIRA:- https://wso2.org/jira/browse/EMM-812
revert-70aa11f8
Dulitha Wijewantha 9 years ago
parent 1c8bb7551d
commit 45ce4dbe4e

@ -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<String> roleNames, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException;

@ -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<String> roleNames, Policy policy) throws PolicyManagerDAOException {
public Policy addPolicyToRole(List<String> rolesToAdd, Policy policy) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
PreparedStatement insertStmt = null;
PreparedStatement deleteStmt = null;
final List<String> currentRoles = policy.getRoles();
SetReferenceTransformer<String> transformer = new SetReferenceTransformer<String>();
transformer.transform(currentRoles, rolesToAdd);
rolesToAdd = transformer.getObjectsToAdd();
List<String> rolesToDelete = transformer.getObjectsToRemove();
try {
conn = this.getConnection();
if (rolesToAdd.size() > 0){
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();
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;
}

@ -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) {

@ -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 {

@ -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<T>{
private List<T> objectsToRemove;
private List<T> 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<T> currentList, List<T> nextList){
TreeSet<T> existingSet = new TreeSet<T>(currentList);
TreeSet<T> newSet = new TreeSet<T>(nextList);;
existingSet.removeAll(newSet);
objectsToRemove = new ArrayList<>(existingSet);
// Clearing and re-initializing the set
existingSet = new TreeSet<T>(currentList);
newSet.removeAll(existingSet);
objectsToAdd = new ArrayList<T>(newSet);
}
public List<T> getObjectsToRemove() {
return objectsToRemove;
}
public List<T> getObjectsToAdd() {
return objectsToAdd;
}
}

@ -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<String> users = new ArrayList<>();
log.debug(policy.getRoles().size());
users.add("Udara");
users.add("Dileesha");
policy.setUsers(users);

Loading…
Cancel
Save