Search in sources :

Example 1 with Cohort

use of org.pmiops.workbench.db.model.Cohort in project workbench by all-of-us.

the class CohortAnnotationDefinitionController method getCohortAnnotationDefinition.

@Override
public ResponseEntity<CohortAnnotationDefinition> getCohortAnnotationDefinition(String workspaceNamespace, String workspaceId, Long cohortId, Long annotationDefinitionId) {
    // This also enforces registered auth domain.
    workspaceService.enforceWorkspaceAccessLevel(workspaceNamespace, workspaceId, WorkspaceAccessLevel.READER);
    Cohort cohort = findCohort(cohortId);
    // this validates that the user is in the proper workspace
    validateMatchingWorkspace(workspaceNamespace, workspaceId, cohort.getWorkspaceId());
    org.pmiops.workbench.db.model.CohortAnnotationDefinition cohortAnnotationDefinition = findCohortAnnotationDefinition(cohortId, annotationDefinitionId);
    return ResponseEntity.ok(TO_CLIENT_COHORT_ANNOTATION_DEFINITION.apply(cohortAnnotationDefinition));
}
Also used : Cohort(org.pmiops.workbench.db.model.Cohort)

Example 2 with Cohort

use of org.pmiops.workbench.db.model.Cohort 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 Cohort

use of org.pmiops.workbench.db.model.Cohort in project workbench by all-of-us.

the class CohortReviewControllerTest method setUp.

@Before
public void setUp() {
    LocalDate personBirthDate = LocalDate.of(1980, Month.FEBRUARY, 17);
    LocalDate procedureDate1 = LocalDate.of(2009, Month.DECEMBER, 2);
    LocalDate procedureDate2 = LocalDate.of(2009, Month.DECEMBER, 3);
    Period age1 = Period.between(personBirthDate, procedureDate1);
    Period age2 = Period.between(personBirthDate, procedureDate2);
    expectedCondition1 = new ParticipantCondition().itemDate("2008-07-22").standardVocabulary("SNOMED").standardName("SNOMED").sourceValue("0020").sourceVocabulary("ICD9CM").sourceName("Typhoid and paratyphoid fevers");
    expectedCondition2 = new ParticipantCondition().itemDate("2008-08-01").standardVocabulary("SNOMED").standardName("SNOMED").sourceValue("0021").sourceVocabulary("ICD9CM").sourceName("Typhoid and paratyphoid fevers");
    expectedProcedure1 = new ParticipantProcedure().itemDate("2009-12-02").standardVocabulary("ICD10CM").standardName("name").sourceValue("val").sourceVocabulary("ICD10CM").sourceName("name").age(age1.getYears());
    expectedProcedure2 = new ParticipantProcedure().itemDate("2009-12-03").standardVocabulary("CPT4").standardName("name").sourceValue("val").sourceVocabulary("CPT4").sourceName("name").age(age2.getYears());
    cdrVersion = new CdrVersion();
    cdrVersion.setBigqueryDataset(testWorkbenchConfig.bigquery.dataSetId);
    cdrVersion.setBigqueryProject(testWorkbenchConfig.bigquery.projectId);
    cdrVersionDao.save(cdrVersion);
    workspace = new Workspace();
    workspace.setCdrVersion(cdrVersion);
    workspace.setWorkspaceNamespace(NAMESPACE);
    workspace.setFirecloudName(NAME);
    workspaceDao.save(workspace);
    cohort = new Cohort();
    cohort.setWorkspaceId(workspace.getWorkspaceId());
    cohortDao.save(cohort);
    CohortReview review = new CohortReview().cdrVersionId(cdrVersion.getCdrVersionId()).cohortId(cohort.getCohortId());
    cohortReviewDao.save(review);
    ParticipantCohortStatusKey key = new ParticipantCohortStatusKey().participantId(PARTICIPANT_ID).cohortReviewId(review.getCohortReviewId());
    ParticipantCohortStatus participantCohortStatus = new ParticipantCohortStatus().participantKey(key);
    participantCohortStatusDao.save(participantCohortStatus);
}
Also used : CdrVersion(org.pmiops.workbench.db.model.CdrVersion) Cohort(org.pmiops.workbench.db.model.Cohort) ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) ParticipantCohortStatusKey(org.pmiops.workbench.db.model.ParticipantCohortStatusKey) CohortReview(org.pmiops.workbench.db.model.CohortReview) Workspace(org.pmiops.workbench.db.model.Workspace) Before(org.junit.Before)

Example 4 with Cohort

use of org.pmiops.workbench.db.model.Cohort in project workbench by all-of-us.

the class CohortReviewControllerTest method createCohort.

private Cohort createCohort(long cohortId, long workspaceId, String definition) {
    Cohort cohort = new Cohort();
    cohort.setCohortId(cohortId);
    cohort.setWorkspaceId(workspaceId);
    cohort.setCriteria(definition);
    return cohort;
}
Also used : Cohort(org.pmiops.workbench.db.model.Cohort)

Example 5 with Cohort

use of org.pmiops.workbench.db.model.Cohort in project workbench by all-of-us.

the class CohortReviewControllerTest method assertFindByCohortIdAndCdrVersionId.

private void assertFindByCohortIdAndCdrVersionId(String namespace, String name, long cohortId, long cdrVersionId, Integer page, Integer pageSize, SortOrder sortOrder, ParticipantCohortStatusColumns sortColumn) {
    Integer pageParam = page == null ? 0 : page;
    Integer pageSizeParam = pageSize == null ? 25 : pageSize;
    sortColumn = (sortColumn == null || sortColumn.name().equals(sortColumn.PARTICIPANTID)) ? ParticipantCohortStatusColumns.PARTICIPANTID : sortColumn;
    sortOrder = sortOrder == null ? SortOrder.ASC : sortOrder;
    ParticipantCohortStatusKey key = new ParticipantCohortStatusKey().cohortReviewId(cohortId).participantId(participantId);
    final Date dob = new Date(System.currentTimeMillis());
    ParticipantCohortStatus dbParticipant = new ParticipantCohortStatus().participantKey(key).status(CohortStatus.INCLUDED).birthDate(dob).ethnicityConceptId(1L).genderConceptId(1L).raceConceptId(1L);
    org.pmiops.workbench.model.ParticipantCohortStatus respParticipant = new org.pmiops.workbench.model.ParticipantCohortStatus().participantId(1L).status(CohortStatus.INCLUDED).birthDate(dob.getTime()).ethnicityConceptId(1L).genderConceptId(1L).raceConceptId(1L);
    org.pmiops.workbench.model.CohortReview respCohortReview = new org.pmiops.workbench.model.CohortReview().cohortReviewId(1L).cohortId(cohortId).cdrVersionId(cdrVersionId).matchedParticipantCount(1000L).reviewedCount(0L).reviewSize(200L).page(pageParam).pageSize(pageSizeParam).sortOrder(sortOrder.toString()).sortColumn(sortColumn.toString()).participantCohortStatuses(Arrays.asList(respParticipant));
    List<ParticipantCohortStatus> participants = new ArrayList<ParticipantCohortStatus>();
    participants.add(dbParticipant);
    CohortReview cohortReviewAfter = new CohortReview();
    cohortReviewAfter.setCohortReviewId(1L);
    cohortReviewAfter.setCohortId(cohortId);
    cohortReviewAfter.setCdrVersionId(cdrVersionId);
    cohortReviewAfter.setMatchedParticipantCount(1000);
    cohortReviewAfter.setReviewSize(200);
    cohortReviewAfter.setCreationTime(new Timestamp(System.currentTimeMillis()));
    Cohort cohort = new Cohort();
    cohort.setWorkspaceId(1);
    Map<String, Map<Long, String>> concepts = new HashMap<>();
    concepts.put(GenderRaceEthnicityType.RACE.name(), new HashMap<>());
    concepts.put(GenderRaceEthnicityType.GENDER.name(), new HashMap<>());
    concepts.put(GenderRaceEthnicityType.ETHNICITY.name(), new HashMap<>());
    GenderRaceEthnicityConcept greConcept = new GenderRaceEthnicityConcept(concepts);
    when(cohortReviewService.findCohortReview(cohortId, cdrVersionId)).thenReturn(cohortReviewAfter);
    when(cohortReviewService.findAll(key.getCohortReviewId(), Collections.<Filter>emptyList(), new PageRequest(pageParam, pageSizeParam, sortOrder, sortColumn.toString()))).thenReturn(participants);
    when(cohortReviewService.validateMatchingWorkspace(namespace, name, workspaceId, WorkspaceAccessLevel.READER)).thenReturn(new Workspace());
    when(cohortReviewService.findCohort(cohortId)).thenReturn(cohort);
    ParticipantCohortStatuses request = new ParticipantCohortStatuses();
    request.page(page);
    request.pageSize(pageSize);
    request.sortOrder(sortOrder);
    request.sortColumn(sortColumn);
    request.pageFilterType(PageFilterType.PARTICIPANTCOHORTSTATUSES);
    ResponseEntity<org.pmiops.workbench.model.CohortReview> response = reviewController.getParticipantCohortStatuses(namespace, name, cohortId, cdrVersionId, request);
    org.pmiops.workbench.model.CohortReview actualCohortReview = response.getBody();
    respCohortReview.setCreationTime(actualCohortReview.getCreationTime());
    assertEquals(respCohortReview, response.getBody());
    verify(cohortReviewService, atLeast(1)).validateMatchingWorkspace(namespace, name, workspaceId, WorkspaceAccessLevel.READER);
    verify(cohortReviewService, atLeast(1)).findCohortReview(cohortId, cdrVersionId);
    verify(cohortReviewService, times(1)).findAll(key.getCohortReviewId(), Collections.<Filter>emptyList(), new PageRequest(pageParam, pageSizeParam, sortOrder, sortColumn.toString()));
    verify(cohortReviewService, atLeast(1)).findCohort(cohortId);
    verifyNoMoreMockInteractions();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) PageRequest(org.pmiops.workbench.cohortreview.util.PageRequest) ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) org.pmiops.workbench.model(org.pmiops.workbench.model) GenderRaceEthnicityConcept(org.pmiops.workbench.cdr.cache.GenderRaceEthnicityConcept) ParticipantCohortStatusKey(org.pmiops.workbench.db.model.ParticipantCohortStatusKey) Date(java.sql.Date) Cohort(org.pmiops.workbench.db.model.Cohort) CohortReview(org.pmiops.workbench.db.model.CohortReview) HashMap(java.util.HashMap) Map(java.util.Map) Workspace(org.pmiops.workbench.db.model.Workspace)

Aggregations

Cohort (org.pmiops.workbench.db.model.Cohort)33 Workspace (org.pmiops.workbench.db.model.Workspace)23 Test (org.junit.Test)17 WorkspaceAccessLevel (org.pmiops.workbench.model.WorkspaceAccessLevel)15 NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)10 CohortReview (org.pmiops.workbench.db.model.CohortReview)6 CohortAnnotationDefinition (org.pmiops.workbench.model.CohortAnnotationDefinition)6 ParticipantCohortStatus (org.pmiops.workbench.db.model.ParticipantCohortStatus)4 ConflictException (org.pmiops.workbench.exceptions.ConflictException)4 ModifyCohortAnnotationDefinitionRequest (org.pmiops.workbench.model.ModifyCohortAnnotationDefinitionRequest)4 Before (org.junit.Before)3 org.pmiops.workbench.model (org.pmiops.workbench.model)3 Gson (com.google.gson.Gson)2 PageRequest (org.pmiops.workbench.cohortreview.util.PageRequest)2 CdrVersion (org.pmiops.workbench.db.model.CdrVersion)2 ParticipantCohortStatusKey (org.pmiops.workbench.db.model.ParticipantCohortStatusKey)2 EmptyResponse (org.pmiops.workbench.model.EmptyResponse)2 Transactional (org.springframework.transaction.annotation.Transactional)2 QueryResult (com.google.cloud.bigquery.QueryResult)1 Date (java.sql.Date)1