Fixes to user api

revert-70aa11f8
Dulitha Wijewantha 9 years ago
parent 9fca18ae90
commit 1025e9a425

@ -41,6 +41,13 @@ public interface PolicyDAO {
*/ */
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToRole(List<String> 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<String> usernameList, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException;

@ -110,23 +110,45 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException { public Policy addPolicyToUser(List<String> usersToAdd, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement insertStmt = null;
PreparedStatement deleteStmt = null;
final List<String> currentUsers = policy.getUsers();
SetReferenceTransformer<String> transformer = new SetReferenceTransformer<String>();
transformer.transform(currentUsers, usersToAdd);
usersToAdd = transformer.getObjectsToAdd();
List<String> usersToDelete = transformer.getObjectsToRemove();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; if (usersToAdd.size() > 0){
stmt = conn.prepareStatement(query); String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)";
for (String username : usernameList) { insertStmt = conn.prepareStatement(query);
stmt.setInt(1, policy.getId()); for (String username : usersToAdd) {
stmt.setString(2, username); insertStmt.setInt(1, policy.getId());
stmt.addBatch(); 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) { } catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e); throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(insertStmt, null);
PolicyManagementDAOUtil.cleanupResources(deleteStmt, null);
} }
return policy; return policy;
} }

@ -471,17 +471,18 @@ public class PolicyManagerImpl implements PolicyManager {
Policy policy; Policy policy;
List<Device> deviceList; List<Device> deviceList;
List<String> roleNames; List<String> roleNames;
List<String> userNames;
try { try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getPolicy(policyId); policy = policyDAO.getPolicy(policyId);
roleNames = policyDAO.getPolicyAppliedRoles(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId);
userNames = policyDAO.getPolicyAppliedUsers(policyId);
Profile profile = profileDAO.getProfile(policy.getProfileId()); Profile profile = profileDAO.getProfile(policy.getProfileId());
policy.setProfile(profile); policy.setProfile(profile);
policy.setRoles(roleNames); policy.setRoles(roleNames);
policy.setUsers(userNames);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" +

@ -311,7 +311,6 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
policy = pap.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId()); pap.activatePolicy(policy.getId());
List<String> users = new ArrayList<>(); List<String> users = new ArrayList<>();
log.debug(policy.getRoles().size());
users.add("Udara"); users.add("Udara");
users.add("Dileesha"); users.add("Dileesha");
policy.setUsers(users); policy.setUsers(users);

Loading…
Cancel
Save