Search in sources :

Example 1 with ReadTransaction

use of bio.terra.common.db.ReadTransaction in project terra-workspace-manager by DataBiosphere.

the class ApplicationDao method applicationInUse.

/**
 * Check if an application is in use by any resources
 *
 * @param applicationId application to check
 * @return true if the application is in use
 */
@ReadTransaction
public boolean applicationInUse(UUID applicationId) {
    final String sql = "SELECT COUNT(*) FROM resource WHERE associated_app = :application_id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("application_id", applicationId.toString());
    Integer count = jdbcTemplate.queryForObject(sql, params, Integer.class);
    return (count != null && count > 0);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 2 with ReadTransaction

use of bio.terra.common.db.ReadTransaction in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method getPrivateResourceUsers.

/**
 * A method for finding all users of private resources and the workspaces they have resources in.
 * The returned set will only have one entry for each {workspace, user} pair, even if a user has
 * multiple private resources in the same workspace.
 */
@ReadTransaction
public List<WorkspaceUserPair> getPrivateResourceUsers() {
    String sql = "SELECT DISTINCT workspace_id, assigned_user FROM resource WHERE assigned_user IS NOT NULL AND private_resource_state = :active_resource_state";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("active_resource_state", PrivateResourceState.ACTIVE.toSql());
    return jdbcTemplate.query(sql, params, WORKSPACE_USER_PAIR_ROW_MAPPER);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 3 with ReadTransaction

use of bio.terra.common.db.ReadTransaction in project terra-workspace-manager by DataBiosphere.

the class WorkspaceDao method getWorkspacesMatchingList.

/**
 * Retrieve workspaces from a list of IDs. IDs not matching workspaces will be ignored.
 *
 * @param idList List of workspaceIds to query for
 * @param offset The number of items to skip before starting to collect the result set.
 * @param limit The maximum number of items to return.
 * @return list of Workspaces corresponding to input IDs.
 */
@ReadTransaction
public List<Workspace> getWorkspacesMatchingList(List<UUID> idList, int offset, int limit) {
    // workspaces, so we return an empty list.
    if (idList.isEmpty()) {
        return Collections.emptyList();
    }
    String sql = WORKSPACE_SELECT_SQL + " WHERE workspace_id IN (:workspace_ids) ORDER BY workspace_id OFFSET :offset LIMIT :limit";
    var params = new MapSqlParameterSource().addValue("workspace_ids", idList.stream().map(UUID::toString).collect(Collectors.toList())).addValue("offset", offset).addValue("limit", limit);
    return jdbcTemplate.query(sql, params, WORKSPACE_ROW_MAPPER);
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) UUID(java.util.UUID) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 4 with ReadTransaction

use of bio.terra.common.db.ReadTransaction in project terra-workspace-manager by DataBiosphere.

the class ResourceDao method enumerateResources.

/**
 * Resource enumeration
 *
 * <p>The default behavior of resource enumeration is to find all resources that are visible to
 * the caller. If the caller has gotten this far, then they are allowed to see all referenced and
 * controlled resources.
 *
 * <p>The enumeration can be filtered by a resource type. If a resource type is specified, then
 * only that type of resource is returned. The enumeration can also be filtered by a stewardship
 * type.
 *
 * @param workspaceId identifier for work space to enumerate
 * @param cloudResourceType filter by this cloud resource type - optional
 * @param stewardshipType filtered by this stewardship type - optional
 * @param offset starting row for result
 * @param limit maximum number of rows to return
 * @return list of resources
 */
@ReadTransaction
public List<WsmResource> enumerateResources(UUID workspaceId, @Nullable WsmResourceFamily cloudResourceType, @Nullable StewardshipType stewardshipType, int offset, int limit) {
    // We supply the toSql() forms of the stewardship values as parameters, so that string is only
    // defined in one place. We do not always use the stewardship values, but there is no harm
    // in having extra params.
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("offset", offset).addValue("limit", limit).addValue("referenced_resource", REFERENCED.toSql()).addValue("controlled_resource", CONTROLLED.toSql());
    StringBuilder sb = new StringBuilder(RESOURCE_SELECT_SQL);
    if (cloudResourceType != null) {
        sb.append(" AND resource_type = :resource_type");
        params.addValue("resource_type", cloudResourceType.toSql());
    }
    // There are three cases for the stewardship type filter
    // 1. If it is REFERENCED, then we ignore id list and just filter for referenced resources.
    // 2. If it is CONTROLLED, then we filter for CONTROLLED resources.
    // 3. If no filter is specified (it is null), then we want both REFERENCED and CONTROLLED
    // resources; that is, we want the OR of 1 and 2
    boolean includeReferenced = (stewardshipType == null || stewardshipType == REFERENCED);
    boolean includeControlled = (stewardshipType == null || stewardshipType == CONTROLLED);
    final String referencedPhrase = "stewardship_type = :referenced_resource";
    final String controlledPhrase = "stewardship_type = :controlled_resource";
    sb.append(" AND ");
    if (includeReferenced && includeControlled) {
        sb.append("(").append(referencedPhrase).append(" OR ").append(controlledPhrase).append(")");
    } else if (includeReferenced) {
        sb.append(referencedPhrase);
    } else if (includeControlled) {
        sb.append(controlledPhrase);
    } else {
        // Nothing is included, so we return an empty result
        return Collections.emptyList();
    }
    sb.append(" ORDER BY name OFFSET :offset LIMIT :limit");
    List<DbResource> dbResourceList = jdbcTemplate.query(sb.toString(), params, DB_RESOURCE_ROW_MAPPER);
    return dbResourceList.stream().map(this::constructResource).collect(toList());
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DbResource(bio.terra.workspace.db.model.DbResource) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 5 with ReadTransaction

use of bio.terra.common.db.ReadTransaction in project terra-workspace-manager by DataBiosphere.

the class ApplicationDao method listWorkspaceApplications.

@ReadTransaction
public List<WsmWorkspaceApplication> listWorkspaceApplications(UUID workspaceId, int offset, int limit) {
    final String sql = WORKSPACE_APPLICATION_QUERY + " OFFSET :offset LIMIT :limit";
    var params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("offset", offset).addValue("limit", limit);
    List<WsmWorkspaceApplication> resultList = jdbcTemplate.query(sql, params, WORKSPACE_APPLICATION_ROW_MAPPER);
    for (WsmWorkspaceApplication result : resultList) {
        result.workspaceId(workspaceId);
    }
    return resultList;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) WsmWorkspaceApplication(bio.terra.workspace.service.workspace.model.WsmWorkspaceApplication) ReadTransaction(bio.terra.common.db.ReadTransaction)

Aggregations

ReadTransaction (bio.terra.common.db.ReadTransaction)11 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)11 DbResource (bio.terra.workspace.db.model.DbResource)3 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 MissingRequiredFieldException (bio.terra.common.exception.MissingRequiredFieldException)1 ApplicationNotFoundException (bio.terra.workspace.db.exception.ApplicationNotFoundException)1 Workspace (bio.terra.workspace.service.workspace.model.Workspace)1 WsmWorkspaceApplication (bio.terra.workspace.service.workspace.model.WsmWorkspaceApplication)1 UUID (java.util.UUID)1