From 61bd91b21f48dde26308362c673e18bc9d630654 Mon Sep 17 00:00:00 2001 From: nipunnadeen Date: Fri, 27 Sep 2019 11:03:35 +0530 Subject: [PATCH] Improve the subscribed devices and category details APIs --- .../mgt/common/BasePaginatedResult.java | 3 - .../common/services/SubscriptionManager.java | 26 +++++---- .../mgt/core/dao/SubscriptionDAO.java | 32 +++++------ .../GenericSubscriptionDAOImpl.java | 27 ++++----- .../core/impl/SubscriptionManagerImpl.java | 7 +-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 14 ++--- .../dao/impl/device/GenericDeviceDAOImpl.java | 6 +- .../dao/impl/device/OracleDeviceDAOImpl.java | 57 ++++++++++++++++++- .../impl/device/PostgreSQLDeviceDAOImpl.java | 57 ++++++++++++++++++- .../impl/device/SQLServerDeviceDAOImpl.java | 57 ++++++++++++++++++- .../DeviceManagementProviderService.java | 11 ++-- 11 files changed, 229 insertions(+), 68 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/BasePaginatedResult.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/BasePaginatedResult.java index b4253d2d02..b2445fcf7f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/BasePaginatedResult.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/BasePaginatedResult.java @@ -22,9 +22,6 @@ import io.swagger.annotations.ApiModelProperty; public class BasePaginatedResult { - /** - * Number of Resources returned. - */ @ApiModelProperty( value = "Number of total resources.", example = "1") diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 98e718425c..2adf3992c0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -33,12 +33,13 @@ public interface SubscriptionManager { /*** * This method used to get the app id ,device ids and pass them to DM service method - * @param appUUID uuid - * @param offsetValue offsetValue - * @param limitValue limitValue - * @param status status - * @return deviceDetails - * @throws ApplicationManagementException Exception of the application management + * @param appUUID UUID of the application release + * @param offsetValue offset value for get paginated result + * @param limitValue limit value for get paginated result + * @param status status of the devices + * @return deviceDetails - device details for given application release. + * @throws ApplicationManagementException throws {@link ApplicationManagementException} Exception + * of the application management */ PaginationResult getAppInstalledDevices(int offsetValue, int limitValue, String appUUID, String status) @@ -46,12 +47,13 @@ public interface SubscriptionManager { /*** * This method used to get category details - * @param appUUID uuid - * @param subType subType - * @param offsetValue offsetValue - * @param limitValue limitValue - * @return paginationResult - * @throws ApplicationManagementException Exception of the application management + * @param appUUID UUID of the application release + * @param subType subscription type of the application + * @param offsetValue offset value for get paginated result + * @param limitValue limit value for get paginated result + * @return {@link PaginationResult} pagination result of the category details. + * @throws ApplicationManagementException throws {@link ApplicationManagementException} Exception + * of the application management */ PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID, String subType) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index 846dd5e2d3..2310f7b1b0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -87,11 +87,11 @@ public interface SubscriptionDAO { /** * this method is used to get the details of users - * @param tenantId tenant id - * @param offsetValue offsetValue - * @param limitValue limitValue - * @param appReleaseId appReleaseId - * @return subscribedUsers + * @param tenantId id of the current tenant + * @param offsetValue offset value for get paginated result + * @param limitValue limit value for get paginated result + * @param appReleaseId id of the application release. + * @return subscribedUsers - list of app subscribed users. * @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if * connections establishment fails. */ @@ -101,11 +101,11 @@ public interface SubscriptionDAO { /** * this method is used to get the details of roles - * @param tenantId tenant id - * @param offsetValue offsetValue - * @param limitValue limitValue - * @param appReleaseId appReleaseId - * @return subscribedRoles + * @param tenantId id of the current tenant + * @param offsetValue offset value for get paginated result + * @param limitValue limit value for get paginated result + * @param appReleaseId id of the application release. + * @return subscribedRoles - list of app subscribed roles. * @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if * connections establishment fails. */ @@ -114,12 +114,12 @@ public interface SubscriptionDAO { throws ApplicationManagementDAOException; /** - * this method is used to get the details of groups - * @param tenantId tenant id - * @param offsetValue offsetValue - * @param limitValue limitValue - * @param appReleaseId appReleaseId - * @return subscribedUsers + * this method is used to get the details of subscribed groups + * @param tenantId id of the current tenant + * @param offsetValue offset value for get paginated result + * @param limitValue limit value for get paginated result + * @param appReleaseId id of the application release. + * @return subscribedGroups - list of app subscribed groups. * @throws ApplicationManagementDAOException throws {@link ApplicationManagementDAOException} if * connections establishment fails. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 85a78d28b9..ff8c8c3b2b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -708,7 +708,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } try { Connection conn = this.getDBConnection(); - int index = 1; List subscribedUsers = new ArrayList<>(); String sql = "SELECT " + "US.USER_NAME AS USER " @@ -716,10 +715,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE " + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setInt(index++, appReleaseId); - stmt.setInt(index++, tenantId); - stmt.setInt(index++, offsetValue); - stmt.setInt(index, limitValue); + stmt.setInt(1, appReleaseId); + stmt.setInt(2, tenantId); + stmt.setInt(3, offsetValue); + stmt.setInt(4, limitValue); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { subscribedUsers.add(rs.getString("USER")); @@ -749,7 +748,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } try { Connection conn = this.getDBConnection(); - int index = 1; List subscribedRoles = new ArrayList<>(); String sql = "SELECT " + "US.ROLE_NAME AS ROLE " @@ -757,10 +755,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE " + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(index++, appReleaseId); - ps.setInt(index++, tenantId); - ps.setInt(index++, offsetValue); - ps.setInt(index, limitValue); + ps.setInt(1, appReleaseId); + ps.setInt(2, tenantId); + ps.setInt(3, offsetValue); + ps.setInt(4, limitValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedRoles.add(rs.getString("ROLE")); @@ -790,7 +788,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } try { Connection conn = this.getDBConnection(); - int index = 1; List subscribedGroups = new ArrayList<>(); String sql = "SELECT " + "GS.GROUP_NAME AS GROUPS " @@ -798,10 +795,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE " + "AP_APP_RELEASE_ID = ? AND TENANT_ID = ? LIMIT ?,?"; try (PreparedStatement ps = conn.prepareStatement(sql)) { - ps.setInt(index++, appReleaseId); - ps.setInt(index++, tenantId); - ps.setInt(index++, offsetValue); - ps.setInt(index, limitValue); + ps.setInt(1, appReleaseId); + ps.setInt(2, tenantId); + ps.setInt(3, offsetValue); + ps.setInt(4, limitValue); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedGroups.add(rs.getString("GROUPS")); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 40976459c1..bc8476ea01 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -571,7 +571,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { try { ConnectionManagerUtil.openDBConnection(); - ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId); int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); @@ -580,14 +579,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (deviceSubscriptionDTOS.isEmpty()) { String msg = "Couldn't found an subscribed devices for application release id: " + applicationReleaseId; - log.error(msg); + log.info(msg); } - List deviceIdList = new ArrayList<>(); for (DeviceSubscriptionDTO deviceIds : deviceSubscriptionDTOS) { deviceIdList.add(deviceIds.getDeviceId()); } - //pass the device id list to device manager service layer method + //pass the device id list to device manager service method try { PaginationResult deviceDetails = deviceManagementProviderService .getAppSubscribedDevices(offsetValue ,limitValue, deviceIdList, status); @@ -630,7 +628,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { PaginationResult paginationResult = new PaginationResult(); try { ConnectionManagerUtil.openDBConnection(); - ApplicationDTO applicationDTO = this.applicationDAO .getAppWithRelatedRelease(appUUID, tenantId); int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); 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 dd01a16594..fb9ab2859b 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 @@ -552,13 +552,13 @@ public interface DeviceDAO { String toDate) throws DeviceManagementDAOException; /** - * this method is used to get the details of devices - * @param deviceIds device ids - * @param tenantId tenant id - * @param offsetValue offsetValue - * @param limitValue limitValue - * @param status status - * @return subscribed device details list + * this method is used to get the details of subscribed devices. + * @param deviceIds device ids of the subscribed devices. + * @param tenantId Id of the current tenant. + * @param offsetValue offset value for get paginated result. + * @param limitValue limit value for get paginated result. + * @param status status of the devices. + * @return devices - subscribed device details list * @throws DeviceManagementDAOException throws {@link DeviceManagementDAOException} if connections * establishment fails. */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java index bba02bd6e4..4c9b363f4f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/GenericDeviceDAOImpl.java @@ -618,8 +618,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { } } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving information " + - "of all registered devices according to device ids and the limit area", e); + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index acd903eda5..61e2b87626 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -37,6 +37,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.StringJoiner; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -572,7 +573,61 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { public List getSubscribedDevices(int offsetValue, int limitValue, List deviceIds, int tenantId, String status) throws DeviceManagementDAOException { - return null; + Connection conn; + + try { + conn = this.getConnection(); + int index = 1; + + boolean isStatusProvided = false; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "f.ID AS DEVICE_ID, f.NAME AS DEVICE_NAME, f.DESCRIPTION AS DESCRIPTION, " + + "f.DEVICE_TYPE_ID, f.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + + "e.DATE_OF_LAST_UPDATE, e.STATUS, t.NAME AS DEVICE_TYPE " + + "FROM DM_ENROLMENT AS e,DM_DEVICE AS f, DM_DEVICE_TYPE t " + + "WHERE " + + "e.DEVICE_ID=f.ID AND " + + "e.DEVICE_ID IN (", ") AND e.TENANT_ID=?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + + if (status != null && !status.isEmpty()) { + query = query + " AND e.STATUS=?"; + isStatusProvided = true; + } + + query = query + " ORDER BY ENROLMENT_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + + for (Integer deviceId : deviceIds) { + ps.setObject(index++, deviceId); + } + + ps.setInt(index++, tenantId); + if (isStatusProvided) { + ps.setString(index++, status); + } + ps.setInt(index++, offsetValue); + ps.setInt(index, limitValue); + + try (ResultSet rs = ps.executeQuery()) { + List devices = new ArrayList<>(); + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } } private Connection getConnection() throws SQLException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java index c88092f65e..7410640805 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/PostgreSQLDeviceDAOImpl.java @@ -34,6 +34,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.StringJoiner; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -550,7 +551,61 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { public List getSubscribedDevices(int offsetValue, int limitValue, List deviceIds, int tenantId, String status) throws DeviceManagementDAOException { - return null; + Connection conn; + + try { + conn = this.getConnection(); + int index = 1; + + boolean isStatusProvided = false; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "f.ID AS DEVICE_ID, f.NAME AS DEVICE_NAME, f.DESCRIPTION AS DESCRIPTION, " + + "f.DEVICE_TYPE_ID, f.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + + "e.DATE_OF_LAST_UPDATE, e.STATUS, t.NAME AS DEVICE_TYPE " + + "FROM DM_ENROLMENT AS e,DM_DEVICE AS f, DM_DEVICE_TYPE t "+ + "WHERE " + + "e.DEVICE_ID=f.ID AND " + + "e.DEVICE_ID IN (", ") AND e.TENANT_ID=?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + + if (status != null && !status.isEmpty()) { + query = query + " AND e.STATUS=?"; + isStatusProvided = true; + } + + query = query + " LIMIT ? OFFSET ?"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + + for (Integer deviceId : deviceIds) { + ps.setObject(index++, deviceId); + } + + ps.setInt(index++, tenantId); + if (isStatusProvided) { + ps.setString(index++, status); + } + ps.setInt(index++, offsetValue); + ps.setInt(index, limitValue); + + try (ResultSet rs = ps.executeQuery()) { + List devices = new ArrayList<>(); + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } } private Connection getConnection() throws SQLException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 87ab2a6c8a..c01a279935 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -34,6 +34,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.StringJoiner; /** * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. @@ -505,7 +506,61 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { public List getSubscribedDevices(int offsetValue, int limitValue, List deviceIds, int tenantId, String status) throws DeviceManagementDAOException { - return null; + Connection conn; + + try { + conn = this.getConnection(); + int index = 1; + + boolean isStatusProvided = false; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "f.ID AS DEVICE_ID, f.NAME AS DEVICE_NAME, f.DESCRIPTION AS DESCRIPTION, " + + "f.DEVICE_TYPE_ID, f.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.ID AS ENROLMENT_ID, e.OWNER, e.OWNERSHIP, e.DATE_OF_ENROLMENT, " + + "e.DATE_OF_LAST_UPDATE, e.STATUS, t.NAME AS DEVICE_TYPE " + + "FROM DM_ENROLMENT AS e,DM_DEVICE AS f, DM_DEVICE_TYPE t "+ + "WHERE " + + "e.DEVICE_ID=f.ID AND " + + "e.DEVICE_ID IN (", ") AND e.TENANT_ID=?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + + if (status != null && !status.isEmpty()) { + query = query + " AND e.STATUS=?"; + isStatusProvided = true; + } + + query = query + " LIMIT ?,?"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + + for (Integer deviceId : deviceIds) { + ps.setObject(index++, deviceId); + } + + ps.setInt(index++, tenantId); + if (isStatusProvided) { + ps.setString(index++, status); + } + ps.setInt(index++, offsetValue); + ps.setInt(index, limitValue); + + try (ResultSet rs = ps.executeQuery()) { + List devices = new ArrayList<>(); + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } } private Connection getConnection() throws SQLException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index cb64aa5da6..6abecd237d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -771,12 +771,13 @@ public interface DeviceManagementProviderService { /** * This method retrieves a list of subscribed devices. - * @param devicesIds devices ids - * @param offsetValue offsetValue - * @param limitValue limitValue - * @param status status + * @param devicesIds devices ids of the subscribed devices + * @param offsetValue offset value for get paginated result + * @param limitValue limit value for get paginated result + * @param status status of the devices * @return {@link PaginationResult} - * @throws DeviceManagementException if any service level or DAO level error occurs + * @throws DeviceManagementException throws {@link DeviceManagementException} if any service + * level or DAO level error occurs */ PaginationResult getAppSubscribedDevices(int offsetValue, int limitValue, List devicesIds, String status)