revert-70aa11f8
Dilshan Edirisuriya 10 years ago
commit fc0dc162ef

@ -34,6 +34,8 @@ import java.util.Map;
@XmlRootElement
public class Policy implements Comparable<Policy>, 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<Policy>, Serializable {
/* This is related criteria based policy */
private List<PolicyCriteria> policyCriterias;
private List<PolicyCriterion> policyCriterias;
/*These are related to time based policies*/
@ -161,11 +163,11 @@ public class Policy implements Comparable<Policy>, Serializable {
}
@XmlElement
public List<PolicyCriteria> getPolicyCriterias() {
public List<PolicyCriterion> getPolicyCriterias() {
return policyCriterias;
}
public void setPolicyCriterias(List<PolicyCriteria> policyCriterias) {
public void setPolicyCriterias(List<PolicyCriterion> policyCriterias) {
this.policyCriterias = policyCriterias;
}

@ -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<String, Object> 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;
}

@ -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;
}

@ -58,7 +58,9 @@ public interface PolicyDAO {
Policy addPolicyCriteria(Policy policy) throws PolicyManagerDAOException;
List<PolicyCriteria> getPolicyCriteria(int policyId) throws PolicyManagerDAOException;
boolean addPolicyCriteriaProperties(List<PolicyCriterion> policyCriteria) throws PolicyManagerDAOException;
List<PolicyCriterion> getPolicyCriteria(int policyId) throws PolicyManagerDAOException;
Policy updatePolicy(Policy policy) throws PolicyManagerDAOException;

@ -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<Criterion> getAllPolicyCriteria() throws PolicyManagerDAOException {
return null;
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Criterion> criteria = new ArrayList<Criterion>();
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<PolicyCriterion> 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<PolicyCriteria> getPolicyCriteria(int policyId) throws PolicyManagerDAOException {
return null;
public boolean addPolicyCriteriaProperties(List<PolicyCriterion> 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<PolicyCriterion> getPolicyCriteria(int policyId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<PolicyCriterion> criteria = new ArrayList<PolicyCriterion>();
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.");
}

@ -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<PolicyCriterion> 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<PolicyCriterion> 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);
}

Loading…
Cancel
Save