diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java index 91788e27d3d..c06542ea15d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ReportManagementServiceImpl.java @@ -81,7 +81,6 @@ public class ReportManagementServiceImpl implements ReportManagementService { String msg = "No devices have enrolled between " + fromDate + " to " + toDate + " or doesn't match with" + " given parameters"; - log.error(msg); return Response.status(Response.Status.OK).entity(msg).build(); } else { devices.setList((List) result.getData()); 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 b7358320676..4c9b363f4fb 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 @@ -423,8 +423,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + - "'" + request.getStatus() + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to status " + + "'" + request.getStatus() + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -552,8 +554,10 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + - "to the mentioned filtering criteria", e); + String msg = "Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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 a60483203a7..61e2b876261 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 @@ -429,8 +429,10 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + - "'" + request.getStatus() + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to status " + + "'" + request.getStatus() + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -557,11 +559,10 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - String msg = "Error occurred while retrieving information of all " + - "registered devices"; + String msg = "Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria"; log.error(msg, e); - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + - "to the mentioned filtering criteria", e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } 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 5f28d0e4eff..913a5fa75c0 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 @@ -406,8 +406,10 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + - "'" + request.getStatus() + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to status " + + "'" + request.getStatus() + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -535,13 +537,76 @@ public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + - "to the mentioned filtering criteria", e); + String msg = "Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); } return devices; } + + @Override + public List getSubscribedDevices(int offsetValue, int limitValue, + List deviceIds, int tenantId, String status) + throws DeviceManagementDAOException { + 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); + } + } @Override public List getSubscribedDevices(int offsetValue, int limitValue, 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 96ffd31ddec..c01a2799350 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 @@ -425,8 +425,10 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + - "'" + request.getStatus() + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to status " + + "'" + request.getStatus() + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -490,8 +492,10 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { devices.add(device); } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while fetching the list of devices corresponding" + - "to the mentioned filtering criteria", e); + String msg = "Error occurred while fetching the list of devices corresponding" + + "to the mentioned filtering criteria"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, rs); }