From 53c22fe9fe3ea825c70f038ed2979d0a1abe888d Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 15 May 2015 20:25:04 +0530 Subject: [PATCH 1/7] Adding criteria based policy --- .../wso2/carbon/policy/mgt/common/Policy.java | 8 +- ...licyCriteria.java => PolicyCriterion.java} | 11 +- .../policy/mgt/common/ProfileFeature.java | 9 - .../carbon/policy/mgt/core/dao/PolicyDAO.java | 4 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 168 +++++++++++++++++- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 33 +++- 6 files changed, 209 insertions(+), 24 deletions(-) rename components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/{PolicyCriteria.java => PolicyCriterion.java} (87%) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index 4d034a32ea..a39f03935a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -34,6 +34,8 @@ import java.util.Map; @XmlRootElement public class Policy implements Comparable, Serializable { + private static final long serialVersionUID = 19981017L; + private int id; // Identifier of the policy. private int priorityId; // Priority of the policies. This will be used only for simple evaluation. private Profile profile; // Profile @@ -48,7 +50,7 @@ public class Policy implements Comparable, Serializable { /* This is related criteria based policy */ - private List policyCriterias; + private List policyCriterias; /*These are related to time based policies*/ @@ -161,11 +163,11 @@ public class Policy implements Comparable, Serializable { } @XmlElement - public List getPolicyCriterias() { + public List getPolicyCriterias() { return policyCriterias; } - public void setPolicyCriterias(List policyCriterias) { + public void setPolicyCriterias(List policyCriterias) { this.policyCriterias = policyCriterias; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyCriteria.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyCriterion.java similarity index 87% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyCriteria.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyCriterion.java index bb474258e7..c1a8481414 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyCriteria.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyCriterion.java @@ -22,9 +22,10 @@ package org.wso2.carbon.policy.mgt.common; import java.util.Map; import java.util.Properties; -public class PolicyCriteria { +public class PolicyCriterion { private int id; + private int criteriaId; private String name; private Properties properties; private Map objectMap; @@ -37,6 +38,14 @@ public class PolicyCriteria { this.id = id; } + public int getCriteriaId() { + return criteriaId; + } + + public void setCriteriaId(int criteriaId) { + this.criteriaId = criteriaId; + } + public String getName() { return name; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java index 6a8e582c20..9c677b72e9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/ProfileFeature.java @@ -25,7 +25,6 @@ public class ProfileFeature implements Serializable { private int id; private String featureCode; -// private Feature feature; private int profileId; private int deviceTypeId; private Object content; @@ -46,14 +45,6 @@ public class ProfileFeature implements Serializable { this.featureCode = featureCode; } -// public Feature getFeature() { -// return feature; -// } -// -// public void setFeature(Feature feature) { -// this.feature = feature; -// } - public int getProfileId() { return profileId; } 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 7ae2371c37..ca4de5a65f 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 @@ -58,7 +58,9 @@ public interface PolicyDAO { Policy addPolicyCriteria(Policy policy) throws PolicyManagerDAOException; - List getPolicyCriteria(int policyId) throws PolicyManagerDAOException; + boolean addPolicyCriteriaProperties(List policyCriteria) throws PolicyManagerDAOException; + + List getPolicyCriteria(int policyId) throws PolicyManagerDAOException; Policy updatePolicy(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 27edad84f5..e00e6c1c43 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 @@ -29,9 +29,8 @@ import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.*; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; +import java.sql.Date; +import java.util.*; public class PolicyDAOImpl implements PolicyDAO { @@ -303,6 +302,7 @@ public class PolicyDAOImpl implements PolicyDAO { throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } } @@ -333,6 +333,7 @@ public class PolicyDAOImpl implements PolicyDAO { throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } } @@ -391,17 +392,163 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public List getAllPolicyCriteria() throws PolicyManagerDAOException { - return null; + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List criteria = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_CRITERIA"; + stmt = conn.prepareStatement(query); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Criterion criterion = new Criterion(); + criterion.setId(resultSet.getInt("ID")); + criterion.setName(resultSet.getString("NAME")); + criteria.add(criterion); + } + return criteria; + + } catch (SQLException e) { + String msg = "Error occurred while reading the policies from the database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } } @Override public Policy addPolicyCriteria(Policy policy) throws PolicyManagerDAOException { - return null; + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys; + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_CRITERIA (CRITERIA_ID, POLICY_ID) VALUES (?, ?)"; + stmt = conn.prepareStatement(query); + + List criteria = policy.getPolicyCriterias(); + for (PolicyCriterion criterion : criteria) { + + stmt.setInt(1, criterion.getCriteriaId()); + stmt.setInt(2, policy.getId()); + stmt.addBatch(); + } + stmt.executeUpdate(); + + generatedKeys = stmt.getGeneratedKeys(); + int i = 0; + + while (generatedKeys.next()) { + policy.getPolicyCriterias().get(i).setId(generatedKeys.getInt(1)); + i++; + } + + } catch (SQLException e) { + String msg = "Error occurred while inserting the criterion to policy (" + policy.getPolicyName() + ") " + + "to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + return policy; + } @Override - public List getPolicyCriteria(int policyId) throws PolicyManagerDAOException { - return null; + public boolean addPolicyCriteriaProperties(List policyCriteria) throws PolicyManagerDAOException { + + + Connection conn; + PreparedStatement stmt = null; + + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, KEY, VALUE, CONTENT) VALUES (?, ?, ?, ?)"; + stmt = conn.prepareStatement(query); + + for (PolicyCriterion criterion : policyCriteria) { + + for (String name : criterion.getProperties().stringPropertyNames()) { + + stmt.setInt(1, criterion.getId()); + stmt.setString(2, name); + stmt.setString(3, criterion.getProperties().getProperty(name)); + stmt.setObject(4, criterion.getObjectMap()); + stmt.addBatch(); + } + } + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Error occurred while inserting the criterion properties to database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + + return false; + } + + @Override + public List getPolicyCriteria(int policyId) throws PolicyManagerDAOException { + + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List criteria = new ArrayList(); + + try { + conn = this.getConnection(); + String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.KEY, DPCP.VALUE, DPCP.CONTENT FROM " + + "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + + "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + + "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, policyId); + resultSet = stmt.executeQuery(); + + int criteriaId = 0; + + PolicyCriterion policyCriterion = null; + Properties prop = null; + while (resultSet.next()) { + + if (criteriaId != resultSet.getInt("ID")) { + if (policyCriterion != null) { + policyCriterion.setProperties(prop); + criteria.add(policyCriterion); + } + policyCriterion = new PolicyCriterion(); + prop = new Properties(); + criteriaId = resultSet.getInt("ID"); + + policyCriterion.setId(resultSet.getInt("ID")); + policyCriterion.setCriteriaId(resultSet.getInt("CRITERIA_ID")); + } else { + prop.setProperty(resultSet.getString("KEY"), resultSet.getString("VALUE")); + } + } + + } catch (SQLException e) { + String msg = "Error occurred while reading the criteria related to policies from the database."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + return criteria; } @Override @@ -972,6 +1119,13 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, policyId); stmt.executeUpdate(); + + String deleteCriteria = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(deleteCriteria); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + + if (log.isDebugEnabled()) { log.debug("Policy (" + policyId + ") related configs deleted from database."); } 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 811a7f087c..72bc9c5943 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 @@ -24,7 +24,6 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.dao.*; @@ -40,7 +39,7 @@ public class PolicyManagerImpl implements PolicyManager { private ProfileDAO profileDAO; private FeatureDAO featureDAO; private DeviceDAO deviceDAO; - private DeviceTypeDAO deviceTypeDAO; +// private DeviceTypeDAO deviceTypeDAO; private ProfileManager profileManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); @@ -49,7 +48,7 @@ public class PolicyManagerImpl implements PolicyManager { this.profileDAO = PolicyManagementDAOFactory.getProfileDAO(); this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); +// this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); this.profileManager = new ProfileManagerImpl(); } @@ -77,6 +76,20 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyToDevice(policy.getDevices(), policy); } + if (policy.getPolicyCriterias() != null) { + List criteria = policy.getPolicyCriterias(); + for (PolicyCriterion criterion : criteria) { + if (!policyDAO.checkCriterionExists(criterion.getName())) { + Criterion criteriaObj = new Criterion(); + criteriaObj.setName(criterion.getName()); + policyDAO.addCriterion(criteriaObj); + } + } + + policyDAO.addPolicyCriteria(policy); + policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); + } + if (policy.getEndDate() != null & policy.getStartDate() != null) { policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy); } @@ -145,6 +158,20 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyToDevice(policy.getDevices(), policy); } + if (policy.getPolicyCriterias() != null) { + List criteria = policy.getPolicyCriterias(); + for (PolicyCriterion criterion : criteria) { + if (!policyDAO.checkCriterionExists(criterion.getName())) { + Criterion criteriaObj = new Criterion(); + criteriaObj.setName(criterion.getName()); + policyDAO.addCriterion(criteriaObj); + } + } + + policyDAO.addPolicyCriteria(policy); + policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); + } + if (policy.getEndDate() != null & policy.getStartDate() != null) { policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy); } From a15970d0e335787c838e92b682bcb61b2b403d78 Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Sat, 16 May 2015 12:10:05 +0530 Subject: [PATCH 2/7] Changes to repo url --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 087a6647e3..0cbacff5b1 100644 --- a/pom.xml +++ b/pom.xml @@ -1081,7 +1081,7 @@ wso2-staging WSO2 Staging Repository - http://maven.wso2.org/nexus/content/repositories/orgwso2carbonapimgt-004/ + http://maven.wso2.org/nexus/content/repositories/orgwso2carbonapimgt-009/ true daily From db2065435b855df93e2358f5089ffeac4e80640e Mon Sep 17 00:00:00 2001 From: harshanl Date: Sun, 17 May 2015 17:56:44 +0530 Subject: [PATCH 3/7] Added getAllDevicesOfUser and getAllDevicesOfRole methods with user feature --- .../org.wso2.carbon.device.mgt.core/pom.xml | 12 ++- .../mgt/core/DeviceManagementRepository.java | 5 + .../DeviceManagementServiceProviderImpl.java | 102 +++++++++++++++++- .../internal/DeviceManagementDataHolder.java | 11 ++ .../DeviceManagementServiceComponent.java | 32 +++++- .../core/service/DeviceManagementService.java | 20 ++++ .../service/DeviceManagementServiceImpl.java | 14 +++ .../DeviceMgtUserServiceComponent.java | 2 +- .../pom.xml | 4 +- .../src/main/resources/build.properties | 0 .../src/main/resources/p2.inf | 0 features/user-mgt/pom.xml | 2 +- 12 files changed, 197 insertions(+), 7 deletions(-) rename features/user-mgt/{org.wso2.carbon.devicemgt.user.server.feature => org.wso2.carbon.device.mgt.user.server.feature}/pom.xml (96%) rename features/user-mgt/{org.wso2.carbon.devicemgt.user.server.feature => org.wso2.carbon.device.mgt.user.server.feature}/src/main/resources/build.properties (100%) rename features/user-mgt/{org.wso2.carbon.devicemgt.user.server.feature => org.wso2.carbon.device.mgt.user.server.feature}/src/main/resources/p2.inf (100%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index b69b06d957..80f79adcc4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -82,7 +82,9 @@ org.wso2.carbon.apimgt.impl, org.wso2.carbon.identity.oauth.stub, org.wso2.carbon.identity.oauth.stub.dto, - org.wso2.carbon.ndatasource.core + org.wso2.carbon.ndatasource.core, + org.wso2.carbon.device.mgt.user.core, + org.wso2.carbon.device.mgt.user.common, !org.wso2.carbon.device.mgt.core.internal, @@ -122,6 +124,14 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.common + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.user.core + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.user.common + org.wso2.carbon org.wso2.carbon.logging diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java index 596d1202a0..7633849ebd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java @@ -21,6 +21,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceMgtService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -52,4 +53,8 @@ public class DeviceManagementRepository { return providers.get(type); } + public Collection getDeviceManagementProviders(){ + return providers.values(); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index de39d9fbf3..db05ae66b3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -471,4 +471,104 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsForStatus(status); } -} + + @Override + public List getAllDevicesOfUser(String userName) + throws DeviceManagementException { + List devicesOfUser = new ArrayList(); + List devicesList; + Device convertedDevice; + DeviceIdentifier deviceIdentifier; + DeviceManager dms; + Device dmsDevice; + org.wso2.carbon.device.mgt.core.dto.Device device; + int tenantId = DeviceManagerUtil.getTenantId(); + //Fetch the DeviceList from Core + try { + devicesList = this.getDeviceDAO().getDeviceListOfUser(userName, tenantId); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the devices of user '" + + userName + "'", e); + } + + //Fetch the DeviceList from device plugin dbs & append the properties + for (int x = 0; x < devicesList.size(); x++) { + device = devicesList.get(x); + try { + //TODO : Possible improvement if DeviceTypes have been cached + device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); + dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName()); + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType()); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + deviceIdentifier.setType(device.getDeviceType().getName()); + dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesOfUser.add(convertedDevice); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the device type of DeviceTypeId '"+ + device.getDeviceTypeId() + "'",e); + } + } + return devicesOfUser; + } + + @Override + public List getAllDevicesOfRole(String roleName) + throws DeviceManagementException { + List devicesOfRole = new ArrayList(); + List devicesList; + List users; + Device convertedDevice; + DeviceIdentifier deviceIdentifier; + DeviceManager dms; + Device dmsDevice; + org.wso2.carbon.device.mgt.core.dto.Device device; + String userName = ""; + int tenantId = DeviceManagerUtil.getTenantId(); + //Obtaining the list of users of role + try { + users = DeviceManagementDataHolder.getInstance().getUserManager().getUsersForTenantAndRole( + tenantId, roleName); + } catch (org.wso2.carbon.device.mgt.user.common.UserManagementException e) { + throw new DeviceManagementException("Error occurred while obtaining the users of role '" + + roleName + "'", e); + } + + //Obtaining the devices per user + for(org.wso2.carbon.device.mgt.user.common.User user:users){ + try { + userName = user.getUserName(); + devicesList = this.getDeviceDAO().getDeviceListOfUser(userName, tenantId); + for (int x = 0; x < devicesList.size(); x++) { + device = devicesList.get(x); + try { + //TODO : Possible improvement if DeviceTypes have been cached + device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); + dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName()); + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType()); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + deviceIdentifier.setType(device.getDeviceType().getName()); + dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesOfRole.add(convertedDevice); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the device type of DeviceTypeId '"+ + device.getDeviceTypeId() + "'",e); + } + } + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the devices of user '" + + userName + "'", e); + } + } + return devicesOfRole; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 30a619ea1e..3651a3f4d0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; +import org.wso2.carbon.device.mgt.user.core.UserManager; public class DeviceManagementDataHolder { @@ -42,6 +43,7 @@ public class DeviceManagementDataHolder { private AppManagerConnector appManager; private AppManagementConfig appManagerConfig; private OperationManager operationManager; + private UserManager userManager; private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); @@ -135,4 +137,13 @@ public class DeviceManagementDataHolder { public void setOperationManager(OperationManager operationManager) { this.operationManager = operationManager; } + + + public UserManager getUserManager() { + return userManager; + } + + public void setUserManager(UserManager userManager) { + this.userManager = userManager; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 54b279bc45..c253594710 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -29,7 +29,6 @@ import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; 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.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.common.spi.DeviceMgtService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; @@ -55,6 +54,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; +import org.wso2.carbon.device.mgt.user.core.UserManager; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -93,6 +93,12 @@ import java.util.List; * policy="dynamic" * bind="setDataSourceService" * unbind="unsetDataSourceService" + * @scr.reference name="org.wso2.carbon.device.mgt.usermanager.service" + * interface="org.wso2.carbon.device.mgt.user.core.UserManager" + * cardinality="1..1" + * policy="dynamic" + * bind="setUserManager" + * unbind="unsetUserManager" */ public class DeviceManagementServiceComponent { @@ -324,6 +330,30 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setRegistryService(null); } + /** + * Sets UserManager Service. + * + * @param userMgtService An instance of UserManager + */ + protected void setUserManager(UserManager userMgtService) { + if (log.isDebugEnabled()) { + log.debug("Setting UserManager Service"); + } + DeviceManagementDataHolder.getInstance().setUserManager(userMgtService); + } + + /** + * Unsets UserManager Service. + * + * @param userMgtService An instance of UserManager + */ + protected void unsetUserManager(UserManager userMgtService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting UserManager Service"); + } + DeviceManagementDataHolder.getInstance().setUserManager(null); + } + private DeviceManagementRepository getPluginRepository() { return pluginRepository; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index 7a3af5b836..9a8ad08398 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -50,4 +50,24 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, */ Device getCoreDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + /** + * Method to get the list of devices owned by an user. + * + * @param userName Username of the user + * @return List of devices owned by a particular user + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getAllDevicesOfUser(String userName) throws DeviceManagementException; + + /** + * Method to get the list of devices owned by users of a particular user-role. + * + * @param roleName Role name of the users + * @return List of devices owned by users of a particular role + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getAllDevicesOfRole(String roleName) throws DeviceManagementException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 582fbe7214..a0c363854d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -189,4 +189,18 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() .sendRegistrationEmail(emailMessageProperties); } + + @Override + public List getAllDevicesOfUser(String userName) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getAllDevicesOfUser(userName); + } + + @Override + public List getAllDevicesOfRole(String roleName) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getAllDevicesOfRole(roleName); + } } \ No newline at end of file diff --git a/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java b/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java index cca59fea57..4e0902269b 100644 --- a/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java +++ b/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java @@ -55,7 +55,7 @@ public class DeviceMgtUserServiceComponent { } /* Registering User Management service */ BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(UserManagementService.class.getName(), + bundleContext.registerService(UserManager.class, new UserManagementService(), null); if (log.isDebugEnabled()) { log.debug("User management core bundle has been successfully initialized"); diff --git a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/pom.xml b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/pom.xml similarity index 96% rename from features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/pom.xml rename to features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/pom.xml index ace0db9693..889c4667c5 100644 --- a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/pom.xml +++ b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/pom.xml @@ -32,7 +32,7 @@ 4.0.0 org.wso2.carbon.devicemgt - org.wso2.carbon.devicemgt.user.server.feature + org.wso2.carbon.device.mgt.user.server.feature pom 0.9.2-SNAPSHOT WSO2 Carbon - User Management Server Feature @@ -91,7 +91,7 @@ p2-feature-gen - org.wso2.carbon.devicemgt.user.server + org.wso2.carbon.device.mgt.user.server ../../../features/etc/feature.properties diff --git a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/build.properties b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/build.properties similarity index 100% rename from features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/build.properties rename to features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/build.properties diff --git a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/p2.inf b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/p2.inf similarity index 100% rename from features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/p2.inf rename to features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/p2.inf diff --git a/features/user-mgt/pom.xml b/features/user-mgt/pom.xml index 1f916bd68a..2dd255a300 100644 --- a/features/user-mgt/pom.xml +++ b/features/user-mgt/pom.xml @@ -41,7 +41,7 @@ - org.wso2.carbon.devicemgt.user.server.feature + org.wso2.carbon.device.mgt.user.server.feature \ No newline at end of file From 38e09d49bbd2f036dc34ea9a6bde922401a8957f Mon Sep 17 00:00:00 2001 From: geethkokila Date: Sun, 17 May 2015 23:18:10 +0530 Subject: [PATCH 4/7] Fixing bugs and fixing the criteria based policies --- .../wso2/carbon/policy/mgt/common/Policy.java | 130 +++--- .../carbon/policy/mgt/common/PolicyDates.java | 43 -- .../policy/mgt/common/PolicyLocations.java | 41 -- .../carbon/policy/mgt/common/PolicyTimes.java | 43 -- .../policy/mgt/core/dao/FeatureDAO.java | 2 - .../carbon/policy/mgt/core/dao/PolicyDAO.java | 20 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 134 +++--- .../mgt/core/dao/impl/PolicyDAOImpl.java | 377 +++++++++-------- .../policy/mgt/core/impl/PolicyFilter.java | 35 ++ .../mgt/core/impl/PolicyFilterImpl.java | 83 ++++ .../core/impl/PolicyInformationPointImpl.java | 29 +- .../policy/mgt/core/mgt/FeatureManager.java | 1 - .../mgt/core/mgt/impl/FeatureManagerImpl.java | 12 +- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 88 ++-- .../core/util/PolicyManagementConstants.java | 3 + .../policy/mgt/core/PolicyDAOTestCase.java | 79 +++- .../policy/mgt/core/util/PolicyCreator.java | 131 +++++- .../mgt/core/util/ProfileFeatureCreator.java | 82 +++- .../src/test/resources/sql/CreateH2TestDB.sql | 107 ++--- .../test/resources/sql/CreateMySqlTestDB.sql | 384 +++++++++--------- pom.xml | 2 +- 21 files changed, 1062 insertions(+), 764 deletions(-) delete mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java delete mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java delete mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index a39f03935a..8d458db744 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -46,6 +46,10 @@ public class Policy implements Comparable, Serializable { private List devices; // Individual devices this policy should be applied private List users; + + /* Compliance data*/ + private String Compliance; + /*Dynamic policy attributes*/ /* This is related criteria based policy */ @@ -54,17 +58,17 @@ public class Policy implements Comparable, Serializable { /*These are related to time based policies*/ - private int startTime; // Start time to apply the policy. - private int endTime; // After this time policy will not be applied - private Date startDate; // Start date to apply the policy - private Date endDate; // After this date policy will not be applied. +// private int startTime; // Start time to apply the policy. +// private int endTime; // After this time policy will not be applied +// private Date startDate; // Start date to apply the policy +// private Date endDate; // After this date policy will not be applied. /*These are related to location based policies*/ - private String latitude; // Latitude - private String longitude; // Longitude - +// private String latitude; // Latitude +// private String longitude; // Longitude +// private int tenantId; private int profileId; @@ -171,58 +175,66 @@ public class Policy implements Comparable, Serializable { this.policyCriterias = policyCriterias; } - public int getStartTime() { - return startTime; - } - - public void setStartTime(int startTime) { - this.startTime = startTime; - } - - @XmlElement - public int getEndTime() { - return endTime; - } - - public void setEndTime(int endTime) { - this.endTime = endTime; - } - - @XmlElement - public Date getStartDate() { - return startDate; - } - - public void setStartDate(Date startDate) { - this.startDate = startDate; - } - - @XmlElement - public Date getEndDate() { - return endDate; - } - - public void setEndDate(Date endDate) { - this.endDate = endDate; - } - - @XmlElement - public String getLatitude() { - return latitude; - } - - public void setLatitude(String latitude) { - this.latitude = latitude; - } - - @XmlElement - public String getLongitude() { - return longitude; - } - - public void setLongitude(String longitude) { - this.longitude = longitude; - } + public String getCompliance() { + return Compliance; + } + + public void setCompliance(String compliance) { + Compliance = compliance; + } + + // public int getStartTime() { +// return startTime; +// } +// +// public void setStartTime(int startTime) { +// this.startTime = startTime; +// } +// +// @XmlElement +// public int getEndTime() { +// return endTime; +// } +// +// public void setEndTime(int endTime) { +// this.endTime = endTime; +// } +// +// @XmlElement +// public Date getStartDate() { +// return startDate; +// } +// +// public void setStartDate(Date startDate) { +// this.startDate = startDate; +// } +// +// @XmlElement +// public Date getEndDate() { +// return endDate; +// } +// +// public void setEndDate(Date endDate) { +// this.endDate = endDate; +// } +// +// @XmlElement +// public String getLatitude() { +// return latitude; +// } +// +// public void setLatitude(String latitude) { +// this.latitude = latitude; +// } +// +// @XmlElement +// public String getLongitude() { +// return longitude; +// } +// +// public void setLongitude(String longitude) { +// this.longitude = longitude; +// } @XmlElement public Map getAttributes() { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java deleted file mode 100644 index e10e58245a..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyDates.java +++ /dev/null @@ -1,43 +0,0 @@ -/* -* 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.policy.mgt.common; - -import java.sql.Date; - -public class PolicyDates { - - private Date startDate; // Start date to apply the policy - private Date endDate; // After this date policy will not be applied. - - public Date getStartDate() { - return startDate; - } - - public void setStartDate(Date startDate) { - this.startDate = startDate; - } - - public Date getEndDate() { - return endDate; - } - - public void setEndDate(Date endDate) { - this.endDate = endDate; - } -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java deleted file mode 100644 index 9533c39ae1..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyLocations.java +++ /dev/null @@ -1,41 +0,0 @@ -/* -* 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.policy.mgt.common; - -public class PolicyLocations { - - private String latitude; // Latitude - private String longitude; // Longitude - - public String getLatitude() { - return latitude; - } - - public void setLatitude(String latitude) { - this.latitude = latitude; - } - - public String getLongitude() { - return longitude; - } - - public void setLongitude(String longitude) { - this.longitude = longitude; - } -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java deleted file mode 100644 index fedf615fdb..0000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyTimes.java +++ /dev/null @@ -1,43 +0,0 @@ -/* -* 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.policy.mgt.common; - -import java.sql.Date; - -public class PolicyTimes { - - private int startTime; // Start time to apply the policy. - private int endTime; // After this time policy will not be applied - - public int getStartTime() { - return startTime; - } - - public void setStartTime(int startTime) { - this.startTime = startTime; - } - - public int getEndTime() { - return endTime; - } - - public void setEndTime(int endTime) { - this.endTime = endTime; - } -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index 080dcc3921..f35e5070ea 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -43,8 +43,6 @@ public interface FeatureDAO { List updateProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; - List getAllFeatures() throws FeatureManagerDAOException; - List getAllProfileFeatures() throws FeatureManagerDAOException; List getAllFeatures(String deviceType) throws FeatureManagerDAOException; 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 ca4de5a65f..a3d4331559 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,11 +36,11 @@ public interface PolicyDAO { Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException; - Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException; - - Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException; - - Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws PolicyManagerDAOException; +// Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException; +// +// Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException; +// +// Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws PolicyManagerDAOException; Criterion addCriterion(Criterion criteria) throws PolicyManagerDAOException; @@ -86,11 +86,11 @@ public interface PolicyDAO { List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException; - PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException; - - PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException; - - PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException; +// PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException; +// +// PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException; +// +// PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException; void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) 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/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 59b25ac507..66f83ee803 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -30,10 +30,10 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -181,7 +181,11 @@ public class FeatureDAOImpl implements FeatureDAO { stmt.setInt(1, profileId); stmt.setString(2, feature.getFeatureCode()); stmt.setInt(3, feature.getDeviceTypeId()); - stmt.setObject(4, feature.getContent()); + if (conn.getMetaData().getDriverName().contains("H2")) { + stmt.setObject(4, feature.getContent(), Types.JAVA_OBJECT); + } else { + stmt.setObject(4, feature.getContent()); + } stmt.addBatch(); //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 } @@ -217,7 +221,11 @@ public class FeatureDAOImpl implements FeatureDAO { stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); for (ProfileFeature feature : features) { - stmt.setObject(1, feature.getContent()); + if (conn.getMetaData().getDriverName().contains("H2")) { + stmt.setObject(4, feature.getContent(), Types.JAVA_OBJECT); + } else { + stmt.setObject(4, feature.getContent()); + } stmt.setInt(2, profileId); stmt.setString(3, feature.getFeatureCode()); stmt.addBatch(); @@ -259,42 +267,6 @@ public class FeatureDAOImpl implements FeatureDAO { } - @Override - public List getAllFeatures() throws FeatureManagerDAOException { - -// Connection conn; -// PreparedStatement stmt = null; -// ResultSet resultSet = null; - List featureList = new ArrayList(); -// -// try { -// conn = this.getConnection(); -// String query = "SELECT ID, NAME, CODE, DEVICE_TYPE_ID, EVALUATION_RULE FROM DM_FEATURES"; -// stmt = conn.prepareStatement(query); -// resultSet = stmt.executeQuery(); -// -// while (resultSet.next()) { -// -// Feature feature = new Feature(); -// feature.setId(resultSet.getInt("ID")); -// feature.setCode(resultSet.getString("CODE")); -// feature.setName(resultSet.getString("NAME")); -// feature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID")); -// feature.setRuleValue(resultSet.getString("EVALUATION_RULE")); -// featureList.add(feature); -// } -// -// } catch (SQLException e) { -// String msg = "Unable to get the list of the features from database."; -// log.error(msg); -// throw new FeatureManagerDAOException(msg, e); -// } finally { -// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); -// this.closeConnection(); -// } - return featureList; - } - @Override public List getAllProfileFeatures() throws FeatureManagerDAOException { @@ -322,8 +294,34 @@ public class FeatureDAOImpl implements FeatureDAO { profileFeature.setFeatureCode(resultSet.getString("FEATURE_CODE")); profileFeature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID")); profileFeature.setId(resultSet.getInt("ID")); - profileFeature.setContent(resultSet.getObject("CONTENT")); +// profileFeature.setContent(resultSet.getObject("CONTENT")); profileFeature.setProfileId(resultSet.getInt("PROFILE_ID")); + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + byte[] contentBytes; + try { + contentBytes = (byte[]) resultSet.getBytes("CONTENT"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + profileFeature.setContent(ois.readObject().toString()); + } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } + } + featureList.add(profileFeature); } @@ -331,6 +329,14 @@ public class FeatureDAOImpl implements FeatureDAO { String msg = "Unable to get the list of the features from database."; log.error(msg); throw new FeatureManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Unable to read the byte stream for content"; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } catch (ClassNotFoundException e) { + String msg = "Class not found while converting the object"; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); this.closeConnection(); @@ -395,17 +401,35 @@ public class FeatureDAOImpl implements FeatureDAO { while (resultSet.next()) { ProfileFeature profileFeature = new ProfileFeature(); -// Feature feature = new Feature(); -// feature.setId(resultSet.getInt("FEATURE_ID")); -// feature.setCode(resultSet.getString("CODE")); -// feature.setName(resultSet.getString("NAME")); -// feature.setRuleValue(resultSet.getString("RULE")); - -// profileFeature.setFeature(feature); profileFeature.setId(resultSet.getInt("ID")); profileFeature.setFeatureCode(resultSet.getString("FEATURE_CODE")); profileFeature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID")); - profileFeature.setContent(resultSet.getObject("CONTENT")); + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + byte[] contentBytes; + try { + contentBytes = resultSet.getBytes("CONTENT"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + profileFeature.setContent(ois.readObject().toString()); + } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } + } + featureList.add(profileFeature); } @@ -413,6 +437,14 @@ public class FeatureDAOImpl implements FeatureDAO { String msg = "Unable to get the list of the features from database."; log.error(msg); throw new FeatureManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Unable to read the byte stream for content"; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); + } catch (ClassNotFoundException e) { + String msg = "Class not found while converting the object"; + log.error(msg); + throw new FeatureManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); this.closeConnection(); 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 a5778e2c32..604403950d 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,81 +142,81 @@ public class PolicyDAOImpl implements PolicyDAO { return policy; } - @Override - public Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_DATE (START_DATE, END_DATE, POLICY_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query); - stmt.setDate(1, startDate); - stmt.setDate(2, endDate); - stmt.setInt(3, policy.getId()); - stmt.executeUpdate(); - - } catch (SQLException e) { - String msg = "Error occurred while adding the start date (" + startDate + ") and end date (" + - endDate + ") with policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - return policy; - } - - @Override - public Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_TIME (STARTING_TIME, ENDING_TIME, POLICY_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, startTime); - stmt.setInt(2, endTime); - stmt.setInt(3, policy.getId()); - stmt.executeUpdate(); - - } catch (SQLException e) { - String msg = "Error occurred while adding the start time (" + startTime + ") and end time (" + - endTime + ") with policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - return policy; - } - - @Override - public Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws - PolicyManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String query = "INSERT INTO DM_LOCATION (LATITUDE, LONGITUDE, POLICY_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); - stmt.setString(1, latitude); - stmt.setString(2, longitude); - stmt.setInt(3, policy.getId()); - stmt.executeUpdate(); - - } catch (SQLException e) { - String msg = "Error occurred while adding the Location (" + latitude + ") (" + - longitude + ") with policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - return policy; - } +// @Override +// public Policy addDatesToPolicy(Date startDate, Date endDate, Policy policy) throws PolicyManagerDAOException { +// +// Connection conn; +// PreparedStatement stmt = null; +// try { +// conn = this.getConnection(); +// String query = "INSERT INTO DM_DATE (START_DATE, END_DATE, POLICY_ID) VALUES (?, ?, ?)"; +// stmt = conn.prepareStatement(query); +// stmt.setDate(1, startDate); +// stmt.setDate(2, endDate); +// stmt.setInt(3, policy.getId()); +// stmt.executeUpdate(); +// +// } catch (SQLException e) { +// String msg = "Error occurred while adding the start date (" + startDate + ") and end date (" + +// endDate + ") with policy to database."; +// log.error(msg, e); +// throw new PolicyManagerDAOException(msg, e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, null); +// } +// return policy; +// } + +// @Override +// public Policy addTimesToPolicy(int startTime, int endTime, Policy policy) throws PolicyManagerDAOException { +// +// Connection conn; +// PreparedStatement stmt = null; +// try { +// conn = this.getConnection(); +// String query = "INSERT INTO DM_TIME (STARTING_TIME, ENDING_TIME, POLICY_ID) VALUES (?, ?, ?)"; +// stmt = conn.prepareStatement(query); +// stmt.setInt(1, startTime); +// stmt.setInt(2, endTime); +// stmt.setInt(3, policy.getId()); +// stmt.executeUpdate(); +// +// } catch (SQLException e) { +// String msg = "Error occurred while adding the start time (" + startTime + ") and end time (" + +// endTime + ") with policy to database."; +// log.error(msg, e); +// throw new PolicyManagerDAOException(msg, e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, null); +// } +// return policy; +// } + +// @Override +// public Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws +// PolicyManagerDAOException { +// +// Connection conn; +// PreparedStatement stmt = null; +// try { +// conn = this.getConnection(); +// String query = "INSERT INTO DM_LOCATION (LATITUDE, LONGITUDE, POLICY_ID) VALUES (?, ?, ?)"; +// stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); +// stmt.setString(1, latitude); +// stmt.setString(2, longitude); +// stmt.setInt(3, policy.getId()); +// stmt.executeUpdate(); +// +// } catch (SQLException e) { +// String msg = "Error occurred while adding the Location (" + latitude + ") (" + +// longitude + ") with policy to database."; +// log.error(msg, e); +// throw new PolicyManagerDAOException(msg, e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, null); +// } +// return policy; +// } @Override public Criterion addCriterion(Criterion criteria) throws PolicyManagerDAOException { @@ -229,7 +229,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_CRITERIA (TENANT_ID, NAME) VALUES (?, ?)"; - stmt = conn.prepareStatement(query); + stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, tenantId); stmt.setString(2, criteria.getName()); stmt.executeUpdate(); @@ -427,12 +427,12 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; - ResultSet generatedKeys; + ResultSet generatedKeys = null; try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CRITERIA (CRITERIA_ID, POLICY_ID) VALUES (?, ?)"; - stmt = conn.prepareStatement(query); + stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); List criteria = policy.getPolicyCriterias(); for (PolicyCriterion criterion : criteria) { @@ -457,7 +457,7 @@ public class PolicyDAOImpl implements PolicyDAO { log.error(msg, e); throw new PolicyManagerDAOException(msg, e); } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } return policy; @@ -472,21 +472,23 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, KEY, VALUE, CONTENT) VALUES (?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, CONTENT) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(query); for (PolicyCriterion criterion : policyCriteria) { - for (String name : criterion.getProperties().stringPropertyNames()) { + Properties prop = criterion.getProperties(); + for (String name : prop.stringPropertyNames()) { stmt.setInt(1, criterion.getId()); stmt.setString(2, name); - stmt.setString(3, criterion.getProperties().getProperty(name)); + stmt.setString(3, prop.getProperty(name)); stmt.setObject(4, criterion.getObjectMap()); stmt.addBatch(); } + stmt.executeBatch(); } - stmt.executeUpdate(); + // stmt.executeUpdate(); } catch (SQLException e) { String msg = "Error occurred while inserting the criterion properties to database."; @@ -510,7 +512,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.KEY, DPCP.VALUE, DPCP.CONTENT FROM " + + String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " + "DM_POLICY_CRITERIA DPC LEFT JOIN DM_POLICY_CRITERIA_PROPERTIES DPCP " + "ON DPCP.POLICY_CRITERION_ID = DPC.ID RIGHT JOIN DM_CRITERIA DC " + "ON DC.ID=DPC.CRITERIA_ID WHERE DPC.POLICY_ID = ?"; @@ -536,7 +538,7 @@ public class PolicyDAOImpl implements PolicyDAO { policyCriterion.setId(resultSet.getInt("ID")); policyCriterion.setCriteriaId(resultSet.getInt("CRITERIA_ID")); } else { - prop.setProperty(resultSet.getString("KEY"), resultSet.getString("VALUE")); + prop.setProperty(resultSet.getString("PROP_KEY"), resultSet.getString("PROP_VALUE")); } } @@ -558,13 +560,14 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ? WHERE ID = ?"; + String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ? 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.setInt(5, policy.getId()); + stmt.setString(5, policy.getCompliance()); + stmt.setInt(6, policy.getId()); stmt.executeUpdate(); } catch (SQLException e) { @@ -597,6 +600,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setTenantId(resultSet.getInt("TENANT_ID")); policy.setPriorityId(resultSet.getInt("PRIORITY")); policy.setProfileId(resultSet.getInt("PROFILE_ID")); + policy.setCompliance(resultSet.getString("COMPLIANCE")); } return policy; @@ -630,6 +634,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); policy.setPriorityId(resultSet.getInt("PRIORITY")); + policy.setCompliance(resultSet.getString("COMPLIANCE")); } return policy; @@ -664,6 +669,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); policy.setPriorityId(resultSet.getInt("PRIORITY")); + policy.setCompliance(resultSet.getString("COMPLIANCE")); policies.add(policy); } return policies; @@ -746,108 +752,98 @@ public class PolicyDAOImpl implements PolicyDAO { } - public PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet resultSet = null; - PolicyTimes times = new PolicyTimes(); - - try { - conn = this.getConnection(); - String query = "SELECT STARTING_TIME, ENDING_TIME FROM DM_TIME WHERE POLICY_ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, policy.getId()); - resultSet = stmt.executeQuery(); - - while (resultSet.next()) { - policy.setStartTime(resultSet.getInt("STARTING_TIME")); - policy.setEndTime(resultSet.getInt("ENDING_TIME")); - - times.setStartTime(resultSet.getInt("STARTING_TIME")); - times.setEndTime(resultSet.getInt("ENDING_TIME")); - } - - } catch (SQLException e) { - String msg = "Error occurred while getting the start time and end time related to policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); - } - return times; - } - - - public PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet resultSet = null; - PolicyDates dates = new PolicyDates(); - - try { - conn = this.getConnection(); - String query = "SELECT START_DATE, END_DATE FROM DM_DATE WHERE POLICY_ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, policy.getId()); - resultSet = stmt.executeQuery(); - - while (resultSet.next()) { - - policy.setStartDate(resultSet.getDate("START_DATE")); - policy.setEndDate(resultSet.getDate("END_DATE")); - - dates.setStartDate(resultSet.getDate("START_DATE")); - dates.setEndDate(resultSet.getDate("END_DATE")); - } - - } catch (SQLException e) { - String msg = "Error occurred while getting the start date and end date related to policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); - } - return dates; - } - - - public PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet resultSet = null; - PolicyLocations locations = new PolicyLocations(); - - try { - conn = this.getConnection(); - String query = "SELECT LATITUDE, LONGITUDE FROM DM_LOCATION WHERE POLICY_ID = ?"; - stmt = conn.prepareStatement(query); - stmt.setInt(1, policy.getId()); - resultSet = stmt.executeQuery(); - - while (resultSet.next()) { - - policy.setLatitude(resultSet.getString("LATITUDE")); - policy.setLongitude(resultSet.getString("LONGITUDE")); - - locations.setLatitude(resultSet.getString("LATITUDE")); - locations.setLongitude(resultSet.getString("LONGITUDE")); - } - - } catch (SQLException e) { - String msg = "Error occurred while getting the start time and end time related to policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); - } - return locations; - } +// public PolicyTimes getTimesOfPolicy(Policy policy) throws PolicyManagerDAOException { +// +// Connection conn; +// PreparedStatement stmt = null; +// ResultSet resultSet = null; +// PolicyTimes times = new PolicyTimes(); +// +// try { +// conn = this.getConnection(); +// String query = "SELECT STARTING_TIME, ENDING_TIME FROM DM_TIME WHERE POLICY_ID = ?"; +// stmt = conn.prepareStatement(query); +// stmt.setInt(1, policy.getId()); +// resultSet = stmt.executeQuery(); +// +// while (resultSet.next()) { +// +// times.setStartTime(resultSet.getInt("STARTING_TIME")); +// times.setEndTime(resultSet.getInt("ENDING_TIME")); +// } +// +// } catch (SQLException e) { +// String msg = "Error occurred while getting the start time and end time related to policies."; +// log.error(msg, e); +// throw new PolicyManagerDAOException(msg, e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); +// this.closeConnection(); +// } +// return times; +// } + + +// public PolicyDates getDatesOfPolicy(Policy policy) throws PolicyManagerDAOException { +// +// Connection conn; +// PreparedStatement stmt = null; +// ResultSet resultSet = null; +// PolicyDates dates = new PolicyDates(); +// +// try { +// conn = this.getConnection(); +// String query = "SELECT START_DATE, END_DATE FROM DM_DATE WHERE POLICY_ID = ?"; +// stmt = conn.prepareStatement(query); +// stmt.setInt(1, policy.getId()); +// resultSet = stmt.executeQuery(); +// +// while (resultSet.next()) { +// dates.setStartDate(resultSet.getDate("START_DATE")); +// dates.setEndDate(resultSet.getDate("END_DATE")); +// } +// +// } catch (SQLException e) { +// String msg = "Error occurred while getting the start date and end date related to policies."; +// log.error(msg, e); +// throw new PolicyManagerDAOException(msg, e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); +// this.closeConnection(); +// } +// return dates; +// } + + +// public PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException { +// +// Connection conn; +// PreparedStatement stmt = null; +// ResultSet resultSet = null; +// PolicyLocations locations = new PolicyLocations(); +// +// try { +// conn = this.getConnection(); +// String query = "SELECT LATITUDE, LONGITUDE FROM DM_LOCATION WHERE POLICY_ID = ?"; +// stmt = conn.prepareStatement(query); +// stmt.setInt(1, policy.getId()); +// resultSet = stmt.executeQuery(); +// +// while (resultSet.next()) { +// locations.setLatitude(resultSet.getString("LATITUDE")); +// locations.setLongitude(resultSet.getString("LONGITUDE")); +// } +// +// } catch (SQLException e) { +// String msg = "Error occurred while getting the start time and end time related to policies."; +// log.error(msg, e); +// throw new PolicyManagerDAOException(msg, e); +// } finally { +// PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); +// this.closeConnection(); +// } +// return locations; +// } @Override public void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) @@ -1161,13 +1157,14 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getProfile().getProfileId()); stmt.setInt(3, tenantId); stmt.setInt(4, readHighestPriorityOfPolicies()); + stmt.setString(5, policy.getCompliance()); int affectedRows = stmt.executeUpdate(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java new file mode 100644 index 0000000000..473476403e --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java @@ -0,0 +1,35 @@ +/* + * 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.policy.mgt.core.impl; + +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + +public interface PolicyFilter { + + void filterRolesBasedPolicies(String roles[], List policies); + + void filterOwnershipTypeBasedPolicies(String ownershipType, List policies); + + void filterDeviceTypeBasedPolicies(String deviceType, List policies); + + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java new file mode 100644 index 0000000000..cef8cba786 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -0,0 +1,83 @@ +/* + * 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.policy.mgt.core.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; + +import java.util.ArrayList; +import java.util.List; + +public class PolicyFilterImpl implements PolicyFilter { + + private static final Log log = LogFactory.getLog(PolicyFilterImpl.class); + + @Override + public void filterRolesBasedPolicies(String roles[], List policies) { + + List temp = new ArrayList(); + for (Policy policy : policies) { + + List tempRoles = policy.getRoles(); + if (PolicyManagementConstants.ANY.equalsIgnoreCase(tempRoles.get(0))) { + temp.add(policy); + continue; + } + + for (String role : roles) { + for (String policyRole : tempRoles) { + if (role.equalsIgnoreCase(policyRole)) { + temp.add(policy); + continue; + } + } + } + } + policies = temp; + + } + + @Override + public void filterOwnershipTypeBasedPolicies(String ownershipType, List policies) { + + List temp = new ArrayList(); + for (Policy policy : policies) { + if (ownershipType.equalsIgnoreCase(policy.getOwnershipType())) { + temp.add(policy); + } + } + policies = temp; + } + + @Override + public void filterDeviceTypeBasedPolicies(String deviceType, List policies) { + List temp = new ArrayList(); + for (Policy policy : policies) { + if (deviceType.equalsIgnoreCase(policy.getProfile().getDeviceType().getName())) { + temp.add(policy); + } + } + policies = temp; + } + + +} 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 412f4350b1..50e12eb8b7 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 @@ -92,19 +92,30 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { @Override public List getRelatedPolicies(PIPDevice pipDevice) throws PolicyManagementException { - List> policies = new ArrayList>(); +// List> policies = new ArrayList>(); + List policies = new ArrayList(); try { // Get the device type related policies - policies.add(policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName())); +// policies.add(policyManager.getPoliciesOfDeviceType(pipDevice.getDeviceType().getName())); - // 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())); - return removeDuplicatePolicies(policies); + // 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())); + + 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() + "."; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java index 4f5b66378d..5af6049f5b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java @@ -45,7 +45,6 @@ public interface FeatureManager { List updateProfileFeatures(List features, int profileId) throws FeatureManagementException; - List getAllFeatures() throws FeatureManagementException; List getAllFeatures(String deviceType) throws FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java index 51e279d5c0..7b9f9aa801 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java @@ -273,17 +273,7 @@ public class FeatureManagerImpl implements FeatureManager { } return features; } - - @Override - public List getAllFeatures() throws FeatureManagementException { - try { - return featureDAO.getAllFeatures(); - } catch (FeatureManagerDAOException e) { - String msg = "Error occurred while getting the features."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); - } - } + @Override public List getAllFeatures(String deviceType) throws FeatureManagementException { 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 e5b41d7a3c..8755d048f8 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 @@ -39,7 +39,7 @@ public class PolicyManagerImpl implements PolicyManager { private ProfileDAO profileDAO; private FeatureDAO featureDAO; private DeviceDAO deviceDAO; -// private DeviceTypeDAO deviceTypeDAO; + // private DeviceTypeDAO deviceTypeDAO; private ProfileManager profileManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); @@ -83,6 +83,7 @@ public class PolicyManagerImpl implements PolicyManager { Criterion criteriaObj = new Criterion(); criteriaObj.setName(criterion.getName()); policyDAO.addCriterion(criteriaObj); + criterion.setCriteriaId(criteriaObj.getId()); } } @@ -90,17 +91,17 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - if (policy.getEndDate() != null & policy.getStartDate() != null) { - policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy); - } - - if (policy.getStartTime() != 0 & policy.getEndTime() != 0) { - policyDAO.addTimesToPolicy(policy.getStartTime(), policy.getEndTime(), policy); - } - - if (policy.getLatitude() != null && policy.getLongitude() != null) { - policyDAO.addLocationToPolicy(policy.getLatitude(), policy.getLongitude(), policy); - } +// if (policy.getEndDate() != null & policy.getStartDate() != null) { +// policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy); +// } +// +// if (policy.getStartTime() != 0 & policy.getEndTime() != 0) { +// policyDAO.addTimesToPolicy(policy.getStartTime(), policy.getEndTime(), policy); +// } +// +// if (policy.getLatitude() != null && policy.getLongitude() != null) { +// policyDAO.addLocationToPolicy(policy.getLatitude(), policy.getLongitude(), policy); +// } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { @@ -165,24 +166,25 @@ public class PolicyManagerImpl implements PolicyManager { Criterion criteriaObj = new Criterion(); criteriaObj.setName(criterion.getName()); policyDAO.addCriterion(criteriaObj); + criterion.setCriteriaId(criteriaObj.getId()); } } policyDAO.addPolicyCriteria(policy); policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - - if (policy.getEndDate() != null & policy.getStartDate() != null) { - policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy); - } - - if (policy.getStartTime() != 0 & policy.getEndTime() != 0) { - policyDAO.addTimesToPolicy(policy.getStartTime(), policy.getEndTime(), policy); - } - if (policy.getLatitude() != null && policy.getLongitude() != null) { - policyDAO.addLocationToPolicy(policy.getLatitude(), policy.getLongitude(), policy); - } +// if (policy.getEndDate() != null & policy.getStartDate() != null) { +// policyDAO.addDatesToPolicy(policy.getStartDate(), policy.getEndDate(), policy); +// } +// +// if (policy.getStartTime() != 0 & policy.getEndTime() != 0) { +// policyDAO.addTimesToPolicy(policy.getStartTime(), policy.getEndTime(), policy); +// } +// +// if (policy.getLatitude() != null && policy.getLongitude() != null) { +// policyDAO.addLocationToPolicy(policy.getLatitude(), policy.getLongitude(), policy); +// } PolicyManagementDAOFactory.commitTransaction(); @@ -362,9 +364,9 @@ public class PolicyManagerImpl implements PolicyManager { policy = policyDAO.getPolicyByProfileID(profileId); deviceList = getPolicyAppliedDevicesIds(policy.getId()); roleNames = policyDAO.getPolicyAppliedRoles(policy.getId()); - policyDAO.getDatesOfPolicy(policy); - policyDAO.getTimesOfPolicy(policy); - policyDAO.getLocationsOfPolicy(policy); +// policyDAO.getDatesOfPolicy(policy); +// policyDAO.getTimesOfPolicy(policy); +// policyDAO.getLocationsOfPolicy(policy); profile = profileDAO.getProfiles(profileId); @@ -395,9 +397,9 @@ public class PolicyManagerImpl implements PolicyManager { policy = policyDAO.getPolicy(policyId); deviceList = getPolicyAppliedDevicesIds(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId); - policyDAO.getDatesOfPolicy(policy); - policyDAO.getTimesOfPolicy(policy); - policyDAO.getLocationsOfPolicy(policy); +// policyDAO.getDatesOfPolicy(policy); +// policyDAO.getTimesOfPolicy(policy); +// policyDAO.getLocationsOfPolicy(policy); Profile profile = profileDAO.getProfiles(policy.getProfileId()); @@ -424,7 +426,8 @@ public class PolicyManagerImpl implements PolicyManager { try { policyList = policyDAO.getAllPolicies(); - List profileList = profileDAO.getAllProfiles(); +// List profileList = profileDAO.getAllProfiles(); + List profileList = profileManager.getAllProfiles(); for (Policy policy : policyList) { for (Profile profile : profileList) { @@ -434,16 +437,17 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setDevices(getPolicyAppliedDevicesIds(policy.getId())); policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); - policyDAO.getDatesOfPolicy(policy); - policyDAO.getTimesOfPolicy(policy); - policyDAO.getLocationsOfPolicy(policy); + policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policy.getId())); +// policyDAO.getDatesOfPolicy(policy); +// policyDAO.getTimesOfPolicy(policy); +// policyDAO.getLocationsOfPolicy(policy); } } catch (PolicyManagerDAOException e) { String msg = "Error occurred while getting all the policies."; log.error(msg, e); throw new PolicyManagementException(msg, e); - } catch (ProfileManagerDAOException e) { + } catch (ProfileManagementException e) { String msg = "Error occurred while getting all the profiles."; log.error(msg, e); throw new PolicyManagementException(msg, e); @@ -459,7 +463,7 @@ public class PolicyManagerImpl implements PolicyManager { try { Device device = deviceDAO.getDevice(deviceIdentifier); policyIdList = policyDAO.getPolicyIdsOfDevice(device); - List tempPolicyList = policyDAO.getAllPolicies(); + List tempPolicyList = this.getPolicies(); for (Policy policy : tempPolicyList) { for (Integer i : policyIdList) { @@ -492,7 +496,7 @@ public class PolicyManagerImpl implements PolicyManager { // DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); - List allPolicies = policyDAO.getAllPolicies(); + List allPolicies = this.getPolicies(); for (Profile profile : profileList) { @@ -504,10 +508,10 @@ public class PolicyManagerImpl implements PolicyManager { } } - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting all the policies."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); +// } catch (PolicyManagerDAOException e) { +// String msg = "Error occurred while getting all the policies."; +// log.error(msg, e); +// throw new PolicyManagementException(msg, e); // } catch (ProfileManagerDAOException e) { // String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")"; // log.error(msg, e); @@ -532,7 +536,7 @@ public class PolicyManagerImpl implements PolicyManager { try { policyIdList = policyDAO.getPolicyOfRole(roleName); - List tempPolicyList = policyDAO.getAllPolicies(); + List tempPolicyList = this.getPolicies(); for (Policy policy : tempPolicyList) { for (Integer i : policyIdList) { @@ -558,7 +562,7 @@ public class PolicyManagerImpl implements PolicyManager { try { policyIdList = policyDAO.getPolicyOfUser(username); - List tempPolicyList = policyDAO.getAllPolicies(); + List tempPolicyList = this.getPolicies(); for (Policy policy : tempPolicyList) { for (Integer i : policyIdList) { 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 a2a770dac7..c8c26213b2 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 @@ -19,5 +19,8 @@ package org.wso2.carbon.policy.mgt.core.util; public final class PolicyManagementConstants { + public static final String DEVICE_CONFIG_XML_NAME = "cdm-config.xml"; + public static final String ANY = "ANY"; + } 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 772c1980ec..b6a44bc277 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 @@ -32,13 +32,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.Profile; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; -import org.wso2.carbon.policy.mgt.common.ProfileManagementException; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.common.DBTypes; import org.wso2.carbon.policy.mgt.core.common.TestDBConfiguration; import org.wso2.carbon.policy.mgt.core.common.TestDBConfigurations; @@ -62,6 +56,7 @@ import java.sql.Connection; import java.sql.Statement; import java.util.ArrayList; import java.util.List; +import java.util.Properties; public class PolicyDAOTestCase { @@ -250,6 +245,15 @@ public class PolicyDAOTestCase { policyManager.addPolicy(policy); } + + @Test(dependsOnMethods = ("addPolicyToDevice")) + public void addThirdPolicy() throws PolicyManagementException { + + PolicyManager policyManager = new PolicyManagerImpl(); + policy = PolicyCreator.createPolicy4(profile); + policyManager.addPolicy(policy); + } + @Test(dependsOnMethods = ("addNewPolicy")) public void getPolicies() throws PolicyManagementException { PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); @@ -302,5 +306,66 @@ public class PolicyDAOTestCase { } } + @Test(dependsOnMethods = ("getRoleRelatedPolicy")) + public void addSecondPolicy() throws PolicyManagementException { + PolicyManager policyManager = new PolicyManagerImpl(); + policy = PolicyCreator.createPolicy3(profile); + policyManager.addPolicy(policy); + } + + @Test(dependsOnMethods = ("getDeviceTypeRelatedPolicy")) + public void getRoleRelatedPolicySecondTime() throws PolicyManagementException { + + PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); + List policyList = policyAdministratorPoint.getPoliciesOfRole("Role_01"); + + log.debug("----------Roles related policy second time ---------"); + + for (Policy policy : policyList) { + log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + + List profileFeatures = policy.getProfile().getProfileFeaturesList(); + + for (ProfileFeature profileFeature : profileFeatures) { + log.debug("Feature Content" + profileFeature.getId() + " - " + profileFeature.getContent()); + } + + } + } + + @Test(dependsOnMethods = ("getRoleRelatedPolicySecondTime")) + public void getRoleRelatedPolicyThirdTime() throws PolicyManagementException { + + PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); + List policyList = policyAdministratorPoint.getPoliciesOfRole("Role_02"); + + + log.debug("----------Roles related policy third time ---------"); + + for (Policy policy : policyList) { + log.debug("Policy Id : " + policy.getId() + " Policy Name : " + policy.getPolicyName()); + + List profileFeatures = policy.getProfile().getProfileFeaturesList(); + +// for (ProfileFeature profileFeature : profileFeatures) { +// log.debug("Feature Content" + profileFeature.getId() + " - " + profileFeature.getContent()); +// } + + List criteria = policy.getPolicyCriterias(); + + for (PolicyCriterion criterion : criteria) { + log.debug("Criterias " + criterion.getName() + " -- " + criterion.getCriteriaId() + " -- " + + criterion.getId()); + + Properties prop = criterion.getProperties(); + + for (String key : prop.stringPropertyNames()) { + log.debug("Property Names : " + key + " -- " + prop.getProperty(key)); + } + } + + } + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java index fdf603a0d4..cf68ff1600 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java @@ -19,10 +19,12 @@ package org.wso2.carbon.policy.mgt.core.util; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyCriterion; import org.wso2.carbon.policy.mgt.common.Profile; import java.util.ArrayList; import java.util.List; +import java.util.Properties; public class PolicyCreator { @@ -35,6 +37,7 @@ public class PolicyCreator { List users = new ArrayList(); users.add("Dilshan"); policy.setUsers(users); + policy.setCompliance("ENFORCE"); return policy; } @@ -43,11 +46,13 @@ public class PolicyCreator { public static Policy createPolicy2(Profile profile) { Policy policy = new Policy(); - policy.setPolicyName("New test Policy"); + policy.setPolicyName("Test_Policy_02"); policy.setGeneric(true); policy.setProfile(profile); policy.setDevices(DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType())); + policy.setCompliance("NOTIFY"); + List roles = new ArrayList(); roles.add("Role_01"); roles.add("Role_02"); @@ -63,8 +68,128 @@ public class PolicyCreator { policy.setUsers(users); - policy.setLatitude("6.927079"); - policy.setLongitude("79.861243"); + + PolicyCriterion criterion = new PolicyCriterion(); + + Properties prop = new Properties(); + prop.put("Start_time", "10.00 AM"); + prop.put("End_time", "4.00 PM"); + + criterion.setProperties(prop); + criterion.setName("Time"); + + + List criteria = new ArrayList(); + + criteria.add(criterion); + + policy.setPolicyCriterias(criteria); +// +// policy.setLatitude("6.927079"); +// policy.setLongitude("79.861243"); + +/* DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd "); + java.util.Date date = new java.util.Date(); + + policy.setStartDate(new java.sql.Timestamp(date.getDate()));*/ + + return policy; + } + + + public static Policy createPolicy3(Profile profile) { + Policy policy = new Policy(); + + policy.setPolicyName("Test_Policy_03"); + policy.setGeneric(true); + policy.setProfile(profile); + policy.setDevices(DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType())); + + List roles = new ArrayList(); + roles.add("Role_01"); + roles.add("Role_02"); + + policy.setRoles(roles); + policy.setCompliance("ENFORCE"); + +// List users = new ArrayList(); +// users.add("Geeth"); +// users.add("Manoj"); +// users.add("Milan"); +// users.add("Dulitha"); +// +// policy.setUsers(users); + + + PolicyCriterion criterion = new PolicyCriterion(); + + Properties prop = new Properties(); + prop.put("Start_time", "10.00 AM"); + prop.put("End_time", "4.00 PM"); + + criterion.setProperties(prop); + criterion.setName("Location"); + + + List criteria = new ArrayList(); + + criteria.add(criterion); + + policy.setPolicyCriterias(criteria); + +/* DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd "); + java.util.Date date = new java.util.Date(); + + policy.setStartDate(new java.sql.Timestamp(date.getDate()));*/ + + return policy; + } + + + public static Policy createPolicy4(Profile profile) { + Policy policy = new Policy(); + + policy.setPolicyName("Test_Policy_04"); + policy.setGeneric(true); + policy.setProfile(profile); + policy.setDevices(DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType())); + + policy.setCompliance("MONITOR"); + + List roles = new ArrayList(); + roles.add("Role_04"); + roles.add("Role_05"); + roles.add("Role_02"); + + policy.setRoles(roles); + + List users = new ArrayList(); + users.add("Geeth"); + users.add("Manoj"); + users.add("Milan"); + users.add("Dulitha"); + + policy.setUsers(users); + + + PolicyCriterion criterion = new PolicyCriterion(); + + Properties prop = new Properties(); + prop.put("Start_time", "10.00 AM"); + prop.put("End_time", "4.00 PM"); + + criterion.setProperties(prop); + criterion.setName("LOCATIONGGGGG"); + + + List criteria = new ArrayList(); + + criteria.add(criterion); + + policy.setPolicyCriterias(criteria); + +// policy.setLatitude("6.927079"); +// policy.setLongitude("79.861243"); /* DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd "); java.util.Date date = new java.util.Date(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java index 6785990ee1..9174f48f1c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java @@ -23,25 +23,103 @@ import org.wso2.carbon.policy.mgt.common.ProfileFeature; import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class ProfileFeatureCreator { public static List getProfileFeature(List features) { List profileFeatureList = new ArrayList(); + int i = 0; for (Feature feature : features) { ProfileFeature profileFeature = new ProfileFeature(); - profileFeature.setContent(feature); + if (i % 2 == 0) { + profileFeature.setContent( getJSON()); + } else { + profileFeature.setContent(getJSON2()); + } profileFeature.setDeviceTypeId(1); profileFeature.setFeatureCode(feature.getCode()); // profileFeature.setContent("rrrrrrrrrrrrrrrrrrrrrrrrrrrrr"); - // profileFeature.setProfileId(1); + // profileFeature.setProfileId(1); // profileFeature.setFeature(feature); profileFeatureList.add(profileFeature); + i++; + } return profileFeatureList; } + + + private static String getJSON() { + return "[\n" + + "\t{\n" + + "\t\tcolor: \"red\",\n" + + "\t\tvalue: \"#f00\"\n" + + "\t},\n" + + "\t{\n" + + "\t\tcolor: \"green\",\n" + + "\t\tvalue: \"#0f0\"\n" + + "\t},\n" + + "\t{\n" + + "\t\tcolor: \"blue\",\n" + + "\t\tvalue: \"#00f\"\n" + + "\t},\n" + + "\t{\n" + + "\t\tcolor: \"cyan\",\n" + + "\t\tvalue: \"#0ff\"\n" + + "\t},\n" + + "\t{\n" + + "\t\tcolor: \"magenta\",\n" + + "\t\tvalue: \"#f0f\"\n" + + "\t},\n" + + "\t{\n" + + "\t\tcolor: \"yellow\",\n" + + "\t\tvalue: \"#ff0\"\n" + + "\t},\n" + + "\t{\n" + + "\t\tcolor: \"black\",\n" + + "\t\tvalue: \"#000\"\n" + + "\t}\n" + + "]"; + } + + private static String getJSON2() { + return "{\n" + + " \"odata.metadata\":\"http://services.odata.org/V3/OData/OData.svc/$metadata#Products\",\n" + + " \"value\":[\n" + + " {\n" + + " \"ID\":0,\n" + + " \"Name\":\"Bread\",\n" + + " \"Description\":\"Whole grain bread\",\n" + + " \"ReleaseDate\":\"1992-01-01T00:00:00\",\n" + + " \"DiscontinuedDate\":null,\n" + + " \"Rating\":4,\n" + + " \"Price\":\"2.5\"\n" + + " },\n" + + " {\n" + + " \"ID\":1,\n" + + " \"Name\":\"Milk\",\n" + + " \"Description\":\"Low fat milk\",\n" + + " \"ReleaseDate\":\"1995-10-01T00:00:00\",\n" + + " \"DiscontinuedDate\":null,\n" + + " \"Rating\":3,\n" + + " \"Price\":\"3.5\"\n" + + " },\n" + + " {\n" + + " \"ID\":2,\n" + + " \"Name\":\"Vint soda\",\n" + + " \"Description\":\"Americana Variety - Mix of 6 flavors\",\n" + + " \"ReleaseDate\":\"2000-10-01T00:00:00\",\n" + + " \"DiscontinuedDate\":null,\n" + + " \"Rating\":3,\n" + + " \"Price\":\"20.9\"\n" + + " },\n" + + " …\n" + + " ]\n" + + "}"; + } } 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 ae97ac9a6e..ca2409c8e6 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 @@ -72,6 +72,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( NAME VARCHAR(45) NULL DEFAULT NULL , TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , + COMPLIANCE VARCHAR(100) NULL, PRIORITY INT NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY @@ -172,56 +173,6 @@ CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( ; --- ----------------------------------------------------- --- Table DM_LOCATION --- ----------------------------------------------------- - - -CREATE TABLE IF NOT EXISTS DM_LOCATION ( - LATITUDE VARCHAR(45) NOT NULL , - LONGITUDE VARCHAR(45) NOT NULL , - POLICY_ID INT(11) NOT NULL , - CONSTRAINT FK_DM_POLICY_DM_LOCATION - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) -; - - --- ----------------------------------------------------- --- Table DM_TIME --- ----------------------------------------------------- - - -CREATE TABLE IF NOT EXISTS DM_TIME ( - STARTING_TIME DATETIME NOT NULL , - ENDING_TIME DATETIME NOT NULL , - POLICY_ID INT(11) NOT NULL , - CONSTRAINT FK_DM_POLICY_DM_TIME - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) -; - - --- ----------------------------------------------------- --- Table DM_DATE --- ----------------------------------------------------- - - -CREATE TABLE IF NOT EXISTS DM_DATE ( - START_DATE DATE NOT NULL , - END_DATE DATE NOT NULL , - POLICY_ID INT NOT NULL , - CONSTRAINT DM_DATE_POLICY - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) -; - -- ----------------------------------------------------- -- Table .DM_USER_POLICY @@ -263,3 +214,59 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( ; +-- ----------------------------------------------------- +-- Table DM_CRITERIA +-- ----------------------------------------------------- +DROP TABLE IF EXISTS DM_CRITERIA ; + +CREATE TABLE IF NOT EXISTS DM_CRITERIA ( + ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT NOT NULL, + NAME VARCHAR(50) NULL, + PRIMARY KEY (ID) +); + + +-- ----------------------------------------------------- +-- Table DM_POLICY_CRITERIA +-- ----------------------------------------------------- +DROP TABLE IF EXISTS DM_POLICY_CRITERIA ; + +CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( + ID INT NOT NULL AUTO_INCREMENT, + CRITERIA_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_CRITERIA_POLICY_CRITERIA + FOREIGN KEY (CRITERIA_ID) + REFERENCES DM_CRITERIA (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_POLICY_POLICY_CRITERIA + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + + +-- ----------------------------------------------------- +-- Table DM_POLICY_CRITERIA_PROPERTIES +-- ----------------------------------------------------- +DROP TABLE IF EXISTS DM_POLICY_CRITERIA_PROPERTIES ; + +CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( + ID INT NOT NULL AUTO_INCREMENT, + POLICY_CRITERION_ID INT NOT NULL, + PROP_KEY VARCHAR(45) NULL, + PROP_VALUE VARCHAR(100) NULL, + CONTENT BLOB NULL COMMENT 'This is used to ', + PRIMARY KEY (ID), + CONSTRAINT FK_POLICY_CRITERIA_PROPERTIES + FOREIGN KEY (POLICY_CRITERION_ID) + REFERENCES DM_POLICY_CRITERIA (ID) + ON DELETE CASCADE + 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 e5aa29f0bf..a85059fa83 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 @@ -1,8 +1,19 @@ +-- MySQL Workbench Forward Engineering + SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; -SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; +SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; + +-- ----------------------------------------------------- +-- Schema mydb +-- ----------------------------------------------------- +-- ----------------------------------------------------- +-- Schema WSO2CDM +-- ----------------------------------------------------- -DROP SCHEMA IF EXISTS `WSO2CDM` ; +-- ----------------------------------------------------- +-- Schema WSO2CDM +-- ----------------------------------------------------- CREATE SCHEMA IF NOT EXISTS `WSO2CDM` DEFAULT CHARACTER SET latin1 ; USE `WSO2CDM` ; @@ -11,36 +22,31 @@ USE `WSO2CDM` ; -- ----------------------------------------------------- DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `NAME` VARCHAR(300) NULL DEFAULT NULL , - PRIMARY KEY (`ID`) ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `NAME` VARCHAR(300) NULL DEFAULT NULL, + PRIMARY KEY (`ID`)) ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_DEVICE` +-- Table `WSO2CDM`.`DM_PROFILE` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_PROFILE` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `DESCRIPTION` TEXT NULL DEFAULT NULL , - `NAME` VARCHAR(100) NULL DEFAULT NULL , - `DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL , - `DATE_OF_LAST_UPDATE` BIGINT(20) NULL DEFAULT NULL , - `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL , - `STATUS` VARCHAR(15) NULL DEFAULT NULL , - `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , - `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , - `OWNER` VARCHAR(45) NULL DEFAULT NULL , - `TENANT_ID` INT(11) NULL DEFAULT '0' , - PRIMARY KEY (`ID`) , - INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2` (`DEVICE_TYPE_ID` ASC) , - CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` - FOREIGN KEY (`DEVICE_TYPE_ID` ) - REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `PROFILE_NAME` VARCHAR(45) NOT NULL, + `TENANT_ID` INT(11) NOT NULL, + `DEVICE_TYPE_ID` INT(11) NOT NULL, + `CREATED_TIME` DATETIME NOT NULL, + `UPDATED_TIME` DATETIME NOT NULL, + PRIMARY KEY (`ID`), + INDEX `DM_PROFILE_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC), + CONSTRAINT `DM_PROFILE_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID`) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB @@ -48,43 +54,41 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_PROFILE` +-- Table `WSO2CDM`.`DM_POLICY` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_PROFILE` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_POLICY` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE` ( - `ID` INT NOT NULL AUTO_INCREMENT , - `PROFILE_NAME` VARCHAR(45) NOT NULL , - `TENANT_ID` INT NOT NULL , - `DEVICE_TYPE_ID` INT NOT NULL , - `CREATED_TIME` DATETIME NOT NULL , - `UPDATED_TIME` DATETIME NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `DM_PROFILE_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , - CONSTRAINT `DM_PROFILE_DEVICE_TYPE` - FOREIGN KEY (`DEVICE_TYPE_ID` ) - REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `NAME` VARCHAR(45) NULL DEFAULT NULL, + `TENANT_ID` INT(11) NOT NULL, + `PROFILE_ID` INT(11) NOT NULL, + `COMPLIANCE` VARCHAR(100) NULL, + `PRIORITY` INT(11) NOT NULL, + PRIMARY KEY (`ID`), + INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC), + CONSTRAINT `FK_DM_PROFILE_DM_POLICY` + FOREIGN KEY (`PROFILE_ID`) + REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) -ENGINE = InnoDB; +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_POLICY` +-- Table `WSO2CDM`.`DM_DATE` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_POLICY` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DATE` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `NAME` VARCHAR(45) NULL DEFAULT NULL , - `TENANT_ID` INT(11) NOT NULL , - `PROFILE_ID` INT(11) NOT NULL , - `PRIORITY` INT NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC) , - CONSTRAINT `FK_DM_PROFILE_DM_POLICY` - FOREIGN KEY (`PROFILE_ID` ) - REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DATE` ( + `START_DATE` DATE NOT NULL, + `END_DATE` DATE NOT NULL, + `POLICY_ID` INT(11) NOT NULL, + INDEX `DM_DATE_POLICY` (`POLICY_ID` ASC), + CONSTRAINT `DM_DATE_POLICY` + FOREIGN KEY (`POLICY_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB @@ -92,25 +96,27 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_DEVICE_POLICY` +-- Table `WSO2CDM`.`DM_DEVICE` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `DEVICE_ID` INT(11) NOT NULL , - `POLICY_ID` INT(11) NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `FK_POLICY_DEVICE_POLICY` (`POLICY_ID` ASC) , - INDEX `FK_DEVICE_DEVICE_POLICY` (`DEVICE_ID` ASC) , - CONSTRAINT `FK_POLICY_DEVICE_POLICY` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `FK_DEVICE_DEVICE_POLICY` - FOREIGN KEY (`DEVICE_ID` ) - REFERENCES `WSO2CDM`.`DM_DEVICE` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `DESCRIPTION` TEXT NULL DEFAULT NULL, + `NAME` VARCHAR(100) NULL DEFAULT NULL, + `DATE_OF_ENROLLMENT` BIGINT(20) NULL DEFAULT NULL, + `DATE_OF_LAST_UPDATE` BIGINT(20) NULL DEFAULT NULL, + `OWNERSHIP` VARCHAR(45) NULL DEFAULT NULL, + `STATUS` VARCHAR(15) NULL DEFAULT NULL, + `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL, + `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL, + `OWNER` VARCHAR(45) NULL DEFAULT NULL, + `TENANT_ID` INT(11) NULL DEFAULT '0', + PRIMARY KEY (`ID`), + INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2` (`DEVICE_TYPE_ID` ASC), + CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` + FOREIGN KEY (`DEVICE_TYPE_ID`) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB @@ -118,25 +124,25 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` +-- Table `WSO2CDM`.`DM_DEVICE_POLICY` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_TYPE_POLICY` ( - `ID` INT(11) NOT NULL , - `DEVICE_TYPE_ID` INT(11) NOT NULL , - `POLICY_ID` INT(11) NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `FK_DEVICE_TYPE_POLICY` (`POLICY_ID` ASC) , - INDEX `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , - CONSTRAINT `FK_DEVICE_TYPE_POLICY` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `DEVICE_ID` INT(11) NOT NULL, + `POLICY_ID` INT(11) NOT NULL, + PRIMARY KEY (`ID`), + INDEX `FK_POLICY_DEVICE_POLICY` (`POLICY_ID` ASC), + INDEX `FK_DEVICE_DEVICE_POLICY` (`DEVICE_ID` ASC), + CONSTRAINT `FK_DEVICE_DEVICE_POLICY` + FOREIGN KEY (`DEVICE_ID`) + REFERENCES `WSO2CDM`.`DM_DEVICE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT `FK_DEVICE_TYPE_POLICY_DEVICE_TYPE` - FOREIGN KEY (`DEVICE_TYPE_ID` ) - REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + CONSTRAINT `FK_POLICY_DEVICE_POLICY` + FOREIGN KEY (`POLICY_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB @@ -144,49 +150,53 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_FEATURES` +-- Table `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_FEATURES` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `NAME` VARCHAR(256) NOT NULL , - `CODE` VARCHAR(45) NULL DEFAULT NULL , - `DEVICE_TYPE_ID` INT NOT NULL , - `DESCRIPTION` TEXT NULL DEFAULT NULL , - `EVALUATION_RULE` VARCHAR(60) NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `DM_FEATURES_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , - CONSTRAINT `DM_FEATURES_DEVICE_TYPE` - FOREIGN KEY (`DEVICE_TYPE_ID` ) - REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `DEVICE_ID` INT(11) NOT NULL, + `POLICY_ID` INT(11) NOT NULL, + `POLICY_CONTENT` BLOB NULL DEFAULT NULL, + `APPLIED` TINYINT(1) NULL DEFAULT NULL, + `CREATED_TIME` TIMESTAMP NULL DEFAULT NULL, + `UPDATED_TIME` TIMESTAMP NULL DEFAULT NULL, + `APPLIED_TIME` TIMESTAMP NULL DEFAULT NULL, + PRIMARY KEY (`ID`), + INDEX `FK_DM_POLICY_DEVCIE_APPLIED` (`DEVICE_ID` ASC), + INDEX `FK_DM_POLICY_DEVICE_APPLIED_POLICY` (`POLICY_ID` ASC), + CONSTRAINT `FK_DM_POLICY_DEVCIE_APPLIED` + FOREIGN KEY (`DEVICE_ID`) + REFERENCES `WSO2CDM`.`DM_DEVICE` (`ID`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_DM_POLICY_DEVICE_APPLIED_POLICY` + FOREIGN KEY (`POLICY_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; + -- ----------------------------------------------------- -- Table `WSO2CDM`.`DM_PROFILE_FEATURES` -- ----------------------------------------------------- DROP TABLE IF EXISTS `WSO2CDM`.`DM_PROFILE_FEATURES` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE_FEATURES` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `PROFILE_ID` INT(11) NOT NULL , - `FEATURE_ID` INT(11) NOT NULL , - `CONTENT` BLOB NULL DEFAULT NULL , - PRIMARY KEY (`ID`) , - INDEX `fk_DM_POLICY_FEATURES_DM_FEATURES1` (`FEATURE_ID` ASC) , - INDEX `FK_DM_PROFILE_DM_POLICY_FEATURES` (`PROFILE_ID` ASC) , - CONSTRAINT `fk_DM_POLICY_FEATURES_DM_FEATURES1` - FOREIGN KEY (`FEATURE_ID` ) - REFERENCES `WSO2CDM`.`DM_FEATURES` (`ID` ) - ON DELETE NO ACTION - ON UPDATE NO ACTION, +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_PROFILE_FEATURES` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `PROFILE_ID` INT(11) NOT NULL, + `FEATURE_CODE` VARCHAR(10) NOT NULL, + `DEVICE_TYPE_ID` INT NOT NULL, + `CONTENT` BLOB NULL DEFAULT NULL, + PRIMARY KEY (`ID`), + INDEX `FK_DM_PROFILE_DM_POLICY_FEATURES` (`PROFILE_ID` ASC), CONSTRAINT `FK_DM_PROFILE_DM_POLICY_FEATURES` - FOREIGN KEY (`PROFILE_ID` ) - REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID` ) + FOREIGN KEY (`PROFILE_ID`) + REFERENCES `WSO2CDM`.`DM_PROFILE` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB @@ -198,125 +208,101 @@ DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- DROP TABLE IF EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ( - `ID` INT(11) NOT NULL AUTO_INCREMENT , - `ROLE_NAME` VARCHAR(45) NOT NULL , - `POLICY_ID` INT(11) NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `FK_ROLE_POLICY_POLICY` (`POLICY_ID` ASC) , +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_ROLE_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `ROLE_NAME` VARCHAR(45) NOT NULL, + `POLICY_ID` INT(11) NOT NULL, + PRIMARY KEY (`ID`), + INDEX `FK_ROLE_POLICY_POLICY` (`POLICY_ID` ASC), CONSTRAINT `FK_ROLE_POLICY_POLICY` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) + FOREIGN KEY (`POLICY_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; --- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_LOCATION` --- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_LOCATION` ; - -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_LOCATION` ( - `LATITUDE` VARCHAR(45) NOT NULL , - `LONGITUDE` VARCHAR(45) NOT NULL , - `POLICY_ID` INT(11) NOT NULL , - INDEX `FK_DM_POLICY_DM_LOCATION` (`POLICY_ID` ASC) , - CONSTRAINT `FK_DM_POLICY_DM_LOCATION` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) -ENGINE = InnoDB; - -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_TIME` +-- Table `WSO2CDM`.`DM_USER_POLICY` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_TIME` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_USER_POLICY` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_TIME` ( - `STARTING_TIME` DATETIME NOT NULL , - `ENDING_TIME` DATETIME NOT NULL , - `POLICY_ID` INT(11) NOT NULL , - INDEX `FK_DM_POLICY_DM_TIME` (`POLICY_ID` ASC) , - CONSTRAINT `FK_DM_POLICY_DM_TIME` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_USER_POLICY` ( + `ID` INT(11) NOT NULL AUTO_INCREMENT, + `POLICY_ID` INT(11) NOT NULL, + `USERNAME` VARCHAR(45) NOT NULL, + PRIMARY KEY (`ID`), + INDEX `DM_POLICY_USER_POLICY` (`POLICY_ID` ASC), + CONSTRAINT `DM_POLICY_USER_POLICY` + FOREIGN KEY (`POLICY_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) -ENGINE = InnoDB; +ENGINE = InnoDB +DEFAULT CHARACTER SET = latin1; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_DATE` +-- Table `WSO2CDM`.`DM_CRITERIA` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_DATE` ; +DROP TABLE IF EXISTS `WSO2CDM`.`DM_CRITERIA` ; -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DATE` ( - `START_DATE` DATE NOT NULL , - `END_DATE` DATE NOT NULL , - `POLICY_ID` INT NOT NULL , - INDEX `DM_DATE_POLICY` (`POLICY_ID` ASC) , - CONSTRAINT `DM_DATE_POLICY` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_CRITERIA` ( + `ID` INT NOT NULL AUTO_INCREMENT, + `TENANT_ID` INT NOT NULL, + `NAME` VARCHAR(50) NULL, + PRIMARY KEY (`ID`)) ENGINE = InnoDB; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_USER_POLICY` +-- Table `WSO2CDM`.`DM_POLICY_CRITERIA` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_USER_POLICY` ; - -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_USER_POLICY` ( - `ID` INT NOT NULL AUTO_INCREMENT , - `POLICY_ID` INT NOT NULL , - `USERNAME` VARCHAR(45) NOT NULL , - PRIMARY KEY (`ID`) , - INDEX `DM_POLICY_USER_POLICY` (`POLICY_ID` ASC) , - CONSTRAINT `DM_POLICY_USER_POLICY` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) +DROP TABLE IF EXISTS `WSO2CDM`.`DM_POLICY_CRITERIA` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CRITERIA` ( + `ID` INT NOT NULL AUTO_INCREMENT, + `CRITERIA_ID` INT NOT NULL, + `POLICY_ID` INT NOT NULL, + PRIMARY KEY (`ID`), + INDEX `FK_CRITERIA_POLICY_CRITERIA_idx` (`CRITERIA_ID` ASC), + INDEX `FK_POLICY_POLICY_CRITERIA_idx` (`POLICY_ID` ASC), + CONSTRAINT `FK_CRITERIA_POLICY_CRITERIA` + FOREIGN KEY (`CRITERIA_ID`) + REFERENCES `WSO2CDM`.`DM_CRITERIA` (`ID`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT `FK_POLICY_POLICY_CRITERIA` + FOREIGN KEY (`POLICY_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB; -- ----------------------------------------------------- --- Table `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` +-- Table `WSO2CDM`.`DM_POLICY_CRITERIA_PROPERTIES` -- ----------------------------------------------------- -DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` ; - -CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` ( - `ID` INT NOT NULL AUTO_INCREMENT , - `DEVICE_ID` INT NOT NULL , - `POLICY_ID` INT NOT NULL , - `POLICY_CONTENT` BLOB NULL , - `APPLIED` TINYINT(1) NULL , - `CREATED_TIME` TIMESTAMP NULL , - `UPDATED_TIME` TIMESTAMP NULL , - `APPLIED_TIME` TIMESTAMP NULL , - PRIMARY KEY (`ID`) , - INDEX `FK_DM_POLICY_DEVCIE_APPLIED` (`DEVICE_ID` ASC) , - INDEX `FK_DM_POLICY_DEVICE_APPLIED_POLICY` (`POLICY_ID` ASC) , - CONSTRAINT `FK_DM_POLICY_DEVCIE_APPLIED` - FOREIGN KEY (`DEVICE_ID` ) - REFERENCES `WSO2CDM`.`DM_DEVICE` (`ID` ) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT `FK_DM_POLICY_DEVICE_APPLIED_POLICY` - FOREIGN KEY (`POLICY_ID` ) - REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` ) - ON DELETE NO ACTION +DROP TABLE IF EXISTS `WSO2CDM`.`DM_POLICY_CRITERIA_PROPERTIES` ; + +CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CRITERIA_PROPERTIES` ( + `ID` INT NOT NULL AUTO_INCREMENT, + `POLICY_CRITERION_ID` INT NOT NULL, + `PROP_KEY` VARCHAR(45) NULL, + `PROP_VALUE` VARCHAR(100) NULL, + `CONTENT` BLOB NULL COMMENT 'This is used to ', + PRIMARY KEY (`ID`), + INDEX `FK_POLICY_CRITERIA_PROPERTIES_idx` (`POLICY_CRITERION_ID` ASC), + CONSTRAINT `FK_POLICY_CRITERIA_PROPERTIES` + FOREIGN KEY (`POLICY_CRITERION_ID`) + REFERENCES `WSO2CDM`.`DM_POLICY_CRITERIA` (`ID`) + ON DELETE CASCADE ON UPDATE NO ACTION) ENGINE = InnoDB; - SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; diff --git a/pom.xml b/pom.xml index 087a6647e3..0cbacff5b1 100644 --- a/pom.xml +++ b/pom.xml @@ -1081,7 +1081,7 @@ wso2-staging WSO2 Staging Repository - http://maven.wso2.org/nexus/content/repositories/orgwso2carbonapimgt-004/ + http://maven.wso2.org/nexus/content/repositories/orgwso2carbonapimgt-009/ true daily From 3589804ede179af57591596495031f8326268727 Mon Sep 17 00:00:00 2001 From: manoj Date: Mon, 18 May 2015 02:31:10 +0530 Subject: [PATCH 5/7] Installation APP service --- .../mgt/common/app/mgt/AppManagerConnector.java | 1 + .../device/mgt/common/spi/DeviceManager.java | 7 ++----- .../DeviceManagementServiceProviderImpl.java | 4 ++-- .../core/app/mgt/AppManagementServiceImpl.java | 17 ++++++++++++++--- .../service/DeviceManagementServiceImpl.java | 5 +++-- .../device/mgt/core/TestDeviceManager.java | 2 +- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/AppManagerConnector.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/AppManagerConnector.java index 7a0b0ead5a..07ab27e61a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/AppManagerConnector.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/AppManagerConnector.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt; import org.wso2.carbon.device.mgt.common.Application; import org.wso2.carbon.device.mgt.common.Credential; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import java.util.List; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java index b90d04f550..aeb84c669e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManager.java @@ -18,10 +18,7 @@ package org.wso2.carbon.device.mgt.common.spi; -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.FeatureManager; +import org.wso2.carbon.device.mgt.common.*; import java.util.List; @@ -124,7 +121,7 @@ public interface DeviceManager { * @param device Updated device information related data * @throws DeviceManagementException If some unusual behaviour is observed while updating the device info */ - boolean updateDeviceInfo(Device device) throws DeviceManagementException; + boolean updateDeviceInfo(Device device, List applicationList) throws DeviceManagementException; /** * Method to set the ownership type of a particular device. i.e. BYOD, COPE. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index de39d9fbf3..71855742e0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -378,10 +378,10 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + public boolean updateDeviceInfo(Device device, List applicationList) throws DeviceManagementException { DeviceManager dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); - return dms.updateDeviceInfo(device); + return dms.updateDeviceInfo(device,applicationList); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java index ca774b901b..170134ee0d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/AppManagementServiceImpl.java @@ -17,18 +17,21 @@ */ package org.wso2.carbon.device.mgt.core.app.mgt; -import org.wso2.carbon.device.mgt.common.Application; -import org.wso2.carbon.device.mgt.common.Credential; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.AppManagerConnector; import org.wso2.carbon.device.mgt.common.app.mgt.AppManagerConnectorException; 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.internal.DeviceManagementDataHolder; +import java.util.ArrayList; import java.util.List; public class AppManagementServiceImpl implements AppManagerConnector { + private static final Log log = LogFactory.getLog(AppManagementServiceImpl.class); @Override public Application[] getApplicationList(String domain, int pageNumber, int size) throws AppManagerConnectorException { return DeviceManagementDataHolder.getInstance().getAppManager().getApplicationList(domain, pageNumber, size); @@ -55,6 +58,14 @@ public class AppManagementServiceImpl implements AppManagerConnector { @Override public void installApplication(Operation operation, List deviceIdentifiers) throws AppManagerConnectorException { + try { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().addOperation(operation, + deviceIdentifiers); + } catch (OperationManagementException opMgtEx) { + String errorMsg = "Error occurred when add operations at install application"; + log.error(errorMsg, opMgtEx); + throw new AppManagerConnectorException(); + } DeviceManagementDataHolder.getInstance().getAppManager().installApplication(operation, deviceIdentifiers); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 582fbe7214..56cf5d5a12 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -98,8 +98,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().updateDeviceInfo(device); + public boolean updateDeviceInfo(Device device, List applicationList) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + updateDeviceInfo(device, applicationList); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index f0d64f389a..24bd9f77f3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -79,7 +79,7 @@ public class TestDeviceManager implements DeviceMgtService { } @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + public boolean updateDeviceInfo(Device device, sList applicationList) throws DeviceManagementException { return false; } From 2d5d0bb4f864b160393e0399174b85528ab36558 Mon Sep 17 00:00:00 2001 From: manoj Date: Mon, 18 May 2015 02:52:38 +0530 Subject: [PATCH 6/7] Install APP service --- .../java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index 24bd9f77f3..ea8ad2e2f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -79,7 +79,7 @@ public class TestDeviceManager implements DeviceMgtService { } @Override - public boolean updateDeviceInfo(Device device, sList applicationList) throws DeviceManagementException { + public boolean updateDeviceInfo(Device device, List applicationList) throws DeviceManagementException { return false; } From 10a585ea6a04c155fd9a9cca2785b6f470990d7b Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 18 May 2015 07:38:35 +0530 Subject: [PATCH 7/7] Moing interfacs to common bundle and fixing test cases --- .../policy/mgt/common}/PolicyFilter.java | 2 +- .../mgt/core/impl/PolicyFilterImpl.java | 1 + .../core/impl/PolicyInformationPointImpl.java | 6 +- .../mgt/core/util/ProfileFeatureCreator.java | 119 +++++++++--------- 4 files changed, 61 insertions(+), 67 deletions(-) rename components/policy-mgt/{org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl => org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common}/PolicyFilter.java (95%) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java similarity index 95% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java index 473476403e..3e8cf71756 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilter.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyFilter.java @@ -17,7 +17,7 @@ */ -package org.wso2.carbon.policy.mgt.core.impl; +package org.wso2.carbon.policy.mgt.common; import org.wso2.carbon.policy.mgt.common.Policy; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index cef8cba786..5c0abca774 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -22,6 +22,7 @@ package org.wso2.carbon.policy.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyFilter; import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import java.util.ArrayList; 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 50e12eb8b7..2addda1a81 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 @@ -27,11 +27,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.common.PIPDevice; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java index 9174f48f1c..c3f99a3258 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileFeatureCreator.java @@ -55,71 +55,68 @@ public class ProfileFeatureCreator { private static String getJSON() { - return "[\n" + - "\t{\n" + - "\t\tcolor: \"red\",\n" + - "\t\tvalue: \"#f00\"\n" + - "\t},\n" + - "\t{\n" + - "\t\tcolor: \"green\",\n" + - "\t\tvalue: \"#0f0\"\n" + - "\t},\n" + - "\t{\n" + - "\t\tcolor: \"blue\",\n" + - "\t\tvalue: \"#00f\"\n" + - "\t},\n" + - "\t{\n" + - "\t\tcolor: \"cyan\",\n" + - "\t\tvalue: \"#0ff\"\n" + - "\t},\n" + - "\t{\n" + - "\t\tcolor: \"magenta\",\n" + - "\t\tvalue: \"#f0f\"\n" + - "\t},\n" + - "\t{\n" + - "\t\tcolor: \"yellow\",\n" + - "\t\tvalue: \"#ff0\"\n" + - "\t},\n" + - "\t{\n" + - "\t\tcolor: \"black\",\n" + - "\t\tvalue: \"#000\"\n" + - "\t}\n" + - "]"; + return "{\n" + + " \"userNameList\": [\n" + + " \"admin\"\n" + + " ],\n" + + " \"roleNameList\": [\n" + + " \"admin\"\n" + + " ],\n" + + " \"deviceIdentifiers\": [\n" + + " {\n" + + " \"id\": \"08:00:27:fe:27:7b\",\n" + + " \"type\": \"ios\"\n" + + " }\n" + + " ],\n" + + " \"application\": {\n" + + " \"id\": \"id\",\n" + + " \"name\": \"test\",\n" + + " \"type\": \"ENTERPRISE\",\n" + + " \"platform\": \"android\",\n" + + " \"version\": \"1.0\",\n" + + " \"identifier\": \"sdfsdfldfs\",\n" + + " \"iconImage\": \"http://gogle.com\",\n" + + " \"packageName\": \"com.google.mail\",\n" + + " \"appIdentifier\": \"asdf\",\n" + + " \"location\": \"location\",\n" + + " \"properties\": {\n" + + " \"isRemoveApp\": true,\n" + + " \"isPreventBackup\": true\n" + + " }\n" + + " }\n" + + "}"; } private static String getJSON2() { return "{\n" + - " \"odata.metadata\":\"http://services.odata.org/V3/OData/OData.svc/$metadata#Products\",\n" + - " \"value\":[\n" + - " {\n" + - " \"ID\":0,\n" + - " \"Name\":\"Bread\",\n" + - " \"Description\":\"Whole grain bread\",\n" + - " \"ReleaseDate\":\"1992-01-01T00:00:00\",\n" + - " \"DiscontinuedDate\":null,\n" + - " \"Rating\":4,\n" + - " \"Price\":\"2.5\"\n" + - " },\n" + - " {\n" + - " \"ID\":1,\n" + - " \"Name\":\"Milk\",\n" + - " \"Description\":\"Low fat milk\",\n" + - " \"ReleaseDate\":\"1995-10-01T00:00:00\",\n" + - " \"DiscontinuedDate\":null,\n" + - " \"Rating\":3,\n" + - " \"Price\":\"3.5\"\n" + - " },\n" + - " {\n" + - " \"ID\":2,\n" + - " \"Name\":\"Vint soda\",\n" + - " \"Description\":\"Americana Variety - Mix of 6 flavors\",\n" + - " \"ReleaseDate\":\"2000-10-01T00:00:00\",\n" + - " \"DiscontinuedDate\":null,\n" + - " \"Rating\":3,\n" + - " \"Price\":\"20.9\"\n" + - " },\n" + - " …\n" + - " ]\n" + + " \"userNameList\": [\n" + + " \"admin\"\n" + + " ],\n" + + " \"roleNameList\": [\n" + + " \"admin\"\n" + + " ],\n" + + " \"deviceIdentifiers\": [\n" + + " {\n" + + " \"id\": \"11:11:11:12\",\n" + + " \"type\": \"ios\"\n" + + " }\n" + + " ],\n" + + " \"application\": {\n" + + " \"id\": \"1d548206-14ee-4672-91f6-9c230626a056\",\n" + + " \"platform\": \"ios\",\n" + + " \"packageName\": \"com.imangi.templerun2\",\n" + + " \"name\": \"Temle Run\",\n" + + " \"appIdentifier\": \"572395608\",\n" + + " \"iconImage\": \"http://10.100.5.6:9763/publisher/api/mobileapp/getfile/FHmJReGEV3cExtf.png\",\n" + + " \"type\": \"PUBLIC\",\n" + + " \"identifier\": \"572395608\",\n" + + " \"version\": \"1\",\n" + + " \"properties\": {\n" + + " \"isRemoveApp\": true,\n" + + " \"isPreventBackup\": true,\n" + + " \"iTunesId\": 572395608\n" + + " }\n" + + " }\n" + "}"; } }