Refactored Policy Management DAO classes

revert-70aa11f8
Dileesha Rajapakse 9 years ago
parent 91d9db1ffa
commit 6446be100d

@ -39,6 +39,8 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
public int addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
int operationId; int operationId;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try { try {
operationId = super.addOperation(operation); operationId = super.addOperation(operation);
operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString());
@ -48,12 +50,33 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)"); "VALUES(?, ?)");
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(operation);
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setObject(2, policyOperation); stmt.setBytes(2, bao.toByteArray());
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding policy operation", e); throw new OperationManagementDAOException("Error occurred while adding policy operation", e);
} catch (IOException e) {
throw new OperationManagementDAOException("Error occurred while serializing policy operation object", e);
} finally { } finally {
if (bao != null) {
try {
bao.close();
} catch (IOException e) {
log.warn("Error occurred while closing ByteArrayOutputStream", e);
}
}
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
OperationManagementDAOUtil.cleanupResources(stmt); OperationManagementDAOUtil.cleanupResources(stmt);
} }
return operationId; return operationId;

@ -46,7 +46,7 @@ public interface MonitoringDAO {
void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException; void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException;
void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature> void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures) complianceFeatures)
throws MonitoringDAOException; throws MonitoringDAOException;

@ -67,7 +67,7 @@ public class FeatureDAOImpl implements FeatureDAO {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " +
"TENANT_ID) VALUES (?, ?, ?, ?, ?)"; "TENANT_ID) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, new String[] {"id"});
for (ProfileFeature feature : features) { for (ProfileFeature feature : features) {
stmt.setInt(1, profileId); stmt.setInt(1, profileId);
@ -113,13 +113,9 @@ public class FeatureDAOImpl implements FeatureDAO {
String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? AND FEATURE_CODE = ? AND" + String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? AND FEATURE_CODE = ? AND" +
" TENANT_ID = ?"; " TENANT_ID = ?";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query);
for (ProfileFeature feature : features) { for (ProfileFeature feature : features) {
if (conn.getMetaData().getDriverName().contains("H2")) {
stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent()));
} else {
stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent())); stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent()));
}
stmt.setInt(2, profileId); stmt.setInt(2, profileId);
stmt.setString(3, feature.getFeatureCode()); stmt.setString(3, feature.getFeatureCode());
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);

@ -206,7 +206,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} }
@Override @Override
public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature> public void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures) throws MonitoringDAOException { complianceFeatures) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -215,7 +215,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " + String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " +
"TENANT_ID) VALUES (?, ?, ?, ?) "; "TENANT_ID) VALUES (?, ?, ?, ?) ";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query);
for (ComplianceFeature feature : complianceFeatures) { for (ComplianceFeature feature : complianceFeatures) {
stmt.setInt(1, policyComplianceStatusId); stmt.setInt(1, policyComplianceStatusId);
stmt.setString(2, feature.getFeatureCode()); stmt.setString(2, feature.getFeatureCode());

@ -442,7 +442,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_CRITERIA (TENANT_ID, NAME) VALUES (?, ?)"; String query = "INSERT INTO DM_CRITERIA (TENANT_ID, NAME) VALUES (?, ?)";
stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, new String[] {"id"});
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setString(2, criteria.getName()); stmt.setString(2, criteria.getName());
stmt.executeUpdate(); stmt.executeUpdate();
@ -622,7 +622,7 @@ public class PolicyDAOImpl implements PolicyDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_CRITERIA (CRITERIA_ID, POLICY_ID) VALUES (?, ?)"; String query = "INSERT INTO DM_POLICY_CRITERIA (CRITERIA_ID, POLICY_ID) VALUES (?, ?)";
stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, new String[] {"id"});
List<PolicyCriterion> criteria = policy.getPolicyCriterias(); List<PolicyCriterion> criteria = policy.getPolicyCriterias();
for (PolicyCriterion criterion : criteria) { for (PolicyCriterion criterion : criteria) {
@ -1348,7 +1348,7 @@ public class PolicyDAOImpl implements PolicyDAO {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," +
"UPDATED, ACTIVE, DESCRIPTION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; "UPDATED, ACTIVE, DESCRIPTION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, new String[] {"id"});
stmt.setString(1, policy.getPolicyName()); stmt.setString(1, policy.getPolicyName());
stmt.setInt(2, policy.getProfile().getProfileId()); stmt.setInt(2, policy.getProfile().getProfileId());

@ -51,7 +51,7 @@ public class ProfileDAOImpl implements ProfileDAO {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE " + String query = "INSERT INTO DM_PROFILE " +
"(PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; "(PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, new String[] {"id"});
stmt.setString(1, profile.getProfileName()); stmt.setString(1, profile.getProfileName());
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);

@ -120,7 +120,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Compliance status primary key " + complianceData.getId()); log.debug("Compliance status primary key " + complianceData.getId());
} }
monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(),
complianceFeatures); complianceFeatures);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();

Loading…
Cancel
Save