Fix app and app-release deleting issues

reporting
tcdlpds@gmail.com 5 years ago
parent df2256ac44
commit fd8b664f81

@ -60,4 +60,14 @@ public interface VisibilityDAO {
*/ */
void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId) void deleteUnrestrictedRoles(List<String> unrestrictedRoles, int applicationId, int tenantId)
throws VisibilityManagementDAOException; throws VisibilityManagementDAOException;
/**
* This method is responsible to delete all application unrestricted roles
*
* @param applicationId Application Id
* @param tenantId Tenant Id
* @throws VisibilityManagementDAOException if error occurred while deleting application unrestricted roles
*/
void deleteAppUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException;
} }

@ -146,4 +146,34 @@ public class GenericVisibilityDAOImpl extends AbstractDAOImpl implements Visibil
throw new VisibilityManagementDAOException(msg, e); throw new VisibilityManagementDAOException(msg, e);
} }
} }
@Override
public void deleteAppUnrestrictedRoles(int applicationId, int tenantId) throws VisibilityManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Application unrestricted roles of application which "
+ "has application Id " + applicationId);
}
String sql = "DELETE "
+ "FROM AP_UNRESTRICTED_ROLE "
+ "WHERE AP_APP_ID = ? AND "
+ "TENANT_ID = ?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to delete application unrestricted roles "
+ "which has application Id " + applicationId;
log.error(msg, e);
throw new VisibilityManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing query to delete application unrestricted roles which has"
+ " application Id " + applicationId + ". executed query: " + sql;
log.error(msg, e);
throw new VisibilityManagementDAOException(msg, e);
}
}
} }

@ -1405,10 +1405,23 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ applicationId); + applicationId);
} }
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
ApplicationDTO applicationDTO = getApplication(applicationId); ApplicationDTO applicationDTO = getApplication(applicationId);
List<ApplicationReleaseDTO> applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs(); deleteApplication(applicationDTO, tenantId);
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { }
/**
* Delete the entire app data and the each app release data and artifacts.
*
* @param applicationDTO ApplicationDTO object
* @param tenantId Tenant Id
* @throws ApplicationManagementException if error occurred while deleting application data or app relase data.
*/
private void deleteApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementException {
List<Integer> deletingAppReleaseIds = new ArrayList<>();
List<String> deletingAppHashVals = new ArrayList<>();
try {
ConnectionManagerUtil.beginDBTransaction();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())){ if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())){
String msg = "Application release which has application release UUID: " + String msg = "Application release which has application release UUID: " +
applicationReleaseDTO.getUuid() + " is not in a deletable state. Therefore Application " applicationReleaseDTO.getUuid() + " is not in a deletable state. Therefore Application "
@ -1417,18 +1430,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
} }
} if (!subscriptionDAO.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId).isEmpty()) {
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid()
try { + " either subscribed to device/s or it had subscribed to device/s. Therefore you are not "
ConnectionManagerUtil.beginDBTransaction();
List<Integer> deletingAppReleaseIds = new ArrayList<>();
List<String> deletingAppHashVals = new ArrayList<>();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) {
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId);
if (!deviceSubscriptionDTOS.isEmpty()){
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() +
" either subscribed to device/s or it had subscribed to device/s. Therefore you are not "
+ "permitted to delete the application release."; + "permitted to delete the application release.";
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
@ -1436,37 +1440,38 @@ public class ApplicationManagerImpl implements ApplicationManager {
deletingAppHashVals.add(applicationReleaseDTO.getAppHashValue()); deletingAppHashVals.add(applicationReleaseDTO.getAppHashValue());
deletingAppReleaseIds.add(applicationReleaseDTO.getId()); deletingAppReleaseIds.add(applicationReleaseDTO.getId());
} }
applicationStorageManager.deleteAllApplicationReleaseArtifacts(deletingAppHashVals, tenantId);
this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds); this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds);
this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds); this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds);
this.applicationDAO.deleteApplicationTags(applicationId, tenantId); this.applicationDAO.deleteApplicationTags(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteAppCategories(applicationId, tenantId); this.applicationDAO.deleteAppCategories(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteApplication(applicationId, tenantId); this.visibilityDAO.deleteAppUnrestrictedRoles(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteApplication(applicationDTO.getId(), tenantId);
APIUtil.getApplicationStorageManager().deleteAllApplicationReleaseArtifacts(deletingAppHashVals, tenantId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while observing the database connection to delete application which has " String msg = "Error occurred while observing the database connection to delete application which has "
+ "application ID: " + applicationId; + "application ID: " + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Database access error is occurred when deleting application which has application ID: " String msg = "Database access error is occurred when deleting application which has application ID: "
+ applicationId; + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when getting application data for application id: " + applicationId; String msg = "Error occurred when getting application data for application id: " + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationStorageManagementException e) { } catch (ApplicationStorageManagementException e) {
String msg = "Error occurred when deleting application artifacts in the file system. Application id: " String msg = "Error occurred when deleting application artifacts in the file system. Application id: "
+ applicationId; + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while deleting life-cycle state data of application releases of the application" String msg = "Error occured while deleting life-cycle state data of application releases of the application"
+ " which has application ID: " + applicationId; + " which has application ID: " + applicationDTO.getId();
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} finally { } finally {
@ -1521,22 +1526,34 @@ public class ApplicationManagerImpl implements ApplicationManager {
public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException { public void deleteApplicationRelease(String releaseUuid) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
AtomicBoolean isDeletingApp = new AtomicBoolean(false); ApplicationDTO applicationDTO;
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.openDBConnection();
ApplicationDTO applicationDTO = this.applicationDAO.getApplication(releaseUuid, tenantId); applicationDTO = this.applicationDAO.getApplication(releaseUuid, tenantId);
if (applicationDTO == null) { if (applicationDTO == null) {
String msg = "Couldn't find an application which has application release UUID: " + releaseUuid; String msg = "Couldn't find an application which has application release UUID: " + releaseUuid;
log.error(msg); log.error(msg);
throw new NotFoundException(msg); throw new NotFoundException(msg);
} }
} catch (DBConnectionException e) {
List<ApplicationReleaseDTO> applicationReleaseDTOS = applicationDTO.getApplicationReleaseDTOs(); String msg = "Error occurred while observing the database connection to get application which has "
if (applicationReleaseDTOS.size() == 1) { + "application release of UUID: " + releaseUuid;
isDeletingApp.set(true); log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg =
"Error occurred when getting application data which has application release UUID: " + releaseUuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
} }
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOS) { if (applicationDTO.getApplicationReleaseDTOs().size() == 1) {
deleteApplication(applicationDTO, tenantId);
} else {
for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
if (releaseUuid.equals(applicationReleaseDTO.getUuid())) { if (releaseUuid.equals(applicationReleaseDTO.getUuid())) {
if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) { if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())) {
String msg = String msg =
@ -1545,56 +1562,56 @@ public class ApplicationManagerImpl implements ApplicationManager {
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
} }
try {
ConnectionManagerUtil.beginDBTransaction();
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
.getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId); .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId);
if (!deviceSubscriptionDTOS.isEmpty()) { if (!deviceSubscriptionDTOS.isEmpty()) {
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid()
+ " either subscribed to device/s or it had subscribed to device/s. Therefore you are not " + " either subscribed to device/s or it had subscribed to device/s. Therefore you "
+ "permitted to delete the application release."; + "are not permitted to delete the application release.";
log.error(msg); log.error(msg);
throw new ForbiddenException(msg); throw new ForbiddenException(msg);
} }
applicationStorageManager.deleteAllApplicationReleaseArtifacts(
Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId);
lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId()); lifecycleStateDAO.deleteLifecycleStateByReleaseId(applicationReleaseDTO.getId());
applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId()); applicationReleaseDAO.deleteRelease(applicationReleaseDTO.getId());
break; applicationStorageManager.deleteAllApplicationReleaseArtifacts(
} Collections.singletonList(applicationReleaseDTO.getAppHashValue()), tenantId);
}
if (isDeletingApp.get()) {
this.applicationDAO.deleteApplicationTags(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteAppCategories(applicationDTO.getId(), tenantId);
this.applicationDAO.deleteApplication(applicationDTO.getId(), tenantId);
}
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while observing the database connection to delete application release which " String msg = "Error occurred while observing the database connection to delete application "
+ "has UUID:" + releaseUuid; + "release which has the UUID:" + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Database access error is occurred when deleting application release which has UUID: " String msg = "Database access error is occurred when deleting application release which has "
+ releaseUuid; + "the UUID: " + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when application release data for application release UUID: " + releaseUuid; String msg = "Error occurred while verifying whether application relase has an subscription or "
+ "not. Application release UUID: " + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (ApplicationStorageManagementException e) { } catch (ApplicationStorageManagementException e) {
String msg = "Error occurred when deleting the application release artifact from the file system. " String msg = "Error occurred when deleting the application release artifact from the file "
+ "Application release UUID: " + releaseUuid; + "system. Application release UUID: " + releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (LifeCycleManagementDAOException e) { } catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when dleting lifecycle data for application release UUID: " + releaseUuid; String msg = "Error occurred when deleting lifecycle data for application release UUID: "
+ releaseUuid;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
break;
}
}
}
} }
@Override @Override

Loading…
Cancel
Save