Add lifecycle adding API

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent e293a236c5
commit a3d75df8a0

@ -16,7 +16,7 @@
* under the License. * 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 java.sql.Timestamp;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
@ -24,13 +24,8 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.List; import java.util.List;
@ApiModel(value = "LifecycleStateDTO", description = "LifecycleStateDTO represents the Lifecycle state for an application release") @ApiModel(value = "LifecycleState", description = "LifecycleState represents the Lifecycle state for an application release")
public class LifecycleStateDTO { public class LifecycleState {
@ApiModelProperty(name = "id",
value = "ID of the application release lifecycle",
required = true)
private int id;
@ApiModelProperty(name = "currentState", @ApiModelProperty(name = "currentState",
value = "Current state of the application release", value = "Current state of the application release",
@ -54,14 +49,6 @@ public class LifecycleStateDTO {
value = "Timestamp of the lifecycle has been updated") value = "Timestamp of the lifecycle has been updated")
private Timestamp updatedAt; private Timestamp updatedAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCurrentState() { public String getCurrentState() {
return currentState; return currentState;
} }

@ -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.ApplicationList;
import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; 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.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.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; 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.Application;
@ -133,24 +133,23 @@ public interface ApplicationManager {
ApplicationDTO getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException; 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 Application Release.
* @param releaseUuid UUID of the ApplicationDTO Release. * @return the List of LifecycleStates which represent the lifecycle change flow of the application releases.
* @return the LifecycleStateDTO of the ApplicationDTO releases related with the particular ApplicationDTO. * @throws ApplicationManagementException Application Management Exception.
* @throws ApplicationManagementException ApplicationDTO Management Exception.
*/ */
LifecycleStateDTO getLifecycleState(int applicationId, String releaseUuid) throws ApplicationManagementException; List<LifecycleState> getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException;
/** /**
* To get all the releases of a particular ApplicationDTO. * To get all the releases of a particular ApplicationDTO.
* *
* @param applicationId ID of the ApplicationDTO. * @param applicationId ID of the ApplicationDTO.
* @param releaseUuid UUID of the ApplicationDTO Release. * @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. * @throws ApplicationManagementException ApplicationDTO Management Exception.
*/ */
void changeLifecycleState(int applicationId, String releaseUuid, LifecycleStateDTO state) void changeLifecycleState(String releaseUuid, String stateName)
throws ApplicationManagementException; throws ApplicationManagementException;
/** /**

@ -178,6 +178,10 @@ public interface ApplicationReleaseDAO {
* @return True if application release package name already exist in the IoT server, Otherwise returns False. * @return True if application release package name already exist in the IoT server, Otherwise returns False.
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @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;
} }

@ -18,7 +18,7 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.dao; 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 org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException;
import java.util.List; import java.util.List;
@ -35,7 +35,7 @@ public interface LifecycleStateDAO {
* @return Latest Lifecycle State for the given application release * @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. * @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. * 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 * @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. * @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. * To get all changed lifecycle states for the given application release id.
* @param appReleaseId id of the application release. * @param appReleaseId id of the application release.
* @param tenantId Tenant Id.
* *
* @return Lifecycle States for the given application release * @return Lifecycle States for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/ */
List<LifecycleStateDTO> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException; List<LifecycleState> getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException;
/** /**
* To add new lifecycle states for the given application release. * To add new lifecycle states for the given application release.
* @param uuid Id of the application release. * @param state LifecycleState.
* @param appId Id of the application.
* @param state LifecycleStateDTO.
* @param tenantId Tenant id * @param tenantId Tenant id
* *
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. * @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; throws LifeCycleManagementDAOException;
/** /**
* To delete lifecycle state data of specific application release. * 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. * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/ */

@ -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);
}
}
} }

@ -19,7 +19,7 @@
package org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate; 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.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.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; 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.common.Util;
@ -41,7 +41,7 @@ import java.util.List;
public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO { public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO {
@Override @Override
public LifecycleStateDTO getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException { public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = 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; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
@ -122,40 +122,45 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
} }
@Override @Override
public List<LifecycleStateDTO> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException { public List<LifecycleState> getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException {
List<LifecycleStateDTO> lifecycleStates = new ArrayList<>(); List<LifecycleState> lifecycleStates = new ArrayList<>();
Connection conn = null; Connection conn ;
PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
String sql = "SELECT ID, CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_AT, UPDATED_BY FROM " String sql = "SELECT "
+ "AP_APP_LIFECYCLE_STATE WHERE AP_APP_RELEASE_ID = ? ORDER BY UPDATED_AT ASC;"; + "CURRENT_STATE, "
stmt = conn.prepareStatement(sql); + "PREVIOUS_STATE, "
stmt.setInt(1,appReleaseId); + "UPDATED_AT, "
rs = stmt.executeQuery(); + "UPDATED_BY "
while (rs.next()) { + "FROM AP_APP_LIFECYCLE_STATE "
LifecycleStateDTO lifecycleState = new LifecycleStateDTO(); + "WHERE AP_APP_RELEASE_ID = ? AND "
lifecycleState.setId(rs.getInt("ID")); + "TENANT_ID = ? "
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); + "ORDER BY UPDATED_AT ASC;";
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); try (PreparedStatement stmt = conn.prepareStatement(sql)){
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); stmt.setInt(1,appReleaseId);
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY")); stmt.setInt(2, tenantId);
lifecycleStates.add(lifecycleState); 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) { } catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection when getting " throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection when getting "
+ "lifecycle states for an application", e); + "lifecycle states for an application", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while retrieving lifecycle states.", e); throw new LifeCycleManagementDAOException("Error occurred while retrieving lifecycle states.", e);
} finally {
Util.cleanupResources(stmt, rs);
} }
return lifecycleStates; return lifecycleStates;
} }
@Override @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; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
@ -168,7 +173,7 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
+ "UPDATED_AT, " + "UPDATED_AT, "
+ "AP_APP_RELEASE_ID, " + "AP_APP_RELEASE_ID, "
+ "AP_APP_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(); Calendar calendar = Calendar.getInstance();
Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
@ -179,10 +184,9 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setString(4, state.getUpdatedBy()); stmt.setString(4, state.getUpdatedBy());
stmt.setTimestamp(5, timestamp); stmt.setTimestamp(5, timestamp);
stmt.setString(6, uuid); stmt.setInt(6, appReleaseId);
stmt.setInt(7, appId); stmt.setInt(7, appReleaseId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection.", e); throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException e) { } catch (SQLException e) {
@ -237,12 +241,11 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
} }
private LifecycleStateDTO constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException {
LifecycleStateDTO lifecycleState = null; LifecycleState lifecycleState = null;
try { try {
if (rs !=null && rs.next()) { if (rs !=null && rs.next()) {
lifecycleState = new LifecycleStateDTO(); lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE")); lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT")); lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));

@ -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.dto.CategoryDTO;
import org.wso2.carbon.device.application.mgt.common.Filter; 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.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.dto.TagDTO;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; 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.ApplicationStorageManagementException;
@ -311,10 +311,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
applicationReleaseDTO.setCurrentState(initialLifecycleState); applicationReleaseDTO.setCurrentState(initialLifecycleState);
applicationReleaseDTO = this.applicationReleaseDAO applicationReleaseDTO = this.applicationReleaseDAO
.createRelease(applicationReleaseDTO, appId, tenantId); .createRelease(applicationReleaseDTO, appId, tenantId);
LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance(initialLifecycleState, LifecycleState lifecycleState = getLifecycleStateInstance(initialLifecycleState,
initialLifecycleState); initialLifecycleState);
this.lifecycleStateDAO this.lifecycleStateDAO
.addLifecycleState(lifecycleStateDTO, appId, applicationReleaseDTO.getUuid(), tenantId); .addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId);
applicationReleaseEntities.add(applicationReleaseDTO); applicationReleaseEntities.add(applicationReleaseDTO);
applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities); applicationDTO.setApplicationReleaseDTOs(applicationReleaseEntities);
Application application = appDtoToAppResponse(applicationDTO); Application application = appDtoToAppResponse(applicationDTO);
@ -745,11 +745,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
String initialstate = lifecycleStateManager.getInitialState(); String initialstate = lifecycleStateManager.getInitialState();
applicationReleaseDTO.setCurrentState(initialstate); applicationReleaseDTO.setCurrentState(initialstate);
LifecycleStateDTO lifecycleState = getLifecycleStateInstance(initialstate, initialstate); LifecycleState lifecycleState = getLifecycleStateInstance(initialstate, initialstate);
applicationReleaseDTO = this.applicationReleaseDAO applicationReleaseDTO = this.applicationReleaseDAO
.createRelease(applicationReleaseDTO, applicationDTO.getId(), tenantId); .createRelease(applicationReleaseDTO, applicationDTO.getId(), tenantId);
this.lifecycleStateDAO this.lifecycleStateDAO
.addLifecycleState(lifecycleState, applicationId, applicationReleaseDTO.getUuid(), tenantId); .addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId);
applicationRelease = releaseDtoToRelease(applicationReleaseDTO); applicationRelease = releaseDtoToRelease(applicationReleaseDTO);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
return applicationRelease; return applicationRelease;
@ -1116,7 +1116,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException(""); throw new ApplicationManagementException("");
} }
for (ApplicationReleaseDTO applicationRelease : applicationReleases) { for (ApplicationReleaseDTO applicationRelease : applicationReleases) {
LifecycleStateDTO lifecycleState = null; LifecycleState lifecycleState = null;
try { try {
lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId()); lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
@ -1139,7 +1139,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
// //
// if (state != null && !state.isEmpty()) { // if (state != null && !state.isEmpty()) {
// for (ApplicationReleaseDTO applicationRelease : applicationReleases) { // for (ApplicationReleaseDTO applicationRelease : applicationReleases) {
// if (state.equals(applicationRelease.getLifecycleState().getCurrentState())) { // if (state.equals(applicationRelease.getLifecycleStateChangeFlow().getCurrentState())) {
// filteredReleases.add(applicationRelease); // filteredReleases.add(applicationRelease);
// } // }
// } // }
@ -1147,9 +1147,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
// if (AppLifecycleState.PUBLISHED.toString().equals(state) && filteredReleases.size() > 1) { // 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"); // log.warn("There are more than one application releases is found which is in PUBLISHED state");
// filteredReleases.sort((r1, r2) -> { // filteredReleases.sort((r1, r2) -> {
// if (r1.getLifecycleState().getUpdatedAt().after(r2.getLifecycleState().getUpdatedAt())) { // if (r1.getLifecycleStateChangeFlow().getUpdatedAt().after(r2.getLifecycleStateChangeFlow().getUpdatedAt())) {
// return -1; // return -1;
// } else if (r2.getLifecycleState().getUpdatedAt().after(r1.getLifecycleState().getUpdatedAt())) { // } else if (r2.getLifecycleStateChangeFlow().getUpdatedAt().after(r1.getLifecycleStateChangeFlow().getUpdatedAt())) {
// return 1; // return 1;
// } // }
// return 0; // return 0;
@ -1327,17 +1327,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId);
for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { if (!deviceSubscriptionDTOS.isEmpty()){
if (!deviceSubscriptionDTO.isUnsubscribed()) { String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() +
String msg = "This application is subscribed to device/s. Therefore you are not permitted to delete " " either subscribed to device/s or it had subscribed to device/s. Therefore you are not "
+ "the application release."; + "permitted to delete the application release.";
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
}
} }
applicationStorageManager.deleteApplicationReleaseArtifacts(applicationReleaseDTO.getAppHashValue()); applicationStorageManager.deleteApplicationReleaseArtifacts(applicationReleaseDTO.getAppHashValue());
lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId());
applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId());
ConnectionManagerUtil.commitDBTransaction();
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when application release data for application release UUID: " + releaseUuid; String msg = "Error occurred when application release data for application release UUID: " + releaseUuid;
@ -1565,31 +1565,58 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public LifecycleStateDTO getLifecycleState(int applicationId, String releaseUuid) public List<LifecycleState> getLifecycleStateChangeFlow(String releaseUuid) throws ApplicationManagementException {
throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
LifecycleStateDTO lifecycleState;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO
if (lifecycleState == null) { .getReleaseByUUID(releaseUuid, tenantId);
return null; 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) { } 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 { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
return lifecycleState;
} }
@Override @Override
public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleStateDTO state) public void changeLifecycleState(String releaseUuid, String stateName)
throws ApplicationManagementException { throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
try { try {
ConnectionManagerUtil.beginDBTransaction(); 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)) { if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)) {
throw new NotFoundException("Couldn't find application for the application Id: " + applicationId); 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 throw new NotFoundException("Couldn't find application release for the application Id: " + applicationId
+ " application release uuid: " + releaseUuid); + " application release uuid: " + releaseUuid);
} }
LifecycleStateDTO currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid); LifecycleState currentState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid);
if (currentState == null) { if (currentState == null) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Couldn't find latest lifecycle state for the appId: " + applicationId "Couldn't find latest lifecycle state for the appId: " + applicationId
+ " and application release UUID: " + releaseUuid); + " and application release UUID: " + releaseUuid);
} }
state.setPreviousState(currentState.getCurrentState()); stateName.setPreviousState(currentState.getCurrentState());
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null) { if (stateName.getCurrentState() != null && stateName.getPreviousState() != null) {
if (lifecycleStateManager.isValidStateChange(state.getPreviousState(), state.getCurrentState(), if (lifecycleStateManager.isValidStateChange(stateName.getPreviousState(), stateName.getCurrentState(),
userName, tenantId)) { userName, tenantId)) {
//todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there //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) //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(); ConnectionManagerUtil.commitDBTransaction();
} else { } else {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '" log.error("Invalid lifecycle state transition from '" + stateName.getPreviousState() + "'" + " to '"
+ state.getCurrentState() + "'"); + stateName.getCurrentState() + "'");
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Lifecycle State Validation failed. ApplicationDTO Id: " + applicationId "Lifecycle State Validation failed. ApplicationDTO Id: " + applicationId
+ " ApplicationDTO release UUID: " + releaseUuid); + " ApplicationDTO release UUID: " + releaseUuid);
@ -1898,11 +1924,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
* By invoking the method, it returns Lifecycle State Instance. * By invoking the method, it returns Lifecycle State Instance.
* @param currentState Current state of the lifecycle * @param currentState Current state of the lifecycle
* @param previousState Previouse 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(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
LifecycleStateDTO lifecycleState = new LifecycleStateDTO(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(currentState); lifecycleState.setCurrentState(currentState);
lifecycleState.setPreviousState(previousState); lifecycleState.setPreviousState(previousState);
lifecycleState.setUpdatedBy(userName); lifecycleState.setUpdatedBy(userName);

@ -666,7 +666,7 @@ public interface ApplicationManagementPublisherAPI {
@Multipart(value = "screenshot3") Attachment screenshot3); @Multipart(value = "screenshot3") Attachment screenshot3);
@GET @GET
@Path("/lifecycle/{appId}/{uuid}") @Path("/life-cycle/state-changes/{uuid}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
@ -687,16 +687,22 @@ public interface ApplicationManagementPublisherAPI {
message = "OK. \n Successfully retrieved lifecycle states.", message = "OK. \n Successfully retrieved lifecycle states.",
response = List.class, response = List.class,
responseContainer = "List"), responseContainer = "List"),
@ApiResponse(
code = 404,
message = "NOT FOUND. \n Couldn't found an application release for application release UUID."),
@ApiResponse( @ApiResponse(
code = 500, code = 500,
message = "Internal Server Error. \n Error occurred while getting the lifecycle list.", message = "Internal Server Error. \n Error occurred while getting the lifecycle list.",
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getLifecycleState(@PathParam("appId") int applicationId, Response getLifecycleStates(
@PathParam("uuid") String applicationUuid); @ApiParam(
name = "uuid",
value = "UUID of the application release.")
@PathParam("uuid") String applicationUuid);
@POST @POST
@Path("/lifecycle/{appId}/{uuid}") @Path("/life-cycle/{uuid}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@ApiOperation( @ApiOperation(
@ -732,11 +738,6 @@ public interface ApplicationManagementPublisherAPI {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response addLifecycleState( Response addLifecycleState(
@ApiParam(
name = "appId",
value = "Identifier of the ApplicationDTO",
required = true)
@PathParam("appId") int applicationId,
@ApiParam( @ApiParam(
name = "uuid", name = "uuid",
value = "UUID of the ApplicationDTO Release", value = "UUID of the ApplicationDTO Release",

@ -18,12 +18,13 @@
*/ */
package org.wso2.carbon.device.application.mgt.publisher.api.services.impl; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.wso2.carbon.device.application.mgt.common.*; 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.LifecycleManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.RequestValidatingException; 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.Application;
@ -457,49 +458,43 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
} }
} }
/*
//todo ----------------------
*/
@GET @GET
@Path("/lifecycle/{appId}/{uuid}") @Path("/life-cycle/state-changes/{uuid}")
public Response getLifecycleState( public Response getLifecycleStates(
@PathParam("appId") int applicationId, @PathParam("uuid") String releaseUuid) {
@PathParam("uuid") String applicationUuid) {
LifecycleStateDTO lifecycleState;
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
try { try {
lifecycleState = applicationManager.getLifecycleState(applicationId, applicationUuid); List<LifecycleState> lifecycleStates = applicationManager.getLifecycleStateChangeFlow(releaseUuid);
if (lifecycleState == null) { return Response.status(Response.Status.OK).entity(lifecycleStates).build();
String msg = "Couldn't found application lifecycle details for appid: " + applicationId } catch (NotFoundException e) {
+ " and app release UUID: " + applicationUuid; String msg = "Couldn't found an application release for UUID: " + releaseUuid;
log.error(msg); log.error(msg);
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
}
} catch (ApplicationManagementException e) { } 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); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} }
return Response.status(Response.Status.OK).entity(lifecycleState).build();
} }
/*
//todo ----------------------
*/
@POST @POST
@Path("/lifecycle/{appId}/{uuid}") @Path("/life-cycle/{uuid}")
public Response addLifecycleState( public Response addLifecycleState(
@PathParam("appId") int applicationId,
@PathParam("uuid") String applicationUuid, @PathParam("uuid") String applicationUuid,
@QueryParam("action") String action) { @QueryParam("action") String action) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
try { try {
if (action == null || action.isEmpty()) { if (StringUtils.isEmpty(action)) {
String msg = "The Action is null or empty. Please check the request"; String msg = "The Action is null or empty. Please verify the request.";
log.error(msg); log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }
LifecycleStateDTO state = new LifecycleStateDTO();
state.setCurrentState(action); applicationManager.changeLifecycleState( applicationUuid, action);
applicationManager.changeLifecycleState(applicationId, applicationUuid, state);
} catch (NotFoundException e) { } catch (NotFoundException e) {
String msg = "Could,t find application release for application id: " + applicationId String msg = "Could,t find application release for application id: " + applicationId
+ " and application release uuid: " + applicationUuid; + " and application release uuid: " + applicationUuid;

@ -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.Log;
import org.apache.commons.logging.LogFactory; 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.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.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.ForbiddenException;
import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; 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.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 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.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.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; 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. * Implementation of Application Management related APIs.

Loading…
Cancel
Save