From cbf10a8b59f8533a0acac1d54d64bbe089172b25 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 2 Sep 2015 17:35:41 +0530 Subject: [PATCH 1/3] Refactored addOperation method --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 50 ++++++++++++------- .../mgt/core/dao/impl/DeviceDAOImpl.java | 46 +++++++++++++++++ .../operation/mgt/OperationManagerImpl.java | 36 ++++++------- .../DeviceManagementProviderServiceImpl.java | 6 +++ 4 files changed, 98 insertions(+), 40 deletions(-) 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 3eb1f48ac4..126cfb2941 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,8 +34,8 @@ 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 @@ -45,8 +45,8 @@ public interface DeviceDAO { /** * This method is used to update a given 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 updated device. * @throws DeviceManagementDAOException @@ -85,7 +85,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 @@ -94,6 +94,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. @@ -112,8 +113,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 */ @@ -122,7 +124,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 @@ -132,22 +134,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 */ @@ -157,9 +159,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 */ @@ -169,7 +171,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 @@ -180,12 +182,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 75e916589d..58de082c66 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.List; public class DeviceDAOImpl implements DeviceDAO { @@ -455,6 +456,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 d5000da889..1489b474d8 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 @@ -245,6 +245,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()); From fbd697edb011df4c5ebf234dfc1612396c6f8a42 Mon Sep 17 00:00:00 2001 From: mharindu Date: Tue, 8 Sep 2015 11:27:29 +0530 Subject: [PATCH 2/3] Fixed issue in policy delegation executor --- .../policy/mgt/core/impl/PolicyAdministratorPointImpl.java | 3 +-- .../wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) 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++; } From 9920f2919a38fc267aa47eb6bd02f5243bb858f7 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 9 Sep 2015 20:00:10 +0530 Subject: [PATCH 3/3] Fixed spelling mistakes of policy component --- .../carbon/policy/mgt/common/monitor/ComplianceFeature.java | 2 +- .../wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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);