Search in sources :

Example 1 with ParticipantCohortAnnotation

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

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

the class CohortReviewServiceImplTest method saveParticipantCohortAnnotationBadRequestCohortAnnotationDefinitionExists.

@Test
public void saveParticipantCohortAnnotationBadRequestCohortAnnotationDefinitionExists() throws Exception {
    long cohortAnnotationDefinitionId = 1;
    long cohortReviewId = 1;
    long participantId = 1;
    ParticipantCohortAnnotation participantCohortAnnotation = new ParticipantCohortAnnotation().annotationValueBoolean(Boolean.TRUE).cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId);
    CohortAnnotationDefinition cohortAnnotationDefinition = createCohortAnnotationDefinition(cohortAnnotationDefinitionId, AnnotationType.BOOLEAN);
    when(cohortAnnotationDefinitionDao.findOne(cohortAnnotationDefinitionId)).thenReturn(cohortAnnotationDefinition);
    when(participantCohortAnnotationDao.findByCohortReviewIdAndCohortAnnotationDefinitionIdAndParticipantId(cohortReviewId, cohortAnnotationDefinitionId, participantId)).thenReturn(participantCohortAnnotation);
    try {
        cohortReviewService.saveParticipantCohortAnnotation(cohortReviewId, participantCohortAnnotation);
        fail("Should have thrown BadRequestException!");
    } catch (BadRequestException e) {
        assertEquals("Invalid Request: Cohort annotation definition exists for id: " + cohortAnnotationDefinitionId, e.getMessage());
    }
    verify(cohortAnnotationDefinitionDao).findOne(cohortAnnotationDefinitionId);
    verifyNoMoreMockInteractions();
}
Also used : CohortAnnotationDefinition(org.pmiops.workbench.db.model.CohortAnnotationDefinition) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) Test(org.junit.Test)

Example 3 with ParticipantCohortAnnotation

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

the class CohortReviewServiceImplTest method saveParticipantCohortAnnotationAllTypes.

@Test
public void saveParticipantCohortAnnotationAllTypes() throws Exception {
    long cohortAnnotationDefinitionId = 1;
    long cohortReviewId = 1;
    long participantId = 1;
    // Boolean Type
    ParticipantCohortAnnotation expectedAnnotation = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueBoolean(Boolean.TRUE);
    assertSaveParticipantCohortAnnotation(expectedAnnotation, AnnotationType.BOOLEAN);
    // Date Type
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    expectedAnnotation = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueDateString("1999-02-01").annotationValueDate(new Date(sdf.parse("1999-02-01").getTime()));
    assertSaveParticipantCohortAnnotation(expectedAnnotation, AnnotationType.DATE);
    // Enum Type
    expectedAnnotation = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueEnum("enumValue");
    assertSaveParticipantCohortAnnotation(expectedAnnotation, AnnotationType.ENUM);
    // Integer Type
    expectedAnnotation = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueInteger(1);
    assertSaveParticipantCohortAnnotation(expectedAnnotation, AnnotationType.INTEGER);
    // String Type
    expectedAnnotation = new ParticipantCohortAnnotation().cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId).annotationValueString("String");
    assertSaveParticipantCohortAnnotation(expectedAnnotation, AnnotationType.STRING);
}
Also used : ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.sql.Date) Test(org.junit.Test)

Example 4 with ParticipantCohortAnnotation

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

the class CohortReviewServiceImplTest method updateParticipantCohortAnnotationModifyNotFoundCohortAnnotationDefinition.

@Test
public void updateParticipantCohortAnnotationModifyNotFoundCohortAnnotationDefinition() throws Exception {
    long annotationId = 1;
    long cohortAnnotationDefinitionId = 1;
    long cohortReviewId = 1;
    long participantId = 1;
    ParticipantCohortAnnotation participantCohortAnnotation = new ParticipantCohortAnnotation().annotationValueBoolean(Boolean.TRUE).cohortAnnotationDefinitionId(cohortAnnotationDefinitionId).cohortReviewId(cohortReviewId).participantId(participantId);
    ModifyParticipantCohortAnnotationRequest modifyRequest = new ModifyParticipantCohortAnnotationRequest().annotationValueBoolean(Boolean.FALSE);
    when(participantCohortAnnotationDao.findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId)).thenReturn(participantCohortAnnotation);
    when(cohortAnnotationDefinitionDao.findOne(cohortAnnotationDefinitionId)).thenReturn(null);
    try {
        cohortReviewService.updateParticipantCohortAnnotation(annotationId, cohortReviewId, participantId, modifyRequest);
        fail("Should have thrown NotFoundException!");
    } catch (NotFoundException e) {
        assertEquals("Not Found: No cohort annotation definition found for id: " + cohortAnnotationDefinitionId, e.getMessage());
    }
    verify(participantCohortAnnotationDao).findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId);
    verify(cohortAnnotationDefinitionDao).findOne(cohortAnnotationDefinitionId);
    verifyNoMoreMockInteractions();
}
Also used : NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) ModifyParticipantCohortAnnotationRequest(org.pmiops.workbench.model.ModifyParticipantCohortAnnotationRequest) Test(org.junit.Test)

Example 5 with ParticipantCohortAnnotation

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

the class CohortReviewServiceImplTest method deleteParticipantCohortAnnotation.

@Test
public void deleteParticipantCohortAnnotation() throws Exception {
    long annotationId = 1;
    long cohortReviewId = 2;
    long participantId = 3;
    when(participantCohortAnnotationDao.findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId)).thenReturn(new ParticipantCohortAnnotation());
    cohortReviewService.deleteParticipantCohortAnnotation(annotationId, cohortReviewId, participantId);
    verify(participantCohortAnnotationDao).findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId);
    verifyNoMoreMockInteractions();
}
Also used : ParticipantCohortAnnotation(org.pmiops.workbench.db.model.ParticipantCohortAnnotation) Test(org.junit.Test)

Aggregations

ParticipantCohortAnnotation (org.pmiops.workbench.db.model.ParticipantCohortAnnotation)16 Test (org.junit.Test)12 CohortAnnotationDefinition (org.pmiops.workbench.db.model.CohortAnnotationDefinition)5 NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)5 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)4 CohortAnnotationEnumValue (org.pmiops.workbench.db.model.CohortAnnotationEnumValue)3 ModifyParticipantCohortAnnotationRequest (org.pmiops.workbench.model.ModifyParticipantCohortAnnotationRequest)3 Date (java.sql.Date)2 SimpleDateFormat (java.text.SimpleDateFormat)2 BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)2 ParseException (java.text.ParseException)1 List (java.util.List)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