Refactor the source

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent f861134ff6
commit b3874ee1ba

@ -188,7 +188,7 @@ public interface ApplicationManager {
* @return Updated Application Release. * @return Updated Application Release.
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
boolean isApplicationReleaseUpdateAcceptable(int appId, String appReleaseUuid) boolean isAcceptableAppReleaseUpdate(int appId, String appReleaseUuid)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**

@ -40,6 +40,7 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep
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.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.ApplicationReleaseDAO;
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;
@ -70,6 +71,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
private VisibilityDAO visibilityDAO; private VisibilityDAO visibilityDAO;
private ApplicationDAO applicationDAO; private ApplicationDAO applicationDAO;
private ApplicationReleaseDAO applicationReleaseDAO;
private LifecycleStateDAO lifecycleStateDAO; private LifecycleStateDAO lifecycleStateDAO;
@ -81,7 +83,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
this.deviceTypeDAO = ApplicationManagementDAOFactory.getDeviceTypeDAO(); this.deviceTypeDAO = ApplicationManagementDAOFactory.getDeviceTypeDAO();
this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO();
this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();; this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
} }
@Override @Override
@ -130,17 +133,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ "the payload contains more than one"); + "the payload contains more than one");
} }
applicationRelease = application.getApplicationReleases().get(0); applicationRelease = application.getApplicationReleases().get(0);
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). applicationRelease = this.applicationReleaseDAO
createRelease(applicationRelease, application.getId(), tenantId); .createRelease(applicationRelease, application.getId(), tenantId);
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setUpdatedBy(userName);
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
lifecycleState.setUpdatedBy(userName);
addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState);
this.lifecycleStateDAO
.addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId);
applicationRelease.setLifecycleState(lifecycleState); applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease); applicationReleases.add(applicationRelease);
@ -161,12 +160,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg, e); log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (LifeCycleManagementDAOException e) { } finally {
String msg = "Error occurred while adding application lifecycle state";
log.error(msg, e);
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(msg, e);
}finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -189,13 +183,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationList = getRoleRestrictedApplicationList(applicationList, userName); applicationList = getRoleRestrictedApplicationList(applicationList, userName);
} }
for (Application application : applicationList.getApplications()) { for (Application application : applicationList.getApplications()) {
applicationReleases = getReleases(application.getId()); applicationReleases = getReleases(application.getId());
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(applicationReleases);
} }
return applicationList; return applicationList;
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
@ -210,10 +201,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) throws public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease)
ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application = getApplicationIfAccessible(applicationId); Application application = getApplicationIfAccessible(applicationId);
validateReleaseCreatingRequest(applicationRelease); validateReleaseCreatingRequest(applicationRelease);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -221,16 +211,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). applicationRelease = this.applicationReleaseDAO
createRelease(applicationRelease, application.getId(), tenantId); .createRelease(applicationRelease, application.getId(), tenantId);
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setUpdatedBy(userName);
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
lifecycleState.setUpdatedBy(userName);
addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState);
this.lifecycleStateDAO
.addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return applicationRelease; return applicationRelease;
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
@ -238,11 +225,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Error occured while adding application release into IoTS app management Application id of the " "Error occured while adding application release into IoTS app management Application id of the "
+ "application release: " + applicationId, e); + "application release: " + applicationId, e);
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Error occured while adding application release lifecycle state to IoTS app management. "
+ "Application id of the application release: " + applicationId, e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
@ -381,8 +363,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
applicationReleases = ApplicationManagementDAOFactory.getApplicationReleaseDAO() applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId);
.getReleases(application.getName(), application.getType(), tenantId);
for (ApplicationRelease applicationRelease : applicationReleases) { for (ApplicationRelease applicationRelease : applicationReleases) {
LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO().
getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
@ -425,7 +406,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState(); LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
newAppLifecycleState.setUpdatedBy(userName);
addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState);
storedLocations.add(applicationRelease.getAppHashValue()); storedLocations.add(applicationRelease.getAppHashValue());
} }
@ -444,8 +424,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public String deleteApplicationRelease(int applicationId, String releaseUuid) public String deleteApplicationRelease(int applicationId, String releaseUuid)
throws ApplicationManagementException { throws ApplicationManagementException {
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
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");
@ -462,7 +440,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState(); LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
newAppLifecycleState.setUpdatedBy(userName);
addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState);
}else{ }else{
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 " +
@ -506,15 +483,14 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ValidationException("Username and tenant Id cannot be empty"); throw new ValidationException("Username and tenant Id cannot be empty");
} }
if (application.getAppCategory() == null) { if (application.getAppCategory() == null) {
throw new ValidationException("Username and tenant Id cannot be empty"); throw new ValidationException("Application category can't be empty");
} }
isValidApplicationType = isValidAppType(application); isValidApplicationType = isValidAppType(application);
if (!isValidApplicationType) { if (!isValidApplicationType) {
throw new ValidationException( throw new ValidationException(
"App Type contains in the application creating payload doesn't match with " + "App Type contains in the application creating payload doesn't match with supported app types");
"supported app types");
} }
validateApplicationExistence(application); validateApplicationExistence(application);
@ -620,8 +596,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ "parameter to get the relevant application."); + "parameter to get the relevant application.");
} }
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO().getReleaseByIds applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId);
(applicationId, applicationUuid, tenantId);
if (applicationRelease == null) { if (applicationRelease == null) {
throw new ApplicationManagementException("Doesn't exist a application release for application ID: " + throw new ApplicationManagementException("Doesn't exist a application release for application ID: " +
applicationId + "and application UUID: " + applicationId + "and application UUID: " +
@ -644,10 +619,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
"Application Id: " + appId); "Application Id: " + appId);
} }
try { try {
ApplicationRelease existingAppRelease = getAppReleaseIfExists(appId, applicationRelease.getUuid());
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
// todo consider about lifecycle // todo consider about lifecycle
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO() applicationRelease = this.applicationReleaseDAO.updateRelease(appId, applicationRelease, tenantId);
.updateRelease(appId, applicationRelease, tenantId);
return applicationRelease; return applicationRelease;
} finally { } finally {
@ -656,7 +631,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public boolean isApplicationReleaseUpdateAcceptable(int appId, String appReleaseUuid) public boolean isAcceptableAppReleaseUpdate(int appId, String appReleaseUuid)
throws ApplicationManagementException { throws ApplicationManagementException {
LifecycleState lifecycleState = getLifecycleState(appId, appReleaseUuid); LifecycleState lifecycleState = getLifecycleState(appId, appReleaseUuid);
return AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) || AppLifecycleState return AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) || AppLifecycleState
@ -713,9 +688,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState; LifecycleState lifecycleState;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid);
lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState())); lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState()));
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException("Failed to get lifecycle state", e); throw new ApplicationManagementException("Failed to get lifecycle state", e);
@ -728,19 +702,20 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state)
ApplicationManagementException { throws ApplicationManagementException {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
Application application = getApplicationIfAccessible(applicationId); Application application = getApplicationIfAccessible(applicationId);
ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid);
LifecycleStateDAO lifecycleStateDAO;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null && state.getUpdatedBy() != null) { if (state.getCurrentState() != null && state.getPreviousState() != null) {
validateLifecycleState(state); validateLifecycleState(state);
lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); this.lifecycleStateDAO
lifecycleStateDAO.addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); .addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId);
} }
} catch (LifeCycleManagementDAOException | DBConnectionException e) { } catch (LifeCycleManagementDAOException | DBConnectionException e) {
throw new ApplicationManagementException("Failed to add lifecycle state", e); throw new ApplicationManagementException("Failed to add lifecycle state", e);

@ -325,7 +325,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
try { try {
Application application = applicationManager.getApplicationIfAccessible(applicationId); Application application = applicationManager.getApplicationIfAccessible(applicationId);
if (!applicationManager.isApplicationReleaseUpdateAcceptable(application.getId(), if (!applicationManager.isAcceptableAppReleaseUpdate(application.getId(),
applicationRelease.getUuid())) { applicationRelease.getUuid())) {
String msg = "Application release is in the " + applicationRelease.getLifecycleState().getCurrentState() String msg = "Application release is in the " + applicationRelease.getLifecycleState().getCurrentState()
+ " state. Hence updating is not acceptable when application in this state"; + " state. Hence updating is not acceptable when application in this state";

Loading…
Cancel
Save