From 6446be100d7810e524709acf7ae8c1366ee39a92 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Mon, 16 Nov 2015 16:49:35 +0530 Subject: [PATCH] Refactored Policy Management DAO classes --- .../mgt/dao/impl/PolicyOperationDAOImpl.java | 25 ++++++++++++++++++- .../policy/mgt/core/dao/MonitoringDAO.java | 2 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 10 +++----- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 4 +-- .../mgt/core/dao/impl/PolicyDAOImpl.java | 6 ++--- .../mgt/core/dao/impl/ProfileDAOImpl.java | 2 +- .../core/mgt/impl/MonitoringManagerImpl.java | 2 +- 7 files changed, 35 insertions(+), 16 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 07c6112bd6..1bff85ae61 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -39,6 +39,8 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { public int addOperation(Operation operation) throws OperationManagementDAOException { int operationId; PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; try { operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); @@ -48,12 +50,33 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(operation); + stmt.setInt(1, operationId); - stmt.setObject(2, policyOperation); + stmt.setBytes(2, bao.toByteArray()); stmt.executeUpdate(); } catch (SQLException 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 { + 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); } return operationId; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java index 522b20f370..a7f42df06c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -46,7 +46,7 @@ public interface MonitoringDAO { void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException; - void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) throws MonitoringDAOException; 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 9688d1c53d..0af516ac70 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 @@ -67,7 +67,7 @@ public class FeatureDAOImpl implements FeatureDAO { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query, new String[] {"id"}); for (ProfileFeature feature : features) { 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" + " TENANT_ID = ?"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query); 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.setString(3, feature.getFeatureCode()); stmt.setInt(4, tenantId); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 54d0ff0198..20dac46f78 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -206,7 +206,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + public void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; @@ -215,7 +215,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " + "TENANT_ID) VALUES (?, ?, ?, ?) "; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query); for (ComplianceFeature feature : complianceFeatures) { stmt.setInt(1, policyComplianceStatusId); stmt.setString(2, feature.getFeatureCode()); 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 33a56b6623..c29c17f832 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 @@ -442,7 +442,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); 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.setString(2, criteria.getName()); stmt.executeUpdate(); @@ -622,7 +622,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); 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 criteria = policy.getPolicyCriterias(); for (PolicyCriterion criterion : criteria) { @@ -1348,7 +1348,7 @@ public class PolicyDAOImpl implements PolicyDAO { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," + "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.setInt(2, policy.getProfile().getProfileId()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index fc10f61ee1..371c545b35 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -51,7 +51,7 @@ public class ProfileDAOImpl implements ProfileDAO { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE " + "(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.setInt(2, tenantId); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index f6d14166a4..1d1844aed2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -120,7 +120,7 @@ public class MonitoringManagerImpl implements MonitoringManager { if (log.isDebugEnabled()) { log.debug("Compliance status primary key " + complianceData.getId()); } - monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), + monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures); PolicyManagementDAOFactory.commitTransaction();