Merge branch 'application-mgt-new' into 'application-mgt-new'

Fix get lifecycle issue

See merge request entgra/carbon-device-mgt!17
feature/appm-store/pbac
Inosh Perara 6 years ago
commit deb501332f

@ -112,7 +112,7 @@ public interface ApplicationManager {
* @return the boolean value, whether application exist or not
* @throws ApplicationManagementException Application Management Exception.
*/
Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException;
boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException;
/**
* To get Application with the given UUID.
@ -122,38 +122,27 @@ public interface ApplicationManager {
*/
Boolean isUserAllowable(List<UnrestrictedRole> unrestrictedRoles, String userName) throws ApplicationManagementException;
// todo
// /**
// * To get all the releases of a particular Application.
// *
// * @param applicationId ID of the Application to get all the releases.
// * @return the List of the Application releases related with the particular Application.
// * @throws ApplicationManagementException Application Management Exception.
// */
// List<ApplicationRelease> getinstallableReleases(int applicationId) throws ApplicationManagementException;
/**
* To get all the releases of a particular Application.
*
* @param applicationId ID of the Application .
* @param applicationUuid UUID of the Application Release.
* @param releaseUuid UUID of the Application Release.
* @return the LifecycleState of the Application releases related with the particular Application.
* @throws ApplicationManagementException Application Management Exception.
*/
LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws ApplicationManagementException;
LifecycleState getLifecycleState(int applicationId, String releaseUuid) throws ApplicationManagementException;
/**
* To get all the releases of a particular Application.
*
* @param applicationId ID of the Application.
* @param applicationUuid UUID of the Application Release.
* @param releaseUuid UUID of the Application Release.
* @param state Lifecycle state to change the app
* @param checkExist whether it is needed to check if the app and release already exist in the database
* @param releaseId The release ID of the application(optional)
* @throws ApplicationManagementException Application Management Exception.
*/
void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean checkExist,
int releaseId) throws ApplicationManagementException;
void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist)
throws ApplicationManagementException;
/**
* Get the application if application is an accessible one.

@ -109,10 +109,11 @@ public interface ApplicationDAO {
* To get the application with the given uuid
*
* @param appId ID of the application
* @param tenantId Tenant Id
* @return the boolean value
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException;
boolean verifyApplicationExistenceById(int appId, int tenantId) throws ApplicationManagementDAOException;
/**
* To get the application id of the application specified by the UUID

@ -117,4 +117,14 @@ public interface ApplicationReleaseDAO {
ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws
ApplicationManagementDAOException;
}
/**
* To verify whether application release exist or not.
*
* @param appId ID of the application.
* @param uuid UUID of the application release.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException;
}

@ -19,7 +19,6 @@
package org.wso2.carbon.device.application.mgt.core.dao;
import org.wso2.carbon.device.application.mgt.common.LifecycleState;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException;
import java.util.List;
@ -29,12 +28,51 @@ import java.util.List;
*/
public interface LifecycleStateDAO {
LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException;
/**
* To get the latest lifecycle state for the given application release id.
* @param applicationReleaseId id of the application release.
*
* @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException;
/**
* To get the latest lifecycle state for the given application id and the application release UUID.
* @param appId id of the application.
* @param uuid UUID of the application release
*
* @return Latest Lifecycle State for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException;
/**
* To get all changed lifecycle states for the given application release id.
* @param appReleaseId id of the application release.
*
* @return Lifecycle States for the given application release
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException;
void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException;
/**
* To add new lifecycle states for the given application release.
* @param uuid Id of the application release.
* @param appId Id of the application.
* @param state LifecycleState.
* @param tenantId Tenant id
*
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
void addLifecycleState(LifecycleState state, int appId, String uuid, int tenantId)
throws LifeCycleManagementDAOException;
/**
* To delete a specific lifecycle state for application release.
* @param identifier Id of the LifecycleState.
*
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/
void deleteLifecycleState(int identifier) throws LifeCycleManagementDAOException;
}

@ -433,7 +433,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
}
@Override
public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException {
public boolean verifyApplicationExistenceById(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting application with the application ID(" + appId + " ) from the database");
}
@ -443,13 +443,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try {
conn = this.getDBConnection();
String sql =
"SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY "
+ "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE, AP_APP_TAG.TAG AS TAG, "
+ "AP_UNRESTRICTED_ROLE.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE "
+ "WHERE AP_APP.ID = ?;";
"SELECT AP_APP.ID AS APP_ID FROM AP_APP WHERE AP_APP.ID = ? AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery();
if (log.isDebugEnabled()) {

@ -43,7 +43,6 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
private static final Log log = LogFactory.getLog(GenericApplicationReleaseDAOImpl.class);
/**
* To insert the Application Release Details.
*
@ -52,20 +51,18 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public ApplicationRelease createRelease(ApplicationRelease applicationRelease, int appId, int tenantId) throws
ApplicationManagementDAOException {
@Override public ApplicationRelease createRelease(ApplicationRelease applicationRelease, int appId, int tenantId)
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
ResultSet resultSet = null;
String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE,"
+ "STORED_LOCATION, BANNER_LOCATION, SC_1_LOCATION,SC_2_LOCATION,SC_3_LOCATION, APP_HASH_VALUE,"
+ "SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
+ "SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
int index = 0;
String generatedColumns[] = {"ID"};
String generatedColumns[] = { "ID" };
try {
connection = this.getDBConnection();
statement = connection.prepareStatement(sql, generatedColumns);
@ -95,7 +92,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
"SQL Exception while trying to release an application by executing the query " + sql, e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Database Connection Exception while trying to release a new version" , e);
"Database Connection Exception while trying to release a new version", e);
} finally {
Util.cleanupResources(statement, resultSet);
}
@ -111,8 +108,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public ApplicationRelease getRelease(String applicationName, String applicationType, String versionName,
@Override public ApplicationRelease getRelease(String applicationName, String applicationType, String versionName,
String releaseType, int tenantId) throws ApplicationManagementDAOException {
Connection connection;
@ -144,11 +140,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
return null;
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Database connection exception while trying to get the "
+ "release details of the application with " + applicationName + " and version " +
versionName, e);
+ "release details of the application with " + applicationName + " and version " + versionName, e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error while getting release details of the application " +
applicationName + " and version " + versionName + " , while executing the query " + sql, e);
throw new ApplicationManagementDAOException(
"Error while getting release details of the application " + applicationName + " and version " + versionName + " , while executing the query " + sql, e);
} finally {
Util.cleanupResources(statement, resultSet);
}
@ -162,21 +157,21 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws
ApplicationManagementDAOException {
@Override public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId)
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
ResultSet resultSet = null;
String sql = "SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE,"
+ " AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, "
+ "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.APP_HASH_VALUE AS " +
"HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, AR" +
".PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AL.CURRENT_STATE, AL.PREVIOUS_STATE, AL.UPDATED_BY, " +
"AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE " +
"AR.AP_APP_ID = ? AND AR.UUID = ? AND AR.TENANT_ID = ? AND AL.AP_APP_RELEASE_ID=AR.ID AND " +
"AL.TENANT_ID = AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;";
String sql =
"SELECT AR.ID AS RELEASE_ID, AR.VERSION AS RELEASE_VERSION, AR.UUID, AR.RELEASE_TYPE, AR.APP_PRICE, "
+ "AR.STORED_LOCATION, AR.BANNER_LOCATION, AR.SC_1_LOCATION AS SCREEN_SHOT_1, "
+ "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.PACKAGE_NAME AS "
+ "PACKAGE_NAME, AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, "
+ "AR.APP_META_INFO AS APP_META_INFO, AR.RATING AS RATING, AL.CURRENT_STATE, AL.PREVIOUS_STATE, "
+ "AL.UPDATED_BY, AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL "
+ "WHERE AR.AP_APP_ID = ? AND AR.UUID = ? AND AR.TENANT_ID = ? AND AL.AP_APP_RELEASE_ID=AR.ID "
+ "AND AL.TENANT_ID = AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;";
try {
connection = this.getDBConnection();
@ -192,14 +187,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
return null;
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Database connection exception while trying to get the release details of the " +
"application id: " + applicationId + "and UUID of the application release: " +
releaseUuid, e);
"Database connection exception while trying to get the release details of the " + "application id: "
+ applicationId + "and UUID of the application release: " + releaseUuid, e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"Error while getting release details of the application id: " + applicationId +
" and theUUID of the application " +
"release: " + releaseUuid + " , while executing the query " + sql, e);
"Error while getting release details of the application id: " + applicationId
+ " and theUUID of the application " + "release: " + releaseUuid + " , while executing the query " + sql, e);
} finally {
Util.cleanupResources(statement, resultSet);
}
@ -212,8 +205,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public List<ApplicationRelease> getReleases(int applicationId, int tenantId)
@Override public List<ApplicationRelease> getReleases(int applicationId, int tenantId)
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
@ -258,8 +250,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param rating given stars for the application release.
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public void updateRatingValue(String uuid, double rating, int ratedUsers) throws ApplicationManagementDAOException {
@Override public void updateRatingValue(String uuid, double rating, int ratedUsers)
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
String sql = "UPDATE AP_APP_RELEASE SET RATING = ?,RATED_USERS = ? WHERE UUID = ?;";
@ -287,8 +279,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param uuid UUID of the application Release.
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException {
@Override public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
ResultSet resultSet = null;
@ -301,7 +292,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
statement.setInt(2, tenantId);
resultSet = statement.executeQuery();
if (resultSet.next()){
if (resultSet.next()) {
rating = new Rating();
rating.setRatingValue(resultSet.getDouble("RATING"));
rating.setNoOfUsers(resultSet.getInt("RATED_USERS"));
@ -324,8 +315,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param applicationRelease Application Release the properties of which that need to be inserted.
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public ApplicationRelease updateRelease(int applicationId, ApplicationRelease applicationRelease, int tenantId)
@Override public ApplicationRelease updateRelease(int applicationId, ApplicationRelease applicationRelease, int tenantId)
throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
@ -349,7 +339,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
statement.setString(11, applicationRelease.getAppHashValue());
statement.setInt(12, applicationRelease.getIsSharedWithAllTenants());
statement.setString(13, applicationRelease.getMetaData());
statement.setInt(14,applicationId);
statement.setInt(14, applicationId);
statement.setInt(15, tenantId);
statement.setInt(16, applicationRelease.getId());
@ -373,8 +363,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param version version name of the application release.
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public void deleteRelease(int id, String version) throws ApplicationManagementDAOException {
@Override public void deleteRelease(int id, String version) throws ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
String sql = "DELETE FROM AP_APP_RELEASE WHERE ID = ? AND VERSION = ?";
@ -396,8 +385,46 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
}
}
@Override
public boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Verifying application release existence by application id:" + appId
+ " and application release uuid: " + uuid);
}
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getDBConnection();
String sql =
"SELECT AR.ID AS RELEASE_ID FROM AP_APP_RELEASE AS AR WHERE AR.AP_APP_ID = ? AND AR.UUID = ? AND "
+ "AR.TENANT_ID = ?;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);
stmt.setString(2, uuid);
stmt.setInt(3, tenantId);
rs = stmt.executeQuery();
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved basic details of the application release with the application ID "
+ appId + " Application release uuid: " + uuid);
}
return rs.next();
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"Error occurred while getting application release details with app ID: " + appId
+ " App release uuid: " + uuid + " While executing query ", e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
/**
* This method is capable to construct {@link ApplicationRelease} and return the object
*
* @param resultSet result set obtained from the query executing.
* @throws SQLException SQL exception while accessing result set data.
*/
@ -418,8 +445,6 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED"));
applicationRelease.setMetaData(resultSet.getString("APP_META_INFO"));
applicationRelease.setRating(resultSet.getDouble("RATING"));
return applicationRelease;
}
}

@ -23,7 +23,6 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util;
import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.LifeCycleManagementDAOException;
import java.sql.Connection;
@ -39,7 +38,7 @@ import java.util.List;
public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO {
@Override
public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws ApplicationManagementDAOException {
public LifecycleState getLatestLifeCycleStateByReleaseID(int applicationReleaseId) throws LifeCycleManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
@ -52,28 +51,44 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
stmt = conn.prepareStatement(sql);
stmt.setInt(1, applicationReleaseId);
rs = stmt.executeQuery();
LifecycleState lifecycleState = null;
if (rs.next()) {
lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
return constructLifecycle(rs);
} catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest"
+ " lifecycle state for a specific application", e);
} finally {
Util.cleanupResources(stmt, rs);
}
return lifecycleState;
}
public LifecycleState getLatestLifeCycleState(int appId, String uuid) throws LifeCycleManagementDAOException{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = this.getDBConnection();
String sql = "SELECT ID, CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_AT, UPDATED_BY FROM "
+ "AP_APP_LIFECYCLE_STATE WHERE AP_APP_ID=? AND AP_APP_RELEASE_ID=(SELECT ID FROM AP_APP_RELEASE "
+ "WHERE UUID=?) ORDER BY UPDATED_AT DESC;";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, appId);
stmt.setString(2, uuid);
rs = stmt.executeQuery();
return constructLifecycle(rs);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
throw new LifeCycleManagementDAOException("Error occurred while getting application List", e);
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection to get latest"
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection to get latest"
+ " lifecycle state for a specific application", e);
} finally {
Util.cleanupResources(stmt, rs);
}
}
@Override
public List<LifecycleState> getLifecycleStates(int appReleaseId) throws LifeCycleManagementDAOException {
List<LifecycleState> lifecycleStates = new ArrayList<>();
@ -108,19 +123,19 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
}
@Override
public void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) throws LifeCycleManagementDAOException {
public void addLifecycleState(LifecycleState state, int appId, String uuid, int tenantId) throws LifeCycleManagementDAOException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = this.getDBConnection();
String sql = "INSERT INTO AP_APP_LIFECYCLE_STATE (CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_BY, "
+ "AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?,?, ?, ?,?,?);";
+ "AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?,?, ?, ?,(SELECT ID FROM AP_APP_RELEASE WHERE UUID=?),?);";
stmt = conn.prepareStatement(sql);
stmt.setString(1, state.getCurrentState());
stmt.setString(2, state.getPreviousState());
stmt.setString(1, state.getCurrentState().toUpperCase());
stmt.setString(2, state.getPreviousState().toUpperCase());
stmt.setInt(3, tenantId);
stmt.setString(4, state.getUpdatedBy());
stmt.setInt(5, releaseId);
stmt.setString(5, uuid);
stmt.setInt(6, appId);
stmt.executeUpdate();
@ -153,4 +168,22 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
Util.cleanupResources(stmt, rs);
}
}
private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException {
LifecycleState lifecycleState = null;
try {
if (rs !=null && rs.next()) {
lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
lifecycleState.setPreviousState(rs.getString("PREVIOUS_STATE"));
lifecycleState.setUpdatedAt(rs.getTimestamp("UPDATED_AT"));
lifecycleState.setUpdatedBy(rs.getString("UPDATED_BY"));
}
} catch (SQLException e) {
throw new LifeCycleManagementDAOException(
"Error occurred while construct lifecycle state by retrieving data from SQL query", e);
}
return lifecycleState;
}
}

@ -50,7 +50,6 @@ import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManger;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
@ -67,7 +66,6 @@ import java.util.List;
public class ApplicationManagerImpl implements ApplicationManager {
private static final Log log = LogFactory.getLog(ApplicationManagerImpl.class);
private DeviceTypeDAO deviceTypeDAO;
private VisibilityDAO visibilityDAO;
private ApplicationDAO applicationDAO;
private ApplicationReleaseDAO applicationReleaseDAO;
@ -81,7 +79,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
private void initDataAccessObjects() {
this.deviceTypeDAO = ApplicationManagementDAOFactory.getDeviceTypeDAO();
this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO();
this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
this.lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
@ -97,7 +94,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName);
}
ConnectionManagerUtil.openDBConnection();
validateAppCreatingRequest(application);
validateAppReleasePayload(application.getApplicationReleases().get(0));
@ -164,8 +160,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false,
applicationRelease.getId());
changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false);
applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease);
@ -249,7 +244,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true, 0);
changeLifecycleState(application.getId(), applicationRelease.getUuid(), lifecycleState, true);
ConnectionManagerUtil.commitDBTransaction();
return applicationRelease;
@ -283,8 +278,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<ApplicationRelease> applicationReleases;
try {
ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplicationById(id, tenantId);
application = this.applicationDAO.getApplicationById(id, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationReleases = getReleases(application, requirePublishedReleases);
application.setApplicationReleases(applicationReleases);
@ -347,8 +341,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<ApplicationRelease> applicationReleases;
try {
ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplication(appName, appType, tenantId);
application = this.applicationDAO.getApplication(appName, appType, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationReleases = getReleases(application, false);
application.setApplicationReleases(applicationReleases);
@ -385,8 +378,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
Application application;
try {
ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplicationByRelease(appReleaseUUID, tenantId);
application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId);
if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(),
userName)) {
@ -401,11 +393,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException {
public boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException {
try {
Boolean isAppExist;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
boolean isAppExist;
ConnectionManagerUtil.openDBConnection();
isAppExist = ApplicationManagementDAOFactory.getApplicationDAO().verifyApplicationExistenceById(appId);
isAppExist = this.applicationDAO.verifyApplicationExistenceById(appId, tenantId);
return isAppExist;
} finally {
ConnectionManagerUtil.closeDBConnection();
@ -422,35 +415,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
// todo
// public List<ApplicationRelease> getinstallableReleases(int applicationId) throws ApplicationManagementException {
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
//
// Application application = getApplicationIfAccessible(applicationId);
// List<ApplicationRelease> applicationReleases;
// List<ApplicationRelease> filteredApplicationReleases = new ArrayList<>();
// if (log.isDebugEnabled()) {
// log.debug("Request is received to retrieve all the releases related with the application " + application
// .toString());
// }
// ConnectionManagerUtil.getDBConnection();
// applicationReleases = this.applicationReleaseDAO.getReleases(application.getName(), application.getType(), tenantId);
// for (ApplicationRelease applicationRelease : applicationReleases) {
// LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO().
// getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
// if (lifecycleState != null) {
// applicationRelease.setLifecycleState(lifecycleState);
//
// if (!AppLifecycleState.REMOVED.toString()
// .equals(applicationRelease.getLifecycleState().getCurrentState())) {
// filteredApplicationReleases.add(applicationRelease);
// }
// }
// }
// return filteredApplicationReleases;
//
// }
private List<ApplicationRelease> getReleases(Application application, boolean requirePublishedRelease)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -463,8 +427,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.getDBConnection();
applicationReleases = this.applicationReleaseDAO.getReleases(application.getId(), tenantId);
for (ApplicationRelease applicationRelease : applicationReleases) {
LifecycleState lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO().
LifecycleState lifecycleState = null;
try {
lifecycleState = ApplicationManagementDAOFactory.getLifecycleStateDAO().
getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
} catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException(
"Error occurred while getting the latest lifecycle state for the application release UUID: "
+ applicationRelease.getUuid(), e);
}
if (lifecycleState != null) {
applicationRelease.setLifecycleState(lifecycleState);
@ -520,11 +491,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0);
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true);
storedLocations.add(applicationRelease.getAppHashValue());
}
ConnectionManagerUtil.openDBConnection();
ApplicationManagementDAOFactory.getApplicationDAO().deleteApplication(applicationId);
this.applicationDAO.deleteApplication(applicationId);
} catch (UserStoreException e) {
String msg = "Error occured while check whether current user has the permission to delete an application";
log.error(msg);
@ -551,7 +522,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0);
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true);
}else{
throw new ApplicationManagementException("Can't delete the application release, You have to move the " +
"lifecycle state from "+ currentState + " to acceptable " +
@ -658,8 +629,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
Application application;
boolean isAppAllowed = false;
try {
application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplicationById(applicationId, tenantId);
application = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
return application;
}
@ -785,24 +755,22 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public LifecycleState getLifecycleState(int applicationId, String applicationUuid) throws
public LifecycleState getLifecycleState(int applicationId, String releaseUuid) throws
ApplicationManagementException {
LifecycleState lifecycleState;
try {
ConnectionManagerUtil.openDBConnection();
lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationId);
lifecycleState = this.lifecycleStateDAO.getLatestLifeCycleState(applicationId, releaseUuid);
if (lifecycleState == null) {
throw new NotFoundException(
"Couldn't find the lifecycle data for appid: " + applicationId + " and app release UUID: "
+ applicationUuid);
+ releaseUuid);
}
lifecycleState.setNextStates(new ArrayList<>(lifecycleStateManger.
lifecycleState.setNextStates(new ArrayList<>(getLifecycleManagementService().
getNextLifecycleStates(lifecycleState.getCurrentState())));
} catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException("Failed to get lifecycle state", e);
} catch (ApplicationManagementException e) {
throw new ApplicationManagementException("Failed to get application and application management", e);
} catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException("Failed to get lifecycle state from database", e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
@ -810,34 +778,48 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
@Override
public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean
checkExist, int releaseId) throws ApplicationManagementException {
public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist)
throws ApplicationManagementException {
try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (checkExist) {
getApplicationIfAccessible(applicationId);
ConnectionManagerUtil.openDBConnection();
if (!this.applicationDAO.verifyApplicationExistenceById(applicationId, tenantId)){
throw new NotFoundException(
"Couldn't found application for the application Id: " + applicationId);
}
if (releaseId < 1) {
releaseId = getAppReleaseIfExists(applicationId, applicationUuid).getId();
if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)){
throw new NotFoundException(
"Couldn't found application release for the application Id: " + applicationId
+ " application release uuid: " + releaseUuid);
}
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
}
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null) {
if (getLifecycleManagementService().isValidStateChange(state.getPreviousState(), state.getCurrentState())) {
this.lifecycleStateDAO
.addLifecycleState(state, applicationId, releaseId, tenantId);
if (getLifecycleManagementService()
.isValidStateChange(state.getPreviousState(), state.getCurrentState())) {
ConnectionManagerUtil.beginDBTransaction();
//todo if current state of the adding lifecycle state is PUBLISHED, need to check whether is there
//todo any other application release in PUBLISHED state for the application( i.e for the appid)
this.lifecycleStateDAO.addLifecycleState(state, applicationId, releaseUuid, tenantId);
ConnectionManagerUtil.commitDBTransaction();
} else {
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'"
+ " to '" + state.getCurrentState() + "'");
throw new ApplicationManagementException("Lifecycle State Validation failed");
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '"
+ state.getCurrentState() + "'");
throw new ApplicationManagementException("Lifecycle State Validation failed. Application Id: " +
applicationId + " Application release UUID: " + releaseUuid);
}
}
} catch (LifeCycleManagementDAOException | DBConnectionException e) {
throw new ApplicationManagementException("Failed to add lifecycle state", e);
} catch (ApplicationManagementException e) {
throw new ApplicationManagementException("Lifecycle State Validation failed", e);
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new ApplicationManagementException(
"Failed to add lifecycle state. Application Id: " + applicationId + " Application release UUID: "
+ releaseUuid, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}

@ -18,12 +18,16 @@ public class LifecycleStateManger {
public LifecycleStateManger(List<LifecycleState> states) {
lifecycleStates = new HashMap<>();
for (LifecycleState s : states) {
lifecycleStates.put(s.getName(), new State(s.getName(), s.getProceedingStates()));
if (s.getProceedingStates() != null) {
s.getProceedingStates().replaceAll(String::toUpperCase);
}
lifecycleStates.put(s.getName().toUpperCase(), new State(s.getName().toUpperCase(), s.getProceedingStates()));
}
}
public Set<String> getNextLifecycleStates(String currentLifecycleState) {
return lifecycleStates.get(currentLifecycleState).getProceedingStates();
return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates();
}
public boolean isValidStateChange(String currentState, String nextState) {

@ -32,14 +32,14 @@ public class LifecycleManagementTest {
public void checkValidNextLifecycleState() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE);
Assert.assertTrue("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(NEXT_STATE));
proceedingStates.contains(NEXT_STATE.toUpperCase()));
}
@Test
public void checkInvalidNextLifecycleState() {
Set<String> proceedingStates = lifecycleStateManger.getNextLifecycleStates(CURRENT_STATE);
Assert.assertFalse("Invalid proceeding state of: " + CURRENT_STATE,
proceedingStates.contains(BOGUS_STATE));
proceedingStates.contains(BOGUS_STATE.toUpperCase()));
}
@Test

@ -529,6 +529,10 @@ public interface ApplicationManagementAPI {
code = 201,
message = "OK. \n Successfully add a lifecycle state.",
response = Application.class),
@ApiResponse(
code = 404,
message = "NOT FOUND. \n Error occurred while adding new lifecycle state.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred adding a lifecycle state.",

@ -414,7 +414,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@DELETE
@Path("/{appid}")
public Response deleteApplication(@PathParam("appid") int applicationId) {
public Response deleteApplication(
@PathParam("appid") int applicationId) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
try {
@ -435,7 +436,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@DELETE
@Path("/{appid}/{uuid}")
public Response deleteApplicationRelease(@PathParam("appid") int applicationId, @PathParam("uuid") String releaseUuid) {
public Response deleteApplicationRelease(
@PathParam("appid") int applicationId,
@PathParam("uuid") String releaseUuid) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
try {
@ -485,7 +488,12 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
LifecycleState state) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true, 0);
applicationManager.changeLifecycleState(applicationId, applicationUuid, state, true);
} catch (NotFoundException e) {
String msg = "Could,t find application release for application id: " + applicationId
+ " and application release uuid: " + applicationUuid;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).build();
} catch (ApplicationManagementException e) {
String msg = "Error occurred while adding lifecycle state.";
log.error(msg, e);

Loading…
Cancel
Save