From 8f8e5e67d4e5923a12aeece351601a233dcd4246 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Tue, 23 Jul 2024 19:21:28 +0530 Subject: [PATCH] Update generic device dao --- .../core/dao/impl/AbstractDeviceDAOImpl.java | 102 ------------------ .../dao/impl/device/GenericDeviceDAOImpl.java | 11 ++ 2 files changed, 11 insertions(+), 102 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index eafa2e0d02..bb0bb97b62 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3408,108 +3408,6 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } - @Override - public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) - throws DeviceManagementDAOException { - List devices = new ArrayList<>(); - if (deviceIds == null || deviceIds.isEmpty()) return devices; - - String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); - boolean isOwnerProvided = false; - boolean isDeviceStatusProvided = false; - boolean isDeviceNameProvided = false; - boolean isDeviceTypeIdProvided = false; - - try { - Connection connection = getConnection(); - String sql = "SELECT e.DEVICE_ID, " + - "d.DEVICE_IDENTIFICATION, " + - "e.STATUS, " + - "e.OWNER, " + - "d.NAME AS DEVICE_NAME, " + - "e.DEVICE_TYPE, " + - "e.OWNERSHIP, " + - "e.DATE_OF_LAST_UPDATE " + - "FROM DM_DEVICE d " + - "INNER JOIN DM_ENROLMENT e " + - "ON d.ID = e.DEVICE_ID " + - "WHERE d.TENANT_ID = ? " + - "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + - "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; - - if (paginationRequest.getOwner() != null) { - sql = sql + " AND e.OWNER LIKE ?"; - isOwnerProvided = true; - } - - if (paginationRequest.getDeviceStatus() != null) { - sql = sql + " AND e.STATUS = ?"; - isDeviceStatusProvided = true; - } - - if (paginationRequest.getDeviceName() != null) { - sql = sql + " AND d.NAME LIKE ?"; - isDeviceNameProvided = true; - } - - if (paginationRequest.getDeviceTypeId() > 0) { - sql = sql + " AND d.DEVICE_TYPE_ID = ?"; - isDeviceTypeIdProvided = true; - } - - sql = sql + " LIMIT ? OFFSET ?"; - - try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - int parameterIdx = 1; - preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId()); - preparedStatement.setInt(parameterIdx++, tenantId); - - for (Integer deviceId : deviceIds) { - preparedStatement.setInt(parameterIdx++, deviceId); - } - - if (isOwnerProvided) { - preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); - } - if (isDeviceStatusProvided) { - preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus()); - } - if (isDeviceNameProvided) { - preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getDeviceName() + "%"); - } - if (isDeviceTypeIdProvided) { - preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId()); - } - - preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); - preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); - - try(ResultSet resultSet = preparedStatement.executeQuery()) { - Device device; - while(resultSet.next()) { - device = new Device(); - device.setId(resultSet.getInt("DEVICE_ID")); - device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION")); - device.setName(resultSet.getString("DEVICE_NAME")); - device.setType(resultSet.getString("DEVICE_TYPE")); - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS"))); - enrolmentInfo.setOwner(resultSet.getString("OWNER")); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP"))); - enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); - device.setEnrolmentInfo(enrolmentInfo); - devices.add(device); - } - } - } - return devices; - } catch (SQLException e) { - String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } - } - @Override public int getDeviceCountByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index 6467f912ff..5710e0181f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -1870,6 +1870,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { boolean isDeviceStatusProvided = false; boolean isDeviceNameProvided = false; boolean isDeviceTypeIdProvided = false; + try { Connection connection = getConnection(); String sql = "SELECT e.DEVICE_ID, " + @@ -1882,32 +1883,41 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "e.DATE_OF_LAST_UPDATE " + "FROM DM_DEVICE d " + "INNER JOIN DM_ENROLMENT e " + + "ON d.ID = e.DEVICE_ID " + "WHERE d.TENANT_ID = ? " + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; + if (paginationRequest.getOwner() != null) { sql = sql + " AND e.OWNER LIKE ?"; isOwnerProvided = true; } + if (paginationRequest.getDeviceStatus() != null) { sql = sql + " AND e.STATUS = ?"; isDeviceStatusProvided = true; } + if (paginationRequest.getDeviceName() != null) { sql = sql + " AND d.NAME LIKE ?"; isDeviceNameProvided = true; } + if (paginationRequest.getDeviceTypeId() > 0) { sql = sql + " AND d.DEVICE_TYPE_ID = ?"; isDeviceTypeIdProvided = true; } + sql = sql + " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int parameterIdx = 1; preparedStatement.setInt(parameterIdx++, tenantId); + for (Integer deviceId : deviceIds) { preparedStatement.setInt(parameterIdx++, deviceId); } + if (isOwnerProvided) { preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); } @@ -1923,6 +1933,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { preparedStatement.setInt(parameterIdx++, paginationRequest.getRowCount()); preparedStatement.setInt(parameterIdx, paginationRequest.getStartIndex()); + try(ResultSet resultSet = preparedStatement.executeQuery()) { Device device; while(resultSet.next()) { -- 2.36.3