diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/PolicyOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/PolicyOperation.java new file mode 100644 index 00000000000..3c39af660fc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/operation/mgt/PolicyOperation.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.mgt.core.dto.operation.mgt; + +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; + +import java.util.List; + +public class PolicyOperation extends Operation{ + + public List getProfileOperations() { + return profileOperations; + } + + public void setProfileOperations(List profileOperations) { + this.profileOperations = profileOperations; + } + + private List profileOperations; + +} 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 4e123090d8f..1458999505a 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 @@ -26,6 +26,8 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.*; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -343,9 +345,22 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status .valueOf(status.toString())); - Operation operation; + Operation operation = null; + PolicyOperation policyOperation; + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { - operation = OperationDAOUtil.convertOperation(dtoOperation); + + 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); + } + + operations.add(operation); } return operations; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java similarity index 81% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyOperation.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java index c0d37ef1903..d34905559df 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/PolicyOperation.java @@ -4,7 +4,7 @@ * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. - * You may obtain a copy of the License at + * you may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * @@ -15,17 +15,15 @@ * specific language governing permissions and limitations * under the License. */ +package org.wso2.carbon.device.mgt.core.operation.mgt; -package org.wso2.carbon.policy.mgt.common; -import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import java.util.List; -public class PolicyOperation extends ProfileOperation { - - private List profileOperations; +public class PolicyOperation extends Operation { public List getProfileOperations() { return profileOperations; @@ -34,4 +32,7 @@ public class PolicyOperation extends ProfileOperation { public void setProfileOperations(List profileOperations) { this.profileOperations = profileOperations; } + + private List profileOperations; + } 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 35f0f0cd10f..8e5efe3c5f7 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 @@ -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.core.dto.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -276,7 +277,11 @@ public class OperationDAOImpl implements OperationDAO { operationDetails = rs.getBytes("OPERATION_DETAILS"); bais = new ByteArrayInputStream(operationDetails); ois = new ObjectInputStream(bais); - operation = (ProfileOperation) ois.readObject(); + if (ois.readObject() instanceof PolicyOperation){ + operation = (PolicyOperation) ois.readObject(); + }else{ + operation = (PolicyOperation) 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 6f9cac8e9e6..efab0be51fb 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 @@ -23,6 +23,7 @@ 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.OperationManagementException; +import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; 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 53329412d4c..a5af43affc3 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 @@ -36,6 +36,8 @@ public interface PolicyDAO { Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException; + boolean updatePolicyPriorities(List policies) throws PolicyManagerDAOException; + // Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException; // // Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException; @@ -88,6 +90,8 @@ public interface PolicyDAO { List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException; + List getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; + // PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException; // // PolicyDates getDatesOfPolicy(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 c488bf515d8..14eece60189 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 @@ -142,6 +142,36 @@ public class PolicyDAOImpl implements PolicyDAO { return policy; } + @Override + public boolean updatePolicyPriorities(List policies) throws PolicyManagerDAOException { + + /* 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(); + + } catch (SQLException e) { + String msg = "Error occurred while updating policy (" + policy.getPolicyName() + ") in database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + return policy;*/ + + + return false; + } + // @Override // public Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException { // @@ -752,6 +782,36 @@ public class PolicyDAOImpl implements PolicyDAO { } } + @Override + public List getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + List users = new ArrayList(); + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_USER_POLICY WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, policyId); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + + users.add(resultSet.getString("USERNAME")); + } + return users; + + } catch (SQLException e) { + String msg = "Error occurred while getting the roles related to policies."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + } + // public PolicyTimes getTimesOfPolicy(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/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 112b3cc4e56..975e5728673 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 @@ -88,7 +88,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { return policyManager.getPolicies(); } - @Override public Policy getPolicy(int policyId) throws PolicyManagementException { + @Override + public Policy getPolicy(int policyId) throws PolicyManagementException { return policyManager.getPolicy(policyId); } @@ -184,35 +185,18 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Feature addFeature(Feature feature) throws FeatureManagementException { - try { - return featureManager.addFeature(feature); - } catch (FeatureManagementException e) { - String msg = "Error occurred while persisting the feature."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } + return featureManager.addFeature(feature); } @Override public Feature updateFeature(Feature feature) throws FeatureManagementException { - try { - return featureManager.updateFeature(feature); - } catch (FeatureManagementException e) { - String msg = "Error occurred while persisting the feature."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } + return featureManager.updateFeature(feature); + } @Override public boolean deleteFeature(int featureId) throws FeatureManagementException { - try { - return featureManager.deleteFeature(featureId); - } catch (FeatureManagementException e) { - String msg = "Error occurred while deleting the feature."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } + return featureManager.deleteFeature(featureId); } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index 2addda1a812..ef537bbc1c1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -88,47 +88,19 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { @Override public List getRelatedPolicies(PIPDevice pipDevice) throws PolicyManagementException { -// List> policies = new ArrayList>(); - List policies = new ArrayList(); - try { - // Get the device type related policies -// policies.add(policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName())); - - - // Commented out because these are already taken when device type based policies retrieved - -// // Get the roles related policies -// for (String role : pipDevice.getRoles()) { -// policies.add(policyManager.getPoliciesOfRole(role)); -// } -// // Get policy related to the device -// policies.add(policyManager.getPoliciesOfDevice(pipDevice.getDeviceIdentifier())); + List policies = policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName()); + PolicyFilter policyFilter = new PolicyFilterImpl(); + policyFilter.filterDeviceTypeBasedPolicies(pipDevice.getDeviceType().getName(), policies); + policyFilter.filterOwnershipTypeBasedPolicies(pipDevice.getOwnershipType(), policies); + policyFilter.filterRolesBasedPolicies(pipDevice.getRoles(), policies); - policies = policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName()); - - PolicyFilter policyFilter = new PolicyFilterImpl(); - policyFilter.filterDeviceTypeBasedPolicies(pipDevice.getDeviceType().getName(), policies); - policyFilter.filterOwnershipTypeBasedPolicies(pipDevice.getOwnershipType(), policies); - policyFilter.filterRolesBasedPolicies(pipDevice.getRoles(), policies); - - return policies; - } catch (PolicyManagementException e) { - String msg = "Error occurred when retrieving related to given device " + - pipDevice.getDeviceIdentifier().getId() + " " + pipDevice.getDeviceIdentifier().getType() + "."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); - } + return policies; } @Override public List getRelatedFeatures(String deviceType) throws FeatureManagementException { - try { - return featureManager.getAllFeatures(deviceType); - } catch (FeatureManagementException e) { - String msg = "Error occurred when retrieving features related to device type."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } + return featureManager.getAllFeatures(deviceType); + } private String[] getRoleOfDevice(Device device) 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 3afdfc25b63..bf75f47200f 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 @@ -468,6 +468,7 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setDevices(getPolicyAppliedDevicesIds(policy.getId())); policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); + policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policy.getId())); // policyDAO.getDatesOfPolicy(policy); // policyDAO.getTimesOfPolicy(policy); 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 b6a44bc277c..720e24a9503 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 @@ -263,6 +263,17 @@ public class PolicyDAOTestCase { for (Policy policy : policyList) { log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + log.debug("Policy Ownership type :" + policy.getOwnershipType()); + + List users = policy.getUsers(); + for (String user : users) { + log.debug("User of the policy : " + user); + } + + List roles = policy.getRoles(); + for (String role : roles) { + log.debug("User of the policy : " + role); + } } } @@ -276,6 +287,17 @@ public class PolicyDAOTestCase { for (Policy policy : policyList) { log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + log.debug("Policy Ownership type :" + policy.getOwnershipType()); + + List users = policy.getUsers(); + for (String user : users) { + log.debug("User of the policy : " + user); + } + + List roles = policy.getRoles(); + for (String role : roles) { + log.debug("User of the policy : " + role); + } } } @@ -290,6 +312,17 @@ public class PolicyDAOTestCase { for (Policy policy : policyList) { log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + log.debug("Policy Ownership type :" + policy.getOwnershipType()); + + List users = policy.getUsers(); + for (String user : users) { + log.debug("User of the policy : " + user); + } + + List roles = policy.getRoles(); + for (String role : roles) { + log.debug("User of the policy : " + role); + } } } @@ -303,6 +336,8 @@ public class PolicyDAOTestCase { for (Policy policy : policyList) { log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + + log.debug("Policy Ownership type :" + policy.getOwnershipType()); } } @@ -324,6 +359,8 @@ public class PolicyDAOTestCase { for (Policy policy : policyList) { log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + log.debug("Policy Ownership type :" + policy.getOwnershipType()); + List profileFeatures = policy.getProfile().getProfileFeaturesList(); for (ProfileFeature profileFeature : profileFeatures) {