From 9b1820dca53decf8a7a039f5295d5b5cfbc513b6 Mon Sep 17 00:00:00 2001 From: lasantha Date: Wed, 14 Feb 2018 16:51:49 +0530 Subject: [PATCH] Adding Lifecycle Management functionalities and fixed related bugs --- .../mgt/common/AppLifecycleState.java | 28 ++ .../mgt/common/LifecycleState.java | 10 + .../common/services/ApplicationManager.java | 23 +- .../services/LifecycleStateManager.java | 5 +- .../mgt/core/dao/LifecycleStateDAO.java | 2 +- .../GenericLifecycleStateImpl.java | 4 +- .../mgt/core/impl/ApplicationManagerImpl.java | 328 ++++++++++++++---- .../impl/ApplicationStorageManagerImpl.java | 29 +- .../core/impl/LifecycleStateManagerImpl.java | 173 +++++++-- .../core/util/ValidateApplicationUtil.java | 78 +++++ .../services/ApplicationManagementAPI.java | 25 +- .../api/services/LifecycleManagementAPI.java | 7 +- .../impl/ApplicationManagementAPIImpl.java | 75 ++-- .../impl/LifecycleManagementAPIImpl.java | 44 ++- .../src/main/webapp/META-INF/permissions.xml | 53 +-- 15 files changed, 639 insertions(+), 245 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppLifecycleState.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ValidateApplicationUtil.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppLifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppLifecycleState.java new file mode 100644 index 0000000000..3d44c6b54b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppLifecycleState.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.common; + + +/** + * States of the Application. + */ +public enum AppLifecycleState { + CREATED, IN_REVIEW, PUBLISHED, APPROVED, UNPUBLISHED, REJECTED, DEPRECATED, REMOVED +} + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java index e62de0b0d9..fdb1a27c51 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.application.mgt.common; import java.sql.Timestamp; +import java.util.List; public class LifecycleState { @@ -28,6 +29,8 @@ public class LifecycleState { private String previousState; + private List nextStates; + private String updatedBy; private Timestamp updatedAt; @@ -102,4 +105,11 @@ public class LifecycleState { this.appId = appId; } + public List getNextStates() { + return nextStates; + } + + public void setNextStates(List nextStates) { + this.nextStates = nextStates; + } } 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 6994ae608f..8c10189a8f 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 @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import java.util.List; @@ -31,6 +32,7 @@ public interface ApplicationManager { /** * Creates an application. + * * @param application Application that need to be created. * @return Created application * @throws ApplicationManagementException Application Management Exception @@ -39,6 +41,7 @@ public interface ApplicationManager { /** * Updates an already existing application. + * * @param application Application that need to be updated. * @return Updated Application * @throws ApplicationManagementException Application Management Exception @@ -47,13 +50,24 @@ public interface ApplicationManager { /** * Delete an application identified by the unique ID. - * @param uuid Unique ID for tha application + * + * @param applicationId ID for tha application * @throws ApplicationManagementException Application Management Exception */ - void deleteApplication(String uuid) throws ApplicationManagementException; + void deleteApplication(int applicationId) throws ApplicationManagementException; + + /** + * Delete an application identified by the unique ID. + * + * @param applicationId ID of tha application + * @param releaseUuid UUID of tha application release + * @throws ApplicationManagementException Application Management Exception + */ + void deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException; /** * To get the applications based on the search filter. + * * @param filter Search filter * @return Applications that matches the given filter criteria. * @throws ApplicationManagementException Application Management Exception @@ -62,6 +76,7 @@ public interface ApplicationManager { /** * To get the applications based on the search filter. + * * @param appId id of the application * @return Application release which is published and release of the Application(appId). * @throws ApplicationManagementException Application Management Exception @@ -135,5 +150,9 @@ public interface ApplicationManager { */ List getReleases(int applicationId) throws ApplicationManagementException; + LifecycleState getLifecycleState(int appReleaseId, String applicationUuid) throws LifecycleManagementException; + + void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws LifecycleManagementException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java index 0e2e8a5c54..712a58ac57 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/LifecycleStateManager.java @@ -28,9 +28,8 @@ import java.util.List; */ public interface LifecycleStateManager { - List getLifecycleStates(int appReleaseId) throws LifecycleManagementException; + LifecycleState getLifecycleState(int appReleaseId, String applicationUuid) throws LifecycleManagementException; - void addLifecycleState(LifecycleState state) throws LifecycleManagementException; + void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws LifecycleManagementException; - void deleteLifecycleState(int identifier) throws LifecycleManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java index bc5ca76903..d9d12356ec 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java @@ -29,7 +29,7 @@ import java.util.List; */ public interface LifecycleStateDAO { - LifecycleState getLatestLifeCycleStateByReleaseID(int identifier) throws ApplicationManagementDAOException; + LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException; List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; 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/lifecyclestate/GenericLifecycleStateImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java index 216a95a1e3..9effb6c1ed 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateImpl.java @@ -39,7 +39,7 @@ import java.util.List; public class GenericLifecycleStateImpl extends AbstractDAOImpl implements LifecycleStateDAO { @Override - public LifecycleState getLatestLifeCycleStateByReleaseID(int identifier) throws ApplicationManagementDAOException { + public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException { Connection conn = null; PreparedStatement stmt = null; @@ -50,7 +50,7 @@ public class GenericLifecycleStateImpl extends AbstractDAOImpl implements Lifecy + "AP_APP_LIFECYCLE_STATE WHERE AP_APP_RELEASE_ID=? ORDER BY UPDATED_AT DESC;"; stmt = conn.prepareStatement(sql); - stmt.setInt(1, identifier); + stmt.setInt(1, applicationReleaseId); rs = stmt.executeQuery(); LifecycleState lifecycleState = null; 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 3c47908545..3a8a791f83 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 @@ -25,11 +25,15 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; @@ -67,7 +71,8 @@ public class ApplicationManagerImpl implements ApplicationManager { this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); } - @Override public Application createApplication(Application application) throws ApplicationManagementException { + @Override + public Application createApplication(Application application) throws ApplicationManagementException { User loggedInUser = new User(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true)); @@ -130,7 +135,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { + @Override + public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); ApplicationList applicationList; @@ -159,7 +165,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException { + @Override + public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException { try { ConnectionManagerUtil.openDBConnection(); return applicationDAO.getUuidOfLatestRelease(appId); @@ -172,7 +179,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private boolean isRoleExists(List unrestrictedRoleList, String userName) throws UserStoreException { String[] roleList; - roleList = getRoleOfUser(userName); + roleList = getRolesOfUser(userName); for (UnrestrictedRole unrestrictedRole : unrestrictedRoleList) { for (String role : roleList) { if (unrestrictedRole.getRole().equals(role)) { @@ -183,7 +190,7 @@ public class ApplicationManagerImpl implements ApplicationManager { return false; } - private String[] getRoleOfUser(String userName) throws UserStoreException { + private String[] getRolesOfUser(String userName) throws UserStoreException { UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); String[] roleList = {}; if (userRealm != null) { @@ -194,7 +201,8 @@ public class ApplicationManagerImpl implements ApplicationManager { return roleList; } - @Override public Application getApplication(String appType, String appName) throws ApplicationManagementException { + @Override + public Application getApplication(String appType, String appName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; @@ -231,7 +239,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override public Application getApplicationById(int applicationId) throws ApplicationManagementException { + @Override + public Application getApplicationById(int applicationId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application; @@ -285,7 +294,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - @Override public List getReleases(int applicationId) throws ApplicationManagementException { + @Override + public List getReleases(int applicationId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Application application = validateApplication(applicationId); @@ -300,7 +310,7 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleases = ApplicationManagementDAOFactory.getApplicationReleaseDAO() .getApplicationReleases(application.getName(), application.getType(), tenantId); for (ApplicationRelease applicationRelease : applicationReleases) { - if (!"REMOVED".equals(ApplicationManagementDAOFactory.getLifecycleStateDAO(). + if (!AppLifecycleState.REMOVED.toString().equals(ApplicationManagementDAOFactory.getLifecycleStateDAO(). getLatestLifeCycleStateByReleaseID(applicationRelease.getId()).getCurrentState())) { filteredApplicationReleases.add(applicationRelease); } @@ -311,32 +321,52 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - /** - * To check whether current user is application owner or admin. - * - * @param applicationUUID UUID of the Application. - * @return true if the current user is application owner or admin, unless false. - * @throws ApplicationManagementException Application Management Exception. - */ - private boolean isApplicationOwnerOrAdmin(String applicationUUID, String userName, int tenantId) - throws ApplicationManagementException { - // try { - // if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - // return true; - // } - // } catch (UserStoreException e) { - // throw new ApplicationManagementException("Userstore exception while checking whether user is an admin", e); - // } - // try { - // ConnectionManagerUtil.openDBConnection(); - // Application application = ApplicationManagementDAOFactory.getApplicationDAO() - // .getApplication(applicationUUID, tenantId, userName); - // return application.getUser().getUserName().equals(userName) - // && application.getUser().getTenantId() == tenantId; - // } finally { - // ConnectionManagerUtil.closeDBConnection(); - // } - return false; + @Override + public void deleteApplication(int applicationId) throws ApplicationManagementException { + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + + if (validateApplication(applicationId) == null) { + throw new ApplicationManagementException("Invalid Application"); + } + List applicationReleases = getReleases(applicationId); + if (log.isDebugEnabled()) { + log.debug("Request is received to delete applications which are related with the application id " + + applicationId); + } + for (ApplicationRelease applicationRelease : applicationReleases) { + LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); + LifecycleState newAppLifecycleState = new LifecycleState(); + newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); + newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); + newAppLifecycleState.setTenantId(tenantId); + newAppLifecycleState.setUpdatedBy(userName); + addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); + } + //todo add column into application and move application into removed state + } + + @Override + public void deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException { + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Application application = validateApplication(applicationId); + + if (application == null) { + throw new ApplicationManagementException("Invalid Application ID is received"); + } + ApplicationRelease applicationRelease = validateApplicationRelease(releaseUuid); + if (applicationRelease == null) { + throw new ApplicationManagementException("Invalid Application Release UUID is received"); + } + + LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); + LifecycleState newAppLifecycleState = new LifecycleState(); + newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); + newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); + newAppLifecycleState.setTenantId(tenantId); + newAppLifecycleState.setUpdatedBy(userName); + addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); } /** @@ -381,6 +411,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + /** + * To validate the application existence + * + * @param application Application that need to be validated + * @throws ValidationException Validation Exception + */ private void validateApplicationExistence(Application application) throws ApplicationManagementException { Filter filter = new Filter(); filter.setFullMatch(true); @@ -415,6 +451,26 @@ public class ApplicationManagerImpl implements ApplicationManager { return application; } + /** + * To validate the pre-request of the ApplicationRelease. + * + * @param applicationUuid UUID of the Application. + * @return Application related with the UUID + */ + private ApplicationRelease validateApplicationRelease(String applicationUuid) throws ApplicationManagementException { + if (applicationUuid == null) { + throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + + "parameter to get the relevant application."); + } + ApplicationRelease applicationRelease = DataHolder.getInstance().getApplicationReleaseManager() + .getReleaseByUuid(applicationUuid); + if (applicationRelease == null) { + throw new NotFoundException( + "Application with UUID " + applicationUuid + " does not exist."); + } + return applicationRelease; + } + /** * To get role restricted application list. * @@ -470,7 +526,146 @@ public class ApplicationManagerImpl implements ApplicationManager { // } } - @Override public Application editApplication(Application application) throws ApplicationManagementException { + @Override + public LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws LifecycleManagementException { + LifecycleState lifecycleState; + try { + ConnectionManagerUtil.openDBConnection(); + LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); + Application application = validateApplication(applicationId); + //todo applicationUuid and applicationId should be passed and util method has to be changed + ApplicationRelease applicationRelease = validateApplicationRelease(applicationUuid); + lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState())); + } catch (ApplicationManagementDAOException e) { + throw new LifecycleManagementException("Failed to get lifecycle state", e); + } catch (DBConnectionException e) { + throw new LifecycleManagementException("Failed to connect with Database", e); + } catch (ApplicationManagementException e) { + throw new LifecycleManagementException("Failed to get application and application management", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + return lifecycleState; + } + + @Override + public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws LifecycleManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + Application application = validateApplication(applicationId); + //todo applicationUuid and applicationId should be passed and util method has to be changed + ApplicationRelease applicationRelease = validateApplicationRelease(applicationUuid); + LifecycleStateDAO lifecycleStateDAO; + + if (application != null) { + state.setAppId(applicationId); + } + if (applicationRelease != null) { + state.setReleaseId(applicationRelease.getId()); + } + if (state.getCurrentState() != null && state.getPreviousState() != null && state.getUpdatedBy() != null) { + validateLifecycleState(state); + lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.addLifecycleState(state); + } + } catch (LifeCycleManagementDAOException | DBConnectionException e) { + throw new LifecycleManagementException("Failed to add lifecycle state", e); + } catch (ApplicationManagementException e) { + throw new LifecycleManagementException("Lifecycle State Validation failed", e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + private List getNextLifecycleStates(String currentLifecycleState) { + List nextLifecycleStates = new ArrayList<>(); + if (AppLifecycleState.CREATED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.IN_REVIEW.toString()); + } + if (AppLifecycleState.IN_REVIEW.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.APPROVED.toString()); + nextLifecycleStates.add(AppLifecycleState.REJECTED.toString()); + } + if (AppLifecycleState.REJECTED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.IN_REVIEW.toString()); + nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); + } + if (AppLifecycleState.APPROVED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.PUBLISHED.toString()); + } + if (AppLifecycleState.PUBLISHED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.UNPUBLISHED.toString()); + nextLifecycleStates.add(AppLifecycleState.DEPRECATED.toString()); + } + if (AppLifecycleState.UNPUBLISHED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.PUBLISHED.toString()); + nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); + } + if (AppLifecycleState.DEPRECATED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); + } + return nextLifecycleStates; + } + + private void validateLifecycleState(LifecycleState state) throws LifecycleManagementException { + + if (AppLifecycleState.CREATED.toString().equals(state.getCurrentState())) { + throw new LifecycleManagementException("Current State Couldn't be " + state.getCurrentState()); + } + if (AppLifecycleState.IN_REVIEW.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.CREATED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.REJECTED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be either " + AppLifecycleState.CREATED.toString() + " or " + + AppLifecycleState.REJECTED.toString()); + } + } + if (AppLifecycleState.APPROVED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.IN_REVIEW.toString()); + } + } + if (AppLifecycleState.PUBLISHED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.APPROVED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be either " + AppLifecycleState.APPROVED.toString() + " or " + + AppLifecycleState.UNPUBLISHED.toString()); + } + } + if (AppLifecycleState.UNPUBLISHED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.PUBLISHED.toString()); + } + } + if (AppLifecycleState.REJECTED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.IN_REVIEW.toString()); + } + } + if (AppLifecycleState.DEPRECATED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.PUBLISHED.toString()); + } + } + if (AppLifecycleState.REMOVED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.DEPRECATED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.REJECTED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be either " + AppLifecycleState.DEPRECATED.toString() + " or " + + AppLifecycleState.REJECTED.toString() + " or " + AppLifecycleState.UNPUBLISHED.toString()); + } + } + } + + @Override + public Application editApplication(Application application) throws ApplicationManagementException { // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); // int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); // if (application.getUuid() == null) { @@ -531,34 +726,8 @@ public class ApplicationManagerImpl implements ApplicationManager { return application; } - @Override public void deleteApplication(String uuid) throws ApplicationManagementException { - // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - // int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - // if (!isApplicationOwnerOrAdmin(uuid, userName, tenantId)) { - // throw new ApplicationManagementException("User '" + userName + "' of tenant - " + tenantId + " does have" - // + " the permission to delete the application with UUID " + uuid); - // } - // try { - // ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); - // ConnectionManagerUtil.beginDBTransaction(); - // int appId = applicationDAO.getApplicationId(uuid, tenantId); - // if (appId != -1) { - // applicationDAO.deleteTags(appId); - // applicationDAO.deleteProperties(appId); - // DataHolder.getInstance().getVisibilityManager().remove(appId); - // applicationDAO.deleteApplication(uuid, tenantId); - // } - // ConnectionManagerUtil.commitDBTransaction(); - // } catch (ApplicationManagementDAOException e) { - // ConnectionManagerUtil.rollbackDBTransaction(); - // String msg = "Failed to delete application: " + uuid; - // throw new ApplicationManagementException(msg, e); - // } finally { - // ConnectionManagerUtil.closeDBConnection(); - // } - } - - @Override public void changeLifecycle(String applicationUuid, String lifecycleIdentifier) + @Override + public void changeLifecycle(String applicationUuid, String lifecycleIdentifier) throws ApplicationManagementException { // boolean isAvailableNextState = false; // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -593,7 +762,8 @@ public class ApplicationManagerImpl implements ApplicationManager { // } } - @Override public List getLifeCycleStates(String applicationUUID) + @Override + public List getLifeCycleStates(String applicationUUID) throws ApplicationManagementException { // int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -649,4 +819,32 @@ public class ApplicationManagerImpl implements ApplicationManager { return null; } + + /** + * To check whether current user is application owner or admin. + * + * @param applicationUUID UUID of the Application. + * @return true if the current user is application owner or admin, unless false. + * @throws ApplicationManagementException Application Management Exception. + */ + private boolean isApplicationOwnerOrAdmin(String applicationUUID, String userName, int tenantId) + throws ApplicationManagementException { +// try { +// if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { +// return true; +// } +// } catch (UserStoreException e) { +// throw new ApplicationManagementException("Userstore exception while checking whether user is an admin", e); +// } +// try { +// ConnectionManagerUtil.openDBConnection(); +// Application application = ApplicationManagementDAOFactory.getApplicationDAO() +// .getApplication(applicationUUID, tenantId, userName); +// return application.getUser().getUserName().equals(userName) +// && application.getUser().getTenantId() == tenantId; +// } finally { +// ConnectionManagerUtil.closeDBConnection(); +// } + return false; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index dace15841d..bb8e370ed9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -68,8 +68,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public ApplicationRelease uploadImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFileStream, - InputStream bannerFileStream,List screenShotStreams) throws ResourceManagementException { - + InputStream bannerFileStream, List screenShotStreams) + throws ResourceManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String artifactDirectoryPath = null; String iconStoredLocation; @@ -77,26 +77,22 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager String scStoredLocation; try { - artifactDirectoryPath = storagePath + applicationRelease.getAppHashValue(); StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); iconStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0]; bannerStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[1]; - if (iconFileStream != null){ + if (iconFileStream != null) { saveFile(iconFileStream, iconStoredLocation); applicationRelease.setIconLoc(iconStoredLocation); } - - if (bannerFileStream != null){ + if (bannerFileStream != null) { saveFile(bannerFileStream, bannerStoredLocation); applicationRelease.setBannerLoc(bannerStoredLocation); } - - if (screenShotStreams.size() > screenShotMaxCount) { throw new ApplicationStorageManagementException("Maximum limit for the screen-shot exceeds"); - }else if(!screenShotStreams.isEmpty() && screenShotStreams.size() <= screenShotMaxCount){ + } else if (!screenShotStreams.isEmpty() && screenShotStreams.size() <= screenShotMaxCount) { int count = 1; for (InputStream screenshotStream : screenShotStreams) { scStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[2] + count; @@ -125,12 +121,11 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager + "update the screen-shot count for the application " + applicationRelease.getUuid() + " for the tenant id " + tenantId, e); } - } @Override public ApplicationRelease updateImageArtifacts(int applicationId, String uuid, InputStream iconFileStream, - InputStream bannerFileStream, List screenShotStreams) + InputStream bannerFileStream, List screenShotStreams) throws ResourceManagementException, ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -154,7 +149,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } @Override - public ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease , InputStream binaryFile) + public ApplicationRelease uploadReleaseArtifacts(ApplicationRelease applicationRelease, InputStream binaryFile) throws ResourceManagementException { String artifactDirectoryPath; @@ -162,7 +157,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager md5OfApp = getMD5(binaryFile); //todo validate binary file. - if(md5OfApp != null){ + if (md5OfApp != null) { artifactDirectoryPath = storagePath + md5OfApp; StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); if (log.isDebugEnabled()) { @@ -179,10 +174,9 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager + applicationRelease.getUuid(), e); } - }else{ + } else { log.error("Verify application existence and md5sum value retrieving process"); } - return applicationRelease; } @@ -195,7 +189,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager ApplicationRelease applicationRelease = null; try { applicationRelease = validateApplicationRelease(applicationUuid); - applicationRelease = uploadReleaseArtifacts(applicationRelease,binaryFile); + applicationRelease = uploadReleaseArtifacts(applicationRelease, binaryFile); return applicationRelease; } catch (ApplicationManagementException e) { throw new ApplicationStorageManagementException("Application Management exception while trying to" @@ -310,6 +304,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager /** * To validate the image artifact names. + * * @param name Name of the image artifact. * @throws ApplicationStorageManagementException Application Storage Management Exception */ @@ -339,7 +334,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager isAppExist = DataHolder.getInstance().getApplicationManager().verifyApplicationExistenceById(appId); } catch (ApplicationManagementException e) { throw new ApplicationStorageManagementException( - "Exception while verifing the application existence for the application with ID "+ appId); + "Exception while verifing the application existence for the application with ID " + appId); } return isAppExist; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java index b88c2e382c..3ab87b902e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/LifecycleStateManagerImpl.java @@ -1,33 +1,40 @@ /* -* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -* -*/ + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.AppLifecycleState; +import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import org.wso2.carbon.device.application.mgt.core.util.ValidateApplicationUtil; +import java.util.ArrayList; import java.util.List; /** @@ -38,44 +45,140 @@ public class LifecycleStateManagerImpl implements LifecycleStateManager { private static final Log log = LogFactory.getLog(LifecycleStateManagerImpl.class); @Override - public List getLifecycleStates(int appReleaseId) throws LifecycleManagementException { - List lifecycleStates = null; + public LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws LifecycleManagementException { + LifecycleState lifecycleState; try { ConnectionManagerUtil.openDBConnection(); LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - lifecycleStates = lifecycleStateDAO.getLifecycleStates(appReleaseId); - } catch (LifeCycleManagementDAOException | DBConnectionException e) { - throw new LifecycleManagementException("Failed get lifecycle states.", e); + Application application = ValidateApplicationUtil.validateApplication(applicationId); + //todo applicationUuid and applicationId should be passed and util method has to be changed + ApplicationRelease applicationRelease = ValidateApplicationUtil.validateApplicationRelease(applicationUuid); + lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState())); + } catch (ApplicationManagementDAOException e) { + throw new LifecycleManagementException("Failed to get lifecycle state", e); + } catch (DBConnectionException e) { + throw new LifecycleManagementException("Failed to connect with Database", e); + } catch (ApplicationManagementException e) { + throw new LifecycleManagementException("Failed to get application and application management", e); } finally { ConnectionManagerUtil.closeDBConnection(); } - return lifecycleStates; + return lifecycleState; } @Override - public void addLifecycleState(LifecycleState state) throws LifecycleManagementException { + public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws LifecycleManagementException { try { ConnectionManagerUtil.openDBConnection(); - LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - lifecycleStateDAO.addLifecycleState(state); + Application application = ValidateApplicationUtil.validateApplication(applicationId); + //todo applicationUuid and applicationId should be passed and util method has to be changed + ApplicationRelease applicationRelease = ValidateApplicationUtil.validateApplicationRelease(applicationUuid); + LifecycleStateDAO lifecycleStateDAO; + + if (application != null) { + state.setAppId(applicationId); + } + if (applicationRelease != null) { + state.setReleaseId(applicationRelease.getId()); + } + if (state.getCurrentState() != null && state.getPreviousState() != null && state.getUpdatedBy() != null) { + validateLifecycleState(state); + lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.addLifecycleState(state); + } } catch (LifeCycleManagementDAOException | DBConnectionException e) { throw new LifecycleManagementException("Failed to add lifecycle state", e); + } catch (ApplicationManagementException e) { + throw new LifecycleManagementException("Lifecycle State Validation failed", e); } finally { ConnectionManagerUtil.closeDBConnection(); } } - @Override - public void deleteLifecycleState(int identifier) throws LifecycleManagementException { + private List getNextLifecycleStates(String currentLifecycleState) { + List nextLifecycleStates = new ArrayList<>(); + if (AppLifecycleState.CREATED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.IN_REVIEW.toString()); + } + if (AppLifecycleState.IN_REVIEW.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.APPROVED.toString()); + nextLifecycleStates.add(AppLifecycleState.REJECTED.toString()); + } + if (AppLifecycleState.REJECTED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.IN_REVIEW.toString()); + nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); + } + if (AppLifecycleState.APPROVED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.PUBLISHED.toString()); + } + if (AppLifecycleState.PUBLISHED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.UNPUBLISHED.toString()); + nextLifecycleStates.add(AppLifecycleState.DEPRECATED.toString()); + } + if (AppLifecycleState.UNPUBLISHED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.PUBLISHED.toString()); + nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); + } + if (AppLifecycleState.DEPRECATED.toString().equals(currentLifecycleState)) { + nextLifecycleStates.add(AppLifecycleState.REMOVED.toString()); + } + return nextLifecycleStates; + } - try { - ConnectionManagerUtil.openDBConnection(); - LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - lifecycleStateDAO.deleteLifecycleState(identifier); - } catch (LifeCycleManagementDAOException | DBConnectionException e) { - throw new LifecycleManagementException("Failed to add lifecycle state: " + identifier, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); + private void validateLifecycleState(LifecycleState state) throws LifecycleManagementException { + + if (AppLifecycleState.CREATED.toString().equals(state.getCurrentState())) { + throw new LifecycleManagementException("Current State Couldn't be " + state.getCurrentState()); + } + if (AppLifecycleState.IN_REVIEW.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.CREATED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.REJECTED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be either " + AppLifecycleState.CREATED.toString() + " or " + + AppLifecycleState.REJECTED.toString()); + } + } + if (AppLifecycleState.APPROVED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.IN_REVIEW.toString()); + } + } + if (AppLifecycleState.PUBLISHED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.APPROVED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be either " + AppLifecycleState.APPROVED.toString() + " or " + + AppLifecycleState.UNPUBLISHED.toString()); + } + } + if (AppLifecycleState.UNPUBLISHED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.PUBLISHED.toString()); + } + } + if (AppLifecycleState.REJECTED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.IN_REVIEW.toString()); + } + } + if (AppLifecycleState.DEPRECATED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be " + AppLifecycleState.PUBLISHED.toString()); + } + } + if (AppLifecycleState.REMOVED.toString().equals(state.getCurrentState())) { + if (!AppLifecycleState.DEPRECATED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.REJECTED.toString().equals(state.getPreviousState()) && + !AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) { + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + + "Previous State should be either " + AppLifecycleState.DEPRECATED.toString() + " or " + + AppLifecycleState.REJECTED.toString() + " or " + AppLifecycleState.UNPUBLISHED.toString()); + } } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ValidateApplicationUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ValidateApplicationUtil.java new file mode 100644 index 0000000000..af041d772e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ValidateApplicationUtil.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.core.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; +import org.wso2.carbon.device.application.mgt.common.services.*; +import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; +import org.wso2.carbon.device.application.mgt.core.config.Extension; +import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; + +import java.lang.reflect.Constructor; + +/** + * This Util class is responsible for making sure single instance of each Extension Manager is used throughout for + * all the tasks. + */ +public class ValidateApplicationUtil { + + /** + * To validate the pre-request of the ApplicationRelease. + * + * @param applicationID ID of the Application. + * @return Application related with the UUID + */ + public static Application validateApplication(int applicationID) throws ApplicationManagementException { + if (applicationID <= 0) { + throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + + "parameter to get the relevant application."); + } + Application application = DataHolder.getInstance().getApplicationManager().getApplicationById(applicationID); + if (application == null) { + throw new NotFoundException("Application of the " + applicationID + " does not exist."); + } + return application; + } + + /** + * To validate the pre-request of the ApplicationRelease. + * + * @param applicationUuid UUID of the Application. + * @return Application related with the UUID + */ + public static ApplicationRelease validateApplicationRelease(String applicationUuid) throws ApplicationManagementException { + if (applicationUuid == null) { + throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " + + "parameter to get the relevant application."); + } + ApplicationRelease applicationRelease = DataHolder.getInstance().getApplicationReleaseManager() + .getReleaseByUuid(applicationUuid); + if (applicationRelease == null) { + throw new NotFoundException( + "Application with UUID " + applicationUuid + " does not exist."); + } + return applicationRelease; + } +} 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java index b1aa3ab3b6..fae021d551 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java @@ -91,8 +91,8 @@ import javax.ws.rs.core.Response; permissions = {"/device-mgt/application/update"} ), @Scope( - name = "Create an Application", - description = "Create an application", + name = "Login to Application Management", + description = "Login to Application Management", key = "perm:application-mgt:login", permissions = {"/device-mgt/application-mgt/login"} ), @@ -101,21 +101,7 @@ import javax.ws.rs.core.Response; description = "Delete an application", key = "perm:application:delete", permissions = {"/device-mgt/application/delete"} - ), - @Scope( - name = "Create an application category", - description = "Create an application category", - key = "perm:application-category:create", - permissions = {"/device-mgt/application/category/create"} - ), - @Scope( - name = "Delete an Application category", - description = "Delete an application category", - key = "perm:application-category:delete", - permissions = {"/device-mgt/application/category/delete"} ) - - } ) @Path("/publisher/applications") @@ -315,7 +301,7 @@ public interface ApplicationManagementAPI { @DELETE @Consumes("application/json") - @Path("/{appuuid}") + @Path("/{appid}") @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON, @@ -345,7 +331,7 @@ public interface ApplicationManagementAPI { name = "UUID", value = "Unique identifier of the Application", required = true) - @PathParam("appuuid") String applicationUUID); + @PathParam("appid") int applicationId); @PUT @Consumes("application/json") @@ -459,7 +445,7 @@ public interface ApplicationManagementAPI { Response updateApplicationArtifact( @ApiParam(name = "id", value = "Id of the application", required = true) @PathParam("uuid") int applicationId, @ApiParam(name = "uuid", value = "UUID of the application", required = true) @PathParam("uuid") String applicationUUID, - @Multipart("binaryFile") Attachment binaryFile ); + @Multipart("binaryFile") Attachment binaryFile); @PUT @Path("/{appId}/{uuid}") @@ -489,7 +475,6 @@ public interface ApplicationManagementAPI { message = "Internal Server Error. \n Error occurred while releasing the application.", response = ErrorResponse.class) }) - Response updateApplicationRelease( @ApiParam(name = "appId", value = "Identifier of the Application", required = true) @PathParam("appId") int applicationId, @ApiParam(name = "UUID", value = "Unique identifier of the Application Release", required = true) @PathParam("uuid") String applicationUUID, 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/LifecycleManagementAPI.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/LifecycleManagementAPI.java index 274b07777c..12c8d42452 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/LifecycleManagementAPI.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/LifecycleManagementAPI.java @@ -103,7 +103,8 @@ public interface LifecycleManagementAPI { message = "Internal Server Error. \n Error occurred while getting the lifecycle list.", response = ErrorResponse.class) }) - Response getLifecycleStates(); + Response getLifecycleState(@PathParam("appId") int applicationId, + @PathParam("uuid") String applicationUuid); @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @@ -136,7 +137,9 @@ public interface LifecycleManagementAPI { message = "Internal Server Error. \n Error occurred adding a lifecycle state.", response = ErrorResponse.class) }) - Response addLifecycleState(LifecycleState state); + Response addLifecycleState(@PathParam("appId") int applicationId, + @PathParam("uuid") String applicationUuid, + LifecycleState state); @Path("/{identifier}") @Produces(MediaType.APPLICATION_JSON) 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 a8b0ad3059..8a8fe3a1e4 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 @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; +import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.publisher.api.APIUtil; import org.wso2.carbon.device.application.mgt.publisher.api.FileStreamingOutput; import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementAPI; @@ -311,50 +312,38 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return Response.status(Response.Status.OK).entity(application).build(); } - //todo this need to be rethink and fix --- > This is somthing change lifecycle @DELETE - @Path("/{appuuid}") - public Response deleteApplication(@PathParam("appuuid") String uuid) { + @Path("/{appid}") + public Response deleteApplication(@PathParam("appid") int applicationId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); - ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); try { - applicationReleaseManager.deleteApplicationReleases(uuid); - applicationStorageManager.deleteApplicationArtifacts(uuid); - applicationManager.deleteApplication(uuid); - String responseMsg = "Successfully deleted the application: " + uuid; + applicationManager.deleteApplication(applicationId); +// todo delete storage details +// applicationStorageManager.deleteApplicationArtifacts(uuid); + String responseMsg = "Successfully deleted the application: " + applicationId; return Response.status(Response.Status.OK).entity(responseMsg).build(); - } catch (NotFoundException e) { - return APIUtil.getResponse(e, Response.Status.NOT_FOUND); } catch (ApplicationManagementException e) { - String msg = "Error occurred while deleting the application: " + uuid; + String msg = "Error occurred while deleting the application: " + applicationId; log.error(msg, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); - } catch (ApplicationStorageManagementException e) { - log.error("Error occurred while deleteing the image artifacts of the application with the uuid " + uuid, e); - return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } - // todo I think we must remove this - @Override - @PUT - @Consumes("application/json") - @Path("/{uuid}/{version}/{channel}") - public Response updateDefaultVersion(@PathParam("uuid") String applicationUUID, @PathParam("version") String - version, @PathParam("channel") String channel, @QueryParam("isDefault") boolean isDefault) { - ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + @DELETE + @Path("/{appid}/{uuid}") + public Response deleteApplicationRelease(@PathParam("appid") int applicationId, @PathParam("uuid") String releaseUuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { - applicationReleaseManager.changeDefaultRelease(applicationUUID, version, isDefault, channel); - return Response.status(Response.Status.OK) - .entity("Successfully changed the default version for the " + "release channel " + channel - + " for the application UUID " + applicationUUID).build(); - } catch (NotFoundException e) { - return Response.status(Response.Status.NOT_FOUND).build(); + applicationManager.deleteApplication(applicationId); +// todo delete release storage details +// applicationStorageManager.deleteApplicationArtifacts(uuid); + String responseMsg = "Successfully deleted the application release of: " + applicationId + ""; + return Response.status(Response.Status.OK).entity(responseMsg).build(); } catch (ApplicationManagementException e) { - log.error("Application Release Management Exception while changing the default release for the release " - + "channel " + channel + " for the application with UUID " + applicationUUID + " for the version " - + version); + String msg = "Error occurred while deleting the application: " + applicationId; + log.error(msg, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -422,4 +411,28 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + + // todo I think we must remove this + @Override + @PUT + @Consumes("application/json") + @Path("/{uuid}/{version}/{channel}") + public Response updateDefaultVersion(@PathParam("uuid") String applicationUUID, @PathParam("version") String + version, @PathParam("channel") String channel, @QueryParam("isDefault") boolean isDefault) { + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + try { + applicationReleaseManager.changeDefaultRelease(applicationUUID, version, isDefault, channel); + return Response.status(Response.Status.OK) + .entity("Successfully changed the default version for the " + "release channel " + channel + + " for the application UUID " + applicationUUID).build(); + } catch (NotFoundException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } catch (ApplicationManagementException e) { + log.error("Application Release Management Exception while changing the default release for the release " + + "channel " + channel + " for the application with UUID " + applicationUUID + " for the version " + + version); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } 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/LifecycleManagementAPIImpl.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/LifecycleManagementAPIImpl.java index 8f8829d6f2..c348ebdecc 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/LifecycleManagementAPIImpl.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/LifecycleManagementAPIImpl.java @@ -29,48 +29,56 @@ import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import javax.ws.rs.*; import javax.ws.rs.core.Response; /** * Lifecycle Management related jax-rs APIs. */ -@Path("/lifecycles") +@Path("/lifecycle") public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); @GET - public Response getLifecycleStates() { - return null; -// LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); -// List lifecycleStates = new ArrayList<>(); -// try { -// lifecycleStates = lifecycleStateManager.getLifecycleStates(); -// } catch (LifecycleManagementException e) { -// String msg = "Error occurred while retrieving lifecycle states."; -// log.error(msg, e); -// return Response.status(Response.Status.BAD_REQUEST).build(); -// } -// return Response.status(Response.Status.OK).entity(lifecycleStates).build(); + @Path("/{appId}/{uuid}") + public Response getLifecycleState( + @PathParam("appId") int applicationId, + @PathParam("uuid") String applicationUuid) { + LifecycleState lifecycleState; + LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); + try { + lifecycleState = lifecycleStateManager.getLifecycleState(applicationId, applicationUuid); + } catch (LifecycleManagementException e) { + String msg = "Error occurred while getting lifecycle state."; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity(lifecycleState).build(); } + @POST - public Response addLifecycleState(LifecycleState state) { + @Path("/{appId}/{uuid}") + public Response addLifecycleState( + @PathParam("appId") int applicationId, + @PathParam("uuid") String applicationUuid, + LifecycleState state) { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); try { - lifecycleStateManager.addLifecycleState(state); + lifecycleStateManager.addLifecycleState(applicationId, applicationUuid, state); } catch (LifecycleManagementException e) { String msg = "Error occurred while adding lifecycle state."; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).build(); } - return Response.status(Response.Status.OK).entity("Lifecycle state added successfully.").build(); + return Response.status(Response.Status.CREATED).entity("Lifecycle state added successfully.").build(); } + + //todo remove below part + @DELETE @Path("/{identifier}") public Response deleteLifecycleState(@PathParam("identifier") String identifier) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/META-INF/permissions.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/META-INF/permissions.xml index df2d1cf5b5..7cac48772c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/META-INF/permissions.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/META-INF/permissions.xml @@ -32,19 +32,19 @@ - Get Application + Get Application Details /device-mgt/application/get /application-mgt/applications GET - Create Application + Create an Application /device-mgt/application/create /application-mgt/applications POST - Edit Application + Update an Application /device-mgt/application/update /application-mgt/applications PUT @@ -56,55 +56,10 @@ PUT - Login to Application Management + Delete an Application device-mgt/application/delete /application-mgt/applications/* DELETE - - - Get Platform - /device-mgt/platform/get - /application-mgt/platforms/* - GET - - - Add Platform - /device-mgt/platform/add - /application-mgt/platforms - POST - - - Update Platform - /device-mgt/platform/update - /application-mgt/platforms/* - PUT - - - Remove Platform - /device-mgt/platform/remove - /application-mgt/platforms/* - DELETE - - - - - Install Application - /device-mgt/subscription/install - /application-mgt/subscription - POST - - - Uninstall Application - /device-mgt/subscription/uninstall - /application-mgt/subscription - POST - - - Get Application - /device-mgt/subscription/getApplication - /application-mgt/subscription - GET -