From e486db868e70585adc5a06e2753239fb948ed33c Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 20 Mar 2020 14:20:24 +0530 Subject: [PATCH 01/73] Change policy --- .../device/mgt/jaxrs/beans/PolicyWrapper.java | 14 -- .../mgt/jaxrs/beans/ProfileFeature.java | 14 ++ .../impl/PolicyManagementServiceImpl.java | 1 - .../device/mgt/jaxrs/util/DeviceMgtUtil.java | 1 + .../device/mgt/common/policy/mgt/Policy.java | 17 --- .../mgt/common/policy/mgt/ProfileFeature.java | 14 ++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 11 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 23 ++-- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 123 +++++++++++++----- .../mgt/core/util/PolicyManagerUtil.java | 55 +++++++- .../src/test/resources/sql/CreateH2TestDB.sql | 10 +- .../test/resources/sql/CreateMySqlTestDB.sql | 6 + .../src/main/resources/dbscripts/cdm/h2.sql | 10 +- .../main/resources/dbscripts/cdm/mssql.sql | 6 + .../main/resources/dbscripts/cdm/mysql.sql | 6 + .../main/resources/dbscripts/cdm/oracle.sql | 6 +- .../resources/dbscripts/cdm/postgresql.sql | 17 +++ 17 files changed, 248 insertions(+), 86 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java index 92e07aeab1..6c8358f257 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java @@ -126,12 +126,6 @@ public class PolicyWrapper { @NotNull private String policyType; - @ApiModelProperty( - name = "correctiveActions", - value = "List of corrective actions to be applied when the policy is violated" - ) - private List correctiveActions; - public String getPolicyType() { return policyType; } @@ -219,12 +213,4 @@ public class PolicyWrapper { public void setDeviceGroups(List deviceGroups) { this.deviceGroups = deviceGroups; } - - public List getCorrectiveActions() { - return correctiveActions; - } - - public void setCorrectiveActions(List correctiveActions) { - this.correctiveActions = correctiveActions; - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java index b32c7613f4..754caa201f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java @@ -21,8 +21,10 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import com.google.gson.Gson; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; import java.io.Serializable; +import java.util.List; @ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile " + "features") @@ -57,6 +59,10 @@ public class ProfileFeature implements Serializable { value = "The payload which is submitted to each feature", required = true) private String payLoad; + @ApiModelProperty(name = "correctiveActions", + value = "List of corrective actions to be applied when the policy is violated", + required = true) + private List correctiveActions; public int getId() { return id; @@ -109,4 +115,12 @@ public class ProfileFeature implements Serializable { public void setContent(Object content) { this.content = content; } + + public List getCorrectiveActions() { + return correctiveActions; + } + + public void setCorrectiveActions(List correctiveActions) { + this.correctiveActions = correctiveActions; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index a9fc47691e..79d8d93e39 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -140,7 +140,6 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setPolicyName(policyWrapper.getPolicyName()); policy.setDescription(policyWrapper.getDescription()); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile())); - policy.setCorrectiveActions(policyWrapper.getCorrectiveActions()); policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setActive(policyWrapper.isActive()); policy.setRoles(policyWrapper.getRoles()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java index c11fd758c9..31d1b38104 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java @@ -60,6 +60,7 @@ public class DeviceMgtUtil { profileFeature.setDeviceType(mdmProfileFeature.getDeviceTypeId()); profileFeature.setFeatureCode(mdmProfileFeature.getFeatureCode()); profileFeature.setId(mdmProfileFeature.getId()); + profileFeature.setCorrectiveActions(mdmProfileFeature.getCorrectiveActions()); return profileFeature; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java index 452521286b..f4585604ff 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java @@ -201,13 +201,6 @@ public class Policy implements Comparable, Serializable { example = "GENERAL") private String policyType; - @ApiModelProperty( - name = "correctiveActions", - value = "List of corrective actions to be applied when the policy is violated", - example = "[{'actionType': 'POLICY', 'policyId': 1}]" - ) - private List correctiveActions; - @XmlElement public int getId() { return id; @@ -379,16 +372,6 @@ public class Policy implements Comparable, Serializable { this.policyType = policyType; } - @XmlElement - public List getCorrectiveActions() { - return correctiveActions; - } - - public void setCorrectiveActions( - List correctiveActions) { - this.correctiveActions = correctiveActions; - } - @Override public int compareTo(Policy o) { if (this.priorityId == o.priorityId) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java index 638814b63d..4d0bde90b6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java @@ -22,6 +22,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; +import java.util.List; @ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile " + "features") @@ -64,6 +65,11 @@ public class ProfileFeature implements Serializable { example = "{\\\"enabled\\\":false}") private Object content; + @ApiModelProperty(name = "correctiveActions", + value = "List of corrective actions to be applied when the policy is violated", + required = true) + private List correctiveActions; + public int getId() { return id; } @@ -103,4 +109,12 @@ public class ProfileFeature implements Serializable { public void setContent(Object content) { this.content = content; } + + public List getCorrectiveActions() { + return correctiveActions; + } + + public void setCorrectiveActions(List correctiveActions) { + this.correctiveActions = correctiveActions; + } } \ No newline at end of file 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 ec2d89cd71..d33a657282 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 @@ -77,7 +77,8 @@ public interface PolicyDAO { * @param policyId is used uniquely identify the policy to which corrective actions are to be added * @throws PolicyManagerDAOException is thrown when there is an error in adding corrective actions to database */ - void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId, + int featureId) throws PolicyManagerDAOException; /** @@ -86,7 +87,7 @@ public interface PolicyDAO { * @return list of retrieved {@link CorrectiveAction} * @throws PolicyManagerDAOException is thrown when there is an error in retrieving corrective actions to database */ - List getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException; + List getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException; /** * This method is used to update corrective actions of policy in the database based on the policy ID @@ -94,7 +95,8 @@ public interface PolicyDAO { * @param policyId is used uniquely identify the policy to which corrective actions are to be updated * @throws PolicyManagerDAOException is thrown when there is an error in updating corrective actions to database */ - void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId, + int featureId) throws PolicyManagerDAOException; /** @@ -103,7 +105,8 @@ public interface PolicyDAO { * @param policyId is used uniquely identify the policy to which corrective actions are to be deleted * @throws PolicyManagerDAOException is thrown when there is an error in deleting corrective actions to database */ - void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId, + int featureId) throws PolicyManagerDAOException; Policy updateUserOfPolicy(List usersToAdd, 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 766c512eb0..3ba4c61a52 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 @@ -263,19 +263,21 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + public void addCorrectiveActionsOfPolicy(List correctiveActions, + int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CORRECTIVE_ACTION " + "(ACTION_TYPE, " + "CORRECTIVE_POLICY_ID, " + - "POLICY_ID) VALUES (?, ?, ?)"; + "POLICY_ID, FEATURE_ID) VALUES (?, ?, ?, ?)"; try (PreparedStatement insertStmt = conn.prepareStatement(query)) { for (CorrectiveAction correctiveAction : correctiveActions) { insertStmt.setString(1, correctiveAction.getActionType()); insertStmt.setInt(2, correctiveAction.getPolicyId()); insertStmt.setInt(3, policyId); + insertStmt.setInt(4, featureId); insertStmt.addBatch(); } insertStmt.executeBatch(); @@ -288,17 +290,18 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public List getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException { + public List getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "SELECT " + "ACTION_TYPE, " + "CORRECTIVE_POLICY_ID " + "FROM DM_POLICY_CORRECTIVE_ACTION " + - "WHERE POLICY_ID = ?"; + "WHERE POLICY_ID = ? AND FEATURE_ID = ?"; try (PreparedStatement selectStmt = conn.prepareStatement(query)) { List correctiveActions = new ArrayList<>(); selectStmt.setInt(1, policyId); + selectStmt.setInt(2, featureId); try (ResultSet rs = selectStmt.executeQuery()) { CorrectiveAction correctiveAction; while (rs.next()) { @@ -318,19 +321,21 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + public void updateCorrectiveActionsOfPolicy(List correctiveActions, + int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "UPDATE DM_POLICY_CORRECTIVE_ACTION " + "SET CORRECTIVE_POLICY_ID = ? " + "WHERE ACTION_TYPE = ? " + - "AND POLICY_ID = ?"; + "AND POLICY_ID = ? AND FEATURE_ID = ?"; try (PreparedStatement updateStmt = conn.prepareStatement(query)) { for (CorrectiveAction correctiveAction : correctiveActions) { updateStmt.setInt(1, correctiveAction.getPolicyId()); updateStmt.setString(2, correctiveAction.getActionType()); updateStmt.setInt(3, policyId); + updateStmt.setInt(4, featureId); updateStmt.addBatch(); } updateStmt.executeBatch(); @@ -343,17 +348,19 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + public void deleteCorrectiveActionsOfPolicy(List correctiveActions, + int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION " + "WHERE ACTION_TYPE = ? " + - "AND POLICY_ID = ?"; + "AND POLICY_ID = ? AND FEATURE_ID = ?"; try (PreparedStatement deleteStmt = conn.prepareStatement(query)) { for (CorrectiveAction correctiveAction : correctiveActions) { deleteStmt.setString(1, correctiveAction.getActionType()); deleteStmt.setInt(2, policyId); + deleteStmt.setInt(3, featureId); deleteStmt.addBatch(); } deleteStmt.executeBatch(); 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 1ac0f519d1..b1261daa11 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 @@ -35,6 +35,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; +import org.apache.commons.collections.map.HashedMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -101,6 +102,18 @@ public class PolicyManagerImpl implements PolicyManager { profileDAO.addProfile(profile); featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); + List profileFeaturesList = profile.getProfileFeaturesList(); + for (ProfileFeature profileFeature : profileFeaturesList) { + if (profileFeature.getCorrectiveActions() != null && + !profileFeature.getCorrectiveActions().isEmpty()) { + if (log.isDebugEnabled()) { + log.debug("Adding corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); + } + policyDAO.addCorrectiveActionsOfPolicy(profileFeature.getCorrectiveActions(), + policy.getId(), profileFeature.getId()); + } + } } policy = policyDAO.addPolicy(policy); @@ -140,13 +153,13 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) { + /*if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) { if (log.isDebugEnabled()) { log.debug("Adding corrective actions for policy " + policy.getPolicyName() + " having policy id " + policy.getId()); } policyDAO.addCorrectiveActionsOfPolicy(policy.getCorrectiveActions(), policy.getId()); - } + }*/ if (policy.isActive()) { policyDAO.activatePolicy(policy.getId()); @@ -189,6 +202,8 @@ public class PolicyManagerImpl implements PolicyManager { List featuresToDelete = new ArrayList<>(); List temp = new ArrayList<>(); List updateDFes = new ArrayList<>(); + Map> updatedCorrectiveActionsMap = new HashMap<>(); + Map> existingCorrectiveActionsMap = new HashMap<>(); List updatedFeatureList = policy.getProfile().getProfileFeaturesList(); @@ -196,7 +211,9 @@ public class PolicyManagerImpl implements PolicyManager { // Checks for the existing features for (ProfileFeature feature : updatedFeatureList) { + updatedCorrectiveActionsMap.put(feature.getId(), feature.getCorrectiveActions()); for (ProfileFeature fe : existingProfileFeaturesList) { + existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions()); if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { existingFeaturesList.add(feature); temp.add(feature.getFeatureCode()); @@ -272,23 +289,39 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - List updatedCorrectiveActions = policy.getCorrectiveActions(); - List existingCorrectiveActions = previousPolicy.getCorrectiveActions(); - List correctiveActionsToUpdate = new ArrayList<>(); - List correctiveActionsToDelete = new ArrayList<>(); - List correctiveActionsToAdd = new ArrayList<>(); - List correctiveActionTypesToUpdate = new ArrayList<>(); - List existingCorrectiveActionTypes = new ArrayList<>(); - - if (updatedCorrectiveActions != null) { - for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + /*List updatedCorrectiveActions = policy.getCorrectiveActions(); + List existingCorrectiveActions = previousPolicy.getCorrectiveActions();*/ + + //Iterate all corrective actions in the new policy payload against it's features + for (Integer featureId : updatedCorrectiveActionsMap.keySet()) { + List correctiveActionsToUpdate = new ArrayList<>(); + List correctiveActionsToDelete = new ArrayList<>(); + List correctiveActionsToAdd = new ArrayList<>(); + + List correctiveActionTypesToUpdate = new ArrayList<>(); + List existingCorrectiveActionTypes = new ArrayList<>(); + List updatedCorrectiveActions = updatedCorrectiveActionsMap.get(featureId); + //Check this feature already have a corrective action + if (existingCorrectiveActionsMap.containsKey(featureId)) { + //Existing corrective actions of the selected feature + List existingCorrectiveActions = existingCorrectiveActionsMap.get(featureId); + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) { + //If old action type is same as new action type, put them into + // updating list + correctiveActionsToUpdate.add(updatedCorrectiveAction); + existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); + } + } + //Newly added action types added to this list + correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); + } for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { - if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) { - correctiveActionsToUpdate.add(updatedCorrectiveAction); - existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); + if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { + correctiveActionsToDelete.add(existingCorrectiveAction); } } - correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); } for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { @@ -296,31 +329,43 @@ public class PolicyManagerImpl implements PolicyManager { correctiveActionsToAdd.add(updatedCorrectiveAction); } } - } - for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { - if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { - correctiveActionsToDelete.add(existingCorrectiveAction); + if (log.isDebugEnabled()) { + log.debug("Updating corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); } - } - if (log.isDebugEnabled()) { - log.debug("Updating corrective actions for policy " + policy.getPolicyName() + - " having policy id " + policy.getId()); - } + if (!correctiveActionsToUpdate.isEmpty()) { + policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, + previousPolicy.getId(), featureId); + } - if (!correctiveActionsToUpdate.isEmpty()) { - policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, previousPolicy.getId()); - } + if (!correctiveActionsToAdd.isEmpty()) { + policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, + previousPolicy.getId(), featureId); + } - if (!correctiveActionsToAdd.isEmpty()) { - policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, previousPolicy.getId()); + if (!correctiveActionsToDelete.isEmpty()) { + policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, + previousPolicy.getId(), featureId); + } } - if (!correctiveActionsToDelete.isEmpty()) { - policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, previousPolicy.getId()); + + /*for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) { + correctiveActionsToAdd.add(updatedCorrectiveAction); + } } + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { + correctiveActionsToDelete.add(existingCorrectiveAction); + } + }*/ + + + PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -656,7 +701,6 @@ public class PolicyManagerImpl implements PolicyManager { log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() + " having policy id " + policy.getId()); } - policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId)); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + @@ -681,6 +725,10 @@ public class PolicyManagerImpl implements PolicyManager { // PolicyManagementDAOFactory.openConnection(); Profile profile = profileManager.getProfile(policy.getProfileId()); policy.setProfile(profile); + + for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) { + profileFeature.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId, profileFeature.getId())); + } } catch (ProfileManagementException e) { throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + policyId + ")", e); @@ -688,8 +736,12 @@ public class PolicyManagerImpl implements PolicyManager { // throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); // } finally { // PolicyManagementDAOFactory.closeConnection(); + } catch (PolicyManagerDAOException e) { + throw new PolicyManagementException("Error occurred while getting the corrective " + + "actions related to policy ID (" + policyId + ")", e); } + return policy; } @@ -1278,6 +1330,10 @@ public class PolicyManagerImpl implements PolicyManager { for (Profile profile : profileList) { if (policy.getProfileId() == profile.getProfileId()) { policy.setProfile(profile); + for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) { + profileFeature.setCorrectiveActions(policyDAO + .getCorrectiveActionsOfPolicy(policy.getId(), profileFeature.getId())); + } } } policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); @@ -1293,7 +1349,6 @@ public class PolicyManagerImpl implements PolicyManager { log.debug("Retrieving corrective actions for policy " + policy.getPolicyName() + " having policy id " + policy.getId()); } - policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policy.getId())); } Collections.sort(policyList); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 714421c431..4c089596e9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -145,8 +145,7 @@ public class PolicyManagerUtil { policyOperation.setEnabled(true); policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY); policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE); - - if (policy.getPolicyType() != null && + /*if (policy.getPolicyType() != null && PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType()) && policy.getCorrectiveActions() != null) { for (CorrectiveAction correctiveAction : policy.getCorrectiveActions()) { @@ -184,13 +183,63 @@ public class PolicyManagerUtil { break; } } - } + }*/ policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures)); + if (policy.getPolicyType() != null && + PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType())) { + setCorrectiveActions(effectiveFeatures, policyOperation, policy); + } policyOperation.setPayLoad(policyOperation.getProfileOperations()); return policyOperation; } + private static void setCorrectiveActions(List features, + PolicyOperation policyOperation, Policy policy) throws PolicyTransformException { + List effectiveFeatures = new ArrayList<>(features); + for (ProfileFeature effectiveFeature : features) { + if (effectiveFeature.getCorrectiveActions() != null) { + for (CorrectiveAction correctiveAction : effectiveFeature.getCorrectiveActions()) { + if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE + .equalsIgnoreCase(correctiveAction.getActionType())) { + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); + try { + Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId()); + if (correctivePolicy == null || !PolicyManagementConstants.CORRECTIVE_POLICY_TYPE + .equalsIgnoreCase(correctivePolicy.getPolicyType() )) { + String msg = "No corrective policy was found for the policy " + policy.getPolicyName() + + " and policy ID " + policy.getId(); + log.error(msg); + throw new PolicyTransformException(msg); + } else { + List correctiveProfileOperations = createProfileOperations( + correctivePolicy.getProfile().getProfileFeaturesList()); + ProfileFeature correctivePolicyFeature = new ProfileFeature(); + correctivePolicyFeature.setProfileId(correctivePolicy.getProfileId()); + correctivePolicyFeature.setContent(new Gson().toJson(correctiveProfileOperations)); + correctivePolicyFeature.setDeviceType(correctivePolicy.getProfile().getDeviceType()); + correctivePolicyFeature.setFeatureCode( + PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE); + correctivePolicyFeature.setId(correctivePolicy.getId()); + List profileOperations = policyOperation.getProfileOperations(); + effectiveFeatures.add(correctivePolicyFeature); + } + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving corrective policy for policy " + + policy.getPolicyName() + " and policy ID " + policy.getId(); + log.error(msg, e); + throw new PolicyTransformException(msg, e); + } + // Currently only supported POLICY corrective action type so the break is added. This should be + // removed when we start supporting other corrective action types + break; + } + } + } + } + } + + public static List createProfileOperations(List effectiveFeatures) { List profileOperations = new ArrayList<>(); for (ProfileFeature feature : effectiveFeatures) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index ccf48c1b0c..4658e2158f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -205,10 +205,16 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index b25fce19f7..51b4a428f5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -85,11 +85,17 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` ( `ACTION_TYPE` VARCHAR(45) NOT NULL, `CORRECTIVE_POLICY_ID` INT(11) DEFAULT NULL, `POLICY_ID` INT(11) NOT NULL, + `FEATURE_ID` INT(11) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION FOREIGN KEY (POLICY_ID) REFERENCES DM_POLICY (ID) ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION ON UPDATE NO ACTION )ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 623bd6b374..370200ca96 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -199,10 +199,16 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 058bceb53b..1d41602fdd 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -249,11 +249,17 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL, POLICY_ID INTEGER NOT NULL, + FEATURE_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION FOREIGN KEY (POLICY_ID) REFERENCES DM_POLICY (ID) ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION ON UPDATE NO ACTION ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index f1244dd87e..13bb74c5d8 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -233,11 +233,17 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION FOREIGN KEY (POLICY_ID) REFERENCES DM_POLICY (ID) ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION ON UPDATE NO ACTION )ENGINE = InnoDB; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 325bb770b3..ffb4892758 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -340,10 +340,14 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR2(45) NOT NULL, CORRECTIVE_POLICY_ID NUMBER(10) DEFAULT NULL, POLICY_ID NUMBER(10) NOT NULL, + FEATURE_ID NUMBER(10) NOT NULL, CONSTRAINT PK_DM_POLICY_CORRECTIVE_ACTION PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) + REFERENCES DM_POLICY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) ) / diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index d695625575..ebb7e55992 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -237,6 +237,23 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( CREATE SEQUENCE DM_DEVICE_POLICY_seq; +CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( + ID BIGSERIAL NOT NULL PRIMARY KEY, + ACTION_TYPE VARCHAR(45) NOT NULL, + CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL, + POLICY_ID INTEGER NOT NULL, + FEATURE_ID INTEGER NOT NULL, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( ID INTEGER NOT NULL DEFAULT NEXTVAL ('DM_DEVICE_POLICY_seq') , From 684ff5df7fe97b4bb81b8cac7554e52536edccc2 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Tue, 24 Mar 2020 11:42:50 +0530 Subject: [PATCH 02/73] Fix test failures --- .../core/operation/mgt/ProfileOperation.java | 10 ++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 20 +++- .../core/util/PolicyManagementConstants.java | 1 + .../mgt/core/util/PolicyManagerUtil.java | 92 ++++++++++++++++--- .../test/resources/sql/CreateMySqlTestDB.sql | 45 +++++---- .../src/main/resources/dbscripts/cdm/h2.sql | 38 ++++---- .../main/resources/dbscripts/cdm/mssql.sql | 40 ++++---- .../main/resources/dbscripts/cdm/mysql.sql | 37 ++++---- .../main/resources/dbscripts/cdm/oracle.sql | 32 +++---- 9 files changed, 200 insertions(+), 115 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java index f984fdfb0c..b89233f967 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java @@ -19,8 +19,11 @@ package org.wso2.carbon.device.mgt.core.operation.mgt; import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; public class ProfileOperation extends ConfigOperation implements Serializable { + private List correctiveActionIds = new ArrayList<>(); public Type getType() { return Type.PROFILE; @@ -30,4 +33,11 @@ public class ProfileOperation extends ConfigOperation implements Serializable { return Control.REPEAT; } + public List getCorrectiveActionIds() { + return correctiveActionIds; + } + + public void setCorrectiveActionIds(List correctiveActionIds) { + this.correctiveActionIds = correctiveActionIds; + } } 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 b1261daa11..79bf61696a 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 @@ -211,9 +211,13 @@ public class PolicyManagerImpl implements PolicyManager { // Checks for the existing features for (ProfileFeature feature : updatedFeatureList) { - updatedCorrectiveActionsMap.put(feature.getId(), feature.getCorrectiveActions()); + if (feature.getCorrectiveActions() != null) { + updatedCorrectiveActionsMap.put(feature.getId(), feature.getCorrectiveActions()); + } for (ProfileFeature fe : existingProfileFeaturesList) { - existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions()); + if (fe.getCorrectiveActions() != null) { + existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions()); + } if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { existingFeaturesList.add(feature); temp.add(feature.getFeatureCode()); @@ -722,12 +726,16 @@ public class PolicyManagerImpl implements PolicyManager { policy.setDevices(deviceList); try { - // PolicyManagementDAOFactory.openConnection(); Profile profile = profileManager.getProfile(policy.getProfileId()); policy.setProfile(profile); + PolicyManagementDAOFactory.openConnection(); for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) { - profileFeature.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId, profileFeature.getId())); + List correctiveActionsOfPolicy = policyDAO + .getCorrectiveActionsOfPolicy(policyId, profileFeature.getId()); + if (correctiveActionsOfPolicy != null) { + profileFeature.setCorrectiveActions(correctiveActionsOfPolicy); + } } } catch (ProfileManagementException e) { throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + @@ -739,6 +747,10 @@ public class PolicyManagerImpl implements PolicyManager { } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the corrective " + "actions related to policy ID (" + policyId + ")", e); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + PolicyManagementDAOFactory.closeConnection(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index e945f98127..3ea0efa51d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -66,6 +66,7 @@ public final class PolicyManagementConstants { public static final String CORRECTIVE_POLICY_TYPE = "CORRECTIVE"; public static final String POLICY_CORRECTIVE_ACTION_TYPE = "POLICY"; public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY"; + public static final String CORRECTIVE_POLICY_LIST = "CORRECTIVE_ACTIONS"; /** Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 4c089596e9..55e39354d5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -195,33 +195,70 @@ public class PolicyManagerUtil { } private static void setCorrectiveActions(List features, - PolicyOperation policyOperation, Policy policy) throws PolicyTransformException { - List effectiveFeatures = new ArrayList<>(features); + PolicyOperation policyOperation, Policy policy) + throws PolicyTransformException { + ProfileOperation correctiveOperations = new ProfileOperation(); + correctiveOperations.setCode(PolicyManagementConstants.CORRECTIVE_POLICY_LIST); for (ProfileFeature effectiveFeature : features) { if (effectiveFeature.getCorrectiveActions() != null) { for (CorrectiveAction correctiveAction : effectiveFeature.getCorrectiveActions()) { if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE .equalsIgnoreCase(correctiveAction.getActionType())) { + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); try { Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId()); - if (correctivePolicy == null || !PolicyManagementConstants.CORRECTIVE_POLICY_TYPE - .equalsIgnoreCase(correctivePolicy.getPolicyType() )) { - String msg = "No corrective policy was found for the policy " + policy.getPolicyName() + - " and policy ID " + policy.getId(); + if (correctivePolicy == null || !PolicyManagementConstants + .CORRECTIVE_POLICY_TYPE.equalsIgnoreCase(correctivePolicy + .getPolicyType())) { + String msg = "No corrective policy was found for the policy " + + policy.getPolicyName() + " and policy ID " + policy.getId(); log.error(msg); throw new PolicyTransformException(msg); } else { - List correctiveProfileOperations = createProfileOperations( - correctivePolicy.getProfile().getProfileFeaturesList()); + createCorrectiveProfileOperations(correctivePolicy, correctiveOperations); + policyOperation.getProfileOperations().add(correctiveOperations); + } + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving corrective policy for policy " + + policy.getPolicyName() + " and policy ID " + policy.getId(); + log.error(msg, e); + throw new PolicyTransformException(msg, e); + } + } + } + + + + /*for (CorrectiveAction correctiveAction : effectiveFeature.getCorrectiveActions()) { + if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE + .equalsIgnoreCase(correctiveAction.getActionType())) { + PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl(); + try { + Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId()); + if (correctivePolicy == null || !PolicyManagementConstants + .CORRECTIVE_POLICY_TYPE.equalsIgnoreCase(correctivePolicy + .getPolicyType() )) { + String msg = "No corrective policy was found for the policy " + + policy.getPolicyName() + " and policy ID " + policy.getId(); + log.error(msg); + throw new PolicyTransformException(msg); + } else { + *//*List correctiveProfileOperations = + createProfileOperations();*//* + createCorrectiveProfileOperations(correctivePolicy.getProfile() + .getProfileFeaturesList()); + + ProfileFeature correctivePolicyFeature = new ProfileFeature(); correctivePolicyFeature.setProfileId(correctivePolicy.getProfileId()); - correctivePolicyFeature.setContent(new Gson().toJson(correctiveProfileOperations)); - correctivePolicyFeature.setDeviceType(correctivePolicy.getProfile().getDeviceType()); + correctivePolicyFeature.setContent(new Gson() + .toJson(correctiveProfileOperations)); + correctivePolicyFeature.setDeviceType(correctivePolicy + .getProfile().getDeviceType()); correctivePolicyFeature.setFeatureCode( PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE); correctivePolicyFeature.setId(correctivePolicy.getId()); - List profileOperations = policyOperation.getProfileOperations(); effectiveFeatures.add(correctivePolicyFeature); } } catch (PolicyManagementException e) { @@ -230,15 +267,39 @@ public class PolicyManagerUtil { log.error(msg, e); throw new PolicyTransformException(msg, e); } - // Currently only supported POLICY corrective action type so the break is added. This should be - // removed when we start supporting other corrective action types + // Currently only supported POLICY corrective action type so the break + // is added. This should be removed when we start supporting other + // corrective action types break; } - } + }*/ + + + + + } } } + private static void createCorrectiveProfileOperations(Policy correctivePolicy, + ProfileOperation correctiveOperationList) { + ProfileOperation profileOperation = new ProfileOperation(); + profileOperation.setId(correctivePolicy.getId()); + profileOperation.setCode(PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE); + profileOperation.setEnabled(true); + profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); + profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); + String correctivePayload =(String) correctiveOperationList.getPayLoad(); + if (correctivePayload == null) { + correctivePayload = ""; + } + List profileOperations = createProfileOperations(correctivePolicy + .getProfile().getProfileFeaturesList()); + String payload = new Gson().toJson(profileOperations); + correctiveOperationList.setPayLoad(correctivePayload.concat(payload)); + } + public static List createProfileOperations(List effectiveFeatures) { List profileOperations = new ArrayList<>(); @@ -249,6 +310,9 @@ public class PolicyManagerUtil { profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); profileOperation.setPayLoad(feature.getContent()); + for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) { + profileOperation.getCorrectiveActionIds().add(correctiveAction.getPolicyId()); + } profileOperations.add(profileOperation); } return profileOperations; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index 51b4a428f5..3d659d099f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -77,29 +77,6 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; --- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT, - `ACTION_TYPE` VARCHAR(45) NOT NULL, - `CORRECTIVE_POLICY_ID` INT(11) DEFAULT NULL, - `POLICY_ID` INT(11) NOT NULL, - `FEATURE_ID` INT(11) NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (FEATURE_ID) - REFERENCES DM_PROFILE_FEATURES (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -)ENGINE = InnoDB -DEFAULT CHARACTER SET = latin1; - -- ----------------------------------------------------- -- Table `WSO2CDM`.`DM_DATE` @@ -227,6 +204,28 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE_FEATURES` ( ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; +-- ----------------------------------------------------- +-- Table `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `ACTION_TYPE` VARCHAR(45) NOT NULL, + `CORRECTIVE_POLICY_ID` INT(11) DEFAULT NULL, + `POLICY_ID` INT(11) NOT NULL, + `FEATURE_ID` INT(11) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION_FEATURE + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +)ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- -- Table `WSO2CDM`.`DM_ROLE_POLICY` diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 370200ca96..0a8f646fdf 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -194,25 +194,6 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( - ID INT(11) NOT NULL AUTO_INCREMENT, - ACTION_TYPE VARCHAR(45) NOT NULL, - CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, - POLICY_ID INT(11) NOT NULL, - FEATURE_ID INT(11) NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (FEATURE_ID) - REFERENCES DM_PROFILE_FEATURES (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , DEVICE_ID INT(11) NOT NULL , @@ -259,6 +240,25 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( + ID INT(11) NOT NULL AUTO_INCREMENT, + ACTION_TYPE VARCHAR(45) NOT NULL, + CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, + POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION_FEATURE + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , ROLE_NAME VARCHAR(45) NOT NULL , diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 1d41602fdd..3b4f65e50e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -243,26 +243,6 @@ CREATE TABLE DM_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); -IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_CORRECTIVE_ACTION]') AND TYPE IN (N'U')) -CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( - ID INTEGER IDENTITY(1,1) NOT NULL, - ACTION_TYPE VARCHAR(45) NOT NULL, - CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL, - POLICY_ID INTEGER NOT NULL, - FEATURE_ID INTEGER NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (FEATURE_ID) - REFERENCES DM_PROFILE_FEATURES (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, @@ -300,6 +280,26 @@ CREATE TABLE DM_PROFILE_FEATURES ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_CORRECTIVE_ACTION]') AND TYPE IN (N'U')) +CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( + ID INTEGER IDENTITY(1,1) NOT NULL, + ACTION_TYPE VARCHAR(45) NOT NULL, + CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL, + POLICY_ID INTEGER NOT NULL, + FEATURE_ID INTEGER NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION_FEATURE + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_ROLE_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_ROLE_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 13bb74c5d8..0ce1b6353b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -228,25 +228,6 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( ON UPDATE NO ACTION )ENGINE = InnoDB; -CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( - ID INT(11) NOT NULL AUTO_INCREMENT, - ACTION_TYPE VARCHAR(45) NOT NULL, - CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, - POLICY_ID INT(11) NOT NULL, - FEATURE_ID INT(11) NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (FEATURE_ID) - REFERENCES DM_PROFILE_FEATURES (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -)ENGINE = InnoDB; - CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , @@ -296,6 +277,24 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ON UPDATE NO ACTION )ENGINE = InnoDB; +CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( + ID INT(11) NOT NULL AUTO_INCREMENT, + ACTION_TYPE VARCHAR(45) NOT NULL, + CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, + POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION_FEATURE + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +)ENGINE = InnoDB; CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index ffb4892758..04fd233713 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -335,22 +335,6 @@ CREATE TABLE DM_POLICY ( ) / -CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( - ID NUMBER(10) NOT NULL, - ACTION_TYPE VARCHAR2(45) NOT NULL, - CORRECTIVE_POLICY_ID NUMBER(10) DEFAULT NULL, - POLICY_ID NUMBER(10) NOT NULL, - FEATURE_ID NUMBER(10) NOT NULL, - CONSTRAINT PK_DM_POLICY_CORRECTIVE_ACTION PRIMARY KEY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (FEATURE_ID) - REFERENCES DM_PROFILE_FEATURES (ID) -) -/ - -- Generate ID using sequence and trigger CREATE SEQUENCE DM_POLICY_seq START WITH 1 INCREMENT BY 1 NOCACHE / @@ -422,6 +406,22 @@ CREATE TABLE DM_PROFILE_FEATURES ( ) / +CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( + ID NUMBER(10) NOT NULL, + ACTION_TYPE VARCHAR2(45) NOT NULL, + CORRECTIVE_POLICY_ID NUMBER(10) DEFAULT NULL, + POLICY_ID NUMBER(10) NOT NULL, + FEATURE_ID NUMBER(10) NOT NULL, + CONSTRAINT PK_DM_POLICY_CORRECTIVE_ACTION PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION_FEATURE + FOREIGN KEY (FEATURE_ID) + REFERENCES DM_PROFILE_FEATURES (ID) +) +/ + -- Generate ID using sequence and trigger CREATE SEQUENCE DM_PROFILE_FEATURES_seq START WITH 1 INCREMENT BY 1 NOCACHE / From 2724d8ac0353d5af52e7e4fe2fa21b63e7961466 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Thu, 26 Mar 2020 18:20:24 +0530 Subject: [PATCH 03/73] Optimize policy pick strategy for corrective action implementation --- .../common/policy/mgt/CorrectiveAction.java | 10 ++ .../core/operation/mgt/ProfileOperation.java | 2 +- .../units/cdmf.unit.policy.create/create.hbs | 54 +----- .../public/js/policy-create.js | 48 +---- .../app/units/cdmf.unit.policy.edit/edit.hbs | 69 +------- .../app/units/cdmf.unit.policy.edit/edit.js | 13 -- .../public/js/policy-edit.js | 65 +------ .../cdmf.unit.policy.view/public/js/view.js | 24 --- .../app/units/cdmf.unit.policy.view/view.hbs | 36 +--- .../app/units/cdmf.unit.policy.view/view.js | 2 +- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 9 +- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 30 +++- .../core/util/PolicyManagementConstants.java | 4 +- .../mgt/core/util/PolicyManagerUtil.java | 167 +++++------------- 15 files changed, 97 insertions(+), 438 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java index dece7db618..6ebe74e17c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java @@ -55,6 +55,8 @@ public class CorrectiveAction implements Serializable { ) private List operations; + private int featureId; + public String getActionType() { return actionType; } @@ -78,4 +80,12 @@ public class CorrectiveAction implements Serializable { public void setOperations(List operations) { this.operations = operations; } + + public int getFeatureId() { + return featureId; + } + + public void setFeatureId(int featureId) { + this.featureId = featureId; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java index b89233f967..21d41f3aed 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ProfileOperation.java @@ -23,7 +23,7 @@ import java.util.ArrayList; import java.util.List; public class ProfileOperation extends ConfigOperation implements Serializable { - private List correctiveActionIds = new ArrayList<>(); + private List correctiveActionIds; public Type getType() { return Type.PROFILE; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs index c67f099c5b..54e89548f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs @@ -9,7 +9,7 @@
- diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js index e4adef0e38..f3cc76c3c7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/public/js/policy-create.js @@ -222,28 +222,6 @@ stepForwardFrom["policy-profile"] = function () { policy["profile"] = generatePolicyProfile(); } - // add policy correction action page - var policyCorrectiveActionTemplateSrc = - "/public/cdmf.unit.policy.corrective-action/templates/policy-corrective-action.hbs"; - var policyCorrectiveActionScriptSrc = - "/public/cdmf.unit.policy.corrective-action/js/policy-corrective-action.js"; - var policyCorrectiveActionTemplateCacheKey = "policy-corrective-action"; - - $.template(policyCorrectiveActionTemplateCacheKey, context + policyCorrectiveActionTemplateSrc, - function (template) { - var content = template( - { - "deviceType": policy["platform"], - "correctivePolicies": $("#logged-in-user").data("corrective-policies") - } - ); - $("#select-general-policy-type").html(content); - var script = document.createElement('script'); - script.type = 'text/javascript'; - script.src = context + policyCorrectiveActionScriptSrc; - document.head.append(script); - }); - // updating next-page wizard title with selected platform $("#policy-type-page-wizard-title").text("ADD " + policy["platform"] + " POLICY"); }; @@ -261,28 +239,6 @@ stepBackFrom["policy-profile"] = function () { } }; - -/** - * Forward action of policy type page. - */ -stepForwardFrom["policy-type"] = function () { - policy["type"] = $("input[name=policy-type-radio-btn]:checked").val(); - var correctiveActionList = []; - if (policy.type === "GENERAL") { - var selectedCorrectivePolicyId = $("#corrective-policy-input").val(); - if (selectedCorrectivePolicyId && selectedCorrectivePolicyId !== "none") { - var correctiveAction = { - "actionType": "POLICY", - "policyId": selectedCorrectivePolicyId - }; - correctiveActionList.push(correctiveAction); - } - } - policy["correctiveActionList"] = correctiveActionList; - //updating next-page wizard title with selected platform - $("#policy-criteria-page-wizard-title").text("ADD " + policy["platform"] + " POLICY"); -}; - /** * Forward action of policy criteria page. */ @@ -483,8 +439,7 @@ var savePolicy = function (policy, isActive, serviceURL) { "compliance": policy["selectedNonCompliantAction"], "ownershipType": null, "active": isActive, - "policyType": policy["type"], - "correctiveActions": policy["correctiveActionList"], + "policyType": "GENERAL", "profile": { "profileName": policy["policyName"], "deviceType": policy["platform"], @@ -639,6 +594,7 @@ $(document).ready(function () { } }); + //todo $('input[type=radio][name=policy-type-radio-btn]').change(function() { if ($(this).val() === "CORRECTIVE") { $("#select-general-policy-type").addClass("hidden"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/edit.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/edit.hbs index f471497415..672fc524f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/edit.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/edit.hbs @@ -1,38 +1,30 @@ {{#zone "content"}} {{#if isAuthorized }} + data-tenant-id="{{@user.tenantId}}" data-iscloud="{{isCloud}}" data-storeapps="{{storeApps}}">