Search in sources :

Example 1 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException 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 2 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException 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 3 with BadRequestException

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

the class CohortsController method createCohort.

@Override
public ResponseEntity<Cohort> createCohort(String workspaceNamespace, String workspaceId, Cohort cohort) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.WRITER);
    Workspace workspace = workspaceService.getRequired(workspaceNamespace, workspaceId);
    Timestamp now = new Timestamp(clock.instant().toEpochMilli());
    org.pmiops.workbench.db.model.Cohort dbCohort = FROM_CLIENT_COHORT.apply(cohort);
    dbCohort.setCreator(userProvider.get());
    dbCohort.setWorkspaceId(workspace.getWorkspaceId());
    dbCohort.setCreationTime(now);
    dbCohort.setLastModifiedTime(now);
    dbCohort.setVersion(1);
    try {
        // TODO Make this a pre-check within a transaction?
        dbCohort = cohortDao.save(dbCohort);
    } catch (DataIntegrityViolationException e) {
        // client by Spring (the client gets a default reason string).
        throw new BadRequestException(String.format("Cohort \"/%s/%s/%d\" already exists.", workspaceNamespace, workspaceId, dbCohort.getCohortId()));
    }
    return ResponseEntity.ok(TO_CLIENT_COHORT.apply(dbCohort));
}
Also used : BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) Timestamp(java.sql.Timestamp) Workspace(org.pmiops.workbench.db.model.Workspace) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 4 with BadRequestException

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

the class WorkspacesController method setCdrVersionId.

private void setCdrVersionId(org.pmiops.workbench.db.model.Workspace dbWorkspace, String cdrVersionId) {
    if (cdrVersionId != null) {
        try {
            CdrVersion cdrVersion = cdrVersionDao.findOne(Long.parseLong(cdrVersionId));
            if (cdrVersion == null) {
                throw new BadRequestException(String.format("CDR version with ID %s not found", cdrVersionId));
            }
            dbWorkspace.setCdrVersion(cdrVersion);
        } catch (NumberFormatException e) {
            throw new BadRequestException(String.format("Invalid cdr version ID: %s", cdrVersionId));
        }
    }
}
Also used : CdrVersion(org.pmiops.workbench.db.model.CdrVersion) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException)

Example 5 with BadRequestException

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

the class WorkspacesController method createWorkspace.

@Override
public ResponseEntity<Workspace> createWorkspace(Workspace workspace) {
    if (Strings.isNullOrEmpty(workspace.getNamespace())) {
        throw new BadRequestException("missing required field 'namespace'");
    } else if (Strings.isNullOrEmpty(workspace.getName())) {
        throw new BadRequestException("missing required field 'name'");
    } else if (workspace.getResearchPurpose() == null) {
        throw new BadRequestException("missing required field 'researchPurpose'");
    } else if (workspace.getDataAccessLevel() == null) {
        throw new BadRequestException("missing required field 'dataAccessLevel'");
    }
    User user = userProvider.get();
    org.pmiops.workbench.db.model.Workspace existingWorkspace = workspaceService.getByName(workspace.getNamespace(), workspace.getName());
    if (existingWorkspace != null) {
        throw new ConflictException(String.format("Workspace %s/%s already exists", workspace.getNamespace(), workspace.getName()));
    }
    // Note: please keep any initialization logic here in sync with CloneWorkspace().
    FirecloudWorkspaceId workspaceId = generateFirecloudWorkspaceId(workspace.getNamespace(), workspace.getName());
    FirecloudWorkspaceId fcWorkspaceId = workspaceId;
    org.pmiops.workbench.firecloud.model.Workspace fcWorkspace = null;
    for (int attemptValue = 0; attemptValue < MAX_FC_CREATION_ATTEMPT_VALUES; attemptValue++) {
        try {
            fcWorkspace = attemptFirecloudWorkspaceCreation(fcWorkspaceId);
            break;
        } catch (ConflictException e) {
            if (attemptValue >= 5) {
                throw e;
            } else {
                fcWorkspaceId = new FirecloudWorkspaceId(workspaceId.getWorkspaceNamespace(), workspaceId.getWorkspaceName() + Integer.toString(attemptValue));
            }
        }
    }
    Timestamp now = new Timestamp(clock.instant().toEpochMilli());
    org.pmiops.workbench.db.model.Workspace dbWorkspace = new org.pmiops.workbench.db.model.Workspace();
    dbWorkspace.setFirecloudName(fcWorkspaceId.getWorkspaceName());
    dbWorkspace.setWorkspaceNamespace(fcWorkspaceId.getWorkspaceNamespace());
    dbWorkspace.setCreator(user);
    dbWorkspace.setCreationTime(now);
    dbWorkspace.setLastModifiedTime(now);
    dbWorkspace.setVersion(1);
    setCdrVersionId(dbWorkspace, workspace.getCdrVersionId());
    writeWorkspaceConfigFile(fcWorkspace, dbWorkspace.getCdrVersion());
    org.pmiops.workbench.db.model.Workspace reqWorkspace = FROM_CLIENT_WORKSPACE.apply(workspace);
    // TODO: enforce data access level authorization
    dbWorkspace.setDataAccessLevel(reqWorkspace.getDataAccessLevel());
    dbWorkspace.setName(reqWorkspace.getName());
    dbWorkspace.setDescription(reqWorkspace.getDescription());
    // Ignore incoming fields pertaining to review status; clients can only request a review.
    setResearchPurposeDetails(dbWorkspace, workspace.getResearchPurpose());
    if (reqWorkspace.getReviewRequested()) {
        // Use a consistent timestamp.
        dbWorkspace.setTimeRequested(now);
    }
    dbWorkspace.setReviewRequested(reqWorkspace.getReviewRequested());
    org.pmiops.workbench.db.model.WorkspaceUserRole permissions = new org.pmiops.workbench.db.model.WorkspaceUserRole();
    permissions.setRole(WorkspaceAccessLevel.OWNER);
    permissions.setWorkspace(dbWorkspace);
    permissions.setUser(user);
    dbWorkspace.addWorkspaceUserRole(permissions);
    dbWorkspace = workspaceService.getDao().save(dbWorkspace);
    return ResponseEntity.ok(TO_SINGLE_CLIENT_WORKSPACE_FROM_FC_AND_DB.apply(dbWorkspace, fcWorkspace));
}
Also used : User(org.pmiops.workbench.db.model.User) ConflictException(org.pmiops.workbench.exceptions.ConflictException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) Timestamp(java.sql.Timestamp) FirecloudWorkspaceId(org.pmiops.workbench.db.model.Workspace.FirecloudWorkspaceId) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) Workspace(org.pmiops.workbench.model.Workspace)

Aggregations

BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)27 Timestamp (java.sql.Timestamp)5 CohortReview (org.pmiops.workbench.db.model.CohortReview)5 Workspace (org.pmiops.workbench.db.model.Workspace)5 NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)5 Test (org.junit.Test)4 CohortAnnotationDefinition (org.pmiops.workbench.db.model.CohortAnnotationDefinition)4 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)4 ConflictException (org.pmiops.workbench.exceptions.ConflictException)4 User (org.pmiops.workbench.db.model.User)3 Workspace (org.pmiops.workbench.model.Workspace)3 QueryResult (com.google.cloud.bigquery.QueryResult)2 Gson (com.google.gson.Gson)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ParticipantCriteria (org.pmiops.workbench.cohortbuilder.ParticipantCriteria)2 TableQueryAndConfig (org.pmiops.workbench.cohortbuilder.TableQueryAndConfig)2 CdrVersion (org.pmiops.workbench.db.model.CdrVersion)2