From f03c3a05176649a0831ec5be4cc9f95073c6e3fd Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 23 Apr 2020 01:31:41 +0530 Subject: [PATCH] Modify app release deleting functionality --- .../mgt/core/impl/ApplicationManagerImpl.java | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index c28f42b979..872da89ae4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -101,6 +101,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -1442,39 +1443,54 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void deleteApplicationRelease(String releaseUuid) - throws ApplicationManagementException { + public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + AtomicBoolean isDeletingApp = new AtomicBoolean(false); try { ConnectionManagerUtil.beginDBTransaction(); - ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO - .getReleaseByUUID(releaseUuid, tenantId); - if (applicationReleaseDTO == null) { - String msg = "Couldn't find an application release for application release UUID: " + releaseUuid; + ApplicationDTO applicationDTO = this.applicationDAO.getApplication(releaseUuid, tenantId); + if (applicationDTO == null) { + String msg = "Couldn't find an application which has application release UUID: " + releaseUuid; log.error(msg); throw new NotFoundException(msg); } - if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { - String msg = "Application state is not in the deletable state. Therefore you are not permitted to " - + "delete the application release."; - log.error(msg); - throw new ForbiddenException(msg); + List applicationReleaseDTOS = applicationDTO.getApplicationReleaseDTOs(); + if (applicationReleaseDTOS.size() == 1) { + isDeletingApp.set(true); } - List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); - if (!deviceSubscriptionDTOS.isEmpty()){ - String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + - " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " - + "permitted to delete the application release."; - log.error(msg); - throw new ForbiddenException(msg); + + for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOS) { + if (releaseUuid.equals(applicationReleaseDTO.getUuid())) { + if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { + String msg = + "Application state is not in the deletable state. Therefore you are not permitted to " + + "delete the application release."; + log.error(msg); + throw new ForbiddenException(msg); + } + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); + if (!deviceSubscriptionDTOS.isEmpty()) { + String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + + " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " + + "permitted to delete the application release."; + log.error(msg); + throw new ForbiddenException(msg); + } + applicationStorageManager.deleteAllApplicationReleaseArtifacts( + Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); + lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); + applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); + break; + } + } + if (isDeletingApp.get()) { + this.applicationDAO.deleteApplicationTags(applicationDTO.getId(), tenantId); + this.applicationDAO.deleteAppCategories(applicationDTO.getId(), tenantId); + this.applicationDAO.deleteApplication(applicationDTO.getId(), tenantId); } - applicationStorageManager.deleteAllApplicationReleaseArtifacts( - Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId); - lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); - applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { String msg = "Error occurred while observing the database connection to delete application release which "