From 57199a5d893413978935d20ab33ec04af7a2def2 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Mon, 22 Jul 2024 20:20:04 +0530 Subject: [PATCH] Fix device type filtering issue --- .../mgt/SubscriptionManagementHelperUtil.java | 6 ++++-- ...sedSubscriptionManagementHelperServiceImpl.java | 12 ++++++++++-- ...sedSubscriptionManagementHelperServiceImpl.java | 4 ++-- ...sedSubscriptionManagementHelperServiceImpl.java | 12 ++++++++++-- ...sedSubscriptionManagementHelperServiceImpl.java | 13 ++++++++++--- .../core/device/mgt/common/PaginationRequest.java | 9 +++++++++ .../mgt/core/dao/impl/AbstractDeviceDAOImpl.java | 14 +++++++++++--- 7 files changed, 56 insertions(+), 14 deletions(-) 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 ebc2e7f7cc..e5c3969329 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 @@ -40,25 +40,27 @@ import java.util.stream.Collectors; public class SubscriptionManagementHelperUtil { public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, - boolean isUnsubscribed, int limit, int offset) + boolean isUnsubscribed, int deviceTypeId, int limit, int offset) throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); PaginationRequest paginationRequest = new PaginationRequest(offset, limit); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + paginationRequest.setDeviceTypeId(deviceTypeId); List devices = HelperUtil.getDeviceManagementProviderService().getDevicesByDeviceIds(paginationRequest, deviceIds); return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed); } public static int getTotalDeviceSubscriptionCount(List deviceSubscriptionDTOS, - DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria, int deviceTypeId) throws DeviceManagementException { List deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); PaginationRequest paginationRequest = new PaginationRequest(-1, -1); paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName()); paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus()); paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner()); + paginationRequest.setDeviceTypeId(deviceTypeId); return HelperUtil.getDeviceManagementProviderService().getDeviceCountByDeviceIds(paginationRequest, deviceIds); } 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 51871d28fc..b0fc7c97bd 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 @@ -25,6 +25,7 @@ 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.SubscriptionResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; +import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -76,6 +77,13 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -101,10 +109,10 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; 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 cac63111a6..1a78df119c 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 @@ -131,10 +131,10 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (GroupManagementException e) { String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); 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 9ed856c09e..b5d527676e 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 @@ -26,6 +26,7 @@ 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.SubscriptionResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; +import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; @@ -86,6 +87,13 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -113,11 +121,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } List deviceSubscriptions = SubscriptionManagementHelperUtil. getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (UserStoreException e) { 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 8b548b2fbb..83bc8c08e2 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 @@ -26,6 +26,7 @@ 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.SubscriptionResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics; +import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO; @@ -83,6 +84,13 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri throw new NotFoundException(msg); } + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); @@ -110,11 +118,10 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1); deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), applicationDTO.getDeviceTypeId()); } - List deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, - subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, limit, offset); + subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset); return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions); } catch (DeviceManagementException e) { String msg = "Error encountered while getting device details"; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java index 95b8a92c6c..c4e01eb282 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/PaginationRequest.java @@ -54,6 +54,7 @@ public class PaginationRequest { private List statusList = new ArrayList<>(); private OperationLogFilters operationLogFilters = new OperationLogFilters(); private List sortColumn = new ArrayList<>(); + private int deviceTypeId; public OperationLogFilters getOperationLogFilters() { return operationLogFilters; } @@ -292,4 +293,12 @@ public class PaginationRequest { public void setTabActionStatus(String tabActionStatus) { this.tabActionStatus = tabActionStatus; } + + public int getDeviceTypeId() { + return deviceTypeId; + } + + public void setDeviceTypeId(int deviceTypeId) { + this.deviceTypeId = deviceTypeId; + } } 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 99b942ca3a..7ae29f29d8 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 @@ -3424,7 +3424,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { 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 " + - "WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "ON d.ID = e.DEVICE_ID " + + "WHERE d.DEVICE_TYPE_ID = ? " + + "AND d.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { @@ -3446,6 +3449,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { int parameterIdx = 1; + preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId()); preparedStatement.setInt(parameterIdx++, tenantId); for (Integer deviceId : deviceIds) { @@ -3501,8 +3505,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { boolean isDeviceNameProvided = false; try { Connection connection = getConnection(); - String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_ENROLMENT e " + - "WHERE e.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + String sql = "SELECT COUNT(DISTINCT e.DEVICE_ID) FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " + + "ON d.ID = e.DEVICE_ID " + + "WHERE e.TENANT_ID = ? " + + "AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " + + "AND d.DEVICE_TYPE_ID = ? " + "AND e.STATUS NOT IN ('DELETED', 'REMOVED')"; if (paginationRequest.getOwner() != null) { @@ -3528,6 +3535,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { preparedStatement.setInt(parameterIdx++, deviceId); } + preparedStatement.setInt(parameterIdx++, paginationRequest.getDeviceTypeId()); if (isOwnerProvided) preparedStatement.setString(parameterIdx++, "%" + paginationRequest.getOwner() + "%"); if (isDeviceStatusProvided)