Adding Lifecycle Management functionalities and fixed related bugs

feature/appm-store/pbac
lasantha 7 years ago
parent fb8d9dd494
commit 9b1820dca5

@ -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
}

@ -19,6 +19,7 @@
package org.wso2.carbon.device.application.mgt.common; package org.wso2.carbon.device.application.mgt.common;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.List;
public class LifecycleState { public class LifecycleState {
@ -28,6 +29,8 @@ public class LifecycleState {
private String previousState; private String previousState;
private List<String> nextStates;
private String updatedBy; private String updatedBy;
private Timestamp updatedAt; private Timestamp updatedAt;
@ -102,4 +105,11 @@ public class LifecycleState {
this.appId = appId; this.appId = appId;
} }
public List<String> getNextStates() {
return nextStates;
}
public void setNextStates(List<String> nextStates) {
this.nextStates = nextStates;
}
} }

@ -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.*;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; 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 org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import java.util.List; import java.util.List;
@ -31,6 +32,7 @@ public interface ApplicationManager {
/** /**
* Creates an application. * Creates an application.
*
* @param application Application that need to be created. * @param application Application that need to be created.
* @return Created application * @return Created application
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException Application Management Exception
@ -39,6 +41,7 @@ public interface ApplicationManager {
/** /**
* Updates an already existing application. * Updates an already existing application.
*
* @param application Application that need to be updated. * @param application Application that need to be updated.
* @return Updated Application * @return Updated Application
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException Application Management Exception
@ -47,13 +50,24 @@ public interface ApplicationManager {
/** /**
* Delete an application identified by the unique ID. * 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 * @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. * To get the applications based on the search filter.
*
* @param filter Search filter * @param filter Search filter
* @return Applications that matches the given filter criteria. * @return Applications that matches the given filter criteria.
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException Application Management Exception
@ -62,6 +76,7 @@ public interface ApplicationManager {
/** /**
* To get the applications based on the search filter. * To get the applications based on the search filter.
*
* @param appId id of the application * @param appId id of the application
* @return Application release which is published and release of the Application(appId). * @return Application release which is published and release of the Application(appId).
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException Application Management Exception
@ -135,5 +150,9 @@ public interface ApplicationManager {
*/ */
List<ApplicationRelease> getReleases(int applicationId) throws ApplicationManagementException; List<ApplicationRelease> getReleases(int applicationId) throws ApplicationManagementException;
LifecycleState getLifecycleState(int appReleaseId, String applicationUuid) throws LifecycleManagementException;
void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws LifecycleManagementException;
} }

@ -28,9 +28,8 @@ import java.util.List;
*/ */
public interface LifecycleStateManager { public interface LifecycleStateManager {
List<LifecycleState> 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;
} }

@ -29,7 +29,7 @@ import java.util.List;
*/ */
public interface LifecycleStateDAO { public interface LifecycleStateDAO {
LifecycleState getLatestLifeCycleStateByReleaseID(int identifier) throws ApplicationManagementDAOException; LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException;
List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException;

@ -39,7 +39,7 @@ import java.util.List;
public class GenericLifecycleStateImpl extends AbstractDAOImpl implements LifecycleStateDAO { public class GenericLifecycleStateImpl extends AbstractDAOImpl implements LifecycleStateDAO {
@Override @Override
public LifecycleState getLatestLifeCycleStateByReleaseID(int identifier) throws ApplicationManagementDAOException { public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = 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;"; + "AP_APP_LIFECYCLE_STATE WHERE AP_APP_RELEASE_ID=? ORDER BY UPDATED_AT DESC;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, identifier); stmt.setInt(1, applicationReleaseId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
LifecycleState lifecycleState = null; LifecycleState lifecycleState = null;

@ -25,11 +25,15 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.*; 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.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.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; 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.LifecycleStateDAO;
import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; 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.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.NotFoundException;
import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.exception.ValidationException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
@ -67,7 +71,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); 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(), User loggedInUser = new User(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(),
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true)); 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); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationList applicationList; 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 { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
return applicationDAO.getUuidOfLatestRelease(appId); return applicationDAO.getUuidOfLatestRelease(appId);
@ -172,7 +179,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private boolean isRoleExists(List<UnrestrictedRole> unrestrictedRoleList, String userName) private boolean isRoleExists(List<UnrestrictedRole> unrestrictedRoleList, String userName)
throws UserStoreException { throws UserStoreException {
String[] roleList; String[] roleList;
roleList = getRoleOfUser(userName); roleList = getRolesOfUser(userName);
for (UnrestrictedRole unrestrictedRole : unrestrictedRoleList) { for (UnrestrictedRole unrestrictedRole : unrestrictedRoleList) {
for (String role : roleList) { for (String role : roleList) {
if (unrestrictedRole.getRole().equals(role)) { if (unrestrictedRole.getRole().equals(role)) {
@ -183,7 +190,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
return false; return false;
} }
private String[] getRoleOfUser(String userName) throws UserStoreException { private String[] getRolesOfUser(String userName) throws UserStoreException {
UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
String[] roleList = {}; String[] roleList = {};
if (userRealm != null) { if (userRealm != null) {
@ -194,7 +201,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
return roleList; 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); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application; 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); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application; Application application;
@ -285,7 +294,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
@Override public List<ApplicationRelease> getReleases(int applicationId) throws ApplicationManagementException { @Override
public List<ApplicationRelease> getReleases(int applicationId) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Application application = validateApplication(applicationId); Application application = validateApplication(applicationId);
@ -300,7 +310,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationReleases = ApplicationManagementDAOFactory.getApplicationReleaseDAO() applicationReleases = ApplicationManagementDAOFactory.getApplicationReleaseDAO()
.getApplicationReleases(application.getName(), application.getType(), tenantId); .getApplicationReleases(application.getName(), application.getType(), tenantId);
for (ApplicationRelease applicationRelease : applicationReleases) { for (ApplicationRelease applicationRelease : applicationReleases) {
if (!"REMOVED".equals(ApplicationManagementDAOFactory.getLifecycleStateDAO(). if (!AppLifecycleState.REMOVED.toString().equals(ApplicationManagementDAOFactory.getLifecycleStateDAO().
getLatestLifeCycleStateByReleaseID(applicationRelease.getId()).getCurrentState())) { getLatestLifeCycleStateByReleaseID(applicationRelease.getId()).getCurrentState())) {
filteredApplicationReleases.add(applicationRelease); filteredApplicationReleases.add(applicationRelease);
} }
@ -311,32 +321,52 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
/** @Override
* To check whether current user is application owner or admin. public void deleteApplication(int applicationId) throws ApplicationManagementException {
* String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
* @param applicationUUID UUID of the Application. int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
* @return true if the current user is application owner or admin, unless false.
* @throws ApplicationManagementException Application Management Exception. if (validateApplication(applicationId) == null) {
*/ throw new ApplicationManagementException("Invalid Application");
private boolean isApplicationOwnerOrAdmin(String applicationUUID, String userName, int tenantId) }
throws ApplicationManagementException { List<ApplicationRelease> applicationReleases = getReleases(applicationId);
// try { if (log.isDebugEnabled()) {
// if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { log.debug("Request is received to delete applications which are related with the application id " +
// return true; applicationId);
// } }
// } catch (UserStoreException e) { for (ApplicationRelease applicationRelease : applicationReleases) {
// throw new ApplicationManagementException("Userstore exception while checking whether user is an admin", e); LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid());
// } LifecycleState newAppLifecycleState = new LifecycleState();
// try { newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
// ConnectionManagerUtil.openDBConnection(); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
// Application application = ApplicationManagementDAOFactory.getApplicationDAO() newAppLifecycleState.setTenantId(tenantId);
// .getApplication(applicationUUID, tenantId, userName); newAppLifecycleState.setUpdatedBy(userName);
// return application.getUser().getUserName().equals(userName) addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState);
// && application.getUser().getTenantId() == tenantId; }
// } finally { //todo add column into application and move application into removed state
// ConnectionManagerUtil.closeDBConnection(); }
// }
return false; @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 { private void validateApplicationExistence(Application application) throws ApplicationManagementException {
Filter filter = new Filter(); Filter filter = new Filter();
filter.setFullMatch(true); filter.setFullMatch(true);
@ -415,6 +451,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
return application; 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. * 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<String> getNextLifecycleStates(String currentLifecycleState) {
List<String> 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(); // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); // int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
// if (application.getUuid() == null) { // if (application.getUuid() == null) {
@ -531,34 +726,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
return application; return application;
} }
@Override public void deleteApplication(String uuid) throws ApplicationManagementException { @Override
// String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); public void changeLifecycle(String applicationUuid, String lifecycleIdentifier)
// 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)
throws ApplicationManagementException { throws ApplicationManagementException {
// boolean isAvailableNextState = false; // boolean isAvailableNextState = false;
// String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -593,7 +762,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
// } // }
} }
@Override public List<LifecycleStateTransition> getLifeCycleStates(String applicationUUID) @Override
public List<LifecycleStateTransition> getLifeCycleStates(String applicationUUID)
throws ApplicationManagementException { throws ApplicationManagementException {
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); // int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
// String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); // String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -649,4 +819,32 @@ public class ApplicationManagerImpl implements ApplicationManager {
return null; 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;
}
} }

@ -68,8 +68,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
@Override @Override
public ApplicationRelease uploadImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFileStream, public ApplicationRelease uploadImageArtifacts(ApplicationRelease applicationRelease, InputStream iconFileStream,
InputStream bannerFileStream,List<InputStream> screenShotStreams) throws ResourceManagementException { InputStream bannerFileStream, List<InputStream> screenShotStreams)
throws ResourceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String artifactDirectoryPath = null; String artifactDirectoryPath = null;
String iconStoredLocation; String iconStoredLocation;
@ -77,7 +77,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
String scStoredLocation; String scStoredLocation;
try { try {
artifactDirectoryPath = storagePath + applicationRelease.getAppHashValue(); artifactDirectoryPath = storagePath + applicationRelease.getAppHashValue();
StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath); StorageManagementUtil.createArtifactDirectory(artifactDirectoryPath);
iconStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0]; iconStoredLocation = artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0];
@ -87,13 +86,10 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
saveFile(iconFileStream, iconStoredLocation); saveFile(iconFileStream, iconStoredLocation);
applicationRelease.setIconLoc(iconStoredLocation); applicationRelease.setIconLoc(iconStoredLocation);
} }
if (bannerFileStream != null) { if (bannerFileStream != null) {
saveFile(bannerFileStream, bannerStoredLocation); saveFile(bannerFileStream, bannerStoredLocation);
applicationRelease.setBannerLoc(bannerStoredLocation); applicationRelease.setBannerLoc(bannerStoredLocation);
} }
if (screenShotStreams.size() > screenShotMaxCount) { if (screenShotStreams.size() > screenShotMaxCount) {
throw new ApplicationStorageManagementException("Maximum limit for the screen-shot exceeds"); throw new ApplicationStorageManagementException("Maximum limit for the screen-shot exceeds");
} else if (!screenShotStreams.isEmpty() && screenShotStreams.size() <= screenShotMaxCount) { } else if (!screenShotStreams.isEmpty() && screenShotStreams.size() <= screenShotMaxCount) {
@ -125,7 +121,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
+ "update the screen-shot count for the application " + applicationRelease.getUuid() + + "update the screen-shot count for the application " + applicationRelease.getUuid() +
" for the tenant id " + tenantId, e); " for the tenant id " + tenantId, e);
} }
} }
@Override @Override
@ -182,7 +177,6 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
} else { } else {
log.error("Verify application existence and md5sum value retrieving process"); log.error("Verify application existence and md5sum value retrieving process");
} }
return applicationRelease; return applicationRelease;
} }
@ -310,6 +304,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
/** /**
* To validate the image artifact names. * To validate the image artifact names.
*
* @param name Name of the image artifact. * @param name Name of the image artifact.
* @throws ApplicationStorageManagementException Application Storage Management Exception * @throws ApplicationStorageManagementException Application Storage Management Exception
*/ */

@ -19,15 +19,22 @@ package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.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.DBConnectionException;
import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; 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.common.services.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; 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.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.LifeCycleManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; 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; import java.util.List;
/** /**
@ -38,44 +45,140 @@ public class LifecycleStateManagerImpl implements LifecycleStateManager {
private static final Log log = LogFactory.getLog(LifecycleStateManagerImpl.class); private static final Log log = LogFactory.getLog(LifecycleStateManagerImpl.class);
@Override @Override
public List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifecycleManagementException { public LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws LifecycleManagementException {
List<LifecycleState> lifecycleStates = null; LifecycleState lifecycleState;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
lifecycleStates = lifecycleStateDAO.getLifecycleStates(appReleaseId); Application application = ValidateApplicationUtil.validateApplication(applicationId);
} catch (LifeCycleManagementDAOException | DBConnectionException e) { //todo applicationUuid and applicationId should be passed and util method has to be changed
throw new LifecycleManagementException("Failed get lifecycle states.", e); 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 { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
return lifecycleStates; return lifecycleState;
} }
@Override @Override
public void addLifecycleState(LifecycleState state) throws LifecycleManagementException { public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws LifecycleManagementException {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); 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); lifecycleStateDAO.addLifecycleState(state);
}
} catch (LifeCycleManagementDAOException | DBConnectionException e) { } catch (LifeCycleManagementDAOException | DBConnectionException e) {
throw new LifecycleManagementException("Failed to add lifecycle state", e); throw new LifecycleManagementException("Failed to add lifecycle state", e);
} catch (ApplicationManagementException e) {
throw new LifecycleManagementException("Lifecycle State Validation failed", e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override private List<String> getNextLifecycleStates(String currentLifecycleState) {
public void deleteLifecycleState(int identifier) throws LifecycleManagementException { List<String> 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 { private void validateLifecycleState(LifecycleState state) throws LifecycleManagementException {
ConnectionManagerUtil.openDBConnection();
LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); if (AppLifecycleState.CREATED.toString().equals(state.getCurrentState())) {
lifecycleStateDAO.deleteLifecycleState(identifier); throw new LifecycleManagementException("Current State Couldn't be " + state.getCurrentState());
} catch (LifeCycleManagementDAOException | DBConnectionException e) { }
throw new LifecycleManagementException("Failed to add lifecycle state: " + identifier, e); if (AppLifecycleState.IN_REVIEW.toString().equals(state.getCurrentState())) {
} finally { if (!AppLifecycleState.CREATED.toString().equals(state.getPreviousState()) &&
ConnectionManagerUtil.closeDBConnection(); !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());
}
} }
} }
} }

@ -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;
}
}

@ -91,8 +91,8 @@ import javax.ws.rs.core.Response;
permissions = {"/device-mgt/application/update"} permissions = {"/device-mgt/application/update"}
), ),
@Scope( @Scope(
name = "Create an Application", name = "Login to Application Management",
description = "Create an application", description = "Login to Application Management",
key = "perm:application-mgt:login", key = "perm:application-mgt:login",
permissions = {"/device-mgt/application-mgt/login"} permissions = {"/device-mgt/application-mgt/login"}
), ),
@ -101,21 +101,7 @@ import javax.ws.rs.core.Response;
description = "Delete an application", description = "Delete an application",
key = "perm:application:delete", key = "perm:application:delete",
permissions = {"/device-mgt/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") @Path("/publisher/applications")
@ -315,7 +301,7 @@ public interface ApplicationManagementAPI {
@DELETE @DELETE
@Consumes("application/json") @Consumes("application/json")
@Path("/{appuuid}") @Path("/{appid}")
@ApiOperation( @ApiOperation(
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -345,7 +331,7 @@ public interface ApplicationManagementAPI {
name = "UUID", name = "UUID",
value = "Unique identifier of the Application", value = "Unique identifier of the Application",
required = true) required = true)
@PathParam("appuuid") String applicationUUID); @PathParam("appid") int applicationId);
@PUT @PUT
@Consumes("application/json") @Consumes("application/json")
@ -489,7 +475,6 @@ public interface ApplicationManagementAPI {
message = "Internal Server Error. \n Error occurred while releasing the application.", message = "Internal Server Error. \n Error occurred while releasing the application.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response updateApplicationRelease( Response updateApplicationRelease(
@ApiParam(name = "appId", value = "Identifier of the Application", required = true) @PathParam("appId") int applicationId, @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, @ApiParam(name = "UUID", value = "Unique identifier of the Application Release", required = true) @PathParam("uuid") String applicationUUID,

@ -103,7 +103,8 @@ public interface LifecycleManagementAPI {
message = "Internal Server Error. \n Error occurred while getting the lifecycle list.", message = "Internal Server Error. \n Error occurred while getting the lifecycle list.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getLifecycleStates(); Response getLifecycleState(@PathParam("appId") int applicationId,
@PathParam("uuid") String applicationUuid);
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(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.", message = "Internal Server Error. \n Error occurred adding a lifecycle state.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response addLifecycleState(LifecycleState state); Response addLifecycleState(@PathParam("appId") int applicationId,
@PathParam("uuid") String applicationUuid,
LifecycleState state);
@Path("/{identifier}") @Path("/{identifier}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart; 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.APIUtil;
import org.wso2.carbon.device.application.mgt.publisher.api.FileStreamingOutput; import org.wso2.carbon.device.application.mgt.publisher.api.FileStreamingOutput;
import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementAPI; 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(); return Response.status(Response.Status.OK).entity(application).build();
} }
//todo this need to be rethink and fix --- > This is somthing change lifecycle
@DELETE @DELETE
@Path("/{appuuid}") @Path("/{appid}")
public Response deleteApplication(@PathParam("appuuid") String uuid) { public Response deleteApplication(@PathParam("appid") int applicationId) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager();
try { try {
applicationReleaseManager.deleteApplicationReleases(uuid); applicationManager.deleteApplication(applicationId);
applicationStorageManager.deleteApplicationArtifacts(uuid); // todo delete storage details
applicationManager.deleteApplication(uuid); // applicationStorageManager.deleteApplicationArtifacts(uuid);
String responseMsg = "Successfully deleted the application: " + uuid; String responseMsg = "Successfully deleted the application: " + applicationId;
return Response.status(Response.Status.OK).entity(responseMsg).build(); return Response.status(Response.Status.OK).entity(responseMsg).build();
} catch (NotFoundException e) {
return APIUtil.getResponse(e, Response.Status.NOT_FOUND);
} catch (ApplicationManagementException e) { } 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); log.error(msg, e);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); 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 @DELETE
@Override @Path("/{appid}/{uuid}")
@PUT public Response deleteApplicationRelease(@PathParam("appid") int applicationId, @PathParam("uuid") String releaseUuid) {
@Consumes("application/json") ApplicationManager applicationManager = APIUtil.getApplicationManager();
@Path("/{uuid}/{version}/{channel}") ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
public Response updateDefaultVersion(@PathParam("uuid") String applicationUUID, @PathParam("version") String
version, @PathParam("channel") String channel, @QueryParam("isDefault") boolean isDefault) {
ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager();
try { try {
applicationReleaseManager.changeDefaultRelease(applicationUUID, version, isDefault, channel); applicationManager.deleteApplication(applicationId);
return Response.status(Response.Status.OK) // todo delete release storage details
.entity("Successfully changed the default version for the " + "release channel " + channel // applicationStorageManager.deleteApplicationArtifacts(uuid);
+ " for the application UUID " + applicationUUID).build(); String responseMsg = "Successfully deleted the application release of: " + applicationId + "";
} catch (NotFoundException e) { return Response.status(Response.Status.OK).entity(responseMsg).build();
return Response.status(Response.Status.NOT_FOUND).build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
log.error("Application Release Management Exception while changing the default release for the release " String msg = "Error occurred while deleting the application: " + applicationId;
+ "channel " + channel + " for the application with UUID " + applicationUUID + " for the version " log.error(msg, e);
+ version);
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); 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); 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);
}
}
} }

@ -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.exception.LifecycleManagementException;
import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/** /**
* Lifecycle Management related jax-rs APIs. * Lifecycle Management related jax-rs APIs.
*/ */
@Path("/lifecycles") @Path("/lifecycle")
public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { public class LifecycleManagementAPIImpl implements LifecycleManagementAPI {
private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class);
@GET @GET
public Response getLifecycleStates() { @Path("/{appId}/{uuid}")
return null; public Response getLifecycleState(
// LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); @PathParam("appId") int applicationId,
// List<LifecycleState> lifecycleStates = new ArrayList<>(); @PathParam("uuid") String applicationUuid) {
// try { LifecycleState lifecycleState;
// lifecycleStates = lifecycleStateManager.getLifecycleStates(); LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager();
// } catch (LifecycleManagementException e) { try {
// String msg = "Error occurred while retrieving lifecycle states."; lifecycleState = lifecycleStateManager.getLifecycleState(applicationId, applicationUuid);
// log.error(msg, e); } catch (LifecycleManagementException e) {
// return Response.status(Response.Status.BAD_REQUEST).build(); String msg = "Error occurred while getting lifecycle state.";
// } log.error(msg, e);
// return Response.status(Response.Status.OK).entity(lifecycleStates).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }
return Response.status(Response.Status.OK).entity(lifecycleState).build();
}
@POST @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(); LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager();
try { try {
lifecycleStateManager.addLifecycleState(state); lifecycleStateManager.addLifecycleState(applicationId, applicationUuid, state);
} catch (LifecycleManagementException e) { } catch (LifecycleManagementException e) {
String msg = "Error occurred while adding lifecycle state."; String msg = "Error occurred while adding lifecycle state.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build(); 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 @DELETE
@Path("/{identifier}") @Path("/{identifier}")
public Response deleteLifecycleState(@PathParam("identifier") String identifier) { public Response deleteLifecycleState(@PathParam("identifier") String identifier) {

@ -32,19 +32,19 @@
<!-- Application related permissions --> <!-- Application related permissions -->
<Permission> <Permission>
<name>Get Application</name> <name>Get Application Details</name>
<path>/device-mgt/application/get</path> <path>/device-mgt/application/get</path>
<url>/application-mgt/applications</url> <url>/application-mgt/applications</url>
<method>GET</method> <method>GET</method>
</Permission> </Permission>
<Permission> <Permission>
<name>Create Application</name> <name>Create an Application</name>
<path>/device-mgt/application/create</path> <path>/device-mgt/application/create</path>
<url>/application-mgt/applications</url> <url>/application-mgt/applications</url>
<method>POST</method> <method>POST</method>
</Permission> </Permission>
<Permission> <Permission>
<name>Edit Application</name> <name>Update an Application</name>
<path>/device-mgt/application/update</path> <path>/device-mgt/application/update</path>
<url>/application-mgt/applications</url> <url>/application-mgt/applications</url>
<method>PUT</method> <method>PUT</method>
@ -56,55 +56,10 @@
<method>PUT</method> <method>PUT</method>
</Permission> </Permission>
<Permission> <Permission>
<name>Login to Application Management</name> <name>Delete an Application</name>
<path>device-mgt/application/delete</path> <path>device-mgt/application/delete</path>
<url>/application-mgt/applications/*</url> <url>/application-mgt/applications/*</url>
<method>DELETE</method> <method>DELETE</method>
</Permission> </Permission>
<!-- Platform related permissions -->
<Permission>
<name>Get Platform</name>
<path>/device-mgt/platform/get</path>
<url>/application-mgt/platforms/*</url>
<method>GET</method>
</Permission>
<Permission>
<name>Add Platform</name>
<path>/device-mgt/platform/add</path>
<url>/application-mgt/platforms</url>
<method>POST</method>
</Permission>
<Permission>
<name>Update Platform</name>
<path>/device-mgt/platform/update</path>
<url>/application-mgt/platforms/*</url>
<method>PUT</method>
</Permission>
<Permission>
<name>Remove Platform</name>
<path>/device-mgt/platform/remove</path>
<url>/application-mgt/platforms/*</url>
<method>DELETE</method>
</Permission>
<!-- Subscription related permissions -->
<Permission>
<name>Install Application</name>
<path>/device-mgt/subscription/install</path>
<url>/application-mgt/subscription</url>
<method>POST</method>
</Permission>
<Permission>
<name>Uninstall Application</name>
<path>/device-mgt/subscription/uninstall</path>
<url>/application-mgt/subscription</url>
<method>POST</method>
</Permission>
<Permission>
<name>Get Application</name>
<path>/device-mgt/subscription/getApplication</path>
<url>/application-mgt/subscription</url>
<method>GET</method>
</Permission>
</PermissionConfiguration> </PermissionConfiguration>

Loading…
Cancel
Save