Search in sources :

Example 1 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class ClusterController method localize.

@Override
public ResponseEntity<ClusterLocalizeResponse> localize(String projectName, String clusterName, ClusterLocalizeRequest body) {
    String workspaceBucket;
    try {
        workspaceBucket = "gs://" + fireCloudService.getWorkspace(body.getWorkspaceNamespace(), body.getWorkspaceId()).getWorkspace().getBucketName();
    } catch (ApiException e) {
        if (e.getCode() == 404) {
            log.log(Level.INFO, "Firecloud workspace not found", e);
            throw new NotFoundException(String.format("workspace %s/%s not found or not accessible", body.getWorkspaceNamespace(), body.getWorkspaceId()));
        }
        throw ExceptionUtils.convertFirecloudException(e);
    }
    // For the common case where the notebook cluster matches the workspace
    // namespace, simply name the directory as the workspace ID; else we
    // include the namespace in the directory name to avoid possible conflicts
    // in workspace IDs.
    String workspacePath = body.getWorkspaceId();
    if (!projectName.equals(body.getWorkspaceNamespace())) {
        workspacePath = body.getWorkspaceNamespace() + ":" + body.getWorkspaceId();
    }
    String apiDir = String.join("/", "workspaces", workspacePath);
    String localDir = String.join("/", "~", apiDir);
    Map<String, String> toLocalize = body.getNotebookNames().stream().collect(Collectors.<String, String, String>toMap(name -> localDir + "/" + name, name -> String.join("/", workspaceBucket, "notebooks", name)));
    // TODO(calbach): Localize a delocalize config JSON file as well, once Leo supports this.
    toLocalize.put(localDir + "/.all_of_us_config.json", workspaceBucket + "/" + WorkspacesController.CONFIG_FILENAME);
    notebooksService.localize(projectName, clusterName, toLocalize);
    ClusterLocalizeResponse resp = new ClusterLocalizeResponse();
    resp.setClusterLocalDirectory(apiDir);
    return ResponseEntity.ok(resp);
}
Also used : ExceptionUtils(org.pmiops.workbench.exceptions.ExceptionUtils) ClusterLocalizeResponse(org.pmiops.workbench.model.ClusterLocalizeResponse) Provider(javax.inject.Provider) ImmutableMap(com.google.common.collect.ImmutableMap) FireCloudService(org.pmiops.workbench.firecloud.FireCloudService) ClusterListResponse(org.pmiops.workbench.model.ClusterListResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ApiException(org.pmiops.workbench.firecloud.ApiException) Logger(java.util.logging.Logger) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) Level(java.util.logging.Level) WorkbenchConfig(org.pmiops.workbench.config.WorkbenchConfig) ClusterLocalizeRequest(org.pmiops.workbench.model.ClusterLocalizeRequest) Map(java.util.Map) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) NotebooksService(org.pmiops.workbench.notebooks.NotebooksService) ResponseEntity(org.springframework.http.ResponseEntity) Cluster(org.pmiops.workbench.model.Cluster) ClusterStatus(org.pmiops.workbench.model.ClusterStatus) User(org.pmiops.workbench.db.model.User) ClusterLocalizeResponse(org.pmiops.workbench.model.ClusterLocalizeResponse) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) ApiException(org.pmiops.workbench.firecloud.ApiException)

Example 2 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class CohortReviewController method createCohortReview.

/**
 * Create a cohort review per the specified workspaceId, cohortId, cdrVersionId and size. If participant cohort status
 * data exists for a review or no cohort review exists for cohortReviewId then throw a
 * {@link BadRequestException}.
 *
 * @param workspaceNamespace
 * @param workspaceId
 * @param cohortId
 * @param cdrVersionId
 * @param request
 */
@Override
public ResponseEntity<org.pmiops.workbench.model.CohortReview> createCohortReview(String workspaceNamespace, String workspaceId, Long cohortId, Long cdrVersionId, CreateReviewRequest request) {
    if (request.getSize() <= 0 || request.getSize() > MAX_REVIEW_SIZE) {
        throw new BadRequestException(String.format("Invalid Request: Cohort Review size must be between %s and %s", 0, MAX_REVIEW_SIZE));
    }
    Cohort cohort = cohortReviewService.findCohort(cohortId);
    // this validates that the user is in the proper workspace
    Workspace workspace = cohortReviewService.validateMatchingWorkspace(workspaceNamespace, workspaceId, cohort.getWorkspaceId(), WorkspaceAccessLevel.WRITER);
    CdrVersionContext.setCdrVersion(workspace.getCdrVersion());
    CohortReview cohortReview = null;
    try {
        cohortReview = cohortReviewService.findCohortReview(cohortId, cdrVersionId);
    } catch (NotFoundException nfe) {
        cohortReview = initializeCohortReview(cdrVersionId, cohort).reviewStatus(ReviewStatus.NONE).reviewSize(0L);
        cohortReviewService.saveCohortReview(cohortReview);
    }
    if (cohortReview.getReviewSize() > 0) {
        throw new BadRequestException(String.format("Invalid Request: Cohort Review already created for cohortId: %s, cdrVersionId: %s", cohortId, cdrVersionId));
    }
    SearchRequest searchRequest = new Gson().fromJson(getCohortDefinition(cohort), SearchRequest.class);
    QueryResult result = bigQueryService.executeQuery(bigQueryService.filterBigQueryConfig(participantCounter.buildParticipantIdQuery(new ParticipantCriteria(searchRequest), request.getSize(), 0L)));
    Map<String, Integer> rm = bigQueryService.getResultMapper(result);
    List<ParticipantCohortStatus> participantCohortStatuses = createParticipantCohortStatusesList(cohortReview.getCohortReviewId(), result, rm);
    cohortReview.reviewSize(participantCohortStatuses.size()).reviewStatus(ReviewStatus.CREATED);
    // when saving ParticipantCohortStatuses to the database the long value of birthdate is mutated.
    cohortReviewService.saveFullCohortReview(cohortReview, participantCohortStatuses);
    ParticipantCohortStatuses filterRequest = new ParticipantCohortStatuses();
    filterRequest.setPage(PAGE);
    filterRequest.setPageSize(PAGE_SIZE);
    filterRequest.setSortOrder(SortOrder.ASC);
    filterRequest.setPageFilterType(PageFilterType.PARTICIPANTCOHORTSTATUSES);
    filterRequest.setSortColumn(ParticipantCohortStatusColumns.PARTICIPANTID);
    List<ParticipantCohortStatus> paginatedPCS = cohortReviewService.findAll(cohortReview.getCohortReviewId(), Collections.<Filter>emptyList(), createPageRequest(filterRequest));
    lookupGenderRaceEthnicityValues(paginatedPCS);
    org.pmiops.workbench.model.CohortReview responseReview = TO_CLIENT_COHORTREVIEW.apply(cohortReview, createPageRequest(filterRequest));
    responseReview.setParticipantCohortStatuses(paginatedPCS.stream().map(TO_CLIENT_PARTICIPANT).collect(Collectors.toList()));
    return ResponseEntity.ok(responseReview);
}
Also used : NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) Gson(com.google.gson.Gson) QueryResult(com.google.cloud.bigquery.QueryResult) Cohort(org.pmiops.workbench.db.model.Cohort) ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) org.pmiops.workbench.model(org.pmiops.workbench.model) CohortReview(org.pmiops.workbench.db.model.CohortReview) ParticipantCriteria(org.pmiops.workbench.cohortbuilder.ParticipantCriteria) Workspace(org.pmiops.workbench.db.model.Workspace)

Example 3 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class CohortsController method materializeCohort.

@Override
public ResponseEntity<MaterializeCohortResponse> materializeCohort(String workspaceNamespace, String workspaceId, MaterializeCohortRequest request) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.READER);
    Workspace workspace = workspaceService.getRequired(workspaceNamespace, workspaceId);
    CdrVersion cdrVersion = workspace.getCdrVersion();
    CdrVersionContext.setCdrVersion(cdrVersion);
    if (request.getCdrVersionName() != null) {
        cdrVersion = cdrVersionDao.findByName(request.getCdrVersionName());
        if (cdrVersion == null) {
            throw new NotFoundException(String.format("Couldn't find CDR version with name %s", request.getCdrVersionName()));
        }
    }
    String cohortSpec;
    CohortReview cohortReview = null;
    if (request.getCohortName() != null) {
        org.pmiops.workbench.db.model.Cohort cohort = cohortDao.findCohortByNameAndWorkspaceId(request.getCohortName(), workspace.getWorkspaceId());
        if (cohort == null) {
            throw new NotFoundException(String.format("Couldn't find cohort with name %s in workspace %s/%s", request.getCohortName(), workspaceNamespace, workspaceId));
        }
        cohortReview = cohortReviewDao.findCohortReviewByCohortIdAndCdrVersionId(cohort.getCohortId(), cdrVersion.getCdrVersionId());
        cohortSpec = cohort.getCriteria();
    } else if (request.getCohortSpec() != null) {
        cohortSpec = request.getCohortSpec();
        if (request.getStatusFilter() != null) {
            throw new BadRequestException("statusFilter cannot be used with cohortSpec");
        }
    } else {
        throw new BadRequestException("Must specify either cohortName or cohortSpec");
    }
    Integer pageSize = request.getPageSize();
    if (pageSize == null || pageSize == 0) {
        request.setPageSize(DEFAULT_PAGE_SIZE);
    } else if (pageSize < 0) {
        throw new BadRequestException(String.format("Invalid page size: %s; must be between 1 and %d", pageSize, MAX_PAGE_SIZE));
    } else if (pageSize > MAX_PAGE_SIZE) {
        request.setPageSize(MAX_PAGE_SIZE);
    }
    SearchRequest searchRequest;
    try {
        searchRequest = new Gson().fromJson(cohortSpec, SearchRequest.class);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid cohort spec");
    }
    MaterializeCohortResponse response = cohortMaterializationService.materializeCohort(cohortReview, searchRequest, request);
    return ResponseEntity.ok(response);
}
Also used : CdrVersion(org.pmiops.workbench.db.model.CdrVersion) SearchRequest(org.pmiops.workbench.model.SearchRequest) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) Gson(com.google.gson.Gson) MaterializeCohortResponse(org.pmiops.workbench.model.MaterializeCohortResponse) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) CohortReview(org.pmiops.workbench.db.model.CohortReview) Workspace(org.pmiops.workbench.db.model.Workspace)

Example 4 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class CohortsController method getDbCohort.

private org.pmiops.workbench.db.model.Cohort getDbCohort(String workspaceNamespace, String workspaceId, Long cohortId) {
    Workspace workspace = workspaceService.getRequired(workspaceNamespace, workspaceId);
    org.pmiops.workbench.db.model.Cohort cohort = cohortDao.findOne(cohortId);
    if (cohort == null) {
        throw new NotFoundException(String.format("No cohort with name %s in workspace %s.", cohortId, workspace.getFirecloudName()));
    }
    return cohort;
}
Also used : NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) Workspace(org.pmiops.workbench.db.model.Workspace)

Example 5 with NotFoundException

use of org.pmiops.workbench.exceptions.NotFoundException in project workbench by all-of-us.

the class WorkspacesController method getNoteBookList.

@Override
public ResponseEntity<List<FileDetail>> getNoteBookList(String workspaceNamespace, String workspaceId) {
    List<FileDetail> fileList = new ArrayList<>();
    try {
        org.pmiops.workbench.firecloud.model.Workspace fireCloudWorkspace = fireCloudService.getWorkspace(workspaceNamespace, workspaceId).getWorkspace();
        String bucketName = fireCloudWorkspace.getBucketName();
        fileList = getFilesFromNotebooks(bucketName);
    } catch (org.pmiops.workbench.firecloud.ApiException e) {
        if (e.getCode() == 404) {
            throw new NotFoundException(String.format("Workspace %s/%s not found", workspaceNamespace, workspaceId));
        }
        throw new ServerErrorException(e);
    }
    return ResponseEntity.ok(fileList);
}
Also used : FileDetail(org.pmiops.workbench.model.FileDetail) ArrayList(java.util.ArrayList) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) ApiException(org.pmiops.workbench.firecloud.ApiException) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException)

Aggregations

NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)32 Test (org.junit.Test)18 WorkspaceAccessLevel (org.pmiops.workbench.model.WorkspaceAccessLevel)16 Workspace (org.pmiops.workbench.db.model.Workspace)14 Cohort (org.pmiops.workbench.db.model.Cohort)10 ApiException (org.pmiops.workbench.firecloud.ApiException)5 ParticipantCohortAnnotation (org.pmiops.workbench.db.model.ParticipantCohortAnnotation)4 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)4 ServerErrorException (org.pmiops.workbench.exceptions.ServerErrorException)4 CohortReview (org.pmiops.workbench.db.model.CohortReview)3 Blob (com.google.cloud.storage.Blob)2 Gson (com.google.gson.Gson)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ParticipantCohortStatus (org.pmiops.workbench.db.model.ParticipantCohortStatus)2 User (org.pmiops.workbench.db.model.User)2 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)2 org.pmiops.workbench.model (org.pmiops.workbench.model)2 ClusterListResponse (org.pmiops.workbench.model.ClusterListResponse)2 ModifyCohortAnnotationDefinitionRequest (org.pmiops.workbench.model.ModifyCohortAnnotationDefinitionRequest)2