Search in sources :

Example 91 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapper method map.

@Transactional(readOnly = true)
public TrackedEntityInstanceQueryParams map(TrackedEntityInstanceCriteria criteria) {
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    final Date programEnrollmentStartDate = ObjectUtils.firstNonNull(criteria.getProgramEnrollmentStartDate(), criteria.getProgramStartDate());
    final Date programEnrollmentEndDate = ObjectUtils.firstNonNull(criteria.getProgramEnrollmentEndDate(), criteria.getProgramEndDate());
    Set<OrganisationUnit> possibleSearchOrgUnits = new HashSet<>();
    User user = currentUserService.getCurrentUser();
    if (user != null) {
        possibleSearchOrgUnits = user.getTeiSearchOrganisationUnitsWithFallback();
    }
    QueryFilter queryFilter = getQueryFilter(criteria.getQuery());
    Map<String, TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes().stream().collect(Collectors.toMap(TrackedEntityAttribute::getUid, att -> att));
    if (criteria.getAttribute() != null) {
        for (String attr : criteria.getAttribute()) {
            QueryItem it = getQueryItem(attr, attributes);
            params.getAttributes().add(it);
        }
    }
    if (criteria.getFilter() != null) {
        for (String filt : criteria.getFilter()) {
            QueryItem it = getQueryItem(filt, attributes);
            params.getFilters().add(it);
        }
    }
    for (String orgUnit : criteria.getOrgUnits()) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnit);
        if (organisationUnit == null) {
            throw new IllegalQueryException("Organisation unit does not exist: " + orgUnit);
        }
        if (!organisationUnitService.isInUserHierarchy(organisationUnit.getUid(), possibleSearchOrgUnits)) {
            throw new IllegalQueryException("Organisation unit is not part of the search scope: " + orgUnit);
        }
        params.getOrganisationUnits().add(organisationUnit);
    }
    validateAssignedUser(criteria);
    if (criteria.getOuMode() == OrganisationUnitSelectionMode.CAPTURE && user != null) {
        params.getOrganisationUnits().addAll(user.getOrganisationUnits());
    }
    Program program = validateProgram(criteria);
    List<OrderParam> orderParams = toOrderParams(criteria.getOrder());
    validateOrderParams(program, orderParams, attributes);
    params.setQuery(queryFilter).setProgram(program).setProgramStage(validateProgramStage(criteria, program)).setProgramStatus(criteria.getProgramStatus()).setFollowUp(criteria.getFollowUp()).setLastUpdatedStartDate(criteria.getLastUpdatedStartDate()).setLastUpdatedEndDate(criteria.getLastUpdatedEndDate()).setLastUpdatedDuration(criteria.getLastUpdatedDuration()).setProgramEnrollmentStartDate(programEnrollmentStartDate).setProgramEnrollmentEndDate(programEnrollmentEndDate).setProgramIncidentStartDate(criteria.getProgramIncidentStartDate()).setProgramIncidentEndDate(criteria.getProgramIncidentEndDate()).setTrackedEntityType(validateTrackedEntityType(criteria)).setOrganisationUnitMode(criteria.getOuMode()).setEventStatus(criteria.getEventStatus()).setEventStartDate(criteria.getEventStartDate()).setEventEndDate(criteria.getEventEndDate()).setAssignedUserSelectionMode(criteria.getAssignedUserMode()).setAssignedUsers(criteria.getAssignedUsers()).setTrackedEntityInstanceUids(criteria.getTrackedEntityInstances()).setSkipMeta(criteria.isSkipMeta()).setPage(criteria.getPage()).setPageSize(criteria.getPageSize()).setTotalPages(criteria.isTotalPages()).setSkipPaging(criteria.isSkipPaging()).setIncludeDeleted(criteria.isIncludeDeleted()).setIncludeAllAttributes(criteria.isIncludeAllAttributes()).setUser(user).setOrders(orderParams);
    return params;
}
Also used : OrderColumn.isStaticColumn(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.OrderColumn.isStaticColumn) QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) TrackerTrackedEntityCriteria(org.hisp.dhis.webapi.controller.event.webrequest.tracker.TrackerTrackedEntityCriteria) Date(java.util.Date) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Function(java.util.function.Function) Program(org.hisp.dhis.program.Program) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) TrackerTrackedEntityCriteriaMapper(org.hisp.dhis.webapi.controller.event.webrequest.tracker.mapper.TrackerTrackedEntityCriteriaMapper) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) User(org.hisp.dhis.user.User) Mappers(org.mapstruct.factory.Mappers) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) QueryOperator(org.hisp.dhis.common.QueryOperator) Set(java.util.Set) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) Collectors(java.util.stream.Collectors) ProgramStage(org.hisp.dhis.program.ProgramStage) OrderParamsHelper.toOrderParams(org.hisp.dhis.webapi.controller.event.mapper.OrderParamsHelper.toOrderParams) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Component(org.springframework.stereotype.Component) DimensionalObject(org.hisp.dhis.common.DimensionalObject) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AssignedUserSelectionMode(org.hisp.dhis.common.AssignedUserSelectionMode) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) QueryFilter(org.hisp.dhis.common.QueryFilter) ProgramService(org.hisp.dhis.program.ProgramService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Transactional(org.springframework.transaction.annotation.Transactional) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Date(java.util.Date) QueryFilter(org.hisp.dhis.common.QueryFilter) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 92 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapper method validateProgramStage.

private ProgramStage validateProgramStage(TrackedEntityInstanceCriteria criteria, Program program) {
    final String programStage = criteria.getProgramStage();
    ProgramStage ps = programStage != null ? getProgramStageFromProgram(program, programStage) : null;
    if (programStage != null && ps == null) {
        throw new IllegalQueryException("Program does not contain the specified programStage: " + programStage);
    }
    return ps;
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 93 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapper method getQueryItem.

/**
 * Creates a QueryItem from the given item string. Item is on format
 * {attribute-id}:{operator}:{filter-value}[:{operator}:{filter-value}].
 * Only the attribute-id is mandatory.
 */
private QueryItem getQueryItem(String item, Map<String, TrackedEntityAttribute> attributes) {
    String[] split = item.split(DimensionalObject.DIMENSION_NAME_SEP);
    if (split.length % 2 != 1) {
        throw new IllegalQueryException("Query item or filter is invalid: " + item);
    }
    QueryItem queryItem = getItem(split[0], attributes);
    if (// Filters specified
    split.length > 1) {
        for (int i = 1; i < split.length; i += 2) {
            QueryOperator operator = QueryOperator.fromString(split[i]);
            queryItem.getFilters().add(new QueryFilter(operator, split[i + 1]));
        }
    }
    return queryItem;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) QueryFilter(org.hisp.dhis.common.QueryFilter) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) QueryOperator(org.hisp.dhis.common.QueryOperator)

Example 94 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapper method validateProgram.

private Program validateProgram(TrackedEntityInstanceCriteria criteria) {
    Function<String, Program> getProgram = uid -> {
        if (isNotEmpty(uid)) {
            return programService.getProgram(uid);
        }
        return null;
    };
    final Program program = getProgram.apply(criteria.getProgram());
    if (isNotEmpty(criteria.getProgram()) && program == null) {
        throw new IllegalQueryException("Program does not exist: " + criteria.getProgram());
    }
    return program;
}
Also used : OrderColumn.isStaticColumn(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.OrderColumn.isStaticColumn) QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) TrackerTrackedEntityCriteria(org.hisp.dhis.webapi.controller.event.webrequest.tracker.TrackerTrackedEntityCriteria) Date(java.util.Date) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Function(java.util.function.Function) Program(org.hisp.dhis.program.Program) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) TrackerTrackedEntityCriteriaMapper(org.hisp.dhis.webapi.controller.event.webrequest.tracker.mapper.TrackerTrackedEntityCriteriaMapper) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) User(org.hisp.dhis.user.User) Mappers(org.mapstruct.factory.Mappers) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) QueryOperator(org.hisp.dhis.common.QueryOperator) Set(java.util.Set) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) Collectors(java.util.stream.Collectors) ProgramStage(org.hisp.dhis.program.ProgramStage) OrderParamsHelper.toOrderParams(org.hisp.dhis.webapi.controller.event.mapper.OrderParamsHelper.toOrderParams) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Component(org.springframework.stereotype.Component) DimensionalObject(org.hisp.dhis.common.DimensionalObject) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AssignedUserSelectionMode(org.hisp.dhis.common.AssignedUserSelectionMode) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) QueryFilter(org.hisp.dhis.common.QueryFilter) ProgramService(org.hisp.dhis.program.ProgramService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Transactional(org.springframework.transaction.annotation.Transactional) Program(org.hisp.dhis.program.Program) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Example 95 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapper method validateTrackedEntityType.

private TrackedEntityType validateTrackedEntityType(TrackedEntityInstanceCriteria criteria) {
    Function<String, TrackedEntityType> getTeiType = uid -> {
        if (isNotEmpty(uid)) {
            return trackedEntityTypeService.getTrackedEntityType(uid);
        }
        return null;
    };
    final TrackedEntityType trackedEntityType = getTeiType.apply(criteria.getTrackedEntityType());
    if (isNotEmpty(criteria.getTrackedEntityType()) && trackedEntityType == null) {
        throw new IllegalQueryException("Tracked entity type does not exist: " + criteria.getTrackedEntityType());
    }
    return trackedEntityType;
}
Also used : OrderColumn.isStaticColumn(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams.OrderColumn.isStaticColumn) QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) TrackerTrackedEntityCriteria(org.hisp.dhis.webapi.controller.event.webrequest.tracker.TrackerTrackedEntityCriteria) Date(java.util.Date) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Function(java.util.function.Function) Program(org.hisp.dhis.program.Program) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) TrackerTrackedEntityCriteriaMapper(org.hisp.dhis.webapi.controller.event.webrequest.tracker.mapper.TrackerTrackedEntityCriteriaMapper) ObjectUtils(org.apache.commons.lang3.ObjectUtils) Map(java.util.Map) User(org.hisp.dhis.user.User) Mappers(org.mapstruct.factory.Mappers) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) QueryOperator(org.hisp.dhis.common.QueryOperator) Set(java.util.Set) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) Collectors(java.util.stream.Collectors) ProgramStage(org.hisp.dhis.program.ProgramStage) OrderParamsHelper.toOrderParams(org.hisp.dhis.webapi.controller.event.mapper.OrderParamsHelper.toOrderParams) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Component(org.springframework.stereotype.Component) DimensionalObject(org.hisp.dhis.common.DimensionalObject) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AssignedUserSelectionMode(org.hisp.dhis.common.AssignedUserSelectionMode) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) QueryFilter(org.hisp.dhis.common.QueryFilter) ProgramService(org.hisp.dhis.program.ProgramService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Transactional(org.springframework.transaction.annotation.Transactional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Aggregations

IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)98 Test (org.junit.jupiter.api.Test)26 ErrorMessage (org.hisp.dhis.feedback.ErrorMessage)22 HashSet (java.util.HashSet)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)17 User (org.hisp.dhis.user.User)14 QueryItem (org.hisp.dhis.common.QueryItem)13 ArrayList (java.util.ArrayList)12 Date (java.util.Date)11 Program (org.hisp.dhis.program.Program)11 QueryFilter (org.hisp.dhis.common.QueryFilter)10 TrackedEntityInstanceCriteria (org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria)10 Transactional (org.springframework.transaction.annotation.Transactional)10 QueryOperator (org.hisp.dhis.common.QueryOperator)9 List (java.util.List)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)7 Map (java.util.Map)6 Set (java.util.Set)6