diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index c709f562fe..12c8014d7b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -34,15 +34,23 @@ public interface DeviceDAO { /** * This method is used to add a device. * - * @param typeId device type id. - * @param device device object. + * @param typeId device type id. + * @param device device object. * @param tenantId tenant id. * @return returns the id of the persisted device record. * @throws DeviceManagementDAOException */ int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; - + /** + * This method is used to update a given device. + * + * @param typeId device type id. + * @param device device object. + * @param tenantId tenant id. + * @return returns the id of updated device. + * @throws DeviceManagementDAOException + */ boolean updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; /** @@ -95,7 +103,7 @@ public interface DeviceDAO { /** * This method is used to retrieve all the devices of a given tenant and device type. * - * @param type device type. + * @param type device type. * @param tenantId tenant id. * @return returns list of devices. * @throws DeviceManagementDAOException @@ -104,6 +112,7 @@ public interface DeviceDAO { /** * This method is used to retrieve devices of a given user. + * * @param username user name. * @param tenantId tenant id. * @return returns list of devices. @@ -122,8 +131,9 @@ public interface DeviceDAO { /** * This method is used to retrieve devices of a given device name. + * * @param deviceName device name. - * @param tenantId tenant id. + * @param tenantId tenant id. * @return returns list of devices. * @throws DeviceManagementDAOException */ @@ -132,7 +142,7 @@ public interface DeviceDAO { /** * This method is used to add an enrollment information of a given device. * - * @param device device object. + * @param device device object. * @param tenantId tenant id. * @return returns the id of the enrollment. * @throws DeviceManagementDAOException @@ -142,22 +152,22 @@ public interface DeviceDAO { /** * This method is used to set the current enrollment status of given device and user. * - * @param deviceId device id. + * @param deviceId device id. * @param currentOwner current user name. - * @param status device status. - * @param tenantId tenant id. + * @param status device status. + * @param tenantId tenant id. * @return returns true if success. * @throws DeviceManagementDAOException */ boolean setEnrolmentStatus(DeviceIdentifier deviceId, String currentOwner, Status status, - int tenantId) throws DeviceManagementDAOException; + int tenantId) throws DeviceManagementDAOException; /** * This method is used to get the status of current enrollment of a given user and device. * - * @param deviceId device id. + * @param deviceId device id. * @param currentOwner device owner. - * @param tenantId tenant id. + * @param tenantId tenant id. * @return returns current enrollment status. * @throws DeviceManagementDAOException */ @@ -167,9 +177,9 @@ public interface DeviceDAO { /** * This method is used to retrieve current enrollment of a given device and user. * - * @param deviceId device id. + * @param deviceId device id. * @param currentUser user name. - * @param tenantId tenant id. + * @param tenantId tenant id. * @return returns EnrolmentInfo object. * @throws DeviceManagementDAOException */ @@ -179,7 +189,7 @@ public interface DeviceDAO { /** * This method is used to retrieve devices of a given enrollment status. * - * @param status enrollment status. + * @param status enrollment status. * @param tenantId tenant id. * @return returns list of devices. * @throws DeviceManagementDAOException @@ -190,12 +200,24 @@ public interface DeviceDAO { * This method is used to retrieve the enrollment id of a given device and status. * * @param deviceId device id. - * @param status enrollment status. + * @param status enrollment status. * @param tenantId tenant id. * @return returns the id of current enrollment. * @throws DeviceManagementDAOException */ int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, - int tenantId) throws DeviceManagementDAOException; + int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to retrieve the enrollment info of a given list of devices and status. + * + * @param deviceIds A list of device identifiers. + * @param status enrollment status. + * @param tenantId tenant id. + * @return returns a list of enrolment info objects. + * @throws DeviceManagementDAOException + */ + List getEnrolmentsByStatus(List deviceIds, Status status, + int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 401557e195..ddd01eb9f2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import java.sql.*; import java.util.ArrayList; import java.util.Date; +import java.util.Iterator; import java.util.HashMap; import java.util.List; @@ -516,6 +517,51 @@ public class DeviceDAOImpl implements DeviceDAO { } } + public List getEnrolmentsByStatus(List deviceIds, Status status, + int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List enrolments = new ArrayList<>(); + try { + conn = this.getConnection(); + StringBuilder sql = new StringBuilder(); + sql.append("SELECT e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE, " + + "e.STATUS FROM DM_ENROLMENT e WHERE e.DEVICE_ID IN (SELECT d.ID FROM DM_DEVICE d " + + "WHERE d.DEVICE_IDENTIFICATION IN ("); + + // adding arguments to the sql query + Iterator iterator = deviceIds.iterator(); + while (iterator.hasNext()) { + iterator.next(); + sql.append(" ?"); + if (iterator.hasNext()) { + sql.append(","); + } + } + sql.append(") AND d.TENANT_ID = ?) AND e.STATUS = ? AND e.TENANT_ID = ?"); + + stmt = conn.prepareStatement(sql.toString()); + int index = 1; + for (DeviceIdentifier id : deviceIds) { + stmt.setString(index++, id.getId()); + } + stmt.setInt(index++, tenantId); + stmt.setString(index++, status.toString()); + stmt.setInt(index, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + enrolments.add(this.loadEnrolment(rs)); + } + return enrolments; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + + "ids of devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + private Device loadDevice(ResultSet rs) throws SQLException { Device device = new Device(); device.setId(rs.getInt("DEVICE_ID")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index c26eea0de4..9044720294 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -80,34 +80,26 @@ public class OperationManagerImpl implements OperationManager { } } try { - OperationManagementDAOFactory.beginTransaction(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + List enrolments; + try { + DeviceManagementDAOFactory.openConnection(); + enrolments = deviceDAO.getEnrolmentsByStatus(deviceIds, EnrolmentInfo.Status.ACTIVE, tenantId); + } catch (SQLException e) { + throw new OperationManagementException("Error occurred while opening a connection the data " + + "source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + OperationManagementDAOFactory.beginTransaction(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil.convertOperation(operation); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); - int enrolmentId; - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - for (DeviceIdentifier deviceId : deviceIds) { - try { - DeviceManagementDAOFactory.openConnection(); - enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); - } catch (SQLException e) { - throw new OperationManagementException("Error occurred while opening a connection the data " + - "source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - if (enrolmentId < 0) { - String errorMsg = "The operation not added for device.The device not found for " + - "device Identifier type -'" + deviceId.getType() + "' and device Id '" + - deviceId.getId(); - log.error(errorMsg); - } else { - operationMappingDAO.addOperationMapping(operationId, enrolmentId); - } + for (EnrolmentInfo enrolmentInfo : enrolments) { + operationMappingDAO.addOperationMapping(operationId, enrolmentInfo.getId()); } OperationManagementDAOFactory.commitTransaction(); return operationId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 234666a823..919a901fa5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -246,6 +246,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); Device device = deviceDAO.getDevice(deviceId, tenantId); + if (device == null) { + if (log.isDebugEnabled()) { + log.debug("Device not found for id '" + deviceId.getId() + "'"); + } + throw new DeviceManagementException("Device not found"); + } DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceFeature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceFeature.java index 0e3e9a9536..ddc238c1ff 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceFeature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceFeature.java @@ -45,7 +45,7 @@ public class ComplianceFeature { this.featureCode = featureCode; } - public boolean isCompliance() { + public boolean isCompliant() { return compliance; } 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 b7d58353d5..54d0ff0198 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 @@ -219,7 +219,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { for (ComplianceFeature feature : complianceFeatures) { stmt.setInt(1, policyComplianceStatusId); stmt.setString(2, feature.getFeatureCode()); - if (feature.isCompliance()) { + if (feature.isCompliant()) { stmt.setInt(3, 1); } else { stmt.setInt(3, 0); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index c536987bd4..5e63355f95 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -133,8 +133,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - triggerInfo.setIntervalMillis(0); - triggerInfo.setRepeatCount(1); + triggerInfo.setRepeatCount(0); Map properties = new HashMap<>(); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java index e1ebc13e9d..a74e85bf3e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java @@ -107,12 +107,12 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { log.debug("Re-enforcing policy started...!"); - int sixe = policies.size(); + int size = policies.size(); sortPolicies(policies); int x = 0; for (Policy policy : policies) { - policy.setPriorityId(sixe - x); + policy.setPriorityId(size - x); x++; }