Add application release updating logic into the service layer

Adding application release updating logic and application release creating logic into the service layer.
feature/appm-store/pbac
lasantha 7 years ago
parent f83b466955
commit 6f18a70d4b

@ -187,4 +187,34 @@ public interface ApplicationManager {
*/ */
ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease)
throws ApplicationManagementException; throws ApplicationManagementException;
/**
* To verify whether application release is acceptable to update or not.
*
* @param appId ID of the Application
* @param appReleaseUuid UUID of the ApplicationRelease
* @return Updated Application Release.
* @throws ApplicationManagementException Application Management Exception.
*/
boolean isApplicationReleaseUpdateAcceptable(int appId, String appReleaseUuid)
throws ApplicationManagementException;
/**
* To create an application release for an Application.
*
* @param applicationId ID of the Application
* @param applicationRelease ApplicatonRelease that need to be be created.
* @return the unique id of the application release, if the application release succeeded else -1
*/
ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease)
throws ApplicationManagementException;
/**
* To get the application release of the Application/
*
* @param applicationUuid UUID of the Application.
* @return ApplicationRelease related with particular Application UUID and version.
* @throws ApplicationManagementException ApplicationManagementException
*/
ApplicationRelease getReleaseByUuid(String applicationUuid) throws ApplicationManagementException;
} }

@ -30,7 +30,6 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.ApplicationType;
import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.Filter;
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.LifecycleStateTransition;
import org.wso2.carbon.device.application.mgt.common.SortingOrder; import org.wso2.carbon.device.application.mgt.common.SortingOrder;
import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole; import org.wso2.carbon.device.application.mgt.common.UnrestrictedRole;
import org.wso2.carbon.device.application.mgt.common.User; import org.wso2.carbon.device.application.mgt.common.User;
@ -85,23 +84,21 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public Application createApplication(Application application) throws ApplicationManagementException { public Application createApplication(Application application) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
User loggedInUser = new User(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(), String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true)); application.setUser(new User(userName, tenantId));
application.setUser(loggedInUser);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Create Application received for the tenant : " + application.getUser().getTenantId() + " From" log.debug("Create Application received for the tenant : " + application.getUser().getTenantId() + " From"
+ " the user : " + application.getUser().getUserName()); + " the user : " + application.getUser().getUserName());
} }
validateAppCreatingRequest(application); validateAppCreatingRequest(application);
validateReleaseCreatingRequest(application.getApplicationReleases()); validateReleaseCreatingRequest(application.getApplicationReleases().get(0));
DeviceType deviceType; DeviceType deviceType;
ApplicationRelease applicationRelease; ApplicationRelease applicationRelease;
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
int tenantId = application.getUser().getTenantId(); deviceType = this.deviceTypeDAO.getDeviceType(application.getType(), tenantId);
deviceType = this.deviceTypeDAO.getDeviceType(application.getType(), application.getUser().getTenantId());
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");
@ -128,12 +125,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationRelease = application.getApplicationReleases().get(0); applicationRelease = application.getApplicationReleases().get(0);
applicationRelease.setCreatedAt((Timestamp) new Date()); applicationRelease.setCreatedAt((Timestamp) new Date());
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO().
createRelease(applicationRelease, application.getId()); createRelease(applicationRelease, application.getId(), tenantId);
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setAppId(application.getId()); lifecycleState.setAppId(application.getId());
lifecycleState.setReleaseId(applicationRelease.getId()); lifecycleState.setReleaseId(applicationRelease.getId());
lifecycleState.setUpdatedBy(loggedInUser.getUserName()); lifecycleState.setUpdatedBy(userName);
lifecycleState.setTenantId(loggedInUser.getTenantId()); lifecycleState.setTenantId(tenantId);
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState);
@ -193,6 +190,35 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
@Override
public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) throws
ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Application application = validateApplication(applicationId);
validateReleaseCreatingRequest(applicationRelease);
if (log.isDebugEnabled()) {
log.debug("Application release request is received for the application " + application.toString());
}
applicationRelease.setCreatedAt((Timestamp) new Date());
try {
ConnectionManagerUtil.beginDBTransaction();
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO().
createRelease(applicationRelease, application.getId(), tenantId);
ConnectionManagerUtil.commitDBTransaction();
return applicationRelease;
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public ApplicationRelease getReleaseByUuid(String applicationUuid) throws ApplicationManagementException {
return null;
}
@Override @Override
public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException { public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException {
try { try {
@ -403,7 +429,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public String deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException { public String deleteApplicationRelease(int applicationId, String releaseUuid)
throws ApplicationManagementException {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Application application = validateApplication(applicationId); Application application = validateApplication(applicationId);
@ -466,7 +493,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
isValidApplicationType = isValidAppType(application); isValidApplicationType = isValidAppType(application);
if (!isValidApplicationType) { if (!isValidApplicationType) {
throw new ValidationException("App Type contains in the application creating payload doesn't match with " + throw new ValidationException(
"App Type contains in the application creating payload doesn't match with " +
"supported app types"); "supported app types");
} }
@ -537,7 +565,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " throw new ApplicationManagementException("Application UUID is null. Application UUID is a required "
+ "parameter to get the relevant application."); + "parameter to get the relevant application.");
} }
ApplicationRelease applicationRelease = DataHolder.getInstance().getApplicationReleaseManager() ApplicationRelease applicationRelease = DataHolder.getInstance().getApplicationManager()
.getReleaseByUuid(applicationUuid); .getReleaseByUuid(applicationUuid);
if (applicationRelease == null) { if (applicationRelease == null) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
@ -549,9 +577,31 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) throws public ApplicationRelease updateRelease(int appId, ApplicationRelease applicationRelease) throws
ApplicationManagementException { ApplicationManagementException {
LifecycleState lifecycleState = getLifecycleState(appId, applicationRelease.getUuid()); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (log.isDebugEnabled()) {
log.debug("Updating the Application release. UUID: " + applicationRelease.getUuid() + ", " +
"Application Id: " + appId);
}
try {
ConnectionManagerUtil.openDBConnection();
applicationRelease.setModifiedBy(userName);
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO()
.updateRelease(appId, applicationRelease, tenantId);
return null; return applicationRelease;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public boolean isApplicationReleaseUpdateAcceptable(int appId, String appReleaseUuid)
throws ApplicationManagementException {
LifecycleState lifecycleState = getLifecycleState(appId, appReleaseUuid);
return AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) || AppLifecycleState
.IN_REVIEW.toString().equals(lifecycleState.getCurrentState()) ||
AppLifecycleState.REJECTED.toString().equals(lifecycleState.getCurrentState());
} }
/** /**
@ -585,28 +635,16 @@ public class ApplicationManagerImpl implements ApplicationManager {
/** /**
* To validate a create release request to make sure all the pre-conditions satisfied. * To validate a create release request to make sure all the pre-conditions satisfied.
* *
* @param applicationReleases ApplicationRelease that need to be created. * @param applicationRelease ApplicationRelease that need to be created.
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
private void validateReleaseCreatingRequest(List<ApplicationRelease> applicationReleases) private void validateReleaseCreatingRequest(ApplicationRelease applicationRelease)
throws ApplicationManagementException { throws ApplicationManagementException {
if (applicationReleases.isEmpty() || applicationReleases.size() > 1) { if (applicationRelease.getVersion() == null) {
throw new ApplicationManagementException("ApplicationRelease size is grater than minimal release size or "
+ "request doesn't contains application release");
}
if (applicationReleases.get(0).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.");
} }
//todo
// if (getRelease(applicationReleases.get(0).getUuid(), applicationReleases.get(0).getVersion(),
// applicationReleases.get(0).getReleaseType()) != null) {
// throw new ApplicationManagementException( "Application Release for the Application UUID " +
// applicationReleases.get(0).getUuid() + " " + "with the version "
// + applicationReleases.get(0).getVersion() + " already exists. Cannot create an " +
// "application release with the same version.");
// }
} }
@Override @Override
@ -616,7 +654,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
Application application = validateApplication(applicationId);
//todo applicationUuid and applicationId should be passed and util method has to be changed //todo applicationUuid and applicationId should be passed and util method has to be changed
ApplicationRelease applicationRelease = validateApplicationRelease(applicationUuid); ApplicationRelease applicationRelease = validateApplicationRelease(applicationUuid);
lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
@ -700,40 +737,46 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (!AppLifecycleState.CREATED.toString().equals(state.getPreviousState()) && if (!AppLifecycleState.CREATED.toString().equals(state.getPreviousState()) &&
!AppLifecycleState.REJECTED.toString().equals(state.getPreviousState())) { !AppLifecycleState.REJECTED.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be either " + AppLifecycleState.CREATED.toString() + " or " + "Previous State should be either " +
AppLifecycleState.CREATED.toString() + " or " +
AppLifecycleState.REJECTED.toString()); AppLifecycleState.REJECTED.toString());
} }
} }
if (AppLifecycleState.APPROVED.toString().equals(state.getCurrentState())) { if (AppLifecycleState.APPROVED.toString().equals(state.getCurrentState())) {
if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) { if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be " + AppLifecycleState.IN_REVIEW.toString()); "Previous State should be " +
AppLifecycleState.IN_REVIEW.toString());
} }
} }
if (AppLifecycleState.PUBLISHED.toString().equals(state.getCurrentState())) { if (AppLifecycleState.PUBLISHED.toString().equals(state.getCurrentState())) {
if (!AppLifecycleState.APPROVED.toString().equals(state.getPreviousState()) && if (!AppLifecycleState.APPROVED.toString().equals(state.getPreviousState()) &&
!AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) { !AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be either " + AppLifecycleState.APPROVED.toString() + " or " + "Previous State should be either " +
AppLifecycleState.APPROVED.toString() + " or " +
AppLifecycleState.UNPUBLISHED.toString()); AppLifecycleState.UNPUBLISHED.toString());
} }
} }
if (AppLifecycleState.UNPUBLISHED.toString().equals(state.getCurrentState())) { if (AppLifecycleState.UNPUBLISHED.toString().equals(state.getCurrentState())) {
if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) { if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be " + AppLifecycleState.PUBLISHED.toString()); "Previous State should be " +
AppLifecycleState.PUBLISHED.toString());
} }
} }
if (AppLifecycleState.REJECTED.toString().equals(state.getCurrentState())) { if (AppLifecycleState.REJECTED.toString().equals(state.getCurrentState())) {
if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) { if (!AppLifecycleState.IN_REVIEW.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be " + AppLifecycleState.IN_REVIEW.toString()); "Previous State should be " +
AppLifecycleState.IN_REVIEW.toString());
} }
} }
if (AppLifecycleState.DEPRECATED.toString().equals(state.getCurrentState())) { if (AppLifecycleState.DEPRECATED.toString().equals(state.getCurrentState())) {
if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) { if (!AppLifecycleState.PUBLISHED.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be " + AppLifecycleState.PUBLISHED.toString()); "Previous State should be " +
AppLifecycleState.PUBLISHED.toString());
} }
} }
if (AppLifecycleState.REMOVED.toString().equals(state.getCurrentState())) { if (AppLifecycleState.REMOVED.toString().equals(state.getCurrentState())) {
@ -741,8 +784,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
!AppLifecycleState.REJECTED.toString().equals(state.getPreviousState()) && !AppLifecycleState.REJECTED.toString().equals(state.getPreviousState()) &&
!AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) { !AppLifecycleState.UNPUBLISHED.toString().equals(state.getPreviousState())) {
throw new LifecycleManagementException("If Current State is " + state.getCurrentState() + throw new LifecycleManagementException("If Current State is " + state.getCurrentState() +
"Previous State should be either " + AppLifecycleState.DEPRECATED.toString() + " or " + "Previous State should be either " +
AppLifecycleState.REJECTED.toString() + " or " + AppLifecycleState.UNPUBLISHED.toString()); AppLifecycleState.DEPRECATED.toString() + " or " +
AppLifecycleState.REJECTED.toString() + " or " +
AppLifecycleState.UNPUBLISHED.toString());
} }
} }
} }
@ -756,19 +801,22 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
if (!existingApplication.getType().equals(application.getType())) { if (!existingApplication.getType().equals(application.getType())) {
throw new ApplicationManagementException("You are trying to change the application type and it is not " + throw new ApplicationManagementException("You are trying to change the application type and it is not " +
"possible after you create an application. Therefore please remove this application and publish " + "possible after you create an application. Therefore " +
"please remove this application and publish " +
"new application with type: " + application.getType()); "new application with type: " + application.getType());
} }
if (existingApplication.getIsFree() != application.getIsFree()) { if (existingApplication.getIsFree() != application.getIsFree()) {
if (existingApplication.getIsFree() == 1) { if (existingApplication.getIsFree() == 1) {
if (application.getPaymentCurrency() != null || !application.getPaymentCurrency().equals("")) { if (application.getPaymentCurrency() != null || !application.getPaymentCurrency().equals("")) {
throw new ApplicationManagementException("If you are going to change Non-Free app as Free app, " + throw new ApplicationManagementException("If you are going to change Non-Free app as Free app, " +
"currency attribute in the application updating payload should be null or \"\""); "currency attribute in the application updating " +
"payload should be null or \"\"");
} }
} else if (existingApplication.getIsFree() == 0) { } else if (existingApplication.getIsFree() == 0) {
if (application.getPaymentCurrency() == null || application.getPaymentCurrency().equals("")) { if (application.getPaymentCurrency() == null || application.getPaymentCurrency().equals("")) {
throw new ApplicationManagementException("If you are going to change Free app as Non-Free app, " + throw new ApplicationManagementException("If you are going to change Free app as Non-Free app, " +
"currency attribute in the application payload should not be null or \"\""); "currency attribute in the application payload " +
"should not be null or \"\"");
} }
} }
} }
@ -777,7 +825,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (existingApplication.getIsRestricted() == 1) { if (existingApplication.getIsRestricted() == 1) {
if (application.getUnrestrictedRoles() == null || application.getUnrestrictedRoles().isEmpty()) { if (application.getUnrestrictedRoles() == null || application.getUnrestrictedRoles().isEmpty()) {
throw new ApplicationManagementException("If you are going to add role restriction for non role " + throw new ApplicationManagementException("If you are going to add role restriction for non role " +
"restricted Application, Unrestricted role list won't be empty or null"); "restricted Application, Unrestricted role list " +
"won't be empty or null");
} }
} else if (existingApplication.getIsRestricted() == 0) { } else if (existingApplication.getIsRestricted() == 0) {
if (application.getUnrestrictedRoles() != null || !application.getUnrestrictedRoles().isEmpty()) { if (application.getUnrestrictedRoles() != null || !application.getUnrestrictedRoles().isEmpty()) {

Loading…
Cancel
Save