Merge branch 'application-mgt-new' of https://gitlab.com/entgra/carbon-device-mgt into application-mgt-new

feature/appm-store/pbac
Jayasanka 6 years ago
commit 29e06ddf4e

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

@ -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,22 @@ 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<LifecycleState> 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;
/**

@ -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 hasExistInstallableAppRelease(String releaseUuid, String installableStateName, int tenantId)
throws ApplicationManagementDAOException;
}

@ -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<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.
* @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.
*/

@ -129,6 +129,7 @@ public class Util {
*/
public static ApplicationReleaseDTO loadAppRelease(ResultSet rs) throws SQLException {
ApplicationReleaseDTO appRelease = new ApplicationReleaseDTO();
appRelease.setId(rs.getInt("RELEASE_ID"));
appRelease.setDescription(rs.getString("RELEASE_DESCRIPTION"));
appRelease.setUuid(rs.getString("RELEASE_UUID"));
appRelease.setReleaseType(rs.getString("RELEASE_TYPE"));

@ -144,6 +144,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "AP_APP.CURRENCY AS APP_CURRENCY, "
+ "AP_APP.RATING AS APP_RATING, "
+ "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, "
+ "AP_APP_RELEASE.ID AS RELEASE_ID, "
+ "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, "
+ "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, "
+ "AP_APP_RELEASE.UUID AS RELEASE_UUID, "
@ -502,6 +503,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
+ "AP_APP.CURRENCY AS APP_CURRENCY, "
+ "AP_APP.RATING AS APP_RATING, "
+ "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, "
+ "AP_APP_RELEASE.ID AS RELEASE_ID, "
+ "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, "
+ "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, "
+ "AP_APP_RELEASE.UUID AS RELEASE_UUID, "

@ -226,7 +226,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
public ApplicationReleaseDTO getReleaseByUUID( String uuid, int tenantId) throws ApplicationManagementDAOException {
Connection connection;
String sql =
"SELECT AR.DESCRIPTION AS RELEASE_DESCRIPTION, "
"SELECT AR.ID AS RELEASE_ID, "
+ "AR.DESCRIPTION AS RELEASE_DESCRIPTION, "
+ "AR.VERSION AS RELEASE_VERSION, "
+ "AR.UUID AS RELEASE_UUID, "
+ "AR.RELEASE_TYPE AS RELEASE_TYPE, "
@ -752,4 +753,38 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
}
}
@Override
public boolean hasExistInstallableAppRelease(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;
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<LifecycleStateDTO> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException {
List<LifecycleStateDTO> lifecycleStates = new ArrayList<>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
public List<LifecycleState> getLifecycleStates(int appReleaseId, int tenantId) throws LifeCycleManagementDAOException {
List<LifecycleState> 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"));

@ -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<DeviceSubscriptionDTO> 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,72 +1565,84 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public LifecycleStateDTO getLifecycleState(int applicationId, String releaseUuid)
throws ApplicationManagementException {
LifecycleStateDTO lifecycleState;
public List<LifecycleState> 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();
if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)) {
throw new NotFoundException("Couldn't find application for the application Id: " + applicationId);
}
if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)) {
throw new NotFoundException("Couldn't find application release for the application Id: " + applicationId
+ " application release uuid: " + releaseUuid);
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);
}
LifecycleStateDTO 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);
if (state.getCurrentState() != null && state.getPreviousState() != null) {
if (lifecycleStateManager.isValidStateChange(state.getPreviousState(), state.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);
ConnectionManagerUtil.commitDBTransaction();
} else {
ConnectionManagerUtil.rollbackDBTransaction();
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '"
+ state.getCurrentState() + "'");
throw new ApplicationManagementException(
"Lifecycle State Validation failed. ApplicationDTO Id: " + applicationId
+ " ApplicationDTO release UUID: " + releaseUuid);
if (lifecycleStateManager
.isValidStateChange(applicationReleaseDTO.getCurrentState(), stateName, userName, tenantId)) {
if (lifecycleStateManager.isInstallableState(stateName) && applicationReleaseDAO
.hasExistInstallableAppRelease(applicationReleaseDTO.getUuid(),
lifecycleStateManager.getInstallableState(), tenantId)) {
String msg = "Installable application release is already registered for the application. "
+ "Therefore it is not permitted to change the lifecycle state from "
+ applicationReleaseDTO.getCurrentState() + " to " + stateName;
log.error(msg);
throw new ForbiddenException(msg);
}
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(stateName);
lifecycleState.setPreviousState(applicationReleaseDTO.getCurrentState());
lifecycleState.setUpdatedBy(userName);
applicationReleaseDTO.setCurrentState(stateName);
this.applicationReleaseDAO.updateRelease(applicationReleaseDTO, tenantId);
this.lifecycleStateDAO.addLifecycleState(lifecycleState, applicationReleaseDTO.getId(), tenantId);
ConnectionManagerUtil.commitDBTransaction();
} else {
String msg = "Invalid lifecycle state transition from '" + applicationReleaseDTO.getCurrentState() + "'"
+ " to '" + stateName + "'";
log.error(msg);
throw new ApplicationManagementException(msg);
}
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Failed to add lifecycle state. ApplicationDTO Id: " + applicationId + " ApplicationDTO release UUID: "
+ releaseUuid, e);
String msg = "Failed to add lifecycle state for Application release UUID: " + releaseUuid;
log.error(msg);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) {
//todo
throw new ApplicationManagementException("");
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when accessing application release data of application release which has the "
+ "application release UUID: " + releaseUuid;
log.error(msg);
throw new ApplicationManagementException(msg);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
@ -1898,11 +1910,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);

@ -47,7 +47,7 @@ public class LifecycleStateManager {
if (lifecycleState.getProceedingStates() != null) {
lifecycleState.getProceedingStates().replaceAll(String::toUpperCase);
}
lifecycleStates.put(lifecycleState.getName(), lifecycleState);
lifecycleStates.put(lifecycleState.getName().toUpperCase(), lifecycleState);
try {
PermissionUtils.putPermission(lifecycleState.getPermission());
} catch (PermissionManagementException e) {
@ -123,15 +123,10 @@ public class LifecycleStateManager {
}
private String getPermissionForStateChange(String nextState) {
Iterator it = lifecycleStates.entrySet().iterator();
LifecycleState nextLifecycleState;
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
if (pair.getKey().toString().equalsIgnoreCase(nextState)) {
nextLifecycleState = lifecycleStates.get(nextState);
return nextLifecycleState.getPermission();
for (Map.Entry<String, LifecycleState> lifecycyleState : lifecycleStates.entrySet()) {
if (lifecycyleState.getKey().equalsIgnoreCase(nextState)) {
return lifecycyleState.getValue().getPermission();
}
it.remove();
}
return null;
}

@ -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",

@ -1,29 +1,28 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
/* Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
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;
@ -166,18 +165,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
List<Attachment> attachmentList = new ArrayList<>();
if (screenshot1 != null) {
attachmentList.add(screenshot1);
}
if (screenshot2 != null) {
attachmentList.add(screenshot2);
}
if (screenshot3 != null) {
attachmentList.add(screenshot3);
}
List<Attachment> attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3);
try {
applicationManager.validateAppCreatingRequest(applicationWrapper);
applicationManager.validateReleaseCreatingRequest(applicationWrapper.getApplicationReleaseWrappers().get(0),
@ -220,18 +208,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
List<Attachment> attachmentList = new ArrayList<>();
if (screenshot1 != null) {
attachmentList.add(screenshot1);
}
if (screenshot2 != null) {
attachmentList.add(screenshot2);
}
if (screenshot3 != null) {
attachmentList.add(screenshot3);
}
List<Attachment> attachmentList = constructAttachmentList(screenshot1, screenshot2, screenshot3);
try {
applicationManager.validateReleaseCreatingRequest(applicationReleaseWrapper, appType);
applicationManager.validateBinaryArtifact(binaryFile, appType);
@ -270,17 +247,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
try {
List<Attachment> attachments = new ArrayList<>();
if (screenshot1 != null) {
attachments.add(screenshot1);
}
if (screenshot2 != null) {
attachments.add(screenshot2);
}
if (screenshot3 != null) {
attachments.add(screenshot3);
}
List<Attachment> attachments = constructAttachmentList(screenshot1, screenshot2, screenshot3);
ApplicationManager applicationManager = APIUtil.getApplicationManager();
applicationManager.validateImageArtifacts(iconFile, bannerFile, attachments);
applicationManager.updateApplicationImageArtifact(applicationReleaseUuid,
@ -384,16 +351,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
@Multipart("screenshot2") Attachment screenshot2,
@Multipart("screenshot3") Attachment screenshot3) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
List<Attachment> screenshots = new ArrayList<>();
if (screenshot1 != null){
screenshots.add(screenshot1);
}
if (screenshot2 != null) {
screenshots.add(screenshot2);
}
if (screenshot3 != null) {
screenshots.add(screenshot3);
}
List<Attachment> screenshots = constructAttachmentList(screenshot1, screenshot2, screenshot3);
try {
applicationManager.validateBinaryArtifact(binaryFile, appType);
applicationManager.validateImageArtifacts(iconFile, bannerFile, screenshots);
@ -457,52 +415,42 @@ 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<LifecycleState> 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();
}
@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;
String msg = "Could,t find application release for application release uuid: " + applicationUuid;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).build();
} catch (ApplicationManagementException e) {
@ -528,6 +476,29 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
}
}
/***
* Construct the screenshot list by evaluating the availability of each screenshot.
*
* @param screenshot1 First Screenshot
* @param screenshot2 Second Screenshot
* @param screenshot3 Third Screenshot
* @return List of {@link Attachment}
*/
private List<Attachment> constructAttachmentList(Attachment screenshot1, Attachment screenshot2,
Attachment screenshot3) {
List<Attachment> attachments = new ArrayList<>();
if (screenshot1 != null) {
attachments.add(screenshot1);
}
if (screenshot2 != null) {
attachments.add(screenshot2);
}
if (screenshot3 != null) {
attachments.add(screenshot3);
}
return attachments;
}
/***
*
* @param binaryFile binary file of the application release
@ -633,7 +604,5 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
log.error(msg, e);
throw new ApplicationManagementException(msg);
}
}
}

@ -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.

Loading…
Cancel
Save