From 4c5cdc75fe7bcfd2def3d865e7a9cb54d5487a82 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sat, 15 Sep 2018 22:44:24 +0530 Subject: [PATCH] Adding application release creating logic --- .../common/services/ApplicationManager.java | 8 +-- .../mgt/core/impl/ApplicationManagerImpl.java | 56 +++++++++++++------ .../impl/ApplicationManagementAPIImpl.java | 6 +- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index cccf451aced..37070b38e4c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -153,20 +153,20 @@ public interface ApplicationManager { void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException; /** - * To validate the application existence for given application id + * Get the application if application is an accessible one. * * @param applicationId ID of the Application. * @throws ApplicationManagementException Application Management Exception. */ - Application validateApplication(int applicationId) throws ApplicationManagementException; + Application getApplicationIfAccessible(int applicationId) throws ApplicationManagementException; /** - * To validate the application release existence for given application release UUID + * Get the application release for given UUID if application release is exists and application id is valid one. * * @param releaseUuid UUID of the Application Release. * @throws ApplicationManagementException Application Management Exception. */ - ApplicationRelease validateApplicationRelease(int applicationId, String releaseUuid) throws + ApplicationRelease getAppReleaseIfExists(int applicationId, String releaseUuid) throws ApplicationManagementException; /** 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 6ffa3699604..fcfa5f4225d 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 @@ -70,6 +70,8 @@ public class ApplicationManagerImpl implements ApplicationManager { private DeviceTypeDAO deviceTypeDAO; private VisibilityDAO visibilityDAO; private ApplicationDAO applicationDAO; + private LifecycleStateDAO lifecycleStateDAO; + public ApplicationManagerImpl() { initDataAccessObjects(); @@ -79,6 +81,7 @@ public class ApplicationManagerImpl implements ApplicationManager { this.deviceTypeDAO = ApplicationManagementDAOFactory.getDeviceTypeDAO(); this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); + this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();; } @Override @@ -134,9 +137,10 @@ public class ApplicationManagerImpl implements ApplicationManager { lifecycleState.setUpdatedBy(userName); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); + lifecycleState.setUpdatedBy(userName); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); - LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - lifecycleStateDAO.addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId); + this.lifecycleStateDAO + .addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -209,21 +213,36 @@ public class ApplicationManagerImpl implements ApplicationManager { public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - Application application = validateApplication(applicationId); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + Application application = getApplicationIfAccessible(applicationId); validateReleaseCreatingRequest(applicationRelease); if (log.isDebugEnabled()) { log.debug("Application release request is received for the application " + application.toString()); } try { ConnectionManagerUtil.beginDBTransaction(); -// todo consider about lifecycle adding applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). createRelease(applicationRelease, application.getId(), tenantId); + LifecycleState lifecycleState = new LifecycleState(); + lifecycleState.setUpdatedBy(userName); + lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); + lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); + lifecycleState.setUpdatedBy(userName); + addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); + this.lifecycleStateDAO + .addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - throw e; + throw new ApplicationManagementException( + "Error occured while adding application release into IoTS app management Application id of the " + + "application release: " + applicationId, e); + } catch (LifeCycleManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationManagementException( + "Error occured while adding application release lifecycle state to IoTS app management. " + + "Application id of the application release: " + applicationId, e); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -353,7 +372,7 @@ public class ApplicationManagerImpl implements ApplicationManager { public List getReleases(int applicationId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - Application application = validateApplication(applicationId); + Application application = getApplicationIfAccessible(applicationId); List applicationReleases; List filteredApplicationReleases = new ArrayList<>(); if (log.isDebugEnabled()) { @@ -393,7 +412,7 @@ public class ApplicationManagerImpl implements ApplicationManager { "need to have admin permission"); } - if (validateApplication(applicationId) == null) { + if (getApplicationIfAccessible(applicationId) == null) { throw new ApplicationManagementException("Invalid Application"); } List applicationReleases = getReleases(applicationId); @@ -427,11 +446,11 @@ public class ApplicationManagerImpl implements ApplicationManager { throws ApplicationManagementException { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - Application application = validateApplication(applicationId); + Application application = getApplicationIfAccessible(applicationId); if (application == null) { throw new ApplicationManagementException("Invalid Application ID is received"); } - ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, releaseUuid); + ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, releaseUuid); if (applicationRelease == null) { throw new ApplicationManagementException("Invalid Application Release UUID is received"); } @@ -537,12 +556,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } /** - * To validate the pre-request of the ApplicationRelease. + * Get the application if application is an accessible one. * * @param applicationId ID of the Application. * @return Application related with the UUID */ - public Application validateApplication(int applicationId) throws ApplicationManagementException { + public Application getApplicationIfAccessible(int applicationId) throws ApplicationManagementException { if (applicationId <= 0) { throw new ApplicationManagementException("Application id could,t be a negative integer. Hence please add " + "valid application id."); @@ -568,7 +587,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } if (!isAppAllowed) { - throw new NotFoundException("Application of the " + applicationId + " does not exist."); + throw new NotFoundException("Application of the " + applicationId + + " does not exist. Please check whether user have permissions to access the application."); } return application; } catch (UserStoreException e) { @@ -580,12 +600,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } /** - * To validate the pre-request of the ApplicationRelease. + * Get the application release for given UUID if application release is exists and application id is valid one. * * @param applicationUuid UUID of the Application. * @return Application related with the UUID */ - public ApplicationRelease validateApplicationRelease(int applicationId, String applicationUuid) throws + public ApplicationRelease getAppReleaseIfExists(int applicationId, String applicationUuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); ApplicationRelease applicationRelease; @@ -694,7 +714,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.openDBConnection(); LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, applicationUuid); + ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState())); } catch (ApplicationManagementDAOException e) { @@ -712,8 +732,8 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationManagementException { try { ConnectionManagerUtil.openDBConnection(); - Application application = validateApplication(applicationId); - ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, applicationUuid); + Application application = getApplicationIfAccessible(applicationId); + ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); LifecycleStateDAO lifecycleStateDAO; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -828,7 +848,7 @@ public class ApplicationManagerImpl implements ApplicationManager { public Application updateApplication(Application application) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - Application existingApplication = validateApplication(application.getId()); + Application existingApplication = getApplicationIfAccessible(application.getId()); List addingRoleList; List removingRoleList; List addingTags; 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/ApplicationManagementAPIImpl.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/ApplicationManagementAPIImpl.java index 7c4fe746c35..653bc60f5e8 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/ApplicationManagementAPIImpl.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/ApplicationManagementAPIImpl.java @@ -208,7 +208,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { attachments.add(screenshot.getDataHandler().getInputStream()); } } - applicationRelease = applicationManager.validateApplicationRelease(appId, applicationUuid); + applicationRelease = applicationManager.getAppReleaseIfExists(appId, applicationUuid); LifecycleState lifecycleState = applicationManager.getLifecycleState(appId, applicationRelease.getUuid()); if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState()) || AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) { @@ -260,7 +260,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse("Uploading artifacts for the application is failed " + applicationUuid, Response.Status.BAD_REQUEST); } - applicationRelease = applicationManager.validateApplicationRelease(applicationId, applicationUuid); + applicationRelease = applicationManager.getAppReleaseIfExists(applicationId, applicationUuid); applicationRelease = applicationStorageManager .updateReleaseArtifacts(applicationRelease, appType, deviceType, binaryFile.getDataHandler().getInputStream()); @@ -323,7 +323,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { List attachments = new ArrayList<>(); try { - Application application = applicationManager.validateApplication(applicationId); + Application application = applicationManager.getApplicationIfAccessible(applicationId); if (!applicationManager.isApplicationReleaseUpdateAcceptable(application.getId(), applicationRelease.getUuid())) {