From 409f6fce149fb3657455ca2b6285cceb857a9578 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 27 Sep 2018 18:47:54 +0530 Subject: [PATCH] Fix get lifecycle issue --- .../common/services/ApplicationManager.java | 14 +------ .../mgt/core/dao/LifecycleStateDAO.java | 2 + .../GenericLifecycleStateDAOImpl.java | 42 ++++++++++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 39 ++--------------- .../core/lifecycle/LifecycleStateManger.java | 6 ++- .../LifecycleManagementTest.java | 6 +-- 6 files changed, 56 insertions(+), 53 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 a4260704d4..83c4e67495 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 @@ -122,25 +122,15 @@ public interface ApplicationManager { */ Boolean isUserAllowable(List unrestrictedRoles, String userName) throws ApplicationManagementException; -// todo -// /** -// * To get all the releases of a particular Application. -// * -// * @param applicationId ID of the Application to get all the releases. -// * @return the List of the Application releases related with the particular Application. -// * @throws ApplicationManagementException Application Management Exception. -// */ -// List getinstallableReleases(int applicationId) throws ApplicationManagementException; - /** * To get all the releases of a particular Application. * * @param applicationId ID of the Application . - * @param applicationUuid UUID of the Application Release. + * @param releaseUuid UUID of the Application Release. * @return the LifecycleState of the Application releases related with the particular Application. * @throws ApplicationManagementException Application Management Exception. */ - LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws ApplicationManagementException; + LifecycleState getLifecycleState(int applicationId, String releaseUuid) throws ApplicationManagementException; /** * To get all the releases of a particular Application. 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 6fec1ee2b0..5ab07f9307 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 @@ -31,6 +31,8 @@ public interface LifecycleStateDAO { LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException; + LifecycleState getLatestLifeCycleState(int appId, String uuid) throws ApplicationManagementDAOException; + List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) 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 3103f2f814..a122d3159d 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 @@ -74,6 +74,44 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } } + public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws ApplicationManagementDAOException{ + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = "SELECT ID, CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_AT, UPDATED_BY FROM " + + "AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID=? AND AP_APP_RELEASE_ID=(SELECT ID FROM AP_APP_RELEASE " + + "WHERE UUID=?) ORDER BY UPDATED_AT DESC;"; + + stmt = conn.prepareStatement(sql); + 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; + + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while getting application List", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection to get latest" + + " lifecycle state for a specific application", e); + } finally { + Util.cleanupResources(stmt, rs); + } + + } + + @Override public List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException { List lifecycleStates = new ArrayList<>(); @@ -116,8 +154,8 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif String sql = "INSERT INTO AP_APP_LIFECYCLE_STATE (CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_BY, " + "AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?,?, ?, ?,?,?);"; stmt = conn.prepareStatement(sql); - stmt.setString(1, state.getCurrentState()); - stmt.setString(2, state.getPreviousState()); + stmt.setString(1, state.getCurrentState().toUpperCase()); + stmt.setString(2, state.getPreviousState().toUpperCase()); stmt.setInt(3, tenantId); stmt.setString(4, state.getUpdatedBy()); stmt.setInt(5, releaseId); 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 2ca8c4b250..9a65522076 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 @@ -67,7 +67,6 @@ import java.util.List; public class ApplicationManagerImpl implements ApplicationManager { private static final Log log = LogFactory.getLog(ApplicationManagerImpl.class); - private DeviceTypeDAO deviceTypeDAO; private VisibilityDAO visibilityDAO; private ApplicationDAO applicationDAO; private ApplicationReleaseDAO applicationReleaseDAO; @@ -81,7 +80,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } private void initDataAccessObjects() { - this.deviceTypeDAO = ApplicationManagementDAOFactory.getDeviceTypeDAO(); this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO(); this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO(); @@ -422,35 +420,6 @@ public class ApplicationManagerImpl implements ApplicationManager { } } -// todo -// public List getinstallableReleases(int applicationId) throws ApplicationManagementException { -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); -// -// Application application = getApplicationIfAccessible(applicationId); -// List applicationReleases; -// List filteredApplicationReleases = new ArrayList<>(); -// if (log.isDebugEnabled()) { -// log.debug("Request is received to retrieve all the releases related with the application " + application -// .toString()); -// } -// 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() -// .equals(applicationRelease.getLifecycleState().getCurrentState())) { -// filteredApplicationReleases.add(applicationRelease); -// } -// } -// } -// return filteredApplicationReleases; -// -// } - private List getReleases(Application application, boolean requirePublishedRelease) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -785,19 +754,19 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws + public LifecycleState getLifecycleState(int applicationId, String releaseUuid) throws ApplicationManagementException { LifecycleState lifecycleState; try { ConnectionManagerUtil.openDBConnection(); - lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationId); + lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); if (lifecycleState == null) { throw new NotFoundException( "Couldn't find the lifecycle data for appid: " + applicationId + " and app release UUID: " - + applicationUuid); + + releaseUuid); } - lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManger. + lifecycleState.setNextStates(new ArrayList<>(getLifecycleManagementService(). getNextLifecycleStates(lifecycleState.getCurrentState()))); } catch (ApplicationManagementDAOException e) { throw new ApplicationManagementException("Failed to get lifecycle state", e); 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 ea36735100..bef2750694 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 @@ -18,7 +18,11 @@ public class LifecycleStateManger { public LifecycleStateManger(List states) { lifecycleStates = new HashMap<>(); for (LifecycleState s : states) { - lifecycleStates.put(s.getName(), new State(s.getName(), s.getProceedingStates())); + if (s.getProceedingStates() != null) { + s.getProceedingStates().replaceAll(String::toUpperCase); + } + lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(), s.getProceedingStates())); + } } 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 4b644debac..b616b0bf8e 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() {