From 6d4252a5dd3d478c4f60e3c459bb648998f38b93 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 6 May 2019 21:53:54 +0530 Subject: [PATCH] Fix issues in lifecycle adding method --- .../mgt/core/dao/ApplicationReleaseDAO.java | 2 +- .../GenericApplicationReleaseDAOImpl.java | 2 +- .../mgt/core/impl/ApplicationManagerImpl.java | 73 ++++++++----------- ...ApplicationManagementPublisherAPIImpl.java | 6 +- 4 files changed, 32 insertions(+), 51 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index 176f37dcae..a97fe2d08b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -181,7 +181,7 @@ public interface ApplicationReleaseDAO { boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId, String inactiveState) throws ApplicationManagementDAOException; - boolean hasExisitInstallableAppRelease(String releaseUuid, String installableStateName, int tenantId) + boolean hasExistInstallableAppRelease(String releaseUuid, String installableStateName, int tenantId) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 2eb26a105f..d3ba1cdaa8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -753,7 +753,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } @Override - public boolean hasExisitInstallableAppRelease(String releaseUuid, String installableStateName, int tenantId) + public boolean hasExistInstallableAppRelease(String releaseUuid, String installableStateName, int tenantId) throws ApplicationManagementDAOException{ if (log.isDebugEnabled()) { log.debug("Verifying application release existence in the installable state: :" + installableStateName); 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 cfe646d12f..c5384ddcba 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 @@ -1598,65 +1598,50 @@ public class ApplicationManagerImpl implements ApplicationManager { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { ConnectionManagerUtil.beginDBTransaction(); - ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(releaseUuid, tenantId); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); - if (applicationReleaseDTO == null ){ + if (applicationReleaseDTO == null) { String msg = "Couldn't found an application release for the UUID: " + releaseUuid; log.error(msg); throw new NotFoundException(msg); } - if (lifecycleStateManager.isValidStateChange(applicationReleaseDTO.getCurrentState(), stateName, userName, tenantId)){ + if (lifecycleStateManager + .isValidStateChange(applicationReleaseDTO.getCurrentState(), stateName, userName, tenantId)) { + if (lifecycleStateManager.isInstallableState(stateName) && applicationReleaseDAO + .hasExistInstallableAppRelease(applicationReleaseDTO.getUuid(), + lifecycleStateManager.getInstallableState(), tenantId)) { + String msg = "Installable application release is already registered for the application. " + + "Therefore it is not permitted to change the lifecycle state from " + + applicationReleaseDTO.getCurrentState() + " to " + stateName; + log.error(msg); + throw new ForbiddenException(msg); + } LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(stateName); lifecycleState.setPreviousState(applicationReleaseDTO.getCurrentState()); lifecycleState.setUpdatedBy(userName); + applicationReleaseDTO.setCurrentState(stateName); + this.applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId); this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId); - - } - - - - if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)) { - throw new NotFoundException("Couldn't find application for the application Id: " + applicationId); - } - if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)) { - throw new NotFoundException("Couldn't find application release for the application Id: " + applicationId - + " application release uuid: " + releaseUuid); - } - LifecycleState currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); - if (currentState == null) { - throw new ApplicationManagementException( - "Couldn't find latest lifecycle state for the appId: " + applicationId - + " and application release UUID: " + releaseUuid); - } - stateName.setPreviousState(currentState.getCurrentState()); - - - if (stateName.getCurrentState() != null && stateName.getPreviousState() != null) { - if (lifecycleStateManager.isValidStateChange(stateName.getPreviousState(), stateName.getCurrentState(), - userName, tenantId)) { - //todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there - //todo any other application release in PUBLISHED state for the application( i.e for the appid) - this.lifecycleStateDAO.addLifecycleState(stateName, applicationId, releaseUuid, tenantId); - ConnectionManagerUtil.commitDBTransaction(); - } else { - ConnectionManagerUtil.rollbackDBTransaction(); - log.error("Invalid lifecycle state transition from '" + stateName.getPreviousState() + "'" + " to '" - + stateName.getCurrentState() + "'"); - throw new ApplicationManagementException( - "Lifecycle State Validation failed. ApplicationDTO Id: " + applicationId - + " ApplicationDTO release UUID: " + releaseUuid); - } + } else { + String msg = "Invalid lifecycle state transition from '" + applicationReleaseDTO.getCurrentState() + "'" + + " to '" + stateName + "'"; + log.error(msg); + throw new ApplicationManagementException(msg); } } catch (LifeCycleManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException( - "Failed to add lifecycle state. ApplicationDTO Id: " + applicationId + " ApplicationDTO release UUID: " - + releaseUuid, e); + String msg = "Failed to add lifecycle state for Application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); + ConnectionManagerUtil.rollbackDBTransaction(); + String msg = "Error occurred when accessing application release data of application release which has the " + + "application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg); } finally { ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index 33b6f800d6..06956b243b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -478,9 +478,6 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } -/* -//todo ---------------------- -*/ @POST @Path("/life-cycle/{uuid}") public Response addLifecycleState( @@ -496,8 +493,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem applicationManager.changeLifecycleState( applicationUuid, action); } catch (NotFoundException e) { - String msg = "Could,t find application release for application id: " + applicationId - + " and application release uuid: " + applicationUuid; + String msg = "Could,t find application release for application release uuid: " + applicationUuid; log.error(msg, e); return Response.status(Response.Status.NOT_FOUND).build(); } catch (ApplicationManagementException e) {