From 409f6fce149fb3657455ca2b6285cceb857a9578 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 27 Sep 2018 18:47:54 +0530 Subject: [PATCH 1/3] 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 a4260704d47..83c4e674955 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 6fec1ee2b03..5ab07f93078 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 3103f2f8144..a122d3159dd 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 2ca8c4b2501..9a655220764 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 ea367351002..bef27506943 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 4b644debac6..b616b0bf8e5 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() { From b283c1dfde2509527e80552873a9040e1258a390 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 28 Sep 2018 04:10:07 +0530 Subject: [PATCH 2/3] 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 From 2c9ea2b5cf05e625f6caa2da503be675e0d5086b Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 28 Sep 2018 17:57:04 +0530 Subject: [PATCH 3/3] Fix lifecycle adding issue --- .../common/services/ApplicationManager.java | 9 +- .../mgt/core/dao/ApplicationDAO.java | 3 +- .../mgt/core/dao/ApplicationReleaseDAO.java | 14 +- .../mgt/core/dao/LifecycleStateDAO.java | 4 +- .../GenericApplicationDAOImpl.java | 8 +- .../GenericApplicationReleaseDAOImpl.java | 137 +++++++++++------- .../GenericLifecycleStateDAOImpl.java | 9 +- .../mgt/core/impl/ApplicationManagerImpl.java | 82 ++++++----- .../services/ApplicationManagementAPI.java | 4 + .../impl/ApplicationManagementAPIImpl.java | 14 +- 10 files changed, 168 insertions(+), 116 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 83c4e674955..cc5a50681c7 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 @@ -112,7 +112,7 @@ public interface ApplicationManager { * @return the boolean value, whether application exist or not * @throws ApplicationManagementException Application Management Exception. */ - Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException; + boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException; /** * To get Application with the given UUID. @@ -136,14 +136,13 @@ public interface ApplicationManager { * 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. * @param state Lifecycle state to change the app * @param checkExist whether it is needed to check if the app and release already exist in the database - * @param releaseId The release ID of the application(optional) * @throws ApplicationManagementException Application Management Exception. */ - void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean checkExist, - int releaseId) throws ApplicationManagementException; + void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist) + throws ApplicationManagementException; /** * Get the application if application is an accessible one. diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 2556238e616..772dea5e8de 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -109,10 +109,11 @@ public interface ApplicationDAO { * To get the application with the given uuid * * @param appId ID of the application + * @param tenantId Tenant Id * @return the boolean value * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException; + boolean verifyApplicationExistenceById(int appId, int tenantId) throws ApplicationManagementDAOException; /** * To get the application id of the application specified by the UUID diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index a62053a377f..d08af3c8c48 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -115,6 +115,16 @@ public interface ApplicationReleaseDAO { * @throws ApplicationManagementDAOException Application Management DAO Exception. */ ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws - ApplicationManagementDAOException; + ApplicationManagementDAOException; + + /** + * To verify whether application release exist or not. + * + * @param appId ID of the application. + * @param uuid UUID of the application release. + * @param tenantId Tenant Id + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException; -} + } 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 f1b857d0c33..40cc7a79ec4 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 @@ -58,14 +58,14 @@ public interface LifecycleStateDAO { /** * To add new lifecycle states for the given application release. - * @param releaseId Id of the application release. + * @param uuid 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) + void addLifecycleState(LifecycleState state, int appId, String uuid, 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/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 5847eda9b81..f2d417ee44b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -433,7 +433,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException { + public boolean verifyApplicationExistenceById(int appId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting application with the application ID(" + appId + " ) from the database"); } @@ -443,13 +443,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic try { conn = this.getDBConnection(); String sql = - "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " - + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP_TAG.TAG AS TAG, " - + "AP_UNRESTRICTED_ROLE.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE " - + "WHERE AP_APP.ID = ?;"; + "SELECT AP_APP.ID AS APP_ID FROM AP_APP WHERE AP_APP.ID = ? AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); stmt.setInt(1, appId); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (log.isDebugEnabled()) { 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/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index 4445bcf717c..68d934d9a8f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -43,29 +43,26 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements private static final Log log = LogFactory.getLog(GenericApplicationReleaseDAOImpl.class); - /** * To insert the Application Release Details. * - * @param appId Id of the application + * @param appId Id of the application * @param applicationRelease Application Release the properties of which that need to be inserted. - * @param tenantId Tenant Id + * @param tenantId Tenant Id * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public ApplicationRelease createRelease(ApplicationRelease applicationRelease, int appId, int tenantId) throws - ApplicationManagementDAOException { + @Override public ApplicationRelease createRelease(ApplicationRelease applicationRelease, int appId, int tenantId) + throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE," + "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE," - + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) " - + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; + + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; int index = 0; - String generatedColumns[] = {"ID"}; + String generatedColumns[] = { "ID" }; try { connection = this.getDBConnection(); statement = connection.prepareStatement(sql, generatedColumns); @@ -95,7 +92,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements "SQL Exception while trying to release an application by executing the query " + sql, e); } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( - "Database Connection Exception while trying to release a new version" , e); + "Database Connection Exception while trying to release a new version", e); } finally { Util.cleanupResources(statement, resultSet); } @@ -106,13 +103,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * * @param applicationName Name of the application. * @param applicationType Type of the application. - * @param versionName version name of the application. - * @param releaseType type of the application release. - * @param tenantId Tenant Id + * @param versionName version name of the application. + * @param releaseType type of the application release. + * @param tenantId Tenant Id * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public ApplicationRelease getRelease(String applicationName, String applicationType, String versionName, + @Override public ApplicationRelease getRelease(String applicationName, String applicationType, String versionName, String releaseType, int tenantId) throws ApplicationManagementDAOException { Connection connection; @@ -144,11 +140,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements return null; } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Database connection exception while trying to get the " - + "release details of the application with " + applicationName + " and version " + - versionName, e); + + "release details of the application with " + applicationName + " and version " + versionName, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error while getting release details of the application " + - applicationName + " and version " + versionName + " , while executing the query " + sql, e); + throw new ApplicationManagementDAOException( + "Error while getting release details of the application " + applicationName + " and version " + versionName + " , while executing the query " + sql, e); } finally { Util.cleanupResources(statement, resultSet); } @@ -158,25 +153,25 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * To get release details of a specific application. * * @param applicationId ID of the application. - * @param releaseUuid UUID of the application release. - * @param tenantId Tenant Id + * @param releaseUuid UUID of the application release. + * @param tenantId Tenant Id * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws - ApplicationManagementDAOException { + @Override public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) + throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; - String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE," - + " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " - + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " + - "HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, AR" + - ".PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AL.CURRENT_STATE, AL.PREVIOUS_STATE, AL.UPDATED_BY, " + - "AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE " + - "AR.AP_APP_ID = ? AND AR.UUID = ? AND AR.TENANT_ID = ? AND AL.AP_APP_RELEASE_ID=AR.ID AND " + - "AL.TENANT_ID = AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;"; + String sql = + "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE, " + + "AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, " + + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.PACKAGE_NAME AS " + + "PACKAGE_NAME, AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, " + + "AR.APP_META_INFO AS APP_META_INFO, AR.RATING AS RATING, AL.CURRENT_STATE, AL.PREVIOUS_STATE, " + + "AL.UPDATED_BY, AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL " + + "WHERE AR.AP_APP_ID = ? AND AR.UUID = ? AND AR.TENANT_ID = ? AND AL.AP_APP_RELEASE_ID=AR.ID " + + "AND AL.TENANT_ID = AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;"; try { connection = this.getDBConnection(); @@ -192,14 +187,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements return null; } catch (DBConnectionException e) { throw new ApplicationManagementDAOException( - "Database connection exception while trying to get the release details of the " + - "application id: " + applicationId + "and UUID of the application release: " + - releaseUuid, e); + "Database connection exception while trying to get the release details of the " + "application id: " + + applicationId + "and UUID of the application release: " + releaseUuid, e); } catch (SQLException e) { throw new ApplicationManagementDAOException( - "Error while getting release details of the application id: " + applicationId + - " and theUUID of the application " + - "release: " + releaseUuid + " , while executing the query " + sql, e); + "Error while getting release details of the application id: " + applicationId + + " and theUUID of the application " + "release: " + releaseUuid + " , while executing the query " + sql, e); } finally { Util.cleanupResources(statement, resultSet); } @@ -209,11 +202,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * To insert the application release properties. * * @param applicationId Id of the application. - * @param tenantId Tenant Id + * @param tenantId Tenant Id * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public List getReleases(int applicationId, int tenantId) + @Override public List getReleases(int applicationId, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; @@ -254,12 +246,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements /** * To Update starts of an application release. * - * @param uuid UUID of the application Release. + * @param uuid UUID of the application Release. * @param rating given stars for the application release. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public void updateRatingValue(String uuid, double rating, int ratedUsers) throws ApplicationManagementDAOException { + @Override public void updateRatingValue(String uuid, double rating, int ratedUsers) + throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; String sql = "UPDATE AP_APP_RELEASE SET RATING = ?,RATED_USERS = ? WHERE UUID = ?;"; @@ -287,8 +279,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * @param uuid UUID of the application Release. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException { + @Override public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; @@ -301,7 +292,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setInt(2, tenantId); resultSet = statement.executeQuery(); - if (resultSet.next()){ + if (resultSet.next()) { rating = new Rating(); rating.setRatingValue(resultSet.getDouble("RATING")); rating.setNoOfUsers(resultSet.getInt("RATED_USERS")); @@ -324,8 +315,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements * @param applicationRelease Application Release the properties of which that need to be inserted. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public ApplicationRelease updateRelease(int applicationId, ApplicationRelease applicationRelease, int tenantId) + @Override public ApplicationRelease updateRelease(int applicationId, ApplicationRelease applicationRelease, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; @@ -349,7 +339,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement.setString(11, applicationRelease.getAppHashValue()); statement.setInt(12, applicationRelease.getIsSharedWithAllTenants()); statement.setString(13, applicationRelease.getMetaData()); - statement.setInt(14,applicationId); + statement.setInt(14, applicationId); statement.setInt(15, tenantId); statement.setInt(16, applicationRelease.getId()); @@ -369,12 +359,11 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements /** * To delete an application release. * - * @param id Id of the application Release. + * @param id Id of the application Release. * @param version version name of the application release. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - @Override - public void deleteRelease(int id, String version) throws ApplicationManagementDAOException { + @Override public void deleteRelease(int id, String version) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; String sql = "DELETE FROM AP_APP_RELEASE WHERE ID = ? AND VERSION = ?"; @@ -396,8 +385,46 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } + @Override + public boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Verifying application release existence by application id:" + appId + + " and application release uuid: " + uuid); + } + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = + "SELECT AR.ID AS RELEASE_ID FROM AP_APP_RELEASE AS AR WHERE AR.AP_APP_ID = ? AND AR.UUID = ? AND " + + "AR.TENANT_ID = ?;"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, appId); + stmt.setString(2, uuid); + stmt.setInt(3, tenantId); + rs = stmt.executeQuery(); + + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved basic details of the application release with the application ID " + + appId + " Application release uuid: " + uuid); + } + return rs.next(); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error occurred while getting application release details with app ID: " + appId + + " App release uuid: " + uuid + " While executing query ", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + /** * This method is capable to construct {@link ApplicationRelease} and return the object + * * @param resultSet result set obtained from the query executing. * @throws SQLException SQL exception while accessing result set data. */ @@ -418,8 +445,6 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED")); applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); applicationRelease.setRating(resultSet.getDouble("RATING")); - return applicationRelease; } - -} +} \ No newline at end of file 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 29cf7c80841..515379ede3c 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 @@ -123,19 +123,19 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } @Override - public void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException { + public void addLifecycleState(LifecycleState state, int appId, String uuid, int tenantId) throws LifeCycleManagementDAOException { Connection conn = null; PreparedStatement stmt = null; try { conn = this.getDBConnection(); String sql = "INSERT INTO AP_APP_LIFECYCLE_STATE (CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_BY, " - + "AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?,?, ?, ?,?,?);"; + + "AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?,?, ?, ?,(SELECT ID FROM AP_APP_RELEASE WHERE UUID=?),?);"; stmt = conn.prepareStatement(sql); 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); + stmt.setString(5, uuid); stmt.setInt(6, appId); stmt.executeUpdate(); @@ -172,7 +172,7 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { LifecycleState lifecycleState = null; try { - if (rs.next()) { + if (rs !=null && rs.next()) { lifecycleState = new LifecycleState(); lifecycleState.setId(rs.getInt("ID")); lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); @@ -185,6 +185,5 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif "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 a1e887f068f..1013f98fa66 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 @@ -50,7 +50,6 @@ import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; @@ -95,7 +94,6 @@ public class ApplicationManagerImpl implements ApplicationManager { log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName); } - ConnectionManagerUtil.openDBConnection(); validateAppCreatingRequest(application); validateAppReleasePayload(application.getApplicationReleases().get(0)); @@ -162,8 +160,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false, - applicationRelease.getId()); + changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false); applicationRelease.setLifecycleState(lifecycleState); applicationReleases.add(applicationRelease); @@ -247,7 +244,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); - changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true, 0); + changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -281,8 +278,7 @@ public class ApplicationManagerImpl implements ApplicationManager { List applicationReleases; try { ConnectionManagerUtil.openDBConnection(); - application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplicationById(id, tenantId); + application = this.applicationDAO.getApplicationById(id, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationReleases = getReleases(application, requirePublishedReleases); application.setApplicationReleases(applicationReleases); @@ -345,8 +341,7 @@ public class ApplicationManagerImpl implements ApplicationManager { List applicationReleases; try { ConnectionManagerUtil.openDBConnection(); - application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplication(appName, appType, tenantId); + application = this.applicationDAO.getApplication(appName, appType, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationReleases = getReleases(application, false); application.setApplicationReleases(applicationReleases); @@ -383,8 +378,7 @@ public class ApplicationManagerImpl implements ApplicationManager { Application application; try { ConnectionManagerUtil.openDBConnection(); - application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplicationByRelease(appReleaseUUID, tenantId); + application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId); if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(), userName)) { @@ -399,11 +393,12 @@ public class ApplicationManagerImpl implements ApplicationManager { } } - public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException { + public boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException { try { - Boolean isAppExist; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + boolean isAppExist; ConnectionManagerUtil.openDBConnection(); - isAppExist = ApplicationManagementDAOFactory.getApplicationDAO().verifyApplicationExistenceById(appId); + isAppExist = this.applicationDAO.verifyApplicationExistenceById(appId, tenantId); return isAppExist; } finally { ConnectionManagerUtil.closeDBConnection(); @@ -496,11 +491,11 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true); storedLocations.add(applicationRelease.getAppHashValue()); } ConnectionManagerUtil.openDBConnection(); - ApplicationManagementDAOFactory.getApplicationDAO().deleteApplication(applicationId); + this.applicationDAO.deleteApplication(applicationId); } catch (UserStoreException e) { String msg = "Error occured while check whether current user has the permission to delete an application"; log.error(msg); @@ -527,7 +522,7 @@ public class ApplicationManagerImpl implements ApplicationManager { LifecycleState newAppLifecycleState = new LifecycleState(); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); - changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0); + changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true); }else{ throw new ApplicationManagementException("Can't delete the application release, You have to move the " + "lifecycle state from "+ currentState + " to acceptable " + @@ -634,8 +629,7 @@ public class ApplicationManagerImpl implements ApplicationManager { Application application; boolean isAppAllowed = false; try { - application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplicationById(applicationId, tenantId); + application = this.applicationDAO.getApplicationById(applicationId, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { return application; } @@ -784,34 +778,48 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean - checkExist, int releaseId) throws ApplicationManagementException { + public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist) + throws ApplicationManagementException { try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (checkExist) { - getApplicationIfAccessible(applicationId); - } - if (releaseId < 1) { - releaseId = getAppReleaseIfExists(applicationId, applicationUuid).getId(); + ConnectionManagerUtil.openDBConnection(); + if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)){ + throw new NotFoundException( + "Couldn't found application for the application Id: " + applicationId); + } + if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)){ + throw new NotFoundException( + "Couldn't found application release for the application Id: " + applicationId + + " application release uuid: " + releaseUuid); + } } - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); state.setUpdatedBy(userName); if (state.getCurrentState() != null && state.getPreviousState() != null) { - - if (getLifecycleManagementService().isValidStateChange(state.getPreviousState(), state.getCurrentState())) { - this.lifecycleStateDAO - .addLifecycleState(state, applicationId, releaseId, tenantId); + if (getLifecycleManagementService() + .isValidStateChange(state.getPreviousState(), state.getCurrentState())) { + ConnectionManagerUtil.beginDBTransaction(); + //todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there + //todo any other application release in PUBLISHED state for the application( i.e for the appid) + this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId); + ConnectionManagerUtil.commitDBTransaction(); } else { - log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" - + " to '" + state.getCurrentState() + "'"); - throw new ApplicationManagementException("Lifecycle State Validation failed"); + log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '" + + state.getCurrentState() + "'"); + throw new ApplicationManagementException("Lifecycle State Validation failed. Application Id: " + + applicationId + " Application release UUID: " + releaseUuid); } } - } catch (LifeCycleManagementDAOException | DBConnectionException e) { - throw new ApplicationManagementException("Failed to add lifecycle state", e); - } catch (ApplicationManagementException e) { - throw new ApplicationManagementException("Lifecycle State Validation failed", e); + } catch (LifeCycleManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationManagementException( + "Failed to add lifecycle state. Application Id: " + applicationId + " Application release UUID: " + + releaseUuid, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } } 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java index 1151dc3d6ec..57b35871e15 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java @@ -529,6 +529,10 @@ public interface ApplicationManagementAPI { code = 201, message = "OK. \n Successfully add a lifecycle state.", response = Application.class), + @ApiResponse( + code = 404, + message = "NOT FOUND. \n Error occurred while adding new lifecycle state.", + response = ErrorResponse.class), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred adding a lifecycle state.", 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 2b1ee720cb3..f3b93e84c38 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 @@ -414,7 +414,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @DELETE @Path("/{appid}") - public Response deleteApplication(@PathParam("appid") int applicationId) { + public Response deleteApplication( + @PathParam("appid") int applicationId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { @@ -435,7 +436,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @DELETE @Path("/{appid}/{uuid}") - public Response deleteApplicationRelease(@PathParam("appid") int applicationId, @PathParam("uuid") String releaseUuid) { + public Response deleteApplicationRelease( + @PathParam("appid") int applicationId, + @PathParam("uuid") String releaseUuid) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); try { @@ -485,7 +488,12 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { LifecycleState state) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true, 0); + applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true); + } catch (NotFoundException e) { + String msg = "Could,t find application release for application id: " + applicationId + + " and application release uuid: " + applicationUuid; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while adding lifecycle state."; log.error(msg, e);