use of org.pmiops.workbench.db.model.CohortAnnotationDefinition 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));
}
}
use of org.pmiops.workbench.db.model.CohortAnnotationDefinition in project workbench by all-of-us.
the class CohortReviewServiceImpl method saveParticipantCohortAnnotation.
@Override
public ParticipantCohortAnnotation saveParticipantCohortAnnotation(Long cohortReviewId, ParticipantCohortAnnotation participantCohortAnnotation) {
CohortAnnotationDefinition cohortAnnotationDefinition = findCohortAnnotationDefinition(participantCohortAnnotation.getCohortAnnotationDefinitionId());
validateParticipantCohortAnnotation(participantCohortAnnotation, cohortAnnotationDefinition);
if (findParticipantCohortAnnotation(cohortReviewId, participantCohortAnnotation.getCohortAnnotationDefinitionId(), participantCohortAnnotation.getParticipantId()) != null) {
throw new BadRequestException(String.format("Invalid Request: Cohort annotation definition exists for id: %s", participantCohortAnnotation.getCohortAnnotationDefinitionId()));
}
return participantCohortAnnotationDao.save(participantCohortAnnotation);
}
use of org.pmiops.workbench.db.model.CohortAnnotationDefinition 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();
}
use of org.pmiops.workbench.db.model.CohortAnnotationDefinition in project workbench by all-of-us.
the class CohortReviewServiceImplTest method assertParticipantCohortAnnotationModifyBadRequest.
private void assertParticipantCohortAnnotationModifyBadRequest(Long annotationId, ParticipantCohortAnnotation participantCohortAnnotation, AnnotationType annotationType) {
Long cohortAnnotationDefinitionId = participantCohortAnnotation.getCohortAnnotationDefinitionId();
Long cohortReviewId = participantCohortAnnotation.getCohortReviewId();
Long participantId = participantCohortAnnotation.getParticipantId();
CohortAnnotationDefinition cohortAnnotationDefinition = createCohortAnnotationDefinition(cohortAnnotationDefinitionId, annotationType);
when(participantCohortAnnotationDao.findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId)).thenReturn(participantCohortAnnotation);
when(cohortAnnotationDefinitionDao.findOne(cohortAnnotationDefinitionId)).thenReturn(cohortAnnotationDefinition);
try {
cohortReviewService.updateParticipantCohortAnnotation(annotationId, cohortReviewId, participantId, new ModifyParticipantCohortAnnotationRequest());
fail("Should have thrown BadRequestExcpetion!");
} catch (BadRequestException e) {
assertEquals("Invalid Request: Please provide a valid " + cohortAnnotationDefinition.getAnnotationType().name() + " value for annotation defintion id: " + cohortAnnotationDefinition.getCohortAnnotationDefinitionId(), e.getMessage());
}
verify(participantCohortAnnotationDao, atLeastOnce()).findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId);
verify(cohortAnnotationDefinitionDao, atLeastOnce()).findOne(cohortAnnotationDefinitionId);
verifyNoMoreMockInteractions();
}
use of org.pmiops.workbench.db.model.CohortAnnotationDefinition in project workbench by all-of-us.
the class CohortReviewServiceImplTest method findCohortAnnotationDefinition.
@Test
public void findCohortAnnotationDefinition() throws Exception {
long cohortAnnotationDefinitionId = 1;
when(cohortAnnotationDefinitionDao.findOne(cohortAnnotationDefinitionId)).thenReturn(new CohortAnnotationDefinition());
cohortReviewService.findCohortAnnotationDefinition(cohortAnnotationDefinitionId);
verify(cohortAnnotationDefinitionDao).findOne(cohortAnnotationDefinitionId);
verifyNoMoreMockInteractions();
}
Aggregations