From fe67d251d5642f2e5b6f0648d65c4c20d68eb6ec Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 6 Jul 2017 11:57:04 +0530 Subject: [PATCH] Fixing the #460 issue --- .../device/mgt/core/dto/DeviceType.java | 4 -- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 71 ++++++++++++++++++- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java index c56257a363..5f04d3d6d1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java @@ -18,8 +18,6 @@ package org.wso2.carbon.device.mgt.core.dto; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition; @@ -36,7 +34,6 @@ public class DeviceType implements Serializable { @ApiModelProperty(name = "name", value = "Device type name", required = true) private String name; - @JsonProperty("metaDefinition") @ApiModelProperty(name = "metaDefinition", value = "Device type definition", required = true) private DeviceTypeMetaDefinition deviceTypeMetaDefinition; @@ -47,7 +44,6 @@ public class DeviceType implements Serializable { this.name = name; } - @JsonIgnore public int getId() { return id; } 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 37a3e148fc..88af6eb50a 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 @@ -23,13 +23,18 @@ import org.apache.commons.logging.LogFactory; 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.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyCriterion; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; +import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; @@ -37,6 +42,10 @@ import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImp import org.wso2.carbon.policy.mgt.common.*; 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.enforcement.PolicyDelegationException; +import org.wso2.carbon.policy.mgt.core.enforcement.PolicyEnforcementDelegator; +import org.wso2.carbon.policy.mgt.core.enforcement.PolicyEnforcementDelegatorImpl; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import org.wso2.carbon.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean; @@ -317,6 +326,22 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { boolean bool; + + List policies = this.getPolicies(); + Policy pol = null; + for (Policy p : policies) { + if (policyId == p.getId()) { + pol = p; + } + } + String deviceType = pol.getProfile().getDeviceType(); + List deviceTypePolicyList = this.getPoliciesOfDeviceType(deviceType); + if (deviceTypePolicyList.size() == 1) { + List devices = this.getPolicyAppliedDevicesIds(policyId); + List deviceIdentifiers = this.convertDevices(devices); + this.addPolicyRevokeOperation(deviceIdentifiers); + } + try { PolicyManagementDAOFactory.beginTransaction(); @@ -836,6 +861,7 @@ public class PolicyManagerImpl implements PolicyManager { List changedDeviceTypes = new ArrayList<>(); List updatedPolicies = new ArrayList<>(); List updatedPolicyIds = new ArrayList<>(); + boolean transactionDone = false; try { //HashMap map = policyDAO.getUpdatedPolicyIdandDeviceTypeId(); // List activePolicies = new ArrayList<>(); @@ -859,6 +885,7 @@ public class PolicyManagerImpl implements PolicyManager { // } } PolicyManagementDAOFactory.beginTransaction(); + transactionDone = true; policyDAO.markPoliciesAsUpdated(updatedPolicyIds); policyDAO.removeRecordsAboutUpdatedPolicies(); PolicyManagementDAOFactory.commitTransaction(); @@ -866,7 +893,9 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while applying the changes to policy operations.", e); } finally { - PolicyManagementDAOFactory.closeConnection(); + if(transactionDone) { + PolicyManagementDAOFactory.closeConnection(); + } } return new UpdatedPolicyDeviceListBean(updatedPolicies, updatedPolicyIds, changedDeviceTypes); } @@ -1045,4 +1074,44 @@ public class PolicyManagerImpl implements PolicyManager { return groupWrappers; } + + private List convertDevices(List devices) { + List deviceIdentifiers = new ArrayList<>(); + for (Device device : devices) { + DeviceIdentifier identifier = new DeviceIdentifier(); + identifier.setId(device.getDeviceIdentifier()); + identifier.setType(device.getType()); + deviceIdentifiers.add(identifier); + } + return deviceIdentifiers; + } + + + private void addPolicyRevokeOperation(List deviceIdentifiers) throws PolicyManagementException { + try { + String type = null; + if (deviceIdentifiers.size() > 0) { + type = deviceIdentifiers.get(0).getType(); + } + PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type, + this.getPolicyRevokeOperation(), deviceIdentifiers); + } catch (InvalidDeviceException e) { + String msg = "Invalid DeviceIdentifiers found."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (OperationManagementException e) { + String msg = "Error occurred while adding the operation to device."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + } + + private Operation getPolicyRevokeOperation() { + CommandOperation policyRevokeOperation = new CommandOperation(); + policyRevokeOperation.setEnabled(true); + policyRevokeOperation.setCode(OperationMgtConstants.OperationCodes.POLICY_REVOKE); + policyRevokeOperation.setType(Operation.Type.COMMAND); + return policyRevokeOperation; + } + }