From cbf10a8b59f8533a0acac1d54d64bbe089172b25 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 2 Sep 2015 17:35:41 +0530 Subject: [PATCH] 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());