MonitoringManagerImpl implements MonitoringManager { @Override public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException { Map<Integer, Device> deviceIds = new HashMap<>(); List<NonComplianceData> complianceDatas = new ArrayList<>(); HashMap<Integer, Integer> devicePolicyIdMap = new HashMap<>(); try { PolicyManagementDAOFactory.openConnection(); List<NonComplianceData> cd = monitoringDAO.getCompliance(); for (Device device : devices) { deviceIds.put(device.getId(), device); for (NonComplianceData data : cd) { if (device.getId() == data.getDeviceId() && device.getEnrolmentInfo().getId() == data .getEnrolmentId()) { complianceDatas.add(data); } } } List<Integer> deviceIDs = new ArrayList<>(deviceIds.keySet()); HashMap<Integer, Integer> temp = policyDAO.getAppliedPolicyIds(); for (Integer id : deviceIDs) { if (temp != null && !temp.isEmpty() && temp.containsKey(id)) { devicePolicyIdMap.put(id, temp.get(id)); } } } catch (SQLException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (PolicyManagerDAOException e) { throw new PolicyComplianceException("SQL error occurred while getting policy details.", e); } finally { PolicyManagementDAOFactory.closeConnection(); } Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>(); Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>(); List<PolicyDeviceWrapper> firstTimeDevices = new ArrayList<>(); Map<Integer, NonComplianceData> tempMap = new HashMap<>(); try { if (complianceDatas != null || !complianceDatas.isEmpty()) { for (NonComplianceData complianceData : complianceDatas) { tempMap.put(complianceData.getDeviceId(), complianceData); if (complianceData.getAttempts() == 0) { deviceIdsToAddOperation.put(complianceData.getDeviceId(), deviceIds.get(complianceData.getDeviceId())); } else { deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), deviceIds.get(complianceData.getDeviceId())); } } } for (Device device : devices) { if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) { deviceIdsToAddOperation.put(device.getId(), device); PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper(); policyDeviceWrapper.setDeviceId(device.getId()); policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId()); policyDeviceWrapper.setPolicyId(devicePolicyIdMap.get(device.getId())); firstTimeDevices.add(policyDeviceWrapper); } } if (log.isDebugEnabled()) { log.debug("These devices are in the system for the first time"); for (PolicyDeviceWrapper wrapper : firstTimeDevices) { log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " + wrapper.getPolicyId()); } } PolicyManagementDAOFactory.beginTransaction(); if (!deviceIdsToAddOperation.isEmpty()) { monitoringDAO.addComplianceDetails(firstTimeDevices); monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false); } if (!deviceIdsWithExistingOperation.isEmpty()) { monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); } PolicyManagementDAOFactory.commitTransaction(); } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred from monitoring dao.", e); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); } finally { PolicyManagementDAOFactory.closeConnection(); } if (!deviceIdsToAddOperation.isEmpty()) { try { this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); } catch (InvalidDeviceException e) { throw new PolicyComplianceException("Invalid Device Identifiers found.", e); } catch (OperationManagementException e) { throw new PolicyComplianceException("Error occurred while adding monitoring operation to devices", e); } } } MonitoringManagerImpl(); @Override List<ComplianceFeature> checkPolicyCompliance(
            DeviceIdentifier deviceIdentifier,
            Object deviceResponse); @Override boolean isCompliant(DeviceIdentifier deviceIdentifier); @Override NonComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier); @Override void addMonitoringOperation(List<Device> devices); @Override List<String> getDeviceTypes();  }
@Test public void testAddMonitoringOperation() throws Exception { monitoringManager.addMonitoringOperation(deviceMgtService.getAllDevices()); }