From 3e2f757c1e4ee2e9df7e8d7d62ae603ef5cb5e10 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sat, 15 Sep 2018 15:32:52 +0530 Subject: [PATCH] Improve lifecycle management --- .../mgt/common/LifecycleState.java | 49 +++++++------------ .../mgt/core/dao/LifecycleStateDAO.java | 2 +- .../GenericLifecycleStateDAOImpl.java | 9 ++-- .../mgt/core/impl/ApplicationManagerImpl.java | 28 +++++------ 4 files changed, 38 insertions(+), 50 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java index fdb1a27c51..2c0dc8349b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java @@ -19,28 +19,41 @@ package org.wso2.carbon.device.application.mgt.common; import java.sql.Timestamp; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import java.util.List; +@ApiModel(value = "LifecycleState", description = "LifecycleState represents the an Lifecycle state for an application release") public class LifecycleState { + @ApiModelProperty(name = "id", + value = "ID of the application release lifecycle", + required = true) private int id; + @ApiModelProperty(name = "currentState", + value = "Current state of the application release", + required = true) private String currentState; + @ApiModelProperty(name = "previousState", + value = "Previous state of the application release", + required = true) private String previousState; + @ApiModelProperty(name = "nextStates", + value = "Next possible transferring states from the current state") private List nextStates; + @ApiModelProperty(name = "updatedBy", + value = "Username who is update the application release state") private String updatedBy; + @ApiModelProperty(name = "updatedAt", + value = "Timestamp of the lifecycle has been updated") private Timestamp updatedAt; - private int tenantId; - - private int releaseId; - - private int appId; - public int getId() { return id; } @@ -81,30 +94,6 @@ public class LifecycleState { this.updatedAt = updatedAt; } - public int getTenantId() { - return tenantId; - } - - public void setTenantId(int tenantId) { - this.tenantId = tenantId; - } - - public int getReleaseId() { - return releaseId; - } - - public void setReleaseId(int releaseId) { - this.releaseId = releaseId; - } - - public int getAppId() { - return appId; - } - - public void setAppId(int appId) { - this.appId = appId; - } - public List getNextStates() { return nextStates; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java index d9d12356ec..6fec1ee2b0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java @@ -33,7 +33,7 @@ public interface LifecycleStateDAO { List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; - void addLifecycleState(LifecycleState state) throws LifeCycleManagementDAOException; + void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException; void deleteLifecycleState(int identifier) throws LifeCycleManagementDAOException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java index e2a0b0198a..c9286d0ffc 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/GenericLifecycleStateDAOImpl.java @@ -92,7 +92,6 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif lifecycleState.setId(rs.getInt("ID")); lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); lifecycleState.setPreviousState(rs.getString("PREVIOUSE_STATE")); - lifecycleState.setTenantId(rs.getInt("TENANT_ID")); lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); lifecycleStates.add(lifecycleState); @@ -109,7 +108,7 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } @Override - public void addLifecycleState(LifecycleState state) throws LifeCycleManagementDAOException { + public void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException { Connection conn = null; PreparedStatement stmt = null; try { @@ -119,10 +118,10 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif stmt = conn.prepareStatement(sql); stmt.setString(1, state.getCurrentState()); stmt.setString(2, state.getPreviousState()); - stmt.setInt(3, state.getTenantId()); + stmt.setInt(3, tenantId); stmt.setString(4, state.getUpdatedBy()); - stmt.setInt(5, state.getReleaseId()); - stmt.setInt(6, state.getAppId()); + stmt.setInt(5, releaseId); + stmt.setInt(6, appId); stmt.executeUpdate(); } catch (DBConnectionException e) { 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 2dfad505f2..6ffa369960 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 @@ -126,21 +126,23 @@ public class ApplicationManagerImpl implements ApplicationManager { "Invalid payload. Application creating payload should contains one application release, but " + "the payload contains more than one"); } - ConnectionManagerUtil.commitDBTransaction(); applicationRelease = application.getApplicationReleases().get(0); applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO(). createRelease(applicationRelease, application.getId(), tenantId); + LifecycleState lifecycleState = new LifecycleState(); - lifecycleState.setAppId(application.getId()); - lifecycleState.setReleaseId(applicationRelease.getId()); lifecycleState.setUpdatedBy(userName); - lifecycleState.setTenantId(tenantId); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); addLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState); + LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); + lifecycleStateDAO.addLifecycleState(lifecycleState, application.getId(), applicationRelease.getId(), tenantId); + applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); application.setApplicationReleases(applicationReleases); + + ConnectionManagerUtil.commitDBTransaction(); } return application; @@ -155,7 +157,12 @@ public class ApplicationManagerImpl implements ApplicationManager { log.error(msg, e); ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException(msg, e); - } finally { + } catch (LifeCycleManagementDAOException e) { + String msg = "Error occurred while adding application lifecycle state"; + log.error(msg, e); + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationManagementException(msg, e); + }finally { ConnectionManagerUtil.closeDBConnection(); } } @@ -399,7 +406,6 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - newAppLifecycleState.setTenantId(tenantId); newAppLifecycleState.setUpdatedBy(userName); addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); storedLocations.add(applicationRelease.getAppHashValue()); @@ -437,7 +443,6 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - newAppLifecycleState.setTenantId(tenantId); newAppLifecycleState.setUpdatedBy(userName); addLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState); }else{ @@ -710,17 +715,12 @@ public class ApplicationManagerImpl implements ApplicationManager { Application application = validateApplication(applicationId); ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, applicationUuid); LifecycleStateDAO lifecycleStateDAO; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - if (application != null) { - state.setAppId(applicationId); - } - if (applicationRelease != null) { - state.setReleaseId(applicationRelease.getId()); - } if (state.getCurrentState() != null && state.getPreviousState() != null && state.getUpdatedBy() != null) { validateLifecycleState(state); lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); - lifecycleStateDAO.addLifecycleState(state); + lifecycleStateDAO.addLifecycleState(state, application.getId(), applicationRelease.getId(), tenantId); } } catch (LifeCycleManagementDAOException | DBConnectionException e) { throw new ApplicationManagementException("Failed to add lifecycle state", e);