Modify Application and Release validation methods

Ahis commit contaisns followings. Added a query to get application release when application id and release UUID is passed. Modified application validation method and removed unnecessary logic. Modified Application release validation method aand removed unnecessary logic. As a result of these modifications, modified relevant method calls as well
feature/appm-store/pbac
lasanthaDLPDS 7 years ago
parent 47a02dc903
commit d5b2bfd510

@ -98,15 +98,6 @@ public interface ApplicationManager {
*/
Application getApplication(String appType, String appName) throws ApplicationManagementException;
/**
* To get Application with the given UUID.
*
* @param applicationId Id of the Application
* @return the Application identified by the application id
* @throws ApplicationManagementException Application Management Exception.
*/
Application getApplicationById(int applicationId) throws ApplicationManagementException;
/**
* To get an application associated with the release.
*
@ -175,7 +166,8 @@ public interface ApplicationManager {
* @param releaseUuid UUID of the Application Release.
* @throws ApplicationManagementException Application Management Exception.
*/
ApplicationRelease validateApplicationRelease(String releaseUuid) throws ApplicationManagementException;
ApplicationRelease validateApplicationRelease(int applicationId, String releaseUuid) throws
ApplicationManagementException;
/**
* To update with a new release for an Application.
@ -209,12 +201,4 @@ public interface ApplicationManager {
ApplicationRelease createRelease(int applicationId, ApplicationRelease applicationRelease)
throws ApplicationManagementException;
/**
* To get the application release of the Application/
*
* @param applicationUuid UUID of the Application.
* @return ApplicationRelease related with particular Application UUID and version.
* @throws ApplicationManagementException ApplicationManagementException
*/
ApplicationRelease getReleaseByUuid(String applicationUuid) throws ApplicationManagementException;
}

@ -95,4 +95,15 @@ public interface ApplicationReleaseDAO {
*/
void deleteRelease(int id, String version) throws ApplicationManagementDAOException;
/**
* To get release details of a specific application.
*
* @param applicationId ID of the application.
* @param releaseUuid UUID of the application release.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws
ApplicationManagementDAOException;
}

@ -22,13 +22,11 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application.release
import org.wso2.carbon.device.application.mgt.common.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
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 java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -173,6 +171,80 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
}
}
/**
* To get release details of a specific application.
*
* @param applicationId ID of the application.
* @param releaseUuid UUID of the application release.
* @param tenantId Tenant Id
* @throws ApplicationManagementDAOException Application Management DAO Exception.
*/
@Override
public ApplicationRelease getReleaseByIds(int applicationId, String releaseUuid, int tenantId) throws
ApplicationManagementDAOException {
Connection connection;
PreparedStatement statement = null;
ResultSet resultSet = null;
ApplicationRelease applicationRelease = null;
String sql = "SELECT AR.ID AS RELESE_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.PREVIOUSE_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();
statement = connection.prepareStatement(sql);
statement.setInt(1, applicationId);
statement.setString(2, releaseUuid);
statement.setInt(3, tenantId);
resultSet = statement.executeQuery();
if (resultSet.next()) {
applicationRelease = new ApplicationRelease();
applicationRelease.setId(resultSet.getInt("RELEASE_ID"));
applicationRelease.setVersion(resultSet.getString("RELEASE_VERSION"));
applicationRelease.setUuid(resultSet.getString("UUID"));
applicationRelease.setReleaseType(resultSet.getString("RELEASE_TYPE"));
applicationRelease.setPrice(resultSet.getDouble("APP_PRICE"));
applicationRelease.setAppStoredLoc(resultSet.getString("STORED_LOCATION"));
applicationRelease.setBannerLoc(resultSet.getString("BANNER_LOCATION"));
applicationRelease.setScreenshotLoc1(resultSet.getString("SCREEN_SHOT_1"));
applicationRelease.setScreenshotLoc2(resultSet.getString("SCREEN_SHOT_2"));
applicationRelease.setScreenshotLoc3(resultSet.getString("SCREEN_SHOT_3"));
applicationRelease.setAppHashValue(resultSet.getString("HASH_VALUE"));
applicationRelease.setIsSharedWithAllTenants(resultSet.getInt("SHARED"));
applicationRelease.setMetaData(resultSet.getString("APP_META_INFO"));
applicationRelease.setApplicationCreator(resultSet.getString("CREATED_BY"));
applicationRelease.setCreatedAt(resultSet.getTimestamp("CREATED_AT"));
applicationRelease.setPublishedBy(resultSet.getString("PUBLISHED_BY"));
applicationRelease.setPublishedAt(resultSet.getTimestamp("PUBLISHED_AT"));
applicationRelease.setStars(resultSet.getInt("STARS"));
applicationRelease.setCurrentState(resultSet.getString("CURRENT_STATE"));
applicationRelease.setPreviousState(resultSet.getString("PREVIOUSE_STATE"));
applicationRelease.setStateModifiedBy(resultSet.getString("UPDATED_BY"));
applicationRelease.setStateModifiedAt(resultSet.getTimestamp("UPDATED_AT"));
}
return applicationRelease;
} 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);
} 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);
} finally {
Util.cleanupResources(statement, resultSet);
}
}
/**
* To insert the application release properties.
*

@ -212,11 +212,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
@Override
public ApplicationRelease getReleaseByUuid(String applicationUuid) throws ApplicationManagementException {
return null;
}
@Override
public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException {
try {
@ -293,40 +288,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
@Override
public Application getApplicationById(int applicationId) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application;
boolean isAppAllowed = false;
try {
ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplicationById(applicationId, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
return application;
}
if (!application.getUnrestrictedRoles().isEmpty()) {
if (isRoleExists(application.getUnrestrictedRoles(), userName)) {
isAppAllowed = true;
}
} else {
isAppAllowed = true;
}
if (!isAppAllowed) {
return null;
}
return application;
} catch (UserStoreException e) {
throw new ApplicationManagementException(
"User-store exception while getting application with the " + "application id " + applicationId, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public Application getApplicationByRelease(String appReleaseUUID) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -448,7 +409,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (application == null) {
throw new ApplicationManagementException("Invalid Application ID is received");
}
ApplicationRelease applicationRelease = validateApplicationRelease(releaseUuid);
ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, releaseUuid);
if (applicationRelease == null) {
throw new ApplicationManagementException("Invalid Application Release UUID is received");
}
@ -557,19 +518,44 @@ public class ApplicationManagerImpl implements ApplicationManager {
/**
* To validate the pre-request of the ApplicationRelease.
*
* @param applicationID ID of the Application.
* @param applicationId ID of the Application.
* @return Application related with the UUID
*/
public Application validateApplication(int applicationID) throws ApplicationManagementException {
if (applicationID <= 0) {
throw new ApplicationManagementException("Application UUID is null. Application UUID is a required "
+ "parameter to get the relevant application.");
public Application validateApplication(int applicationId) throws ApplicationManagementException {
if (applicationId <= 0) {
throw new ApplicationManagementException("Application id could,t be a negative integer. Hence please add " +
"valid application id.");
}
Application application = DataHolder.getInstance().getApplicationManager().getApplicationById(applicationID);
if (application == null) {
throw new NotFoundException("Application of the " + applicationID + " does not exist.");
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
Application application;
boolean isAppAllowed = false;
try {
ConnectionManagerUtil.openDBConnection();
application = ApplicationManagementDAOFactory.getApplicationDAO()
.getApplicationById(applicationId, tenantId);
if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) {
return application;
}
if (!application.getUnrestrictedRoles().isEmpty()) {
if (isRoleExists(application.getUnrestrictedRoles(), userName)) {
isAppAllowed = true;
}
} else {
isAppAllowed = true;
}
if (!isAppAllowed) {
throw new NotFoundException("Application of the " + applicationId + " does not exist.");
}
return application;
} catch (UserStoreException e) {
throw new ApplicationManagementException(
"User-store exception while getting application with the " + "application id " + applicationId, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
/**
@ -578,18 +564,33 @@ public class ApplicationManagerImpl implements ApplicationManager {
* @param applicationUuid UUID of the Application.
* @return Application related with the UUID
*/
public ApplicationRelease validateApplicationRelease(String applicationUuid) throws ApplicationManagementException {
public ApplicationRelease validateApplicationRelease(int applicationId, String applicationUuid) throws
ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationRelease applicationRelease;
try {
if (applicationId <= 0) {
throw new ApplicationManagementException(
"Application id could,t be a negative integer. Hence please add " +
"valid application id.");
}
if (applicationUuid == null) {
throw new ApplicationManagementException("Application UUID is null. Application UUID is a required "
+ "parameter to get the relevant application.");
}
ApplicationRelease applicationRelease = DataHolder.getInstance().getApplicationManager()
.getReleaseByUuid(applicationUuid);
ConnectionManagerUtil.openDBConnection();
applicationRelease = ApplicationManagementDAOFactory.getApplicationReleaseDAO().getReleaseByIds
(applicationId, applicationUuid, tenantId);
if (applicationRelease == null) {
throw new ApplicationManagementException(
"Application with UUID " + applicationUuid + " does not exist.");
throw new ApplicationManagementException("Doesn't exist a application release for application ID: " +
applicationId + "and application UUID: " +
applicationUuid);
}
return applicationRelease;
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
@ -672,8 +673,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
try {
ConnectionManagerUtil.openDBConnection();
LifecycleStateDAO lifecycleStateDAO = ApplicationManagementDAOFactory.getLifecycleStateDAO();
//todo applicationUuid and applicationId should be passed and util method has to be changed
ApplicationRelease applicationRelease = validateApplicationRelease(applicationUuid);
ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, applicationUuid);
lifecycleState = lifecycleStateDAO.getLatestLifeCycleStateByReleaseID(applicationRelease.getId());
lifecycleState.setNextStates(getNextLifecycleStates(lifecycleState.getCurrentState()));
} catch (ApplicationManagementDAOException e) {
@ -692,8 +692,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
try {
ConnectionManagerUtil.openDBConnection();
Application application = validateApplication(applicationId);
//todo applicationUuid and applicationId should be passed and util method has to be changed
ApplicationRelease applicationRelease = validateApplicationRelease(applicationUuid);
ApplicationRelease applicationRelease = validateApplicationRelease(applicationId, applicationUuid);
LifecycleStateDAO lifecycleStateDAO;
if (application != null) {

@ -222,7 +222,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
attachments.add(screenshot.getDataHandler().getInputStream());
}
}
applicationRelease = applicationManager.validateApplicationRelease(applicationUuid);
applicationRelease = applicationManager.validateApplicationRelease(appId, applicationUuid);
LifecycleState lifecycleState = applicationManager.getLifecycleState(appId, applicationRelease.getUuid());
if (AppLifecycleState.PUBLISHED.toString().equals(lifecycleState.getCurrentState()) ||
AppLifecycleState.DEPRECATED.toString().equals(lifecycleState.getCurrentState())) {
@ -273,7 +273,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
return Response.status(Response.Status.BAD_REQUEST)
.entity("Uploading artifacts for the application is failed " + applicationUuid).build();
}
applicationRelease = applicationManager.validateApplicationRelease(applicationUuid);
applicationRelease = applicationManager.validateApplicationRelease(applicationId, applicationUuid);
applicationRelease = applicationStorageManager.updateReleaseArtifacts(applicationRelease, appType,
binaryFile.getDataHandler().getInputStream());
applicationManager.updateRelease(applicationId, applicationRelease);

Loading…
Cancel
Save