code cleanup

revert-70aa11f8
prabathabey 9 years ago
parent cd31662a96
commit 2311572d49

@ -40,7 +40,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public int addComplianceDetails(int deviceId, int policyId) throws MonitoringDAOException { public int addComplianceDetails(int deviceId, int policyId) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet generatedKeys = null; ResultSet generatedKeys = null;
@ -65,11 +64,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} else { } else {
return 0; return 0;
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the none compliance to the database."; throw new MonitoringDAOException("Error occurred while adding the none compliance to the database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
} }
@ -78,7 +74,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public void addComplianceDetails(Map<Integer, Integer> devicePolicyMap) throws MonitoringDAOException { public void addComplianceDetails(Map<Integer, Integer> devicePolicyMap) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet generatedKeys = null; ResultSet generatedKeys = null;
@ -91,7 +86,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
log.debug(map.getKey() + " -- " + map.getValue()); log.debug(map.getKey() + " -- " + map.getValue());
} }
} }
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " +
@ -107,30 +101,22 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the none compliance to the database."; throw new MonitoringDAOException("Error occurred while adding the none compliance to the database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
} }
} }
@Override @Override
public void setDeviceAsNoneCompliance(int deviceId, int policyId) throws public void setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException {
MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet generatedKeys = null; ResultSet generatedKeys = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," +
" ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ?"; " ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
@ -141,9 +127,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while updating the none compliance to the database."; throw new MonitoringDAOException("Error occurred while updating the none compliance to the database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
} }
@ -152,13 +136,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException { public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet generatedKeys = null; ResultSet generatedKeys = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" +
@ -178,9 +160,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
// } // }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while deleting the none compliance to the database."; throw new MonitoringDAOException("Error occurred while deleting the none compliance to the database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
} }
@ -189,21 +169,18 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature> public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures) throws MonitoringDAOException { complianceFeatures) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
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, PreparedStatement.RETURN_GENERATED_KEYS);
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());
if (feature.isCompliance() == true) { if (feature.isCompliance()) {
stmt.setInt(3, 1); stmt.setInt(3, 1);
} else { } else {
stmt.setInt(3, 0); stmt.setInt(3, 0);
@ -212,11 +189,9 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the none compliance features to the database."; throw new MonitoringDAOException("Error occurred while adding the none compliance features to the " +
log.error(msg, e); "database.", e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -253,9 +228,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
return complianceData; return complianceData;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to retrieve compliance data from database."; throw new MonitoringDAOException("Unable to retrieve compliance data from database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -263,13 +236,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public List<ComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException { public List<ComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<ComplianceData> complianceDataList = new ArrayList<>(); List<ComplianceData> complianceDataList = new ArrayList<>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE TENANT_ID = ? AND DEVICE_ID IN (?)"; String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE TENANT_ID = ? AND DEVICE_ID IN (?)";
@ -278,11 +249,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setString(2, PolicyManagerUtil.makeString(deviceIds)); stmt.setString(2, PolicyManagerUtil.makeString(deviceIds));
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
ComplianceData complianceData = new ComplianceData(); ComplianceData complianceData = new ComplianceData();
complianceData.setId(resultSet.getInt("ID")); complianceData.setId(resultSet.getInt("ID"));
complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); complianceData.setDeviceId(resultSet.getInt("DEVICE_ID"));
complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID"));
@ -295,11 +263,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
complianceDataList.add(complianceData); complianceDataList.add(complianceData);
} }
return complianceDataList; return complianceDataList;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to retrieve compliance data from database."; throw new MonitoringDAOException("Unable to retrieve compliance data from database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -308,7 +273,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws public List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws
MonitoringDAOException { MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -331,11 +295,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
complianceFeatures.add(feature); complianceFeatures.add(feature);
} }
return complianceFeatures; return complianceFeatures;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to retrieve compliance features data from database."; throw new MonitoringDAOException("Unable to retrieve compliance features data from database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -343,11 +304,9 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException { public void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ? AND TENANT_ID = ?"; String query = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ? AND TENANT_ID = ?";
@ -355,11 +314,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setInt(1, policyComplianceStatusId); stmt.setInt(1, policyComplianceStatusId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to delete compliance data from database."; throw new MonitoringDAOException("Unable to delete compliance data from database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -368,14 +324,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = ""; String query = "";
if (reset) { if (reset) {
@ -390,27 +343,20 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setInt(2, deviceId); stmt.setInt(2, deviceId);
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to update the attempts data in database."; throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
} }
@Override @Override
public void updateAttempts(List<Integer> deviceIds, boolean reset) throws MonitoringDAOException { public void updateAttempts(List<Integer> deviceIds, boolean reset) throws MonitoringDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = ""; String query = "";
if (reset) { if (reset) {
@ -428,17 +374,13 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to update the attempts data in database."; throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
} }
private Connection getConnection() throws MonitoringDAOException { private Connection getConnection() throws MonitoringDAOException {
try { try {
return PolicyManagementDAOFactory.getConnection(); return PolicyManagementDAOFactory.getConnection();
@ -448,8 +390,4 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} }
} }
} }

Loading…
Cancel
Save