Search in sources :

Example 1 with DbResource

use of bio.terra.workspace.db.model.DbResource in project terra-workspace-manager by DataBiosphere.

the class ResourceDao method claimCleanupForWorkspacePrivateResources.

/**
 * Reads all private controlled resources assigned to a given user in a given workspace which are
 * not being cleaned up by other flights and marks them as being cleaned up by the current flight.
 *
 * @return A list of all controlled resources assigned to the given user in the given workspace
 *     which were not locked by another flight. Every item in this list will have
 *     cleanup_flight_id set to the provided flight id.
 */
@WriteTransaction
public List<ControlledResource> claimCleanupForWorkspacePrivateResources(UUID workspaceId, String userEmail, String flightId) {
    String filterClause = " AND stewardship_type = :controlled_resource" + " AND access_scope = :access_scope" + " AND assigned_user = :user_email" + " AND (cleanup_flight_id IS NULL" + " OR cleanup_flight_id = :flight_id)";
    String readSql = RESOURCE_SELECT_SQL + filterClause;
    String writeSql = "UPDATE resource SET cleanup_flight_id = :flight_id WHERE workspace_id = :workspace_id" + filterClause;
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("controlled_resource", CONTROLLED.toSql()).addValue("access_scope", AccessScopeType.ACCESS_SCOPE_PRIVATE.toSql()).addValue("user_email", userEmail).addValue("flight_id", flightId);
    List<DbResource> dbResources = jdbcTemplate.query(readSql, params, DB_RESOURCE_ROW_MAPPER);
    jdbcTemplate.update(writeSql, params);
    return dbResources.stream().map(this::constructResource).map(WsmResource::castToControlledResource).collect(Collectors.toList());
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DbResource(bio.terra.workspace.db.model.DbResource) WriteTransaction(bio.terra.common.db.WriteTransaction)

Example 2 with DbResource

use of bio.terra.workspace.db.model.DbResource 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 3 with DbResource

use of bio.terra.workspace.db.model.DbResource in project terra-workspace-manager by DataBiosphere.

the class ResourceDao method listControlledResources.

/**
 * Returns a list of all controlled resources in a workspace, optionally filtering by cloud
 * platform.
 *
 * @param workspaceId ID of the workspace to return resources from.
 * @param cloudPlatform Optional. If present, this will only return resources from the specified
 *     cloud platform. If null, this will return resources from all cloud platforms.
 */
@ReadTransaction
public List<ControlledResource> listControlledResources(UUID workspaceId, @Nullable CloudPlatform cloudPlatform) {
    String sql = RESOURCE_SELECT_SQL + " AND stewardship_type = :controlled_resource ";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("controlled_resource", CONTROLLED.toSql());
    if (cloudPlatform != null) {
        sql += " AND cloud_platform = :cloud_platform";
        params.addValue("cloud_platform", cloudPlatform.toSql());
    }
    List<DbResource> dbResources = jdbcTemplate.query(sql, params, DB_RESOURCE_ROW_MAPPER);
    return dbResources.stream().map(this::constructResource).map(WsmResource::castToControlledResource).collect(Collectors.toList());
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DbResource(bio.terra.workspace.db.model.DbResource) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 4 with DbResource

use of bio.terra.workspace.db.model.DbResource in project terra-workspace-manager by DataBiosphere.

the class ResourceDao method enumerateReferences.

/**
 * enumerateReferences - temporary This is a temporary implementation to support the old
 * DataReference model. It also does not filter by what is visible to the user. I think we will
 * probably change to use a single enumerate across all resources.
 *
 * @param workspaceId workspace of interest
 * @param offset paging support
 * @param limit paging support
 * @return list of reference resources
 */
@ReadTransaction
public List<ReferencedResource> enumerateReferences(UUID workspaceId, int offset, int limit) {
    String sql = RESOURCE_SELECT_SQL + " AND stewardship_type = :stewardship_type ORDER BY name OFFSET :offset LIMIT :limit";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceId.toString()).addValue("stewardship_type", REFERENCED.toSql()).addValue("offset", offset).addValue("limit", limit);
    List<DbResource> dbResourceList = jdbcTemplate.query(sql, params, DB_RESOURCE_ROW_MAPPER);
    return dbResourceList.stream().map(this::constructResource).map(ReferencedResource.class::cast).collect(toList());
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DbResource(bio.terra.workspace.db.model.DbResource) ReadTransaction(bio.terra.common.db.ReadTransaction)

Aggregations

DbResource (bio.terra.workspace.db.model.DbResource)4 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)4 ReadTransaction (bio.terra.common.db.ReadTransaction)3 WriteTransaction (bio.terra.common.db.WriteTransaction)1