From b3874ee1baa589c20e4278a8f3e98001b620e69d Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sun, 16 Sep 2018 01:57:08 +0530 Subject: [PATCH] Refactor the source --- .../common/services/ApplicationManager.java | 2 +- .../mgt/core/impl/ApplicationManagerImpl.java | 79 +++++++------------ .../impl/ApplicationManagementAPIImpl.java | 2 +- 3 files changed, 29 insertions(+), 54 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 37070b38e4..1335c6d9dc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -188,7 +188,7 @@ public interface ApplicationManager { * @return Updated Application Release. * @throws ApplicationManagementException Application Management Exception. */ - boolean isApplicationReleaseUpdateAcceptable(int appId, String appReleaseUuid) + boolean isAcceptableAppReleaseUpdate(int appId, String appReleaseUuid) throws ApplicationManagementException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index fcfa5f4225..853ac72e93 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -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.services.ApplicationManager; 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.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; @@ -70,6 +71,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private DeviceTypeDAO deviceTypeDAO; private VisibilityDAO visibilityDAO; private ApplicationDAO applicationDAO; + private ApplicationReleaseDAO applicationReleaseDAO; private LifecycleStateDAO lifecycleStateDAO; @@ -81,7 +83,8 @@ public class ApplicationManagerImpl implements ApplicationManager { this.deviceTypeDAO = ApplicationManagementDAOFactory.getDeviceTypeDAO(); this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); - this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();; + this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); + this.applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); } @Override @@ -130,17 +133,13 @@ public class ApplicationManagerImpl implements ApplicationManager { + "the payload contains more than one"); } applicationRelease = application.getApplicationReleases().get(0); - applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). - createRelease(applicationRelease, application.getId(), tenantId); + applicationRelease = this.applicationReleaseDAO + .createRelease(applicationRelease, application.getId(), tenantId); LifecycleState lifecycleState = new LifecycleState(); - lifecycleState.setUpdatedBy(userName); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - lifecycleState.setUpdatedBy(userName); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -161,12 +160,7 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); - } catch (LifeCycleManagementDAOException e) { - String msg = "Error occurred while adding application lifecycle state"; - log.error(msg, e); - ConnectionManagerUtil.rollbackDBTransaction(); - throw new ApplicationManagementException(msg, e); - }finally { + } finally { ConnectionManagerUtil.closeDBConnection(); } } @@ -189,13 +183,10 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationList = getRoleRestrictedApplicationList(applicationList, userName); } - for (Application application : applicationList.getApplications()) { - applicationReleases = getReleases(application.getId()); application.setApplicationReleases(applicationReleases); } - return applicationList; } catch (UserStoreException e) { throw new ApplicationManagementException( @@ -210,10 +201,9 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) throws - ApplicationManagementException { + public ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease) + throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); Application application = getApplicationIfAccessible(applicationId); validateReleaseCreatingRequest(applicationRelease); if (log.isDebugEnabled()) { @@ -221,16 +211,13 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { ConnectionManagerUtil.beginDBTransaction(); - applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). - createRelease(applicationRelease, application.getId(), tenantId); + applicationRelease = this.applicationReleaseDAO + .createRelease(applicationRelease, application.getId(), tenantId); LifecycleState lifecycleState = new LifecycleState(); - lifecycleState.setUpdatedBy(userName); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - lifecycleState.setUpdatedBy(userName); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); - this.lifecycleStateDAO - .addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; } catch (ApplicationManagementDAOException e) { @@ -238,11 +225,6 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException( "Error occured while adding application release into IoTS app management Application id of the " + "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 { ConnectionManagerUtil.closeDBConnection(); } @@ -381,8 +363,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { ConnectionManagerUtil.openDBConnection(); - applicationReleases = ApplicationManagementDAOFactory.getApplicationReleaseDAO() - .getReleases(application.getName(), application.getType(), tenantId); + applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId); for (ApplicationRelease applicationRelease : applicationReleases) { LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); @@ -425,7 +406,6 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - newAppLifecycleState.setUpdatedBy(userName); addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); storedLocations.add(applicationRelease.getAppHashValue()); } @@ -444,8 +424,6 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public String deleteApplicationRelease(int applicationId, String releaseUuid) throws ApplicationManagementException { - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Application application = getApplicationIfAccessible(applicationId); if (application == null) { throw new ApplicationManagementException("Invalid Application ID is received"); @@ -462,7 +440,6 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - newAppLifecycleState.setUpdatedBy(userName); addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); }else{ 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"); } 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); if (!isValidApplicationType) { throw new ValidationException( - "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); @@ -620,8 +596,7 @@ public class ApplicationManagerImpl implements ApplicationManager { + "parameter to get the relevant application."); } ConnectionManagerUtil.openDBConnection(); - applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO().getReleaseByIds - (applicationId, applicationUuid, tenantId); + applicationRelease = this.applicationReleaseDAO.getReleaseByIds(applicationId, applicationUuid, tenantId); if (applicationRelease == null) { throw new ApplicationManagementException("Doesn't exist a application release for application ID: " + applicationId + "and application UUID: " + @@ -644,10 +619,10 @@ public class ApplicationManagerImpl implements ApplicationManager { "Application Id: " + appId); } try { + ApplicationRelease existingAppRelease = getAppReleaseIfExists(appId, applicationRelease.getUuid()); ConnectionManagerUtil.openDBConnection(); // todo consider about lifecycle - applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO() - .updateRelease(appId, applicationRelease, tenantId); + applicationRelease = this.applicationReleaseDAO.updateRelease(appId, applicationRelease, tenantId); return applicationRelease; } finally { @@ -656,7 +631,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public boolean isApplicationReleaseUpdateAcceptable(int appId, String appReleaseUuid) + public boolean isAcceptableAppReleaseUpdate(int appId, String appReleaseUuid) throws ApplicationManagementException { LifecycleState lifecycleState = getLifecycleState(appId, appReleaseUuid); return AppLifecycleState.CREATED.toString().equals(lifecycleState.getCurrentState()) || AppLifecycleState @@ -713,9 +688,8 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState; try { ConnectionManagerUtil.openDBConnection(); - LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); - lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState())); } catch (ApplicationManagementDAOException e) { throw new ApplicationManagementException("Failed to get lifecycle state", e); @@ -728,19 +702,20 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) throws - ApplicationManagementException { + public void addLifecycleState(int applicationId, String applicationUuid, LifecycleState state) + throws ApplicationManagementException { try { ConnectionManagerUtil.openDBConnection(); Application application = getApplicationIfAccessible(applicationId); ApplicationRelease applicationRelease = getAppReleaseIfExists(applicationId, applicationUuid); - LifecycleStateDAO lifecycleStateDAO; 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); - lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - lifecycleStateDAO.addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); + this.lifecycleStateDAO + .addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); } } catch (LifeCycleManagementDAOException | DBConnectionException e) { throw new ApplicationManagementException("Failed to add lifecycle state", e); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java index 653bc60f5e..e49c1bebff 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java @@ -325,7 +325,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { try { Application application = applicationManager.getApplicationIfAccessible(applicationId); - if (!applicationManager.isApplicationReleaseUpdateAcceptable(application.getId(), + if (!applicationManager.isAcceptableAppReleaseUpdate(application.getId(), applicationRelease.getUuid())) { String msg = "Application release is in the " + applicationRelease.getLifecycleState().getCurrentState() + " state. Hence updating is not acceptable when application in this state";