From a3d75df8a0a11914bbd82731a0d6303ddf032f3a Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Mon, 6 May 2019 18:01:52 +0530 Subject: [PATCH] Add lifecycle adding API --- ...cycleStateDTO.java => LifecycleState.java} | 19 +--- .../common/services/ApplicationManager.java | 17 ++- .../mgt/core/dao/ApplicationReleaseDAO.java | 6 +- .../mgt/core/dao/LifecycleStateDAO.java | 17 ++- .../GenericApplicationReleaseDAOImpl.java | 34 ++++++ .../GenericLifecycleStateDAOImpl.java | 67 +++++------ .../mgt/core/impl/ApplicationManagerImpl.java | 104 +++++++++++------- .../ApplicationManagementPublisherAPI.java | 19 ++-- ...ApplicationManagementPublisherAPIImpl.java | 47 ++++---- ...cationManagementPublisherAdminAPIImpl.java | 31 ------ 10 files changed, 189 insertions(+), 172 deletions(-) rename components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/{dto/LifecycleStateDTO.java => LifecycleState.java} (83%) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LifecycleStateDTO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java similarity index 83% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LifecycleStateDTO.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java index 9130e8a9fb..f499ba948e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/dto/LifecycleStateDTO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.application.mgt.common.dto; +package org.wso2.carbon.device.application.mgt.common; import java.sql.Timestamp; import io.swagger.annotations.ApiModel; @@ -24,13 +24,8 @@ import io.swagger.annotations.ApiModelProperty; import java.util.List; -@ApiModel(value = "LifecycleStateDTO", description = "LifecycleStateDTO represents the Lifecycle state for an application release") -public class LifecycleStateDTO { - - @ApiModelProperty(name = "id", - value = "ID of the application release lifecycle", - required = true) - private int id; +@ApiModel(value = "LifecycleState", description = "LifecycleState represents the Lifecycle state for an application release") +public class LifecycleState { @ApiModelProperty(name = "currentState", value = "Current state of the application release", @@ -54,14 +49,6 @@ public class LifecycleStateDTO { value = "Timestamp of the lifecycle has been updated") private Timestamp updatedAt; - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - public String getCurrentState() { return currentState; } 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 7b82727d70..756b4505e7 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 @@ -24,7 +24,7 @@ import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.response.Application; @@ -133,24 +133,23 @@ public interface ApplicationManager { ApplicationDTO getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException; /** - * To get all the releases of a particular ApplicationDTO. + * To get lifecycle state change flow of a particular Application Release. * - * @param applicationId ID of the ApplicationDTO . - * @param releaseUuid UUID of the ApplicationDTO Release. - * @return the LifecycleStateDTO of the ApplicationDTO releases related with the particular ApplicationDTO. - * @throws ApplicationManagementException ApplicationDTO Management Exception. + * @param releaseUuid UUID of the Application Release. + * @return the List of LifecycleStates which represent the lifecycle change flow of the application releases. + * @throws ApplicationManagementException Application Management Exception. */ - LifecycleStateDTO getLifecycleState(int applicationId, String releaseUuid) throws ApplicationManagementException; + List getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException; /** * To get all the releases of a particular ApplicationDTO. * * @param applicationId ID of the ApplicationDTO. * @param releaseUuid UUID of the ApplicationDTO Release. - * @param state Lifecycle state to change the app + * @param stateName Lifecycle state to change the app * @throws ApplicationManagementException ApplicationDTO Management Exception. */ - void changeLifecycleState(int applicationId, String releaseUuid, LifecycleStateDTO state) + void changeLifecycleState(String releaseUuid, String stateName) throws ApplicationManagementException; /** 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 93ec3d4908..176f37dcae 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 @@ -178,6 +178,10 @@ public interface ApplicationReleaseDAO { * @return True if application release package name already exist in the IoT server, Otherwise returns False. * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId, String inactiveState) throws ApplicationManagementDAOException; + boolean isActiveReleaseExisitForPackageName(String packageName, int tenantId, String inactiveState) + throws ApplicationManagementDAOException; + + boolean hasExisitInstallableAppRelease(String releaseUuid, String installableStateName, 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 fe1b3eccc4..a41c779ff0 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 @@ -18,7 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; -import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException; import java.util.List; @@ -35,7 +35,7 @@ public interface LifecycleStateDAO { * @return Latest Lifecycle State for the given application release * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. */ - LifecycleStateDTO getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException; + LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException; /** * To get the latest lifecycle state for the given application id and the application release UUID. @@ -45,32 +45,31 @@ public interface LifecycleStateDAO { * @return Latest Lifecycle State for the given application release * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. */ - LifecycleStateDTO getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException; + 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. + * @param tenantId Tenant Id. * * @return Lifecycle States for the given application release * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. */ - List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; + List getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException; /** * To add new lifecycle states for the given application release. - * @param uuid Id of the application release. - * @param appId Id of the application. - * @param state LifecycleStateDTO. + * @param state LifecycleState. * @param tenantId Tenant id * * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. */ - void addLifecycleState(LifecycleStateDTO state, int appId, String uuid, int tenantId) + void addLifecycleState(LifecycleState state, int appReleaseId, int tenantId) throws LifeCycleManagementDAOException; /** * To delete lifecycle state data of specific application release. - * @param releaseId Id of the LifecycleStateDTO. + * @param releaseId Id of the LifecycleState. * * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. */ 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 2c9a74370f..2eb26a105f 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 @@ -752,4 +752,38 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } + @Override + public boolean hasExisitInstallableAppRelease(String releaseUuid, String installableStateName, int tenantId) + throws ApplicationManagementDAOException{ + if (log.isDebugEnabled()) { + log.debug("Verifying application release existence in the installable state: :" + installableStateName); + } + Connection conn; + try { + conn = this.getDBConnection(); + String sql = "SELECT AR.ID AS RELEASE_ID " + + "FROM AP_APP_RELEASE AS AR " + + "WHERE AR.CURRENT_STATE = ? AND " + + "AR.AP_APP_ID = (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE UUID = ?) AND " + + "AR.TENANT_ID = ?"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, installableStateName); + stmt.setString(2, releaseUuid); + stmt.setInt(3, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + return rs.next(); + } + } + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error occurred while getting application release details in installable state: " + + installableStateName, e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Error occurred while obtaining the DB connection to get application release data in installable " + + "state.", e); + } + } + } \ 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 184fdf13ef..78cf1e7a5b 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 @@ -19,7 +19,7 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate; import org.wso2.carbon.device.application.mgt.common.AppLifecycleState; -import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; @@ -41,7 +41,7 @@ import java.util.List; public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO { @Override - public LifecycleStateDTO getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException { + public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException { Connection conn = null; PreparedStatement stmt = null; @@ -65,7 +65,7 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } } - public LifecycleStateDTO getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException{ + public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException{ Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; @@ -122,40 +122,45 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } @Override - public List getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException { - List lifecycleStates = new ArrayList<>(); - Connection conn = null; - PreparedStatement stmt = null; - ResultSet rs = null; + public List getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException { + List lifecycleStates = new ArrayList<>(); + Connection conn ; 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_RELEASE_ID = ? ORDER BY UPDATED_AT ASC;"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1,appReleaseId); - rs = stmt.executeQuery(); - while (rs.next()) { - LifecycleStateDTO lifecycleState = new LifecycleStateDTO(); - 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")); - lifecycleStates.add(lifecycleState); + String sql = "SELECT " + + "CURRENT_STATE, " + + "PREVIOUS_STATE, " + + "UPDATED_AT, " + + "UPDATED_BY " + + "FROM AP_APP_LIFECYCLE_STATE " + + "WHERE AP_APP_RELEASE_ID = ? AND " + + "TENANT_ID = ? " + + "ORDER BY UPDATED_AT ASC;"; + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + stmt.setInt(1,appReleaseId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()){ + while (rs.next()) { + LifecycleState lifecycleState = new LifecycleState(); + lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); + lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); + lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); + lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); + lifecycleStates.add(lifecycleState); + } + } } } catch (DBConnectionException e) { throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection when getting " + "lifecycle states for an application", e); } catch (SQLException e) { throw new LifeCycleManagementDAOException("Error occurred while retrieving lifecycle states.", e); - } finally { - Util.cleanupResources(stmt, rs); } return lifecycleStates; } @Override - public void addLifecycleState(LifecycleStateDTO state, int appId, String uuid, int tenantId) throws LifeCycleManagementDAOException { + public void addLifecycleState(LifecycleState state, int appReleaseId, int tenantId) throws LifeCycleManagementDAOException { Connection conn = null; PreparedStatement stmt = null; try { @@ -168,7 +173,7 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif + "UPDATED_AT, " + "AP_APP_RELEASE_ID, " + "AP_APP_ID) " - + "VALUES (?,?, ?, ?, ?, (SELECT ID FROM AP_APP_RELEASE WHERE UUID=?),?);"; + + "VALUES (?, ?, ?, ?, ?, ?, (SELECT AP_APP_ID FROM AP_APP_RELEASE WHERE ID = ?));"; Calendar calendar = Calendar.getInstance(); Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); @@ -179,10 +184,9 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif stmt.setInt(3, tenantId); stmt.setString(4, state.getUpdatedBy()); stmt.setTimestamp(5, timestamp); - stmt.setString(6, uuid); - stmt.setInt(7, appId); + stmt.setInt(6, appReleaseId); + stmt.setInt(7, appReleaseId); stmt.executeUpdate(); - } catch (DBConnectionException e) { throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection.", e); } catch (SQLException e) { @@ -237,12 +241,11 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif } - private LifecycleStateDTO constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { - LifecycleStateDTO lifecycleState = null; + private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { + LifecycleState lifecycleState = null; try { if (rs !=null && rs.next()) { - lifecycleState = new LifecycleStateDTO(); - lifecycleState.setId(rs.getInt("ID")); + lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); 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 c4f225c691..cfe646d12f 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 @@ -39,7 +39,7 @@ import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.dto.CategoryDTO; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; -import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.dto.TagDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; @@ -311,10 +311,10 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationReleaseDTO.setCurrentState(initialLifecycleState); applicationReleaseDTO = this.applicationReleaseDAO .createRelease(applicationReleaseDTO, appId, tenantId); - LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance(initialLifecycleState, + LifecycleState lifecycleState = getLifecycleStateInstance(initialLifecycleState, initialLifecycleState); this.lifecycleStateDAO - .addLifecycleState(lifecycleStateDTO, appId, applicationReleaseDTO.getUuid(), tenantId); + .addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId); applicationReleaseEntities.add(applicationReleaseDTO); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); Application application = appDtoToAppResponse(applicationDTO); @@ -745,11 +745,11 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.beginDBTransaction(); String initialstate = lifecycleStateManager.getInitialState(); applicationReleaseDTO.setCurrentState(initialstate); - LifecycleStateDTO lifecycleState = getLifecycleStateInstance(initialstate, initialstate); + LifecycleState lifecycleState = getLifecycleStateInstance(initialstate, initialstate); applicationReleaseDTO = this.applicationReleaseDAO .createRelease(applicationReleaseDTO, applicationDTO.getId(), tenantId); this.lifecycleStateDAO - .addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); + .addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId); applicationRelease = releaseDtoToRelease(applicationReleaseDTO); ConnectionManagerUtil.commitDBTransaction(); return applicationRelease; @@ -1116,7 +1116,7 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ApplicationManagementException(""); } for (ApplicationReleaseDTO applicationRelease : applicationReleases) { - LifecycleStateDTO lifecycleState = null; + LifecycleState lifecycleState = null; try { lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); } catch (LifeCycleManagementDAOException e) { @@ -1139,7 +1139,7 @@ public class ApplicationManagerImpl implements ApplicationManager { // // if (state != null && !state.isEmpty()) { // for (ApplicationReleaseDTO applicationRelease : applicationReleases) { -// if (state.equals(applicationRelease.getLifecycleState().getCurrentState())) { +// if (state.equals(applicationRelease.getLifecycleStateChangeFlow().getCurrentState())) { // filteredReleases.add(applicationRelease); // } // } @@ -1147,9 +1147,9 @@ public class ApplicationManagerImpl implements ApplicationManager { // if (AppLifecycleState.PUBLISHED.toString().equals(state) && filteredReleases.size() > 1) { // log.warn("There are more than one application releases is found which is in PUBLISHED state"); // filteredReleases.sort((r1, r2) -> { -// if (r1.getLifecycleState().getUpdatedAt().after(r2.getLifecycleState().getUpdatedAt())) { +// if (r1.getLifecycleStateChangeFlow().getUpdatedAt().after(r2.getLifecycleStateChangeFlow().getUpdatedAt())) { // return -1; -// } else if (r2.getLifecycleState().getUpdatedAt().after(r1.getLifecycleState().getUpdatedAt())) { +// } else if (r2.getLifecycleStateChangeFlow().getUpdatedAt().after(r1.getLifecycleStateChangeFlow().getUpdatedAt())) { // return 1; // } // return 0; @@ -1327,17 +1327,17 @@ public class ApplicationManagerImpl implements ApplicationManager { } List deviceSubscriptionDTOS = subscriptionDAO .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); - for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { - if (!deviceSubscriptionDTO.isUnsubscribed()) { - String msg = "This application is subscribed to device/s. Therefore you are not permitted to delete " - + "the application release."; - log.error(msg); - throw new ForbiddenException(msg); - } + if (!deviceSubscriptionDTOS.isEmpty()){ + String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() + + " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " + + "permitted to delete the application release."; + log.error(msg); + throw new ForbiddenException(msg); } applicationStorageManager.deleteApplicationReleaseArtifacts(applicationReleaseDTO.getAppHashValue()); lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); + ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred when application release data for application release UUID: " + releaseUuid; @@ -1565,31 +1565,58 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public LifecycleStateDTO getLifecycleState(int applicationId, String releaseUuid) - throws ApplicationManagementException { - LifecycleStateDTO lifecycleState; + public List getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); - if (lifecycleState == null) { - return null; + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO + .getReleaseByUUID(releaseUuid, tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't found an application release for application release UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); } - lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManager.getNextLifecycleStates(lifecycleState.getCurrentState()))); - + return this.lifecycleStateDAO.getLifecycleStates(applicationReleaseDTO.getId(), tenantId); } catch (LifeCycleManagementDAOException e) { - throw new ApplicationManagementException("Failed to get lifecycle state from database", e); + String msg = "Failed to get lifecycle state for application release uuid " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = + "Error occurred while getting application release for application release UUID: " + releaseUuid; + log.error(msg); + throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } - return lifecycleState; } @Override - public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleStateDTO state) + public void changeLifecycleState(String releaseUuid, String stateName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); try { ConnectionManagerUtil.beginDBTransaction(); + ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(releaseUuid, tenantId); + + if (applicationReleaseDTO == null ){ + String msg = "Couldn't found an application release for the UUID: " + releaseUuid; + log.error(msg); + throw new NotFoundException(msg); + } + + if (lifecycleStateManager.isValidStateChange(applicationReleaseDTO.getCurrentState(), stateName, userName, tenantId)){ + LifecycleState lifecycleState = new LifecycleState(); + lifecycleState.setCurrentState(stateName); + lifecycleState.setPreviousState(applicationReleaseDTO.getCurrentState()); + lifecycleState.setUpdatedBy(userName); + this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId); + + } + + + if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)) { throw new NotFoundException("Couldn't find application for the application Id: " + applicationId); } @@ -1597,27 +1624,26 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new NotFoundException("Couldn't find application release for the application Id: " + applicationId + " application release uuid: " + releaseUuid); } - LifecycleStateDTO currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); + LifecycleState currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); if (currentState == null) { throw new ApplicationManagementException( "Couldn't find latest lifecycle state for the appId: " + applicationId + " and application release UUID: " + releaseUuid); } - state.setPreviousState(currentState.getCurrentState()); - String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - state.setUpdatedBy(userName); + stateName.setPreviousState(currentState.getCurrentState()); + - if (state.getCurrentState() != null && state.getPreviousState() != null) { - if (lifecycleStateManager.isValidStateChange(state.getPreviousState(), state.getCurrentState(), + if (stateName.getCurrentState() != null && stateName.getPreviousState() != null) { + if (lifecycleStateManager.isValidStateChange(stateName.getPreviousState(), stateName.getCurrentState(), userName, tenantId)) { //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); + this.lifecycleStateDAO.addLifecycleState(stateName, applicationId, releaseUuid, tenantId); ConnectionManagerUtil.commitDBTransaction(); } else { ConnectionManagerUtil.rollbackDBTransaction(); - log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '" - + state.getCurrentState() + "'"); + log.error("Invalid lifecycle state transition from '" + stateName.getPreviousState() + "'" + " to '" + + stateName.getCurrentState() + "'"); throw new ApplicationManagementException( "Lifecycle State Validation failed. ApplicationDTO Id: " + applicationId + " ApplicationDTO release UUID: " + releaseUuid); @@ -1898,11 +1924,11 @@ public class ApplicationManagerImpl implements ApplicationManager { * By invoking the method, it returns Lifecycle State Instance. * @param currentState Current state of the lifecycle * @param previousState Previouse state of the Lifecycle - * @return {@link LifecycleStateDTO} + * @return {@link LifecycleState} */ - private LifecycleStateDTO getLifecycleStateInstance(String currentState, String previousState) { + private LifecycleState getLifecycleStateInstance(String currentState, String previousState) { String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - LifecycleStateDTO lifecycleState = new LifecycleStateDTO(); + LifecycleState lifecycleState = new LifecycleState(); lifecycleState.setCurrentState(currentState); lifecycleState.setPreviousState(previousState); lifecycleState.setUpdatedBy(userName); 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java index 3d3d0fa08d..dceaecac72 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/ApplicationManagementPublisherAPI.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/ApplicationManagementPublisherAPI.java @@ -666,7 +666,7 @@ public interface ApplicationManagementPublisherAPI { @Multipart(value = "screenshot3") Attachment screenshot3); @GET - @Path("/lifecycle/{appId}/{uuid}") + @Path("/life-cycle/state-changes/{uuid}") @Produces(MediaType.APPLICATION_JSON) @ApiOperation( produces = MediaType.APPLICATION_JSON, @@ -687,16 +687,22 @@ public interface ApplicationManagementPublisherAPI { message = "OK. \n Successfully retrieved lifecycle states.", response = List.class, responseContainer = "List"), + @ApiResponse( + code = 404, + message = "NOT FOUND. \n Couldn't found an application release for application release UUID."), @ApiResponse( code = 500, message = "Internal Server Error. \n Error occurred while getting the lifecycle list.", response = ErrorResponse.class) }) - Response getLifecycleState(@PathParam("appId") int applicationId, - @PathParam("uuid") String applicationUuid); + Response getLifecycleStates( + @ApiParam( + name = "uuid", + value = "UUID of the application release.") + @PathParam("uuid") String applicationUuid); @POST - @Path("/lifecycle/{appId}/{uuid}") + @Path("/life-cycle/{uuid}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -732,11 +738,6 @@ public interface ApplicationManagementPublisherAPI { response = ErrorResponse.class) }) Response addLifecycleState( - @ApiParam( - name = "appId", - value = "Identifier of the ApplicationDTO", - required = true) - @PathParam("appId") int applicationId, @ApiParam( name = "uuid", value = "UUID of the ApplicationDTO Release", 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java index 15f7568e84..33b6f800d6 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/ApplicationManagementPublisherAPIImpl.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/ApplicationManagementPublisherAPIImpl.java @@ -18,12 +18,13 @@ */ package org.wso2.carbon.device.application.mgt.publisher.api.services.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.common.*; -import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; import org.wso2.carbon.device.application.mgt.common.response.Application; @@ -457,49 +458,43 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } } - - /* -//todo ---------------------- -*/ @GET - @Path("/lifecycle/{appId}/{uuid}") - public Response getLifecycleState( - @PathParam("appId") int applicationId, - @PathParam("uuid") String applicationUuid) { - LifecycleStateDTO lifecycleState; + @Path("/life-cycle/state-changes/{uuid}") + public Response getLifecycleStates( + @PathParam("uuid") String releaseUuid) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - lifecycleState = applicationManager.getLifecycleState(applicationId, applicationUuid); - if (lifecycleState == null) { - String msg = "Couldn't found application lifecycle details for appid: " + applicationId - + " and app release UUID: " + applicationUuid; - log.error(msg); - return Response.status(Response.Status.NOT_FOUND).build(); - } + List lifecycleStates = applicationManager.getLifecycleStateChangeFlow(releaseUuid); + return Response.status(Response.Status.OK).entity(lifecycleStates).build(); + } catch (NotFoundException e) { + String msg = "Couldn't found an application release for UUID: " + releaseUuid; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (ApplicationManagementException e) { - String msg = "Error occurred while getting lifecycle state."; + String msg = + "Error occurred while getting lifecycle states for application release UUID: " + releaseUuid; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } - return Response.status(Response.Status.OK).entity(lifecycleState).build(); } +/* +//todo ---------------------- +*/ @POST - @Path("/lifecycle/{appId}/{uuid}") + @Path("/life-cycle/{uuid}") public Response addLifecycleState( - @PathParam("appId") int applicationId, @PathParam("uuid") String applicationUuid, @QueryParam("action") String action) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - if (action == null || action.isEmpty()) { - String msg = "The Action is null or empty. Please check the request"; + if (StringUtils.isEmpty(action)) { + String msg = "The Action is null or empty. Please verify the request."; log.error(msg); return Response.status(Response.Status.BAD_REQUEST).build(); } - LifecycleStateDTO state = new LifecycleStateDTO(); - state.setCurrentState(action); - applicationManager.changeLifecycleState(applicationId, applicationUuid, state); + + applicationManager.changeLifecycleState( applicationUuid, action); } catch (NotFoundException e) { String msg = "Could,t find application release for application id: " + applicationId + " and application release uuid: " + applicationUuid; 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/admin/ApplicationManagementPublisherAdminAPIImpl.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/admin/ApplicationManagementPublisherAdminAPIImpl.java index 61ace448eb..6f2525fe3b 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/admin/ApplicationManagementPublisherAdminAPIImpl.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/admin/ApplicationManagementPublisherAdminAPIImpl.java @@ -20,49 +20,18 @@ package org.wso2.carbon.device.application.mgt.publisher.api.services.impl.admin import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.cxf.jaxrs.ext.multipart.Attachment; -import org.apache.cxf.jaxrs.ext.multipart.Multipart; -import org.wso2.carbon.device.application.mgt.common.ApplicationArtifact; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; -import org.wso2.carbon.device.application.mgt.common.ApplicationType; -import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; -import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; -import org.wso2.carbon.device.application.mgt.common.response.Application; -import org.wso2.carbon.device.application.mgt.common.response.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseWrapper; -import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationUpdateWrapper; -import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; -import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; -import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementPublisherAPI; import org.wso2.carbon.device.application.mgt.publisher.api.services.admin.ApplicationManagementPublisherAdminAPI; -import javax.activation.DataHandler; -import javax.validation.Valid; -import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; /** * Implementation of Application Management related APIs.