From ad2a558d5c8bbe0f0dacf73089e68b95a1605067 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Mon, 11 Nov 2024 18:37:09 +0530 Subject: [PATCH] Add validation for having pending operations while app installation --- .../core/impl/SubscriptionManagerImpl.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 92f0734c9f..556d19b0e3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -180,6 +180,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); ApplicationSubscriptionInfo applicationSubscriptionInfo = getAppSubscriptionInfo(applicationDTO, subType, params); + validatePendingAppSubscription(applicationSubscriptionInfo, applicationDTO); performExternalStoreSubscription(applicationDTO, applicationSubscriptionInfo); ApplicationInstallResponse applicationInstallResponse = performActionOnDevices( applicationSubscriptionInfo.getAppSupportingDeviceTypeName(), applicationSubscriptionInfo.getDevices(), @@ -707,6 +708,37 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + private void validatePendingAppSubscription(ApplicationSubscriptionInfo applicationSubscriptionInfo, ApplicationDTO applicationDTO) throws BadRequestException, ApplicationManagementException{ + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); + try { + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseId, tenantId, null, null); + List deviceIdList = new ArrayList<>(); + deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> { + if (!deviceSubscriptionDTO.isUnsubscribed() && + Operation.Status.PENDING.toString().equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) { + deviceIdList.add(deviceSubscriptionDTO.getDeviceId()); + } + }); + + List subscriptionDeviceIds = applicationSubscriptionInfo.getDevices().stream() + .map(Device::getId) + .collect(Collectors.toList()); + boolean allDevicesPending = subscriptionDeviceIds.stream() + .allMatch(deviceIdList::contains); + if (allDevicesPending) { + String msg = "All devices in the subscription have pending operations for this application."; + log.error(msg); + throw new BadRequestException(msg); + } + } catch (ApplicationManagementDAOException e) { + String msg = "Error while retrieving device subscriptions"; + log.error(msg, e); + throw new ApplicationManagementException("Error while validating application subscription", e); + } + } + /** * This method perform given action (i.e APP INSTALL or APP UNINSTALL) on given set of devices. *