App create fixed

4.x.x
Inosh Perara 6 years ago committed by Dharmakeerthi Lasantha
parent 1c124f45e6
commit 107e46a0bc

@ -146,9 +146,13 @@ public interface ApplicationManager {
*
* @param applicationId ID of the Application.
* @param applicationUuid UUID of the Application Release.
* @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
* @param releaseId The release ID of the application(optional)
* @throws ApplicationManagementException Application Management Exception.
*/
void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws ApplicationManagementException;
void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean checkExist,
int releaseId) throws ApplicationManagementException;
/**
* Get the application if application is an accessible one.

@ -137,10 +137,11 @@ public class Util {
tag.setTagName(rs.getString("APP_TAG"));
UnrestrictedRole unrestrictedRole = new UnrestrictedRole();
unrestrictedRole.setRole(rs.getString("ROLE"));
if (application.getTags().contains(tag)) {
if (application.getTags() != null && application.getTags().contains(tag)) {
application.getTags().add(tag);
}
if (application.getUnrestrictedRoles().contains(unrestrictedRole)) {
if (application.getUnrestrictedRoles() != null && application.getUnrestrictedRoles()
.contains(unrestrictedRole)) {
application.getUnrestrictedRoles().add(unrestrictedRole);
}
iteration++;

@ -230,7 +230,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
+ " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, AR.SC_2_LOCATION AS "
+ "SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS HASH_VALUE, "
+ "AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, "
+ "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS FROM AP_APP_RELEASE AS "
+ "AR.PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AR.RATING FROM AP_APP_RELEASE AS "
+ "AR where AR.AP_APP_ID=(SELECT ID FROM AP_APP WHERE NAME = ? AND TYPE = ? "
+ "AND TENANT_ID = ?) AND AR.TENANT_ID = ? ;";

@ -99,15 +99,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName);
}
ConnectionManagerUtil.openDBConnection();
validateAppCreatingRequest(application);
validateAppReleasePayload(application.getApplicationReleases().get(0));
DeviceType deviceType;
ApplicationRelease applicationRelease;
List<ApplicationRelease> applicationReleases = new ArrayList<>();
try {
ConnectionManagerUtil.openDBConnection();
ConnectionManagerUtil.beginDBTransaction();
MAMDeviceConnectorImpl mamDeviceConnector = new MAMDeviceConnectorImpl();
// Getting the device type details to get device type ID for internal mappings
deviceType = mamDeviceConnector.getDeviceManagementService().getDeviceType(application.getDeviceType());
if (deviceType == null) {
@ -115,6 +117,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.rollbackDBTransaction();
return null;
}
// Insert to application table
int appId = this.applicationDAO.createApplication(application, deviceType.getId());
if (appId == -1) {
@ -122,13 +125,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.rollbackDBTransaction();
return null;
} else {
if(log.isDebugEnabled()){
log.debug("New Application entry added to AP_APP table. App Id:" + appId);
}
if (!application.getTags().isEmpty()) {
this.applicationDAO.addTags(application.getTags(), appId, tenantId);
if(log.isDebugEnabled()){
log.debug("New tags entry added to AP_APP_TAG table. App Id:" + appId);
}
}
if (!application.getUnrestrictedRoles().isEmpty()) {
application.setIsRestricted(true);
this.visibilityDAO.addUnrestrictedRoles(application.getUnrestrictedRoles(), appId, tenantId);
if(log.isDebugEnabled()){
log.debug("New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table." +
" App Id:" + appId);
}
} else {
if(log.isDebugEnabled()){
log.debug("App is not restricted to role. App Id:" + appId);
}
application.setIsRestricted(false);
}
if (application.getApplicationReleases().size() > 1 ){
@ -136,14 +152,22 @@ public class ApplicationManagerImpl implements ApplicationManager {
"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);
}
applicationRelease = application.getApplicationReleases().get(0);
applicationRelease = this.applicationReleaseDAO
.createRelease(applicationRelease, appId, tenantId);
if(log.isDebugEnabled()){
log.debug("Changing lifecycle state. App Id:" + appId);
}
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState);
changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false,
applicationRelease.getId());
applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease);
@ -160,12 +184,18 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while getting device type id of " + application.getType();
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e);
} catch (Exception e) {
String msg = "Unknown exception while creating application.";
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e);
} finally {
//ConnectionManagerUtil.closeDBConnection(); //todo: check this again
ConnectionManagerUtil.closeDBConnection();
}
}
@ -182,8 +212,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
try {
ConnectionManagerUtil.openDBConnection();
ConnectionManagerUtil.getDBConnection();
applicationList = applicationDAO.getApplications(filter, tenantId);
if(applicationList != null && applicationList.getApplications() != null && applicationList
.getApplications().size() > 0) {
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationList = getRoleRestrictedApplicationList(applicationList, userName);
}
@ -191,6 +223,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationReleases = getReleases(application.getId());
application.setApplicationReleases(applicationReleases);
}
}
return applicationList;
} catch (UserStoreException e) {
throw new ApplicationManagementException(
@ -199,8 +232,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
} catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException(
"DAO exception while getting applications for the user " + userName + " of tenant " + tenantId, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@ -220,7 +251,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState);
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true, 0);
ConnectionManagerUtil.commitDBTransaction();
return applicationRelease;
@ -365,12 +396,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("Request is received to retrieve all the releases related with the application " + application
.toString());
}
try {
ConnectionManagerUtil.openDBConnection();
ConnectionManagerUtil.getDBConnection();
applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId);
for (ApplicationRelease applicationRelease : applicationReleases) {
LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO().
getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
if (lifecycleState != null) {
applicationRelease.setLifecycleState(lifecycleState);
if (!AppLifecycleState.REMOVED.toString()
@ -378,10 +409,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
filteredApplicationReleases.add(applicationRelease);
}
}
return filteredApplicationReleases;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
return filteredApplicationReleases;
}
@Override
@ -410,7 +440,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState);
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0);
storedLocations.add(applicationRelease.getAppHashValue());
}
ConnectionManagerUtil.openDBConnection();
@ -441,7 +471,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState);
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0);
}else{
throw new ApplicationManagementException("Can't delete the application release, You have to move the " +
"lifecycle state from "+ currentState + " to acceptable " +
@ -583,7 +613,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationRelease applicationRelease;
try {
if (applicationId <= 0) {
throw new ApplicationManagementException(
"Application id could,t be a negative integer. Hence please add " +
@ -593,7 +623,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException("Application UUID is null. Application UUID is a required "
+ "parameter to get the relevant application.");
}
ConnectionManagerUtil.openDBConnection();
ConnectionManagerUtil.getDBConnection();
applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId);
if (applicationRelease == null) {
throw new ApplicationManagementException("Doesn't exist a application release for application ID: " +
@ -601,9 +631,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationUuid);
}
return applicationRelease;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@ -703,19 +730,24 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state)
throws ApplicationManagementException {
public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean
checkExist, int releaseId) throws ApplicationManagementException {
try {
Application application = getApplicationIfAccessible(applicationId);
ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid);
if (checkExist) {
getApplicationIfAccessible(applicationId);
}
if (releaseId < 1) {
releaseId = getAppReleaseIfExists(applicationId, applicationUuid).getId();
}
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null) {
if (lifecycleStateManger.isValidStateChange(state.getPreviousState(), state.getCurrentState())) {
if (getLifecycleManagementService().isValidStateChange(state.getPreviousState(), state.getCurrentState())) {
this.lifecycleStateDAO
.addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId);
.addLifecycleState(state, applicationId, releaseId, tenantId);
} else {
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'"
+ " to '" + state.getCurrentState() + "'");
@ -829,4 +861,16 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
return list;
}
public LifecycleStateManger getLifecycleManagementService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
LifecycleStateManger deviceManagementProviderService =
(LifecycleStateManger) ctx.getOSGiService(LifecycleStateManger.class, null);
if (deviceManagementProviderService == null) {
String msg = "DeviceImpl Management provider service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
return deviceManagementProviderService;
}
}

@ -95,6 +95,7 @@ public class ApplicationManagementServiceComponent {
getConfiguration().getLifecycleStates();
LifecycleStateManger lifecycleStateManger = new LifecycleStateManger(lifecycleStates);
DataHolder.getInstance().setLifecycleStateManger(lifecycleStateManger);
bundleContext.registerService(LifecycleStateManger.class.getName(), lifecycleStateManger, null);
log.info("ApplicationManagement core bundle has been successfully initialized");
} catch (Throwable e) {

@ -3,6 +3,7 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle;
import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -26,6 +27,36 @@ public class LifecycleStateManger {
}
public boolean isValidStateChange(String currentState, String nextState) {
return lifecycleStates.get(currentState).getProceedingStates().contains(nextState);
if (currentState.equalsIgnoreCase(nextState)) {
return true;
}
State state = getMatchingState(currentState);
if (state != null) {
return getMatchingNextState(state.getProceedingStates(), nextState);
}
return false;
}
private State getMatchingState(String currentState) {
Iterator it = lifecycleStates.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(pair.getKey().toString().equalsIgnoreCase(currentState)) {
return lifecycleStates.get(pair.getKey().toString());
}
it.remove();
}
return null;
}
private boolean getMatchingNextState(Set<String> proceedingStates, String nextState) {
for (String state: proceedingStates) {
if (state.equalsIgnoreCase(nextState)) {
return true;
}
}
return false;
}
}

@ -136,6 +136,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
if (!isValidAppCreatingRequest(binaryFile, iconFile, bannerFile, attachmentList, application)) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
// The application executable artifacts such as apks are uploaded.
if (!ApplicationType.ENTERPRISE.toString().equals(application.getType())) {
applicationRelease = application.getApplicationReleases().get(0);
applicationRelease = applicationStorageManager
@ -157,11 +159,15 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
for (Attachment screenshot : attachmentList) {
attachments.add(screenshot.getDataHandler().getInputStream());
}
// Upload images
applicationRelease = applicationStorageManager.uploadImageArtifacts(applicationRelease, iconFileStream,
bannerFileStream, attachments);
applicationRelease.setUuid(UUID.randomUUID().toString());
applicationReleases.add(applicationRelease);
application.setApplicationReleases(applicationReleases);
// Created new application entry
Application createdApplication = applicationManager.createApplication(application);
if (createdApplication != null) {
return Response.status(Response.Status.CREATED).entity(createdApplication).build();
@ -460,7 +466,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
LifecycleState state) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
applicationManager.changeLifecycleState(applicationId, applicationUuid, state);
applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true, 0);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while adding lifecycle state.";
log.error(msg, e);

Loading…
Cancel
Save