Search in sources :

Example 6 with ReadTransaction

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

the class ResourceDao method resourceExists.

/**
 * Wait for a resource row to appear in the database. This is used so async resource creation
 * calls return after the database row has been inserted. That allows clients to see the resource
 * with a resource enumeration.
 *
 * @param workspaceUuid workspace where the resource is being created
 * @param resourceId resource being created
 */
@ReadTransaction
public boolean resourceExists(UUID workspaceUuid, UUID resourceId) {
    final String sql = "SELECT COUNT(1) FROM resource" + " WHERE workspace_id = :workspace_id AND resource_id = :resource_id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.toString()).addValue("resource_id", resourceId.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 7 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 workspaceUuid 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 workspaceUuid, @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", workspaceUuid.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 8 with ReadTransaction

use of bio.terra.common.db.ReadTransaction 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 workspaceUuid 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 workspaceUuid, @Nullable CloudPlatform cloudPlatform) {
    String sql = RESOURCE_SELECT_SQL + " AND stewardship_type = :controlled_resource ";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.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 9 with ReadTransaction

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

the class WorkspaceDao method getWorkspaceIfExists.

@ReadTransaction
public Optional<Workspace> getWorkspaceIfExists(UUID uuid) {
    if (uuid == null) {
        throw new MissingRequiredFieldException("Valid workspace id is required");
    }
    String sql = WORKSPACE_SELECT_SQL + " WHERE workspace_id = :id";
    MapSqlParameterSource params = new MapSqlParameterSource().addValue("id", uuid.toString());
    try {
        Workspace result = DataAccessUtils.requiredSingleResult(jdbcTemplate.query(sql, params, WORKSPACE_ROW_MAPPER));
        logger.info("Retrieved workspace record {}", result);
        return Optional.of(result);
    } catch (EmptyResultDataAccessException e) {
        return Optional.empty();
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) MissingRequiredFieldException(bio.terra.common.exception.MissingRequiredFieldException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Workspace(bio.terra.workspace.service.workspace.model.Workspace) ReadTransaction(bio.terra.common.db.ReadTransaction)

Example 10 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 workspaceUuid, int offset, int limit) {
    final String sql = WORKSPACE_APPLICATION_QUERY + " OFFSET :offset LIMIT :limit";
    var params = new MapSqlParameterSource().addValue("workspace_id", workspaceUuid.toString()).addValue("offset", offset).addValue("limit", limit);
    List<WsmWorkspaceApplication> resultList = jdbcTemplate.query(sql, params, WORKSPACE_APPLICATION_ROW_MAPPER);
    for (WsmWorkspaceApplication result : resultList) {
        result.workspaceUuid(workspaceUuid);
    }
    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)12 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)12 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