Search in sources :

Example 6 with BadRequestException

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

the class FieldSetQueryBuilder method handleComparison.

private void handleComparison(ColumnFilter columnFilter, ColumnConfig columnConfig, StringBuilder sqlBuilder, Map<String, QueryParameterValue> paramMap) {
    String paramName = "p" + paramMap.size();
    Operator operator = columnFilter.getOperator();
    if (columnFilter.getValueNumbers() != null || columnFilter.getValues() != null) {
        throw new BadRequestException("Can't use valueNumbers or values with operator " + operator);
    }
    if (!((columnFilter.getValue() != null) ^ (columnFilter.getValueDate() != null) ^ (columnFilter.getValueNumber() != null) ^ (columnFilter.getValueNull() != null && columnFilter.getValueNull()))) {
        throw new BadRequestException("Exactly one of value, valueDate, valueNumber, and valueNull " + "must be specified for filter on column " + columnConfig.name);
    }
    if (operator.equals(Operator.LIKE) && columnFilter.getValue() == null) {
        throw new BadRequestException("LIKE operator only support with value");
    }
    if (columnFilter.getValue() != null) {
        if (!columnConfig.type.equals(ColumnType.STRING)) {
            throw new BadRequestException("Can't use value with column " + columnConfig.name + " of type " + columnConfig.type);
        }
        paramMap.put(paramName, QueryParameterValue.string(columnFilter.getValue()));
    } else if (columnFilter.getValueDate() != null) {
        if (columnConfig.type.equals(ColumnType.DATE)) {
            try {
                DATE_FORMAT.parse(columnFilter.getValueDate());
                paramMap.put(paramName, QueryParameterValue.date(columnFilter.getValueDate()));
            } catch (ParseException e) {
                throw new BadRequestException("Couldn't parse date value " + columnFilter.getValueDate() + "; expected format: " + DATE_FORMAT_PATTERN);
            }
        } else if (columnConfig.type.equals(ColumnType.TIMESTAMP)) {
            try {
                long timestamp = DATE_TIME_FORMAT.parseDateTime(columnFilter.getValueDate()).getMillis() * 1000;
                paramMap.put(paramName, QueryParameterValue.timestamp(timestamp));
            } catch (IllegalArgumentException e) {
                throw new BadRequestException("Couldn't parse timestamp value " + columnFilter.getValueDate() + "; expected format: " + DATE_TIME_FORMAT_PATTERN);
            }
        } else {
            throw new BadRequestException("Can't use valueDate with column " + columnConfig.name + " of type " + columnConfig.type);
        }
    } else if (columnFilter.getValueNumber() != null) {
        if (columnConfig.type.equals(ColumnType.FLOAT)) {
            paramMap.put(paramName, QueryParameterValue.float64(columnFilter.getValueNumber().doubleValue()));
        } else if (columnConfig.type.equals(ColumnType.INTEGER)) {
            paramMap.put(paramName, QueryParameterValue.int64(columnFilter.getValueNumber().longValue()));
        } else {
            throw new BadRequestException("Can't use valueNumber with column " + columnConfig.name + " of type " + columnConfig.type);
        }
    } else if (columnFilter.getValueNull() != null && columnFilter.getValueNull()) {
        if (operator != Operator.EQUAL) {
            throw new BadRequestException("Unsupported operator for valueNull: " + operator);
        }
        sqlBuilder.append(columnFilter.getColumnName());
        sqlBuilder.append(" is null\n");
        return;
    }
    sqlBuilder.append(columnFilter.getColumnName());
    sqlBuilder.append(' ');
    sqlBuilder.append(OperatorUtils.getSqlOperator(columnFilter.getOperator()));
    sqlBuilder.append(" @");
    sqlBuilder.append(paramName);
    sqlBuilder.append("\n");
}
Also used : Operator(org.pmiops.workbench.model.Operator) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ParseException(java.text.ParseException)

Example 7 with BadRequestException

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

the class ParticipantCounter method buildQuery.

public QueryJobConfiguration buildQuery(ParticipantCriteria participantCriteria, String sqlTemplate, String endSql, String mainTable, Map<String, QueryParameterValue> params) {
    SearchRequest request = participantCriteria.getSearchRequest();
    StringBuilder queryBuilder = new StringBuilder(sqlTemplate.replace("${mainTable}", mainTable));
    if (request == null) {
        queryBuilder.append(PERSON_ID_WHITELIST_TEMPLATE.replace("${mainTable}", mainTable));
        params.put(PERSON_ID_WHITELIST_PARAM, QueryParameterValue.array(participantCriteria.getParticipantIdsToInclude().toArray(new Long[0]), Long.class));
    } else {
        domainLookupService.findCodesForEmptyDomains(request.getIncludes());
        domainLookupService.findCodesForEmptyDomains(request.getExcludes());
        if (request.getIncludes().isEmpty() && request.getExcludes().isEmpty()) {
            throw new BadRequestException("Invalid SearchRequest: includes[] and excludes[] cannot both be empty");
        }
        // build query for included search groups
        StringJoiner joiner = buildQuery(request.getIncludes(), mainTable, params, false);
        // if includes is empty then don't add the excludes clause
        if (joiner.toString().isEmpty()) {
            joiner.merge(buildQuery(request.getExcludes(), mainTable, params, false));
        } else {
            joiner.merge(buildQuery(request.getExcludes(), mainTable, params, true));
        }
        Set<Long> participantIdsToExclude = participantCriteria.getParticipantIdsToExclude();
        if (!participantIdsToExclude.isEmpty()) {
            joiner.add(PERSON_ID_BLACKLIST_TEMPLATE.replace("${mainTable}", mainTable));
            params.put(PERSON_ID_BLACKLIST_PARAM, QueryParameterValue.array(participantIdsToExclude.toArray(new Long[0]), Long.class));
        }
        queryBuilder.append(joiner.toString());
    }
    queryBuilder.append(endSql.replace("${mainTable}", mainTable));
    return QueryJobConfiguration.newBuilder(queryBuilder.toString()).setNamedParameters(params).setUseLegacySql(false).build();
}
Also used : SearchRequest(org.pmiops.workbench.model.SearchRequest) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) StringJoiner(java.util.StringJoiner)

Example 8 with BadRequestException

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

use of org.pmiops.workbench.exceptions.BadRequestException 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);
}
Also used : CohortAnnotationDefinition(org.pmiops.workbench.db.model.CohortAnnotationDefinition) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException)

Example 10 with BadRequestException

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

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