From b283c1dfde2509527e80552873a9040e1258a390 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 28 Sep 2018 04:10:07 +0530 Subject: [PATCH] Improve lifecycle management --- .../mgt/core/dao/LifecycleStateDAO.java | 46 +++++++++++++-- .../GenericLifecycleStateDAOImpl.java | 58 +++++++++---------- .../mgt/core/impl/ApplicationManagerImpl.java | 17 ++++-- .../core/lifecycle/LifecycleStateManger.java | 2 +- .../LifecycleManagementTest.java | 10 ++-- 5 files changed, 85 insertions(+), 48 deletions(-) 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 5ab07f93078..f1b857d0c33 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 @@ -19,7 +19,6 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.LifecycleState; -import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import java.util.List; @@ -29,14 +28,51 @@ import java.util.List; */ public interface LifecycleStateDAO { - LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException; + /** + * To get the latest lifecycle state for the given application release id. + * @param applicationReleaseId id of the application release. + * + * @return Latest Lifecycle State for the given application release + * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. + */ + LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException; - LifecycleState getLatestLifeCycleState(int appId, String uuid) throws ApplicationManagementDAOException; + /** + * To get the latest lifecycle state for the given application id and the application release UUID. + * @param appId id of the application. + * @param uuid UUID of the application release + * + * @return Latest Lifecycle State for the given application release + * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. + */ + LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException; + /** + * To get all changed lifecycle states for the given application release id. + * @param appReleaseId id of the application release. + * + * @return Lifecycle States for the given application release + * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. + */ List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; - void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException; + /** + * To add new lifecycle states for the given application release. + * @param releaseId Id of the application release. + * @param appId Id of the application. + * @param state LifecycleState. + * @param tenantId Tenant id + * + * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. + */ + void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) + throws LifeCycleManagementDAOException; + /** + * To delete a specific lifecycle state for application release. + * @param identifier Id of the LifecycleState. + * + * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. + */ 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 a122d3159dd..29cf7c80841 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 @@ -23,7 +23,6 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; -import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import java.sql.Connection; @@ -39,7 +38,7 @@ import java.util.List; public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO { @Override - public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException { + public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException { Connection conn = null; PreparedStatement stmt = null; @@ -52,29 +51,18 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif stmt = conn.prepareStatement(sql); stmt.setInt(1, applicationReleaseId); rs = stmt.executeQuery(); - LifecycleState lifecycleState = null; - - if (rs.next()) { - lifecycleState = new LifecycleState(); - lifecycleState.setId(rs.getInt("ID")); - lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); - lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); - lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); - lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); - } - return lifecycleState; - + return constructLifecycle(rs); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting application List", e); + throw new LifeCycleManagementDAOException("Error occurred while getting application List", e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection to get latest" + throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest" + " lifecycle state for a specific application", e); } finally { Util.cleanupResources(stmt, rs); } } - public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws ApplicationManagementDAOException{ + public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException{ Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; @@ -88,22 +76,11 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif stmt.setInt(1, appId); stmt.setString(2, uuid); rs = stmt.executeQuery(); - LifecycleState lifecycleState = null; - - if (rs.next()) { - lifecycleState = new LifecycleState(); - lifecycleState.setId(rs.getInt("ID")); - lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); - lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); - lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); - lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); - } - return lifecycleState; - + return constructLifecycle(rs); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting application List", e); + throw new LifeCycleManagementDAOException("Error occurred while getting application List", e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection to get latest" + throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest" + " lifecycle state for a specific application", e); } finally { Util.cleanupResources(stmt, rs); @@ -191,4 +168,23 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif Util.cleanupResources(stmt, rs); } } + + private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { + LifecycleState lifecycleState = null; + try { + if (rs.next()) { + lifecycleState = new LifecycleState(); + lifecycleState.setId(rs.getInt("ID")); + lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); + lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); + lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); + lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); + } + } catch (SQLException e) { + throw new LifeCycleManagementDAOException( + "Error occurred while construct lifecycle state by retrieving data from SQL query", e); + } + return lifecycleState; + + } } 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 9a655220764..a1e887f068f 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 @@ -432,8 +432,15 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.getDBConnection(); applicationReleases = this.applicationReleaseDAO.getReleases(application.getId(), tenantId); for (ApplicationRelease applicationRelease : applicationReleases) { - LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). - getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + LifecycleState lifecycleState = null; + try { + lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO(). + getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); + } catch (LifeCycleManagementDAOException e) { + throw new ApplicationManagementException( + "Error occurred while getting the latest lifecycle state for the application release UUID: " + + applicationRelease.getUuid(), e); + } if (lifecycleState != null) { applicationRelease.setLifecycleState(lifecycleState); @@ -768,10 +775,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } lifecycleState.setNextStates(new ArrayList<>(getLifecycleManagementService(). getNextLifecycleStates(lifecycleState.getCurrentState()))); - } catch (ApplicationManagementDAOException e) { - throw new ApplicationManagementException("Failed to get lifecycle state", e); - } catch (ApplicationManagementException e) { - throw new ApplicationManagementException("Failed to get application and application management", e); + } catch (LifeCycleManagementDAOException e) { + throw new ApplicationManagementException("Failed to get lifecycle state from database", e); } finally { ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java index bef27506943..b32ab3ab8e0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManger.java @@ -27,7 +27,7 @@ public class LifecycleStateManger { } public Set getNextLifecycleStates(String currentLifecycleState) { - return lifecycleStates.get(currentLifecycleState).getProceedingStates(); + return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates(); } public boolean isValidStateChange(String currentState, String nextState) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java index b616b0bf8e5..919a92d7c52 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/LifecycleManagementTest.java @@ -16,9 +16,9 @@ public class LifecycleManagementTest { private List lifecycleStates; private LifecycleStateManger lifecycleStateManger; - private final String CURRENT_STATE = "APPROVED"; - private final String NEXT_STATE = "PUBLISHED"; - private final String BOGUS_STATE = "REMOVED"; + private final String CURRENT_STATE = "Approved"; + private final String NEXT_STATE = "Published"; + private final String BOGUS_STATE = "Removed"; @BeforeClass public void init() { @@ -32,14 +32,14 @@ public class LifecycleManagementTest { public void checkValidNextLifecycleState() { Set proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE, - proceedingStates.contains(NEXT_STATE)); + proceedingStates.contains(NEXT_STATE.toUpperCase())); } @Test public void checkInvalidNextLifecycleState() { Set proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE); Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE, - proceedingStates.contains(BOGUS_STATE)); + proceedingStates.contains(BOGUS_STATE.toUpperCase())); } @Test