From 2c9ea2b5cf05e625f6caa2da503be675e0d5086b Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 28 Sep 2018 17:57:04 +0530 Subject: [PATCH] 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 83c4e67495..cc5a50681c 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 2556238e61..772dea5e8d 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 a62053a377..d08af3c8c4 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 f1b857d0c3..40cc7a79ec 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 5847eda9b8..f2d417ee44 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 4445bcf717..68d934d9a8 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 29cf7c8084..515379ede3 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 a1e887f068..1013f98fa6 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 1151dc3d6e..57b35871e1 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 2b1ee720cb..f3b93e84c3 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);