From 023ce0f9ec3e8d1d576f256e403151d91dce5917 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 13:22:00 +0530 Subject: [PATCH] Fix issues in services --- .../mgt/common/SubscriptionMetadata.java | 34 ++++++- .../common/dto/SubscriptionStatisticDTO.java | 18 +++- .../mgt/core/dao/SubscriptionDAO.java | 8 +- .../GenericSubscriptionDAOImpl.java | 97 ++++++++++++++----- .../mgt/SubscriptionManagementHelperUtil.java | 5 + ...bscriptionManagementHelperServiceImpl.java | 5 +- ...bscriptionManagementHelperServiceImpl.java | 11 ++- ...bscriptionManagementHelperServiceImpl.java | 7 +- ...bscriptionManagementHelperServiceImpl.java | 11 ++- .../core/dao/impl/AbstractDeviceDAOImpl.java | 70 +++++++++---- .../DeviceManagementProviderServiceImpl.java | 1 - 11 files changed, 200 insertions(+), 67 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java index 6d1b128f42..9e9a87d9a1 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionMetadata.java @@ -19,12 +19,22 @@ package io.entgra.device.mgt.core.application.mgt.common; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class SubscriptionMetadata { public static final class DeviceSubscriptionStatus { public static final String NEW = "NEW"; public static final String PENDING = "PENDING"; public static final String COMPLETED = "COMPLETED"; - public static final String FAILED = "FAILED"; + public static final String ERROR = "ERROR"; + public static final String INVALID = "INVALID"; + public static final String UNAUTHORIZED = "UNAUTHORIZED"; + public static final String IN_PROGRESS = "IN_PROGRESS"; + public static final String REPEAT = "REPEAT"; } public static final class SubscriptionTypes { @@ -33,4 +43,26 @@ public class SubscriptionMetadata { public static final String GROUP = "group"; public static final String USER = "user"; } + + public static final class DBSubscriptionStatus { + public static final List COMPLETED_STATUS_LIST = + Collections.singletonList(DeviceSubscriptionStatus.COMPLETED); + public static final List ERROR_STATUS_LIST = + Arrays.asList(DeviceSubscriptionStatus.ERROR, DeviceSubscriptionStatus.INVALID, DeviceSubscriptionStatus.UNAUTHORIZED); + public static final List PENDING_STATUS_LIST = + Arrays.asList(DeviceSubscriptionStatus.PENDING, DeviceSubscriptionStatus.IN_PROGRESS, DeviceSubscriptionStatus.REPEAT); + } + + public static Map> deviceSubscriptionStatusToDBSubscriptionStatusMap; + static { + Map> statusMap = new HashMap<>(); + statusMap.put(DeviceSubscriptionStatus.COMPLETED, DBSubscriptionStatus.COMPLETED_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.PENDING, DBSubscriptionStatus.PENDING_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.IN_PROGRESS, DBSubscriptionStatus.PENDING_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.REPEAT, DBSubscriptionStatus.PENDING_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.ERROR, DBSubscriptionStatus.ERROR_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.INVALID, DBSubscriptionStatus.ERROR_STATUS_LIST); + statusMap.put(DeviceSubscriptionStatus.UNAUTHORIZED, DBSubscriptionStatus.ERROR_STATUS_LIST); + deviceSubscriptionStatusToDBSubscriptionStatusMap = Collections.unmodifiableMap(statusMap); + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java index 1f02402fae..c50e47a5ac 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatisticDTO.java @@ -20,9 +20,21 @@ package io.entgra.device.mgt.core.application.mgt.common.dto; public class SubscriptionStatisticDTO { - private int completedDeviceCount; - private int pendingDevicesCount; - private int failedDevicesCount; + private int completedDeviceCount = 0; + private int pendingDevicesCount = 0; + private int failedDevicesCount = 0; + + public void addToComplete(int count) { + completedDeviceCount += count; + } + + public void addToPending(int count) { + pendingDevicesCount += count; + } + + public void addToFailed(int count) { + failedDevicesCount += count ; + } public int getCompletedDeviceCount() { return completedDeviceCount; diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index e7098c37a5..d7a1d37d4a 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -400,10 +400,10 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException; int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException; /** @@ -420,11 +420,11 @@ public interface SubscriptionDAO { * @return {@link DeviceOperationDTO} which contains the details of device subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, + List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, List actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException; int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, - String actionStatus, String actionType, String actionTriggeredBy) + List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException; /** diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 53e4409911..e2fd221943 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -46,6 +46,7 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -1914,14 +1915,20 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + // passed the required list for the action status @Override public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); } + + if (deviceIds == null || deviceIds.isEmpty()) { + return Collections.emptyList(); + } + try { Connection conn = this.getDBConnection(); String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; @@ -1940,7 +1947,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); @@ -1961,16 +1969,20 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, appReleaseId); ps.setBoolean(paramIdx++, unsubscribe); ps.setInt(paramIdx++, tenantId); - for (int i = 0; i < deviceIds.size(); i++) { - ps.setInt(paramIdx++, deviceIds.get(i)); + for (Integer deviceId : deviceIds) { + ps.setInt(paramIdx++, deviceId); } if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } @@ -2018,9 +2030,12 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId, - List deviceIds, String actionStatus, String actionType, + List deviceIds, List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { int deviceCount = 0; + + if (deviceIds == null || deviceIds.isEmpty()) return deviceCount; + try { Connection conn = this.getDBConnection(); StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT " @@ -2029,8 +2044,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } + if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); } @@ -2043,16 +2060,20 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, appReleaseId); ps.setBoolean(paramIdx++, unsubscribe); ps.setInt(paramIdx++, tenantId); - for (int i = 0; i < deviceIds.size(); i++) { - ps.setInt(paramIdx++, deviceIds.get(i)); + for (Integer deviceId : deviceIds) { + ps.setInt(paramIdx++, deviceId); } if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx, "%" + actionTriggeredBy + "%"); } @@ -2175,7 +2196,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, - String actionStatus, String actionType, String actionTriggeredBy, + List actionStatus, String actionType, String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId @@ -2198,8 +2219,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } + if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); } @@ -2222,11 +2245,15 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, tenantId); if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } + if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } @@ -2274,7 +2301,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId, - String actionStatus, String actionType, String actionTriggeredBy) + List actionStatus, String actionType, String actionTriggeredBy) throws ApplicationManagementDAOException { int deviceCount = 0; if (log.isDebugEnabled()) { @@ -2288,7 +2315,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? "); if (actionStatus != null && !actionStatus.isEmpty()) { - sql.append(" AND DS.STATUS = ? "); + sql.append(" AND DS.STATUS IN ("). + append(actionStatus.stream().map(status -> "?").collect(Collectors.joining(","))).append(") "); } if (actionType != null && !actionType.isEmpty()) { sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); @@ -2306,8 +2334,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(paramIdx++, tenantId); if (actionStatus != null && !actionStatus.isEmpty()) { - ps.setString(paramIdx++, actionStatus); + for (String status : actionStatus) { + ps.setString(paramIdx++, status); + } } + if (actionType != null && !actionType.isEmpty()) { ps.setString(paramIdx++, actionType); } @@ -2730,42 +2761,56 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + // todo: fixed the status @Override public SubscriptionStatisticDTO getSubscriptionStatistic(List deviceIds, String subscriptionType, boolean isUnsubscribed, int tenantId) throws ApplicationManagementDAOException { SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO(); - String deviceIdsString = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + if (deviceIds == null || deviceIds.isEmpty()) return subscriptionStatisticDTO; + boolean doesAllEntriesRequired = true; try { Connection connection = getDBConnection(); String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " + "STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " + - "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ deviceIdsString + ")"; + "TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) { - sql = sql + " AND ACTION_TRIGGERED_FROM = ?"; + sql += " AND ACTION_TRIGGERED_FROM = ?"; doesAllEntriesRequired = false; } - sql = sql + " GROUP BY (STATUS)"; + + sql += " GROUP BY (STATUS)"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int idx = 1; preparedStatement.setInt(idx++, tenantId); preparedStatement.setBoolean(idx++, isUnsubscribed); + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(idx++, deviceId); + } + if (!doesAllEntriesRequired) { preparedStatement.setString(idx, subscriptionType); } try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { + // add the error and in progress int count = resultSet.getInt("COUNT"); String status = resultSet.getString("STATUS"); - if (Objects.equals(status, "COMPLETED")) { - subscriptionStatisticDTO.setCompletedDeviceCount(count); - } else if (Objects.equals(status, "PENDING")) { - subscriptionStatisticDTO.setPendingDevicesCount(count); - } else { - subscriptionStatisticDTO.setFailedDevicesCount(count); + + if (SubscriptionMetadata.DBSubscriptionStatus.COMPLETED_STATUS_LIST.contains(status)) { + subscriptionStatisticDTO.addToComplete(count); + } + + if (SubscriptionMetadata.DBSubscriptionStatus.PENDING_STATUS_LIST.contains(status)) { + subscriptionStatisticDTO.addToPending(count); + } + if (SubscriptionMetadata.DBSubscriptionStatus.ERROR_STATUS_LIST.contains(status)) { + subscriptionStatisticDTO.addToFailed(count); } } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java index e75ec5dd94..0028e71581 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; @@ -115,4 +116,8 @@ public class SubscriptionManagementHelperUtil { } return ((float) numerator / (float) denominator) * 100; } + + public static List getDBSubscriptionStatus(String deviceSubscriptionStatus) { + return SubscriptionMetadata.deviceSubscriptionStatusToDBSubscriptionStatusMap.get(deviceSubscriptionStatus); + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java index 1963e8dcd7..13ccecc0ee 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java @@ -79,6 +79,7 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. @@ -96,11 +97,11 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription); } else { deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit); deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO. - getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null, + getId(), isUnsubscribe, tenantId, dbSubscriptionStatus, null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java index 3da06cc787..0756495098 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java @@ -97,6 +97,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); GroupDetailsDTO groupDetailsDTO; + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); List allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(), applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(), @@ -105,7 +106,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -126,12 +127,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr List paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java index 939b7227ff..30a1dd38a6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -89,11 +89,12 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByRole, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -108,11 +109,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri deviceCount = deviceIdsOwnByRole.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(), + isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus, subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 1e67916804..830153d142 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java @@ -86,11 +86,12 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); + List dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus()); if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, deviceIdsOwnByUser, null, - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); List deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -105,12 +106,12 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri deviceCount = deviceIdsOwnByUser.size(); } else { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); + isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(), - subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy()); + isUnsubscribe, tenantId, deviceIdsOwnByUser, dbSubscriptionStatus, + null, deviceSubscriptionFilterCriteria.getTriggeredBy()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, 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 93d6c75fb0..946d1ec3c6 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 @@ -3304,15 +3304,28 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public List getDevicesNotInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { List filteredDeviceIds = new ArrayList<>(); - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); try { Connection connection = getConnection(); - String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " + - "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; + String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + + sql += " LIMIT ? OFFSET ?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, tenantId); - preparedStatement.setInt(2, request.getRowCount()); - preparedStatement.setInt(3, request.getStartIndex()); + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); @@ -3331,15 +3344,22 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public List getDevicesInGivenIdList(PaginationRequest request, List deviceIds, int tenantId) throws DeviceManagementDAOException { List filteredDeviceIds = new ArrayList<>(); - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + if (deviceIds == null || deviceIds.isEmpty()) return filteredDeviceIds; + + String deviceIdStringList = deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")); try { Connection connection = getConnection(); String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID IN " + "(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, tenantId); - preparedStatement.setInt(2, request.getRowCount()); - preparedStatement.setInt(3, request.getStartIndex()); + int paraIdx = 1; + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + + preparedStatement.setInt(paraIdx++, tenantId); + preparedStatement.setInt(paraIdx++, request.getRowCount()); + preparedStatement.setInt(paraIdx, request.getStartIndex()); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { filteredDeviceIds.add(resultSet.getInt("DEVICE_ID")); @@ -3358,13 +3378,24 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public int getDeviceCountNotInGivenIdList(List deviceIds, int tenantId) throws DeviceManagementDAOException { int deviceCount = 0; - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); try { Connection connection = getConnection(); - String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE ID NOT IN " + - "(" + deviceIdStringList + ") AND TENANT_ID = ?"; + String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE TENANT_ID = ?"; + + if (deviceIds != null && !deviceIds.isEmpty()) { + sql += " AND ID NOT IN ( " + deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + } + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - preparedStatement.setInt(1, tenantId); + int paraIdx = 1; + preparedStatement.setInt(paraIdx++, tenantId); + + if (deviceIds != null && !deviceIds.isEmpty()) { + for (Integer deviceId : deviceIds) { + preparedStatement.setInt(paraIdx++, deviceId); + } + } + try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { deviceCount = resultSet.getInt("COUNT"); @@ -3383,7 +3414,9 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { public List getDevicesByDeviceIds(PaginationRequest paginationRequest, List deviceIds, int tenantId) throws DeviceManagementDAOException { List devices = new ArrayList<>(); - String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(",")); + 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; @@ -3409,11 +3442,14 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { isDeviceNameProvided = true; } - int parameterIdx = 1; 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()); if (isDeviceStatusProvided) 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/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 086bc4e509..6de1167704 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -5596,7 +5596,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesNotInGivenIdList(List deviceIds, PaginationRequest paginationRequest) throws DeviceManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (paginationRequest == null) { String msg = "Received null for pagination request";