Add modification to application creating logic

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent 0cb26066c5
commit fbcb1d0e32

@ -43,7 +43,8 @@ public interface ApplicationManager {
* @return Created application * @return Created application
* @throws ApplicationManagementException Application Management Exception * @throws ApplicationManagementException Application Management Exception
*/ */
Application createApplication(Application application) throws ApplicationManagementException; Application createApplication(Application application)
throws ApplicationManagementException, RequestValidatingException;
/** /**
* Updates an already existing application. * Updates an already existing application.
@ -154,10 +155,9 @@ public interface ApplicationManager {
* @param applicationId ID of the Application. * @param applicationId ID of the Application.
* @param releaseUuid UUID of the Application Release. * @param releaseUuid UUID of the Application Release.
* @param state Lifecycle state to change the app * @param state Lifecycle state to change the app
* @param checkExist whether it is needed to check if the app and release already exist in the database
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist) void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**

@ -50,7 +50,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException
import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException;
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.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.VisibilityManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
@ -92,8 +92,16 @@ public class ApplicationManagerImpl implements ApplicationManager {
this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
} }
/***
* The responsbility of this method is the creating an application.
* @param application Application that need to be created.
* @return {@link Application}
* @throws RequestValidatingException if application creating request is invalid, returns {@link RequestValidatingException}
* @throws ApplicationManagementException Catch all other throwing exceptions and returns {@link ApplicationManagementException}
*/
@Override @Override
public Application createApplication(Application application) throws ApplicationManagementException { public Application createApplication(Application application)
throws RequestValidatingException, ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
application.setUser(new User(userName, tenantId)); application.setUser(new User(userName, tenantId));
@ -101,6 +109,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName); log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName);
} }
validateAppCreatingRequest(application, tenantId); validateAppCreatingRequest(application, tenantId);
//todo throw different exception
validateAppReleasePayload(application.getApplicationReleases().get(0)); validateAppReleasePayload(application.getApplicationReleases().get(0));
DeviceType deviceType; DeviceType deviceType;
ApplicationRelease applicationRelease; ApplicationRelease applicationRelease;
@ -108,8 +117,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
try { try {
// Getting the device type details to get device type ID for internal mappings // Getting the device type details to get device type ID for internal mappings
deviceType = Util.getDeviceManagementService().getDeviceType(application.getDeviceType()); deviceType = Util.getDeviceManagementService().getDeviceType(application.getDeviceType());
ConnectionManagerUtil.beginDBTransaction();
ConnectionManagerUtil.beginDBTransaction();
if (deviceType == null) { if (deviceType == null) {
log.error("Device type is not matched with application type"); log.error("Device type is not matched with application type");
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
@ -117,80 +126,75 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
if (!application.getUnrestrictedRoles().isEmpty()) { if (!application.getUnrestrictedRoles().isEmpty()) {
application.setIsRestricted(true); application.setIsRestricted(true);
} else {
application.setIsRestricted(false);
} }
// Insert to application table // Insert to application table
int appId = this.applicationDAO.createApplication(application, deviceType.getId()); int appId = this.applicationDAO.createApplication(application, deviceType.getId());
if (appId == -1) { if (appId == -1) {
log.error("Application creation Failed"); log.error("Application creation is Failed");
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
return null; return null;
} else { } else {
if(log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("New Application entry added to AP_APP table. App Id:" + appId); log.debug("New Application entry added to AP_APP table. App Id:" + appId);
} }
if (!application.getTags().isEmpty()) { if (!application.getTags().isEmpty()) {
this.applicationDAO.addTags(application.getTags(), appId, tenantId); this.applicationDAO.addTags(application.getTags(), appId, tenantId);
if(log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId); log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId);
} }
} }
if (application.getIsRestricted()) { if (application.getIsRestricted()) {
this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId); this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId);
if(log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." + log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table."
" App Id:" + appId); + " App Id:" + appId);
} }
} }
if (application.getApplicationReleases().size() > 1 ){ if (log.isDebugEnabled()) {
throw new ApplicationManagementException(
"Invalid payload. Application creating payload should contains one application release, but "
+ "the payload contains more than one");
}
if(log.isDebugEnabled()){
log.debug("Creating a new release. App Id:" + appId); log.debug("Creating a new release. App Id:" + appId);
} }
applicationRelease = application.getApplicationReleases().get(0); applicationRelease = application.getApplicationReleases().get(0);
applicationRelease = this.applicationReleaseDAO applicationRelease = this.applicationReleaseDAO.createRelease(applicationRelease, appId, tenantId);
.createRelease(applicationRelease, appId, tenantId);
if(log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("Changing lifecycle state. App Id:" + appId); log.debug("Changing lifecycle state. App Id:" + appId);
} }
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = getLifecycleStateInstant(AppLifecycleState.CREATED.toString(),
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
lifecycleState.setUpdatedBy(userName);
this.lifecycleStateDAO.addLifecycleState(lifecycleState, appId, applicationRelease.getUuid(), tenantId); this.lifecycleStateDAO.addLifecycleState(lifecycleState, appId, applicationRelease.getUuid(), tenantId);
applicationRelease.setLifecycleState(lifecycleState); applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease); applicationReleases.add(applicationRelease);
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(applicationReleases);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} }
return application; return application;
} catch (ApplicationManagementException e) {
String msg = "Error occurred while adding application";
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while getting device type id of " + application.getType();
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(
} catch (Exception e) { "Error occurred while getting device type id of " + application.getType(), e);
String msg = "Unknown exception while creating application."; } catch (LifeCycleManagementDAOException e) {
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(
"Error occured while adding lifecycle state. application name: " + application.getName()
+ " application type: is " + application.getType(), e);
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Error occured while adding application or application release. application name: " + application
.getName() + " application type: " + application.getType(), e);
} catch (DBConnectionException e) {
throw new ApplicationManagementException("Error occured while getting database connection. ", e);
} catch (VisibilityManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Error occured while adding unrestricted roles. application name: " + application.getName()
+ " application type: " + application.getType(), e);
} catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException("Error occured while disabling AutoCommit. ", e);
} }
} }
@ -242,7 +246,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("Application release request is received for the application " + application.toString()); log.debug("Application release request is received for the application " + application.toString());
} }
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.beginDBTransaction();
Application existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId); Application existingApplication = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (existingApplication == null){ if (existingApplication == null){
throw new NotFoundException( throw new NotFoundException(
@ -258,13 +262,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new BadRequestException( throw new BadRequestException(
"Package name in the payload is different from the existing package name of other application releases."); "Package name in the payload is different from the existing package name of other application releases.");
} }
ConnectionManagerUtil.beginDBTransaction();
applicationRelease = this.applicationReleaseDAO applicationRelease = this.applicationReleaseDAO
.createRelease(applicationRelease, application.getId(), tenantId); .createRelease(applicationRelease, application.getId(), tenantId);
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = getLifecycleStateInstant(AppLifecycleState.CREATED.toString(),
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationId, applicationRelease.getUuid(), tenantId);
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, false);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return applicationRelease; return applicationRelease;
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
@ -278,12 +280,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
"Error occurred while adding application release into IoTS app management Application id of the " "Error occurred while adding application release into IoTS app management Application id of the "
+ "application release: " + applicationId, e); + "application release: " + applicationId, e);
} catch (ApplicationManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
// todo throws when adding lifecycle state
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occurred while adding application release into IoTS app management Application id of the " "Error occurred while adding application release into IoTS app management Application id of the "
+ "application release: " + applicationId, e); + "application release: " + applicationId, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -556,36 +558,47 @@ public class ApplicationManagerImpl implements ApplicationManager {
try { try {
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"You don't have permission to delete this application. In order to delete an application you " + "You don't have permission to delete this application. In order to delete an application you "
"need to have admin permission"); + "need to have admin permission");
} }
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.beginDBTransaction();
application = getApplicationIfAccessible(applicationId); application = getApplicationIfAccessible(applicationId);
if ( application == null) { if (application == null) {
throw new ApplicationManagementException("Invalid Application"); throw new ApplicationManagementException("Invalid Application");
} }
List<ApplicationRelease> applicationReleases = getReleases(application, null); List<ApplicationRelease> applicationReleases = getReleases(application, null);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request is received to delete applications which are related with the application id " + log.debug("Request is received to delete applications which are related with the application id "
applicationId); + applicationId);
} }
ConnectionManagerUtil.beginDBTransaction();
for (ApplicationRelease applicationRelease : applicationReleases) { for (ApplicationRelease applicationRelease : applicationReleases) {
LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid());
LifecycleState newAppLifecycleState = new LifecycleState(); LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(),
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(),
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, false); newAppLifecycleState.getCurrentState())) {
this.lifecycleStateDAO
.addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(),
tenantId);
} else {
// todo move to appropriate lifecycle changing flow and end by remving release
}
storedLocations.add(applicationRelease.getAppHashValue()); storedLocations.add(applicationRelease.getAppHashValue());
} }
this.applicationDAO.deleteApplication(applicationId); this.applicationDAO.deleteApplication(applicationId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while check whether current user has the permission to delete an application"; String msg = "Error occured while check whether current user has the permission to delete an application";
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg,e); throw new ApplicationManagementException(msg, e);
} finally { } catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.rollbackDBTransaction();
// todo
String msg = "Error occured while check whether current user has the permission to delete an application";
log.error(msg);
throw new ApplicationManagementException(msg, e);
} }
return storedLocations; return storedLocations;
} }
@ -593,34 +606,45 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public String deleteApplicationRelease(int applicationId, String releaseUuid, boolean handleConnections) public String deleteApplicationRelease(int applicationId, String releaseUuid, boolean handleConnections)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Application application = getApplicationIfAccessible(applicationId); Application application = getApplicationIfAccessible(applicationId);
if (application == null) { if (application == null) {
throw new ApplicationManagementException("Invalid Application ID is received"); throw new ApplicationManagementException("Invalid Application ID is received");
} }
if (handleConnections) {
ConnectionManagerUtil.openDBConnection();
}
try { try {
ConnectionManagerUtil.beginDBTransaction();
ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, releaseUuid); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, releaseUuid);
LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid()); LifecycleState appLifecycleState = getLifecycleState(applicationId, applicationRelease.getUuid());
String currentState = appLifecycleState.getCurrentState(); String currentState = appLifecycleState.getCurrentState();
if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState if (AppLifecycleState.DEPRECATED.toString().equals(currentState) || AppLifecycleState
.REJECTED.toString().equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals .REJECTED.toString().equals(currentState) || AppLifecycleState.UNPUBLISHED.toString().equals
(currentState)) { (currentState)) {
LifecycleState newAppLifecycleState = new LifecycleState(); LifecycleState newAppLifecycleState = getLifecycleStateInstant(AppLifecycleState.REMOVED.toString(),
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); if (lifecycleStateManger.isValidStateChange(newAppLifecycleState.getPreviousState(),
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, false); newAppLifecycleState.getCurrentState())) {
this.lifecycleStateDAO
.addLifecycleState(newAppLifecycleState, applicationId, applicationRelease.getUuid(),
tenantId);
ConnectionManagerUtil.commitDBTransaction();
} else {
// todo
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException("Lifecycle State Validation failed. Application Id: " +
applicationId + " Application release UUID: " + releaseUuid); }
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException("Can't delete the application release, You have to move the " + throw new ApplicationManagementException("Can't delete the application release, You have to move the " +
"lifecycle state from " + currentState + " to acceptable " + "lifecycle state from " + currentState + " to acceptable " +
"state"); "state");
} }
return applicationRelease.getAppHashValue(); return applicationRelease.getAppHashValue();
} finally { } catch (LifeCycleManagementDAOException e) {
if (handleConnections) { ConnectionManagerUtil.rollbackDBTransaction();
ConnectionManagerUtil.closeDBConnection(); // todo
} throw new ApplicationManagementException("Can't delete the application release, You have to move the " +
"lifecycle state from " + "" + " to acceptable " +
"state");
} }
} }
@ -641,82 +665,85 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
/** /**
* To validate the application * To validate the application creating request
* *
* @param application Application that need to be created * @param application Application that need to be created
* @throws ValidationException Validation Exception * @throws RequestValidatingException Validation Exception
*/ */
private void validateAppCreatingRequest(Application application, int tenantId) throws ValidationException { private void validateAppCreatingRequest(Application application, int tenantId) throws RequestValidatingException {
Boolean isValidApplicationType; Boolean isValidApplicationType;
Filter filter = new Filter();
try { try {
filter.setFullMatch(true);
filter.setAppName(application.getName().trim());
filter.setOffset(0);
filter.setLimit(1);
if (application.getName() == null) { if (application.getName() == null) {
throw new ValidationException("Application name cannot be empty"); throw new RequestValidatingException("Application name cannot be empty");
} }
if (application.getUser() == null || application.getUser().getUserName() == null if (application.getUser() == null || application.getUser().getUserName() == null
|| application.getUser().getTenantId() == -1) { || application.getUser().getTenantId() == -1) {
throw new ValidationException("Username and tenant Id cannot be empty"); throw new RequestValidatingException("Username and tenant Id cannot be empty");
} }
if (application.getAppCategory() == null) { if (application.getAppCategory() == null) {
throw new ValidationException("Application category can't be empty"); throw new RequestValidatingException("Application category can't be empty");
} }
isValidApplicationType = isValidAppType(application); isValidApplicationType = isValidAppType(application.getType());
if (!isValidApplicationType) { if (!isValidApplicationType) {
throw new ValidationException( throw new RequestValidatingException(
"App Type contains in the application creating payload doesn't match with supported app types"); "App Type contains in the application creating payload doesn't match with supported app types");
} }
validateApplicationExistence(application, tenantId); if (application.getApplicationReleases().size() > 1 ){
} catch (ApplicationManagementException e) { throw new RequestValidatingException(
throw new ValidationException("Error occured while validating whether there is already an application " "Invalid payload. Application creating payload should contains one application release, but "
+ "registered with same name.", e); + "the payload contains more than one");
}
}
private Boolean isValidAppType(Application application) {
for (ApplicationType applicationType : ApplicationType.values()) {
if (applicationType.toString().equals(application.getType())) {
return true;
} }
}
return false;
}
/** //Check whether application is already existing one or not
* To validate the application existence
*
* @param application Application that need to be validated
* @throws ValidationException Validation Exception
*/
private void validateApplicationExistence(Application application, int tenantId)
throws ApplicationManagementException {
Filter filter = new Filter();
filter.setFullMatch(true);
filter.setAppName(application.getName().trim());
filter.setOffset(0);
filter.setLimit(1);
try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId);
if (applicationList != null && applicationList.getApplications() != null && !applicationList if (applicationList != null && applicationList.getApplications() != null && !applicationList
.getApplications().isEmpty()) { .getApplications().isEmpty()) {
throw new ApplicationManagementException( throw new RequestValidatingException(
"Already an application registered with same name - " + applicationList.getApplications().get(0) "Already an application registered with same name - " + applicationList.getApplications().get(0)
.getName()); .getName());
} }
} catch (DBConnectionException e) {
throw new ApplicationManagementException("test 1");
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException("test 2"); throw new RequestValidatingException(
"Error occured while getting existing applications for application name: " + application.getName()
+ " and application type " + application.getType() + ". Tenant ID is " + tenantId, e);
} catch (DBConnectionException e) {
throw new RequestValidatingException(
"Error occured while getting database connection to get existing applications for application name: "
+ application.getName() + " and application type: " + application.getType()
+ ". Tenant id is " + tenantId, e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
/***
* To verify whether application type is valid one or not
* @param appType application type {@link ApplicationType}
* @return true returns if appType is valid on, otherwise returns false
*/
private Boolean isValidAppType(String appType) {
if (appType == null) {
return false;
}
for (ApplicationType applicationType : ApplicationType.values()) {
if (applicationType.toString().equals(appType)) {
return true;
}
}
return false;
}
/** /**
* Get the application if application is an accessible one. * Get the application if application is an accessible one.
* *
@ -901,7 +928,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
*/ */
private void validateAppReleasePayload(ApplicationRelease applicationRelease) private void validateAppReleasePayload(ApplicationRelease applicationRelease)
throws ApplicationManagementException { throws ApplicationManagementException {
if (applicationRelease.getVersion() == null) { if (applicationRelease.getVersion() == null) {
throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for " throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for "
+ "creating release. It cannot be found."); + "creating release. It cannot be found.");
@ -933,31 +959,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
return lifecycleState; return lifecycleState;
} }
@Override public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, @Override
Boolean checkExist) throws ApplicationManagementException { public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
boolean handleDBConnection = false;
try { try {
if (checkExist) { ConnectionManagerUtil.beginDBTransaction();
ConnectionManagerUtil.openDBConnection(); if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)) {
handleDBConnection = true; throw new NotFoundException("Couldn't find application for the application Id: " + applicationId);
if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)){ }
throw new NotFoundException( if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)) {
"Couldn't find application for the application Id: " + applicationId); throw new NotFoundException("Couldn't find application release for the application Id: " + applicationId
} + " application release uuid: " + releaseUuid);
if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)){ }
throw new NotFoundException( LifecycleState currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid);
"Couldn't find application release for the application Id: " + applicationId if (currentState == null) {
+ " application release uuid: " + releaseUuid); throw new ApplicationManagementException(
} "Couldn't find latest lifecycle state for the appId: " + applicationId
LifecycleState currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); + " and application release UUID: " + releaseUuid);
if (currentState == null){
throw new ApplicationManagementException(
"Couldn't find latest lifecycle state for the appId: " + applicationId
+ " and application release UUID: " + releaseUuid);
}
state.setPreviousState(currentState.getCurrentState());
} }
state.setPreviousState(currentState.getCurrentState());
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName); state.setUpdatedBy(userName);
@ -967,21 +988,21 @@ public class ApplicationManagerImpl implements ApplicationManager {
//todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there //todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there
//todo any other application release in PUBLISHED state for the application( i.e for the appid) //todo any other application release in PUBLISHED state for the application( i.e for the appid)
this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId); this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId);
ConnectionManagerUtil.commitDBTransaction();
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction();
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '" log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '"
+ state.getCurrentState() + "'"); + state.getCurrentState() + "'");
throw new ApplicationManagementException("Lifecycle State Validation failed. Application Id: " + throw new ApplicationManagementException(
applicationId + " Application release UUID: " + releaseUuid); "Lifecycle State Validation failed. Application Id: " + applicationId
+ " Application release UUID: " + releaseUuid);
} }
} }
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Failed to add lifecycle state. Application Id: " + applicationId + " Application release UUID: " "Failed to add lifecycle state. Application Id: " + applicationId + " Application release UUID: "
+ releaseUuid, e); + releaseUuid, e);
} finally {
if (handleDBConnection) {
ConnectionManagerUtil.closeDBConnection();
}
} }
} }
@ -1059,19 +1080,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
private Filter validateFilter(Filter filter) { private Filter validateFilter(Filter filter) {
if (filter != null) { if (filter != null && filter.getAppType() != null) {
if (filter.getAppType() != null) { boolean isValidRequest = false;
boolean isValidRequest = false; for (ApplicationType applicationType : ApplicationType.values()) {
for (ApplicationType applicationType : ApplicationType.values()) { if (applicationType.toString().equals(filter.getAppType())) {
if (applicationType.toString().equals(filter.getAppType())) { isValidRequest = true;
isValidRequest = true; break;
break;
}
}
if (!isValidRequest) {
return null;
} }
} }
if (!isValidRequest) {
return null;
}
} }
return filter; return filter;
} }
@ -1086,4 +1105,18 @@ public class ApplicationManagerImpl implements ApplicationManager {
return list; return list;
} }
/***
* By invoking the method, it returns Lifecycle State Instance.
* @param currentState Current state of the lifecycle
* @param previousState Previouse state of the Lifecycle
* @return {@link LifecycleState}
*/
private LifecycleState getLifecycleStateInstant(String currentState, String previousState) {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(currentState);
lifecycleState.setPreviousState(previousState);
lifecycleState.setUpdatedBy(userName);
return lifecycleState;
}
} }

@ -202,31 +202,27 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} else { } else {
String msg = "Application creation is failed"; String msg = "Application creation is failed";
log.error(msg); log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} }
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while creating the application"; String msg = "Error occurred while creating the application";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
.entity(new ApplicationManagementException(msg, e)).build();
} catch (ResourceManagementException e) { } catch (ResourceManagementException e) {
String msg = String msg =
"Error occurred while uploading the releases artifacts of the application " + application.getName(); "Error occurred while uploading the releases artifacts of the application " + application.getName();
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
.entity(new ApplicationManagementException(msg, e)).build();
} catch (IOException e) { } catch (IOException e) {
String msg = String msg =
"Error while uploading binary file and resources for the application release of the application " "Error while uploading binary file and resources for the application release of the application "
+ application.getName(); + application.getName();
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
.entity(new ApplicationManagementException(msg, e)).build();
} catch (RequestValidatingException e) { } catch (RequestValidatingException e) {
String msg = "Error occurred while handling the application creating request"; String msg = "Error occurred while handling the application creating request";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(new ApplicationManagementException(msg, e)) return Response.status(Response.Status.BAD_REQUEST).build();
.build();
} }
} }
@ -585,7 +581,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
} }
LifecycleState state = new LifecycleState(); LifecycleState state = new LifecycleState();
state.setCurrentState(action); state.setCurrentState(action);
applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true); applicationManager.changeLifecycleState(applicationId, applicationUuid, state);
} catch (NotFoundException e) { } catch (NotFoundException e) {
String msg = "Could,t find application release for application id: " + applicationId String msg = "Could,t find application release for application id: " + applicationId
+ " and application release uuid: " + applicationUuid; + " and application release uuid: " + applicationUuid;
@ -602,11 +598,11 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
private boolean isValidAppCreatingRequest(Attachment iconFile, Attachment bannerFile, private boolean isValidAppCreatingRequest(Attachment iconFile, Attachment bannerFile,
List<Attachment> attachmentList, Application application) { List<Attachment> attachmentList, Application application) {
if (application.getApplicationReleases().size() > 1) { // if (application.getApplicationReleases().size() > 1) {
log.error("Invalid application creating request. Application creating request must have single application " // log.error("Invalid application creating request. Application creating request must have single application "
+ "release. Application name:" + application.getName() + " and type: " + application.getType()); // + "release. Application name:" + application.getName() + " and type: " + application.getType());
return false; // return false;
} // }
if (iconFile == null) { if (iconFile == null) {
log.error("Icon file is not found for the application release. Application name: " + application.getName() log.error("Icon file is not found for the application release. Application name: " + application.getName()

@ -37,7 +37,7 @@
<Extension name="ApplicationStorageManager"> <Extension name="ApplicationStorageManager">
<ClassName>org.wso2.carbon.device.application.mgt.core.impl.ApplicationStorageManagerImpl</ClassName> <ClassName>org.wso2.carbon.device.application.mgt.core.impl.ApplicationStorageManagerImpl</ClassName>
<Parameters> <Parameters>
<Parameter name="StoragePath">repository/resources/apps</Parameter> <Parameter name="StoragePath">repository/resources/apps/</Parameter>
<Parameter name="MaxScreenShotCount">6</Parameter> <Parameter name="MaxScreenShotCount">6</Parameter>
</Parameters> </Parameters>
</Extension> </Extension>

Loading…
Cancel
Save