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 4d034a32ea6..a39f03935a3 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 bb474258e75..c1a8481414a 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 6a8e582c207..9c677b72e9a 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 7ae2371c373..ca4de5a65f9 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 27edad84f50..e00e6c1c43a 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 811a7f087c2..72bc9c59431 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); }