Search in sources :

Example 1 with CohortAnnotationEnumValue

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

the class CohortReviewServiceImpl method validateParticipantCohortAnnotation.

/**
 * Helper method to validate that requested annotations are proper.
 *
 * @param participantCohortAnnotation
 */
private void validateParticipantCohortAnnotation(ParticipantCohortAnnotation participantCohortAnnotation, CohortAnnotationDefinition cohortAnnotationDefinition) {
    if (cohortAnnotationDefinition.getAnnotationType().equals(AnnotationType.BOOLEAN)) {
        if (participantCohortAnnotation.getAnnotationValueBoolean() == null) {
            throw createBadRequestException(AnnotationType.BOOLEAN.name(), participantCohortAnnotation.getCohortAnnotationDefinitionId());
        }
    } else if (cohortAnnotationDefinition.getAnnotationType().equals(AnnotationType.STRING)) {
        if (StringUtils.isBlank(participantCohortAnnotation.getAnnotationValueString())) {
            throw createBadRequestException(AnnotationType.STRING.name(), participantCohortAnnotation.getCohortAnnotationDefinitionId());
        }
    } else if (cohortAnnotationDefinition.getAnnotationType().equals(AnnotationType.DATE)) {
        if (StringUtils.isBlank(participantCohortAnnotation.getAnnotationValueDateString())) {
            throw createBadRequestException(AnnotationType.DATE.name(), participantCohortAnnotation.getCohortAnnotationDefinitionId());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = new Date(sdf.parse(participantCohortAnnotation.getAnnotationValueDateString()).getTime());
            participantCohortAnnotation.setAnnotationValueDate(date);
        } catch (ParseException e) {
            throw new BadRequestException(String.format("Invalid Request: Please provide a valid %s value (%s) for annotation defintion id: %s", AnnotationType.DATE.name(), sdf.toPattern(), participantCohortAnnotation.getCohortAnnotationDefinitionId()));
        }
    } else if (cohortAnnotationDefinition.getAnnotationType().equals(AnnotationType.INTEGER)) {
        if (participantCohortAnnotation.getAnnotationValueInteger() == null) {
            throw createBadRequestException(AnnotationType.INTEGER.name(), participantCohortAnnotation.getCohortAnnotationDefinitionId());
        }
    } else if (cohortAnnotationDefinition.getAnnotationType().equals(AnnotationType.ENUM)) {
        if (StringUtils.isBlank(participantCohortAnnotation.getAnnotationValueEnum())) {
            throw createBadRequestException(AnnotationType.ENUM.name(), participantCohortAnnotation.getCohortAnnotationDefinitionId());
        }
        List<CohortAnnotationEnumValue> enumValues = cohortAnnotationDefinition.getEnumValues().stream().filter(enumValue -> participantCohortAnnotation.getAnnotationValueEnum().equals(enumValue.getName())).collect(Collectors.toList());
        if (enumValues.isEmpty()) {
            throw createBadRequestException(AnnotationType.ENUM.name(), participantCohortAnnotation.getCohortAnnotationDefinitionId());
        }
        participantCohortAnnotation.setCohortAnnotationEnumValue(enumValues.get(0));
    }
}
Also used : AnnotationType(org.pmiops.workbench.model.AnnotationType) CohortReview(org.pmiops.workbench.db.model.CohortReview) PageRequest(org.pmiops.workbench.cohortreview.util.PageRequest) Provider(javax.inject.Provider) ModifyParticipantCohortAnnotationRequest(org.pmiops.workbench.model.ModifyParticipantCohortAnnotationRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) StringUtils(org.apache.commons.lang3.StringUtils) CohortReviewDao(org.pmiops.workbench.db.dao.CohortReviewDao) CohortAnnotationDefinitionDao(org.pmiops.workbench.db.dao.CohortAnnotationDefinitionDao) GenderRaceEthnicityConcept(org.pmiops.workbench.cdr.cache.GenderRaceEthnicityConcept) ParticipantCohortAnnotationDao(org.pmiops.workbench.db.dao.ParticipantCohortAnnotationDao) Service(org.springframework.stereotype.Service) CohortAnnotationEnumValue(org.pmiops.workbench.db.model.CohortAnnotationEnumValue) Workspace(org.pmiops.workbench.db.model.Workspace) ParseException(java.text.ParseException) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) WorkspaceService(org.pmiops.workbench.db.dao.WorkspaceService) ParticipantCohortStatusDao(org.pmiops.workbench.db.dao.ParticipantCohortStatusDao) Cohort(org.pmiops.workbench.db.model.Cohort) CohortDao(org.pmiops.workbench.db.dao.CohortDao) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Date(java.sql.Date) Filter(org.pmiops.workbench.model.Filter) List(java.util.List) CohortAnnotationDefinition(org.pmiops.workbench.db.model.CohortAnnotationDefinition) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) WorkspaceAccessLevel(org.pmiops.workbench.model.WorkspaceAccessLevel) ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) Transactional(org.springframework.transaction.annotation.Transactional) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.sql.Date) CohortAnnotationEnumValue(org.pmiops.workbench.db.model.CohortAnnotationEnumValue)

Example 2 with CohortAnnotationEnumValue

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

the class CohortReviewControllerTest method createParticipantCohortAnnotation.

private org.pmiops.workbench.db.model.ParticipantCohortAnnotation createParticipantCohortAnnotation(ParticipantCohortAnnotation request) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = request.getAnnotationValueDate() == null ? null : new Date(sdf.parse(request.getAnnotationValueDate().toString()).getTime());
    CohortAnnotationEnumValue enumValue = null;
    if (request.getAnnotationValueEnum() != null) {
        enumValue = new CohortAnnotationEnumValue();
        enumValue.setName(request.getAnnotationValueEnum());
    }
    return new org.pmiops.workbench.db.model.ParticipantCohortAnnotation().cohortAnnotationDefinitionId(request.getCohortAnnotationDefinitionId()).annotationId(request.getAnnotationId()).participantId(request.getParticipantId()).cohortReviewId(request.getCohortReviewId()).annotationValueBoolean(request.getAnnotationValueBoolean()).annotationValueString(request.getAnnotationValueString()).annotationValueInteger(request.getAnnotationValueInteger()).annotationValueDate(date).annotationValueEnum(request.getAnnotationValueEnum()).cohortAnnotationEnumValue(enumValue);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.sql.Date) CohortAnnotationEnumValue(org.pmiops.workbench.db.model.CohortAnnotationEnumValue)

Example 3 with CohortAnnotationEnumValue

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

the class ParticipantCohortAnnotationDaoTest method findByCohortReviewIdAndParticipantIdEnum.

@Test
public void findByCohortReviewIdAndParticipantIdEnum() throws Exception {
    ParticipantCohortAnnotation expectedPCA = new ParticipantCohortAnnotation().annotationId(annotationId).cohortAnnotationDefinitionId(cohortAnnotationDefinitionId1).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueEnum("test");
    List<ParticipantCohortAnnotation> annotations = participantCohortAnnotationDao.findByCohortReviewIdAndParticipantId(cohortReviewId, participantId);
    assertEquals(2, annotations.size());
    assertEquals(expectedPCA, annotations.get(1));
    assertEquals(new CohortAnnotationEnumValue().name("z").order(0), annotations.get(1).getCohortAnnotationEnumValue());
}
Also used : ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) CohortAnnotationEnumValue(org.pmiops.workbench.db.model.CohortAnnotationEnumValue) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 4 with CohortAnnotationEnumValue

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

the class ParticipantCohortAnnotationDaoTest method setUp.

@Before
public void setUp() throws Exception {
    CohortAnnotationDefinition cohortAnnotationDefinition = new CohortAnnotationDefinition().cohortId(COHORT_ID).columnName("enum").annotationType(AnnotationType.ENUM);
    CohortAnnotationEnumValue enumValue1 = new CohortAnnotationEnumValue().name("z").order(0).cohortAnnotationDefinition(cohortAnnotationDefinition);
    CohortAnnotationEnumValue enumValue2 = new CohortAnnotationEnumValue().name("r").order(1).cohortAnnotationDefinition(cohortAnnotationDefinition);
    CohortAnnotationEnumValue enumValue3 = new CohortAnnotationEnumValue().name("a").order(2).cohortAnnotationDefinition(cohortAnnotationDefinition);
    cohortAnnotationDefinition.getEnumValues().add(enumValue1);
    cohortAnnotationDefinition.getEnumValues().add(enumValue2);
    cohortAnnotationDefinition.getEnumValues().add(enumValue3);
    cohortAnnotationDefinitionDao.save(cohortAnnotationDefinition);
    ParticipantCohortAnnotation pca = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueBoolean(Boolean.TRUE);
    ParticipantCohortAnnotation pca1 = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId1).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueEnum("test");
    pca1.setCohortAnnotationEnumValue(enumValue1);
    annotationId = participantCohortAnnotationDao.save(pca).getAnnotationId();
    participantCohortAnnotationDao.save(pca1);
}
Also used : CohortAnnotationDefinition(org.pmiops.workbench.db.model.CohortAnnotationDefinition) ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) CohortAnnotationEnumValue(org.pmiops.workbench.db.model.CohortAnnotationEnumValue) Before(org.junit.Before)

Example 5 with CohortAnnotationEnumValue

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

the class CohortReviewControllerTest method assertCreateParticipantCohortAnnotation.

private void assertCreateParticipantCohortAnnotation(ParticipantCohortAnnotation request, AnnotationType annotationType) throws Exception {
    ParticipantCohortStatusKey key = new ParticipantCohortStatusKey().cohortReviewId(cohortReviewId).participantId(participantId);
    ParticipantCohortStatus participantCohortStatus = new ParticipantCohortStatus().participantKey(key);
    CohortAnnotationDefinition cohortAnnotationDefinition = new CohortAnnotationDefinition().annotationType(annotationType);
    if (request.getAnnotationValueEnum() != null) {
        CohortAnnotationEnumValue cohortAnnotationEnumValue = new CohortAnnotationEnumValue().name(request.getAnnotationValueEnum());
        cohortAnnotationDefinition.setEnumValues(new TreeSet(Arrays.asList(cohortAnnotationEnumValue)));
    }
    when(cohortReviewService.findCohort(cohortId)).thenReturn(createCohort(cohortId, workspaceId, null));
    when(cohortReviewService.validateMatchingWorkspace(namespace, name, workspaceId, WorkspaceAccessLevel.WRITER)).thenReturn(new Workspace());
    when(cohortReviewService.findCohortReview(cohortId, cdrVersionId)).thenReturn(createCohortReview(0, cohortId, cohortReviewId, cdrVersionId, null));
    when(cohortReviewService.saveParticipantCohortAnnotation(isA(Long.class), isA(org.pmiops.workbench.db.model.ParticipantCohortAnnotation.class))).thenReturn(createParticipantCohortAnnotation(request));
    ParticipantCohortAnnotation response = reviewController.createParticipantCohortAnnotation(namespace, name, cohortId, cdrVersionId, participantId, request).getBody();
    assertEquals(request, response);
    verify(cohortReviewService, atLeastOnce()).findCohort(cohortId);
    verify(cohortReviewService, atLeastOnce()).validateMatchingWorkspace(namespace, name, workspaceId, WorkspaceAccessLevel.WRITER);
    verify(cohortReviewService, atLeastOnce()).findCohortReview(cohortId, cdrVersionId);
    verify(cohortReviewService, atLeastOnce()).saveParticipantCohortAnnotation(isA(Long.class), isA(org.pmiops.workbench.db.model.ParticipantCohortAnnotation.class));
    verifyNoMoreMockInteractions();
}
Also used : CohortAnnotationDefinition(org.pmiops.workbench.db.model.CohortAnnotationDefinition) ParticipantCohortStatus(org.pmiops.workbench.db.model.ParticipantCohortStatus) TreeSet(java.util.TreeSet) ParticipantCohortStatusKey(org.pmiops.workbench.db.model.ParticipantCohortStatusKey) CohortAnnotationEnumValue(org.pmiops.workbench.db.model.CohortAnnotationEnumValue) Workspace(org.pmiops.workbench.db.model.Workspace)

Aggregations

CohortAnnotationEnumValue (org.pmiops.workbench.db.model.CohortAnnotationEnumValue)6 CohortAnnotationDefinition (org.pmiops.workbench.db.model.CohortAnnotationDefinition)4 ParticipantCohortAnnotation (org.pmiops.workbench.db.model.ParticipantCohortAnnotation)3 Date (java.sql.Date)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Test (org.junit.Test)2 ParticipantCohortStatus (org.pmiops.workbench.db.model.ParticipantCohortStatus)2 Workspace (org.pmiops.workbench.db.model.Workspace)2 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)2 ParseException (java.text.ParseException)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Provider (javax.inject.Provider)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Before (org.junit.Before)1 GenderRaceEthnicityConcept (org.pmiops.workbench.cdr.cache.GenderRaceEthnicityConcept)1 PageRequest (org.pmiops.workbench.cohortreview.util.PageRequest)1 CohortAnnotationDefinitionDao (org.pmiops.workbench.db.dao.CohortAnnotationDefinitionDao)1