Fix lifecycle adding issue

feature/appm-store/pbac
lasanthaDLPDS 6 years ago
parent b283c1dfde
commit 2c9ea2b5cf

@ -112,7 +112,7 @@ public interface ApplicationManager {
* @return the boolean value, whether application exist or not * @return the boolean value, whether application exist or not
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException; boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException;
/** /**
* To get Application with the given UUID. * To get Application with the given UUID.
@ -136,14 +136,13 @@ public interface ApplicationManager {
* To get all the releases of a particular Application. * To get all the releases of a particular Application.
* *
* @param applicationId ID of the 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 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 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. * @throws ApplicationManagementException Application Management Exception.
*/ */
void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean checkExist, void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist)
int releaseId) throws ApplicationManagementException; throws ApplicationManagementException;
/** /**
* Get the application if application is an accessible one. * Get the application if application is an accessible one.

@ -109,10 +109,11 @@ public interface ApplicationDAO {
* To get the application with the given uuid * To get the application with the given uuid
* *
* @param appId ID of the application * @param appId ID of the application
* @param tenantId Tenant Id
* @return the boolean value * @return the boolean value
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @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 * 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 ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws
ApplicationManagementDAOException; ApplicationManagementDAOException;
/**
* To verify whether application release exist or not.
*
* @param appId ID of the application.
* @param uuid UUID of the application release.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
boolean verifyReleaseExistence(int appId, String uuid, int tenantId) throws ApplicationManagementDAOException;
} }

@ -58,14 +58,14 @@ public interface LifecycleStateDAO {
/** /**
* To add new lifecycle states for the given application release. * To add new lifecycle states for the given application release.
* @param releaseId Id of the application release. * @param uuid Id of the application release.
* @param appId Id of the application. * @param appId Id of the application.
* @param state LifecycleState. * @param state LifecycleState.
* @param tenantId Tenant id * @param tenantId Tenant id
* *
* @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception. * @throws LifeCycleManagementDAOException Lifecycle Management DAO Exception.
*/ */
void addLifecycleState(LifecycleState state, int appId, int releaseId, int tenantId) void addLifecycleState(LifecycleState state, int appId, String uuid, int tenantId)
throws LifeCycleManagementDAOException; throws LifeCycleManagementDAOException;
/** /**

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

@ -43,7 +43,6 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
private static final Log log = LogFactory.getLog(GenericApplicationReleaseDAOImpl.class); private static final Log log = LogFactory.getLog(GenericApplicationReleaseDAOImpl.class);
/** /**
* To insert the Application Release Details. * To insert the Application Release Details.
* *
@ -52,17 +51,15 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id * @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public ApplicationRelease createRelease(ApplicationRelease applicationRelease, int appId, int tenantId)
public ApplicationRelease createRelease(ApplicationRelease applicationRelease, int appId, int tenantId) throws throws ApplicationManagementDAOException {
ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
String sql = "INSERT INTO AP_APP_RELEASE (VERSION,TENANT_ID,UUID,RELEASE_TYPE, PACKAGE_NAME, APP_PRICE," 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," + "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) " + "SHARED_WITH_ALL_TENANTS, APP_META_INFO,AP_APP_ID) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
int index = 0; int index = 0;
String generatedColumns[] = { "ID" }; String generatedColumns[] = { "ID" };
@ -111,8 +108,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id * @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public ApplicationRelease getRelease(String applicationName, String applicationType, String versionName,
public ApplicationRelease getRelease(String applicationName, String applicationType, String versionName,
String releaseType, int tenantId) throws ApplicationManagementDAOException { String releaseType, int tenantId) throws ApplicationManagementDAOException {
Connection connection; Connection connection;
@ -144,11 +140,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
return null; return null;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Database connection exception while trying to get the " throw new ApplicationManagementDAOException("Database connection exception while trying to get the "
+ "release details of the application with " + applicationName + " and version " + + "release details of the application with " + applicationName + " and version " + versionName, e);
versionName, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error while getting release details of the application " + throw new ApplicationManagementDAOException(
applicationName + " and version " + versionName + " , while executing the query " + sql, e); "Error while getting release details of the application " + applicationName + " and version " + versionName + " , while executing the query " + sql, e);
} finally { } finally {
Util.cleanupResources(statement, resultSet); Util.cleanupResources(statement, resultSet);
} }
@ -162,21 +157,21 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id * @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId)
public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws throws ApplicationManagementDAOException {
ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = 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," 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.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 " + + "AR.SC_2_LOCATION AS SCREEN_SHOT_2, AR.SC_3_LOCATION AS SCREEN_SHOT_3, AR.PACKAGE_NAME AS "
"HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, AR.APP_META_INFO, AR.CREATED_BY, AR.CREATED_AT, AR" + + "PACKAGE_NAME, AR.APP_HASH_VALUE AS HASH_VALUE, AR.SHARED_WITH_ALL_TENANTS AS SHARED, "
".PUBLISHED_BY, AR.PUBLISHED_AT, AR.STARS, AL.CURRENT_STATE, AL.PREVIOUS_STATE, AL.UPDATED_BY, " + + "AR.APP_META_INFO AS APP_META_INFO, AR.RATING AS RATING, AL.CURRENT_STATE, AL.PREVIOUS_STATE, "
"AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL WHERE " + + "AL.UPDATED_BY, AL.UPDATED_AT FROM AP_APP_RELEASE AS AR, AP_APP_LIFECYCLE_STATE AS AL "
"AR.AP_APP_ID = ? AND AR.UUID = ? AND AR.TENANT_ID = ? AND AL.AP_APP_RELEASE_ID=AR.ID AND " + + "WHERE AR.AP_APP_ID = ? AND AR.UUID = ? AND AR.TENANT_ID = ? AND AL.AP_APP_RELEASE_ID=AR.ID "
"AL.TENANT_ID = AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;"; + "AND AL.TENANT_ID = AR.TENANT_ID ORDER BY AL.UPDATED_AT DESC;";
try { try {
connection = this.getDBConnection(); connection = this.getDBConnection();
@ -192,14 +187,12 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
return null; return null;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( throw new ApplicationManagementDAOException(
"Database connection exception while trying to get the release details of the " + "Database connection exception while trying to get the release details of the " + "application id: "
"application id: " + applicationId + "and UUID of the application release: " + + applicationId + "and UUID of the application release: " + releaseUuid, e);
releaseUuid, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException( throw new ApplicationManagementDAOException(
"Error while getting release details of the application id: " + applicationId + "Error while getting release details of the application id: " + applicationId
" and theUUID of the application " + + " and theUUID of the application " + "release: " + releaseUuid + " , while executing the query " + sql, e);
"release: " + releaseUuid + " , while executing the query " + sql, e);
} finally { } finally {
Util.cleanupResources(statement, resultSet); Util.cleanupResources(statement, resultSet);
} }
@ -212,8 +205,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param tenantId Tenant Id * @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public List<ApplicationRelease> getReleases(int applicationId, int tenantId)
public List<ApplicationRelease> getReleases(int applicationId, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
@ -258,8 +250,8 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param rating given stars for the application release. * @param rating given stars for the application release.
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public void updateRatingValue(String uuid, double rating, int ratedUsers)
public void updateRatingValue(String uuid, double rating, int ratedUsers) throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
String sql = "UPDATE AP_APP_RELEASE SET RATING = ?,RATED_USERS = ? WHERE UUID = ?;"; 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. * @param uuid UUID of the application Release.
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException {
public Rating getRating(String uuid, int tenantId) throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -324,8 +315,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param applicationRelease Application Release the properties of which that need to be inserted. * @param applicationRelease Application Release the properties of which that need to be inserted.
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public ApplicationRelease updateRelease(int applicationId, ApplicationRelease applicationRelease, int tenantId)
public ApplicationRelease updateRelease(int applicationId, ApplicationRelease applicationRelease, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
@ -373,8 +363,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
* @param version version name of the application release. * @param version version name of the application release.
* @throws ApplicationManagementDAOException Application Management DAO Exception. * @throws ApplicationManagementDAOException Application Management DAO Exception.
*/ */
@Override @Override public void deleteRelease(int id, String version) throws ApplicationManagementDAOException {
public void deleteRelease(int id, String version) throws ApplicationManagementDAOException {
Connection connection; Connection connection;
PreparedStatement statement = null; PreparedStatement statement = null;
String sql = "DELETE FROM AP_APP_RELEASE WHERE ID = ? AND VERSION = ?"; 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 * This method is capable to construct {@link ApplicationRelease} and return the object
*
* @param resultSet result set obtained from the query executing. * @param resultSet result set obtained from the query executing.
* @throws SQLException SQL exception while accessing result set data. * @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.setIsSharedWithAllTenants(resultSet.getInt("SHARED"));
applicationRelease.setMetaData(resultSet.getString("APP_META_INFO")); applicationRelease.setMetaData(resultSet.getString("APP_META_INFO"));
applicationRelease.setRating(resultSet.getDouble("RATING")); applicationRelease.setRating(resultSet.getDouble("RATING"));
return applicationRelease; return applicationRelease;
} }
} }

@ -123,19 +123,19 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
} }
@Override @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; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getDBConnection(); conn = this.getDBConnection();
String sql = "INSERT INTO AP_APP_LIFECYCLE_STATE (CURRENT_STATE, PREVIOUS_STATE, TENANT_ID, UPDATED_BY, " 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 = conn.prepareStatement(sql);
stmt.setString(1, state.getCurrentState().toUpperCase()); stmt.setString(1, state.getCurrentState().toUpperCase());
stmt.setString(2, state.getPreviousState().toUpperCase()); stmt.setString(2, state.getPreviousState().toUpperCase());
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.setString(4, state.getUpdatedBy()); stmt.setString(4, state.getUpdatedBy());
stmt.setInt(5, releaseId); stmt.setString(5, uuid);
stmt.setInt(6, appId); stmt.setInt(6, appId);
stmt.executeUpdate(); stmt.executeUpdate();
@ -172,7 +172,7 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { private LifecycleState constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException {
LifecycleState lifecycleState = null; LifecycleState lifecycleState = null;
try { try {
if (rs.next()) { if (rs !=null && rs.next()) {
lifecycleState = new LifecycleState(); lifecycleState = new LifecycleState();
lifecycleState.setId(rs.getInt("ID")); lifecycleState.setId(rs.getInt("ID"));
lifecycleState.setCurrentState(rs.getString("CURRENT_STATE")); lifecycleState.setCurrentState(rs.getString("CURRENT_STATE"));
@ -185,6 +185,5 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
"Error occurred while construct lifecycle state by retrieving data from SQL query", e); "Error occurred while construct lifecycle state by retrieving data from SQL query", e);
} }
return lifecycleState; 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.lifecycle.LifecycleStateManger;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreException;
@ -95,7 +94,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName); log.debug("Create Application received for the tenant : " + tenantId + " From" + " the user : " + userName);
} }
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
validateAppCreatingRequest(application); validateAppCreatingRequest(application);
validateAppReleasePayload(application.getApplicationReleases().get(0)); validateAppReleasePayload(application.getApplicationReleases().get(0));
@ -162,8 +160,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString()); lifecycleState.setPreviousState(AppLifecycleState.CREATED.toString());
changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false, changeLifecycleState(appId, applicationRelease.getUuid(), lifecycleState, false);
applicationRelease.getId());
applicationRelease.setLifecycleState(lifecycleState); applicationRelease.setLifecycleState(lifecycleState);
applicationReleases.add(applicationRelease); applicationReleases.add(applicationRelease);
@ -247,7 +244,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState lifecycleState = new LifecycleState(); LifecycleState lifecycleState = new LifecycleState();
lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString()); lifecycleState.setCurrentState(AppLifecycleState.CREATED.toString());
lifecycleState.setPreviousState(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(); ConnectionManagerUtil.commitDBTransaction();
return applicationRelease; return applicationRelease;
@ -281,8 +278,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<ApplicationRelease> applicationReleases; List<ApplicationRelease> applicationReleases;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO() application = this.applicationDAO.getApplicationById(id, tenantId);
.getApplicationById(id, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationReleases = getReleases(application, requirePublishedReleases); applicationReleases = getReleases(application, requirePublishedReleases);
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(applicationReleases);
@ -345,8 +341,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
List<ApplicationRelease> applicationReleases; List<ApplicationRelease> applicationReleases;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO() application = this.applicationDAO.getApplication(appName, appType, tenantId);
.getApplication(appName, appType, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
applicationReleases = getReleases(application, false); applicationReleases = getReleases(application, false);
application.setApplicationReleases(applicationReleases); application.setApplicationReleases(applicationReleases);
@ -383,8 +378,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
Application application; Application application;
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO() application = this.applicationDAO.getApplicationByRelease(appReleaseUUID, tenantId);
.getApplicationByRelease(appReleaseUUID, tenantId);
if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(), if (application.getUnrestrictedRoles().isEmpty() || isRoleExists(application.getUnrestrictedRoles(),
userName)) { userName)) {
@ -399,11 +393,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
} }
public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException { public boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementException {
try { try {
Boolean isAppExist; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
boolean isAppExist;
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
isAppExist = ApplicationManagementDAOFactory.getApplicationDAO().verifyApplicationExistenceById(appId); isAppExist = this.applicationDAO.verifyApplicationExistenceById(appId, tenantId);
return isAppExist; return isAppExist;
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
@ -496,11 +491,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState(); LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0); changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true);
storedLocations.add(applicationRelease.getAppHashValue()); storedLocations.add(applicationRelease.getAppHashValue());
} }
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
ApplicationManagementDAOFactory.getApplicationDAO().deleteApplication(applicationId); this.applicationDAO.deleteApplication(applicationId);
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occured while check whether current user has the permission to delete an application"; String msg = "Error occured while check whether current user has the permission to delete an application";
log.error(msg); log.error(msg);
@ -527,7 +522,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
LifecycleState newAppLifecycleState = new LifecycleState(); LifecycleState newAppLifecycleState = new LifecycleState();
newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState()); newAppLifecycleState.setPreviousState(appLifecycleState.getCurrentState());
newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString()); newAppLifecycleState.setCurrentState(AppLifecycleState.REMOVED.toString());
changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true, 0); changeLifecycleState(applicationId, applicationRelease.getUuid(), newAppLifecycleState, true);
}else{ }else{
throw new ApplicationManagementException("Can't delete the application release, You have to move the " + throw new ApplicationManagementException("Can't delete the application release, You have to move the " +
"lifecycle state from "+ currentState + " to acceptable " + "lifecycle state from "+ currentState + " to acceptable " +
@ -634,8 +629,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
Application application; Application application;
boolean isAppAllowed = false; boolean isAppAllowed = false;
try { try {
application = ApplicationManagementDAOFactory.getApplicationDAO() application = this.applicationDAO.getApplicationById(applicationId, tenantId);
.getApplicationById(applicationId, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
return application; return application;
} }
@ -784,34 +778,48 @@ public class ApplicationManagerImpl implements ApplicationManager {
} }
@Override @Override
public void changeLifecycleState(int applicationId, String applicationUuid, LifecycleState state, Boolean public void changeLifecycleState(int applicationId, String releaseUuid, LifecycleState state, Boolean checkExist)
checkExist, int releaseId) throws ApplicationManagementException { throws ApplicationManagementException {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (checkExist) { 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) { if (!this.applicationReleaseDAO.verifyReleaseExistence(applicationId, releaseUuid, tenantId)){
releaseId = getAppReleaseIfExists(applicationId, applicationUuid).getId(); 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(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
state.setUpdatedBy(userName); state.setUpdatedBy(userName);
if (state.getCurrentState() != null && state.getPreviousState() != null) { if (state.getCurrentState() != null && state.getPreviousState() != null) {
if (getLifecycleManagementService()
if (getLifecycleManagementService().isValidStateChange(state.getPreviousState(), state.getCurrentState())) { .isValidStateChange(state.getPreviousState(), state.getCurrentState())) {
this.lifecycleStateDAO ConnectionManagerUtil.beginDBTransaction();
.addLifecycleState(state, applicationId, releaseId, 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 { } else {
log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" log.error("Invalid lifecycle state transition from '" + state.getPreviousState() + "'" + " to '"
+ " to '" + state.getCurrentState() + "'"); + state.getCurrentState() + "'");
throw new ApplicationManagementException("Lifecycle State Validation failed"); throw new ApplicationManagementException("Lifecycle State Validation failed. Application Id: " +
applicationId + " Application release UUID: " + releaseUuid);
} }
} }
} catch (LifeCycleManagementDAOException | DBConnectionException e) { } catch (LifeCycleManagementDAOException e) {
throw new ApplicationManagementException("Failed to add lifecycle state", e); ConnectionManagerUtil.rollbackDBTransaction();
} catch (ApplicationManagementException e) { throw new ApplicationManagementException(
throw new ApplicationManagementException("Lifecycle State Validation failed", e); "Failed to add lifecycle state. Application Id: " + applicationId + " Application release UUID: "
+ releaseUuid, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
} }

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

@ -414,7 +414,8 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@DELETE @DELETE
@Path("/{appid}") @Path("/{appid}")
public Response deleteApplication(@PathParam("appid") int applicationId) { public Response deleteApplication(
@PathParam("appid") int applicationId) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
try { try {
@ -435,7 +436,9 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@DELETE @DELETE
@Path("/{appid}/{uuid}") @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(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
try { try {
@ -485,7 +488,12 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
LifecycleState state) { LifecycleState state) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
try { 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) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while adding lifecycle state."; String msg = "Error occurred while adding lifecycle state.";
log.error(msg, e); log.error(msg, e);

Loading…
Cancel
Save