Add validation for having pending operations while app installation

pull/550/head
prathabanKavin 3 weeks ago
parent 7d91cfff3c
commit ad2a558d5c

@ -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 <T> void validatePendingAppSubscription(ApplicationSubscriptionInfo applicationSubscriptionInfo, ApplicationDTO applicationDTO) throws BadRequestException, ApplicationManagementException{
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId();
try {
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
.getDeviceSubscriptions(applicationReleaseId, tenantId, null, null);
List<Integer> deviceIdList = new ArrayList<>();
deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> {
if (!deviceSubscriptionDTO.isUnsubscribed() &&
Operation.Status.PENDING.toString().equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) {
deviceIdList.add(deviceSubscriptionDTO.getDeviceId());
}
});
List<Integer> 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.
*

Loading…
Cancel
Save