From de6572d81931c8a768c32dc9ed4fa9c110eac595 Mon Sep 17 00:00:00 2001 From: milanperera Date: Wed, 22 Jul 2015 17:46:40 +0530 Subject: [PATCH] Fixed for JIRA MDM-372 --- .../policy/mgt/core/dao/FeatureDAO.java | 2 + .../policy/mgt/core/dao/ProfileDAO.java | 2 + .../mgt/core/dao/impl/FeatureDAOImpl.java | 22 +++++ .../mgt/core/dao/impl/ProfileDAOImpl.java | 23 +++++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 99 ++++++++++++++----- .../policy/mgt/core/PolicyDAOTestCase.java | 23 +++-- 6 files changed, 140 insertions(+), 31 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index 1c08a18789..19c9c13715 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -53,4 +53,6 @@ public interface FeatureDAO { boolean deleteFeaturesOfProfile(Profile profile) throws FeatureManagerDAOException; + boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException; + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java index 811a52d1ac..174ca08c48 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/ProfileDAO.java @@ -32,6 +32,8 @@ public interface ProfileDAO { boolean deleteProfile(Profile profile) throws ProfileManagerDAOException; + boolean deleteProfile(int policyId) throws ProfileManagerDAOException; + Profile getProfiles(int profileId) throws ProfileManagerDAOException; List getAllProfiles() throws ProfileManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index e6e5b93abe..f5933350cf 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -261,6 +261,28 @@ public class FeatureDAOImpl implements FeatureDAO { } } + @Override + public boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profileId); + stmt.executeUpdate(); + return true; + + } catch (SQLException e) { + String msg = "Error occurred while deleting the feature related to a profile."; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public List getAllProfileFeatures() throws FeatureManagerDAOException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index 1ec9c10e00..6755878ccc 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -154,6 +154,29 @@ public class ProfileDAOImpl implements ProfileDAO { } } + @Override + public boolean deleteProfile(int profileId) throws ProfileManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_PROFILE WHERE ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, profileId); + stmt.executeUpdate(); + return true; + + } catch (SQLException e) { + String msg = "Error occurred while deleting the profile from the data base."; + log.error(msg); + throw new ProfileManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override public Profile getProfiles(int profileId) throws ProfileManagerDAOException { 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 f80f6964fb..353e26ef34 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 @@ -122,7 +122,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); @@ -133,7 +133,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the profile related to policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (FeatureManagerDAOException e) { @@ -143,7 +143,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the features of profile related to policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -206,7 +206,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while updating the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -236,12 +236,14 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { - boolean bool; try { PolicyManagementDAOFactory.beginTransaction(); - bool = policyDAO.deletePolicy(policy); + policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); + policyDAO.deletePolicy(policy.getId()); + featureDAO.deleteFeaturesOfProfile(policy.getProfileId()); + profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); - + return true; } catch (PolicyManagerDAOException e) { try { PolicyManagementDAOFactory.rollbackTransaction(); @@ -249,21 +251,51 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (ProfileManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Error occurred while deleting the profile for policy (" + + policy.getId() + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (FeatureManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Error occurred while deleting the profile features for policy (" + + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } - return bool; + } @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { - boolean bool; + try { PolicyManagementDAOFactory.beginTransaction(); + Policy policy = policyDAO.getPolicy(policyId); policyDAO.deleteAllPolicyRelatedConfigs(policyId); - bool = policyDAO.deletePolicy(policyId); + policyDAO.deletePolicy(policyId); + + if(log.isDebugEnabled()){ + log.debug(policy.getProfileId() + " ----------------------------------"); + } + + featureDAO.deleteFeaturesOfProfile(policy.getProfileId()); + + profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); + return true; } catch (PolicyManagerDAOException e) { try { @@ -272,16 +304,35 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while deleting the policy (" - + policyId + ")"; + + policyId + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (ProfileManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Error occurred while deleting the profile for policy (" + + policyId + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (FeatureManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Error occurred while deleting the profile features for policy (" + + policyId + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } - return bool; } @Override public Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws - PolicyManagementException { + PolicyManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -315,7 +366,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementDAOException e) { @@ -362,7 +413,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; + + policy.getId() + " - " + policy.getPolicyName() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -400,7 +451,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ") to user list."; + + policy.getId() + " - " + policy.getPolicyName() + ") to user list."; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -534,12 +585,12 @@ public class PolicyManagerImpl implements PolicyManager { Collections.sort(policies); } catch (PolicyManagerDAOException e) { String msg = "Error occurred while getting the policies for device identifier (" + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; + deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while getting device related to device identifier (" + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; + deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } @@ -655,7 +706,7 @@ public class PolicyManagerImpl implements PolicyManager { @Override public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, List profileFeatures) throws - PolicyManagementException { + PolicyManagementException { int deviceId = -1; try { @@ -677,7 +728,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policyId + ")"; + deviceId + " - " + policyId + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementDAOException e) { @@ -690,7 +741,7 @@ public class PolicyManagerImpl implements PolicyManager { @Override public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws - PolicyManagementException { + PolicyManagementException { int deviceId = -1; try { @@ -717,7 +768,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policy.getId() + ")"; + deviceId + " - " + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementDAOException e) { @@ -757,7 +808,7 @@ public class PolicyManagerImpl implements PolicyManager { return true; } catch (PolicyManagerDAOException e) { String msg = "Error occurred while setting the policy has applied to device (" + - deviceIdentifier.getId() + ")"; + deviceIdentifier.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementDAOException e) { 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 7276c82b5d..14b472c8a8 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 @@ -99,8 +99,10 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("addProfileFeatures")) - public void addPolicy() throws PolicyManagementException { - + public void addPolicy() throws PolicyManagementException, ProfileManagementException { + ProfileManager profileManager = new ProfileManagerImpl(); + profile = ProfileCreator.getProfile(featureList); + profileManager.addProfile(profile); PolicyManager policyManager = new PolicyManagerImpl(); policy = PolicyCreator.createPolicy(profile); policyManager.addPolicy(policy); @@ -135,8 +137,10 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("addPolicyToDevice")) - public void addNewPolicy() throws PolicyManagementException { - + public void addNewPolicy() throws PolicyManagementException, ProfileManagementException { + ProfileManager profileManager = new ProfileManagerImpl(); + profile = ProfileCreator.getProfile(featureList); + profileManager.addProfile(profile); PolicyManager policyManager = new PolicyManagerImpl(); policy = PolicyCreator.createPolicy2(profile); policyManager.addPolicy(policy); @@ -144,8 +148,10 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { @Test(dependsOnMethods = ("addPolicyToDevice")) - public void addThirdPolicy() throws PolicyManagementException { - + public void addThirdPolicy() throws PolicyManagementException, ProfileManagementException { + ProfileManager profileManager = new ProfileManagerImpl(); + profile = ProfileCreator.getProfile(featureList); + profileManager.addProfile(profile); PolicyManager policyManager = new PolicyManagerImpl(); policy = PolicyCreator.createPolicy4(profile); policyManager.addPolicy(policy); @@ -239,7 +245,10 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("getRoleRelatedPolicy")) - public void addSecondPolicy() throws PolicyManagementException { + public void addSecondPolicy() throws PolicyManagementException, ProfileManagementException { + ProfileManager profileManager = new ProfileManagerImpl(); + profile = ProfileCreator.getProfile(featureList); + profileManager.addProfile(profile); PolicyManager policyManager = new PolicyManagerImpl(); policy = PolicyCreator.createPolicy3(profile); policyManager.addPolicy(policy);