Search in sources :

Example 1 with ApplicationInUseException

use of bio.terra.workspace.db.exception.ApplicationInUseException in project terra-workspace-manager by DataBiosphere.

the class ApplicationDao method disableWorkspaceApplication.

/**
 * Disable an application in a workspace. It is not an error to disable an already disabled
 * application.
 *
 * @param workspaceId workspace of interest
 * @param applicationId application of interest
 * @return workspace-application object
 */
@WriteTransaction
public WsmWorkspaceApplication disableWorkspaceApplication(UUID workspaceId, UUID applicationId) {
    // Validate that the application exists; workspace is validated in layers above this
    getApplicationOrThrow(applicationId);
    // It is an error to have application resources in the workspace if we are disabling it.
    final String countAppUsesSql = "SELECT COUNT(*) FROM resource" + " WHERE associated_app = :application_id AND workspace_id = :workspace_id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("application_id", applicationId.toString());
    Integer count = jdbcTemplate.queryForObject(countAppUsesSql, params, Integer.class);
    if (count != null && count > 0) {
        throw new ApplicationInUseException(String.format("Application %s in use in workspace %s", applicationId, workspaceId));
    }
    // No uses, so we disable
    final String sql = "DELETE FROM enabled_application" + " WHERE workspace_id = :workspace_id AND application_id = :application_id";
    int rowCount = jdbcTemplate.update(sql, params);
    if (rowCount > 0) {
        logger.info("Deleted record enabling application {} for workspace {}", applicationId, workspaceId);
    } else {
        logger.info("Ignoring duplicate disabling of application {} for workspace {}", applicationId, workspaceId);
    }
    return getWorkspaceApplicationWorker(workspaceId, applicationId);
}
Also used : ApplicationInUseException(bio.terra.workspace.db.exception.ApplicationInUseException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Aggregations

WriteTransaction (bio.terra.common.db.WriteTransaction)1 ApplicationInUseException (bio.terra.workspace.db.exception.ApplicationInUseException)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1