From 6e18b7c11acf97e0e282c8fd5e2d13d10860dbb8 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 22 May 2015 02:05:16 +0530 Subject: [PATCH 1/2] Adding the policy priority changing dao methods --- .../mgt/common/PolicyAdministratorPoint.java | 2 ++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 27 +++++++++---------- .../impl/PolicyAdministratorPointImpl.java | 7 +++++ .../policy/mgt/core/mgt/PolicyManager.java | 2 ++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 24 +++++++++++++++-- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 07c4e66f1d..c207516a4d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -38,6 +38,8 @@ public interface PolicyAdministratorPoint { Policy updatePolicy(Policy policy) throws PolicyManagementException; + boolean updatePolicyPriorities(List policies) throws PolicyManagementException; + boolean deletePolicy(Policy policy) throws PolicyManagementException; boolean deletePolicy(int policyId) throws PolicyManagementException; 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 a01fada772..3fd6cca1f7 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 @@ -145,31 +145,28 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean updatePolicyPriorities(List policies) throws PolicyManagerDAOException { - /* Connection conn; + Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); String query = "UPDATE DM_POLICY SET PRIORITY = ? WHERE ID = ?"; stmt = conn.prepareStatement(query); - stmt.setString(1, policy.getPolicyName()); - stmt.setInt(2, policy.getTenantId()); - stmt.setInt(3, policy.getProfile().getProfileId()); - stmt.setInt(4, policy.getPriorityId()); - stmt.setString(5, policy.getCompliance()); - stmt.setInt(6, policy.getId()); - stmt.executeUpdate(); + + for (Policy policy : policies) { + stmt.setInt(1, policy.getPriorityId()); + stmt.setInt(2, policy.getId()); + stmt.addBatch(); + } + stmt.executeBatch(); } catch (SQLException e) { - String msg = "Error occurred while updating policy (" + policy.getPolicyName() + ") in database."; + String msg = "Error occurred while updating policy priorities in database."; log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } - return policy;*/ - - - return false; + return true; } // @Override @@ -518,7 +515,7 @@ public class PolicyDAOImpl implements PolicyDAO { } stmt.executeBatch(); } - // stmt.executeUpdate(); + // stmt.executeUpdate(); } catch (SQLException e) { String msg = "Error occurred while inserting the criterion properties to database."; @@ -1146,7 +1143,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.executeUpdate(); if (log.isDebugEnabled()) { - log.debug("Policy (" + policyId+ ") delete from database."); + log.debug("Policy (" + policyId + ") delete from database."); } return true; } catch (SQLException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 859f29bd1c..2dad309545 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -63,6 +63,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { return policyManager.updatePolicy(policy); } + @Override + public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { + return policyManager.updatePolicyPriorities(policies); + } + @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { return policyManager.deletePolicy(policy); @@ -198,4 +203,6 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { public boolean deleteFeature(int featureId) throws FeatureManagementException { return featureManager.deleteFeature(featureId); } + + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index dfe9ff7e9d..f1d78cb1d3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -32,6 +32,8 @@ public interface PolicyManager { Policy updatePolicy(Policy policy) throws PolicyManagementException; + boolean updatePolicyPriorities(List policies) throws PolicyManagementException; + boolean deletePolicy(Policy policy) throws PolicyManagementException; boolean deletePolicy(int policyId) throws PolicyManagementException; 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 18e832f8be..bac0a22c7e 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 @@ -60,7 +60,7 @@ public class PolicyManagerImpl implements PolicyManager { try { PolicyManagementDAOFactory.beginTransaction(); if (policy.getProfile() != null && policy.getProfile().getProfileId() == 0) { - Profile profile = policy.getProfile(); + Profile profile = policy.getProfile(); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); profile.setCreatedDate(currentTimestamp); @@ -210,6 +210,26 @@ public class PolicyManagerImpl implements PolicyManager { return policy; } + @Override + public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { + boolean bool; + try { + PolicyManagementDAOFactory.beginTransaction(); + bool = policyDAO.updatePolicyPriorities(policies); + PolicyManagementDAOFactory.commitTransaction(); + } catch (PolicyManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Error occurred while updating the policy priorities"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return bool; + } + @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { @@ -249,7 +269,7 @@ 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); } From 48a2062f64be0feb002b3d28b32ce6a97dd445d9 Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 22 May 2015 02:32:04 +0530 Subject: [PATCH 2/2] Add operations for effective policy --- .../operation/mgt/OperationManagerImpl.java | 13 +------ .../mgt/dao/impl/OperationDAOImpl.java | 6 +-- .../mgt/core/PolicyManagerServiceImpl.java | 39 ++++++++++--------- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 1458999505..530ca559d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -349,18 +349,7 @@ public class OperationManagerImpl implements OperationManager { PolicyOperation policyOperation; for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { - - if (dtoOperation instanceof org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation){ - policyOperation = (PolicyOperation)dtoOperation; - for(org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation - profileOperation:policyOperation.getProfileOperations()){ - operation = OperationDAOUtil.convertOperation(profileOperation); - } - }else{ - operation = OperationDAOUtil.convertOperation(dtoOperation); - } - - + operation = OperationDAOUtil.convertOperation(dtoOperation); operations.add(operation); } return operations; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java index 8e5efe3c5f..bdb380bae0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java @@ -277,11 +277,7 @@ public class OperationDAOImpl implements OperationDAO { operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); - if (ois.readObject() instanceof PolicyOperation){ - operation = (PolicyOperation) ois.readObject(); - }else{ - operation = (PolicyOperation) ois.readObject(); - } + operation = (ProfileOperation) ois.readObject(); } else { operation = new Operation(); operation.setId(rs.getInt("ID")); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 77b61e75ea..fdbdeff5fa 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; +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.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; @@ -93,27 +94,26 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { getEffectivePolicy(deviceIdentifier); if (policy != null) { + List deviceIdentifiers = new ArrayList(); + deviceIdentifiers.add(deviceIdentifier); List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); - PolicyOperation policyOperation = new PolicyOperation(); - - List profileOperationList = new ArrayList(); for (ProfileFeature feature : effectiveFeatures) { ProfileOperation operation = new ProfileOperation(); operation.setCode(feature.getFeatureCode()); + operation.setEnabled(true); + operation.setStatus(Operation.Status.PENDING); + operation.setType(Operation.Type.PROFILE); operation.setPayLoad(feature.getContent()); - profileOperationList.add(operation); + PolicyManagementDataHolder.getInstance().getDeviceManagementService(). + addOperation(operation, deviceIdentifiers); } - policyOperation.setProfileOperations(profileOperationList); - policyOperation.setCode(PolicyManagementConstants.POLICY_BUNDLE); - List deviceIdentifiers = new ArrayList(); - deviceIdentifiers.add(deviceIdentifier); - PolicyManagementDataHolder.getInstance().getDeviceManagementService(). - addOperation(policyOperation, deviceIdentifiers); + + } else { return null; } @@ -140,25 +140,26 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { List effectiveFeatures = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). getEffectiveFeatures(deviceIdentifier); + List deviceIdentifiers = new ArrayList(); + deviceIdentifiers.add(deviceIdentifier); + if (!effectiveFeatures.isEmpty()) { PolicyOperation policyOperation = new PolicyOperation(); - List profileOperationList = new ArrayList(); for (ProfileFeature feature : effectiveFeatures) { ProfileOperation operation = new ProfileOperation(); operation.setCode(feature.getFeatureCode()); operation.setPayLoad(feature.getContent()); - profileOperationList.add(operation); - } - policyOperation.setProfileOperations(profileOperationList); - policyOperation.setCode(PolicyManagementConstants.POLICY_BUNDLE); + operation.setStatus(Operation.Status.PENDING); + operation.setType(Operation.Type.PROFILE); + operation.setEnabled(true); - List deviceIdentifiers = new ArrayList(); - deviceIdentifiers.add(deviceIdentifier); - PolicyManagementDataHolder.getInstance().getDeviceManagementService(). - addOperation(policyOperation, deviceIdentifiers); + PolicyManagementDataHolder.getInstance().getDeviceManagementService(). + addOperation(policyOperation, deviceIdentifiers); + } + } else { return null; }