Search in sources :

Example 6 with TrackedEntityInstanceCriteria

use of org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapperTest method verifyCriteriaMappingFailOnUserNonInOuHierarchy.

@Test
void verifyCriteriaMappingFailOnUserNonInOuHierarchy() {
    // Force Current User Service to return a User without search org unit
    ReflectionTestUtils.setField(trackedEntityCriteriaMapper, "currentUserService", new MockCurrentUserService(createUser("testUser2")));
    TrackedEntityInstanceCriteria criteria = new TrackedEntityInstanceCriteria();
    criteria.setOu(organisationUnit.getUid());
    IllegalQueryException e = assertThrows(IllegalQueryException.class, () -> trackedEntityCriteriaMapper.map(criteria));
    assertEquals("Organisation unit is not part of the search scope: " + organisationUnit.getUid(), e.getMessage());
}
Also used : MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.jupiter.api.Test)

Example 7 with TrackedEntityInstanceCriteria

use of org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapperTest method verifyCriteriaMapping.

@Test
void verifyCriteriaMapping() {
    TrackedEntityInstanceCriteria criteria = new TrackedEntityInstanceCriteria();
    criteria.setQuery("query-test");
    criteria.setAttribute(newHashSet(attrD.getUid(), attrE.getUid()));
    criteria.setFilter(newHashSet(filtF.getUid(), filtG.getUid()));
    criteria.setOu(organisationUnit.getUid());
    criteria.setOuMode(OrganisationUnitSelectionMode.DESCENDANTS);
    criteria.setProgram(programA.getUid());
    criteria.setProgramStatus(ProgramStatus.ACTIVE);
    criteria.setFollowUp(true);
    criteria.setLastUpdatedStartDate(getDate(2019, 1, 1));
    criteria.setLastUpdatedEndDate(getDate(2020, 1, 1));
    criteria.setLastUpdatedDuration("20");
    criteria.setProgramEnrollmentStartDate(getDate(2019, 8, 5));
    criteria.setProgramEnrollmentEndDate(getDate(2020, 8, 5));
    criteria.setProgramIncidentStartDate(getDate(2019, 5, 5));
    criteria.setProgramIncidentEndDate(getDate(2020, 5, 5));
    criteria.setTrackedEntityType(trackedEntityTypeA.getUid());
    criteria.setEventStatus(EventStatus.COMPLETED);
    criteria.setEventStartDate(getDate(2019, 7, 7));
    criteria.setEventEndDate(getDate(2020, 7, 7));
    criteria.setAssignedUserMode(AssignedUserSelectionMode.PROVIDED);
    criteria.setAssignedUser(userIds);
    criteria.setSkipMeta(true);
    criteria.setPage(1);
    criteria.setPageSize(50);
    criteria.setTotalPages(false);
    criteria.setSkipPaging(false);
    criteria.setIncludeDeleted(true);
    criteria.setIncludeAllAttributes(true);
    criteria.setOrder(Collections.singletonList(OrderCriteria.of("created", OrderParam.SortDirection.ASC)));
    final TrackedEntityInstanceQueryParams queryParams = trackedEntityCriteriaMapper.map(criteria);
    assertThat(queryParams.getQuery().getFilter(), is("query-test"));
    assertThat(queryParams.getQuery().getOperator(), is(QueryOperator.EQ));
    assertThat(queryParams.getProgram(), is(programA));
    assertThat(queryParams.getTrackedEntityType(), is(trackedEntityTypeA));
    assertThat(queryParams.getOrganisationUnits(), hasSize(1));
    assertThat(queryParams.getOrganisationUnits().iterator().next(), is(organisationUnit));
    assertThat(queryParams.getAttributes(), hasSize(2));
    assertTrue(queryParams.getAttributes().stream().anyMatch(a -> a.getItem().getUid().equals(attrD.getUid())));
    assertTrue(queryParams.getAttributes().stream().anyMatch(a -> a.getItem().getUid().equals(attrE.getUid())));
    assertThat(queryParams.getFilters(), hasSize(2));
    assertTrue(queryParams.getFilters().stream().anyMatch(a -> a.getItem().getUid().equals(filtF.getUid())));
    assertTrue(queryParams.getFilters().stream().anyMatch(a -> a.getItem().getUid().equals(filtG.getUid())));
    assertThat(queryParams.getPageSizeWithDefault(), is(50));
    assertThat(queryParams.getPageSize(), is(50));
    assertThat(queryParams.getPage(), is(1));
    assertThat(queryParams.isTotalPages(), is(false));
    assertThat(queryParams.getProgramStatus(), is(ProgramStatus.ACTIVE));
    assertThat(queryParams.getFollowUp(), is(true));
    assertThat(queryParams.getLastUpdatedStartDate(), is(criteria.getLastUpdatedStartDate()));
    assertThat(queryParams.getLastUpdatedEndDate(), is(criteria.getLastUpdatedEndDate()));
    assertThat(queryParams.getProgramEnrollmentStartDate(), is(criteria.getProgramEnrollmentStartDate()));
    assertThat(queryParams.getProgramEnrollmentEndDate(), is(DateUtils.addDays(criteria.getProgramEnrollmentEndDate(), 1)));
    assertThat(queryParams.getProgramIncidentStartDate(), is(criteria.getProgramIncidentStartDate()));
    assertThat(queryParams.getProgramIncidentEndDate(), is(DateUtils.addDays(criteria.getProgramIncidentEndDate(), 1)));
    assertThat(queryParams.getEventStatus(), is(EventStatus.COMPLETED));
    assertThat(queryParams.getEventStartDate(), is(criteria.getEventStartDate()));
    assertThat(queryParams.getEventEndDate(), is(criteria.getEventEndDate()));
    assertThat(queryParams.getAssignedUserSelectionMode(), is(AssignedUserSelectionMode.PROVIDED));
    assertTrue(queryParams.getAssignedUsers().stream().anyMatch(u -> u.equals(userId1)));
    assertTrue(queryParams.getAssignedUsers().stream().anyMatch(u -> u.equals(userId2)));
    assertThat(queryParams.getAssignedUsers().stream().anyMatch(u -> u.equals(userId3)), is(false));
    assertThat(queryParams.isIncludeDeleted(), is(true));
    assertThat(queryParams.isIncludeAllAttributes(), is(true));
    assertTrue(queryParams.getOrders().stream().anyMatch(orderParam -> orderParam.equals(OrderParam.builder().field("created").direction(OrderParam.SortDirection.ASC).build())));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) Autowired(org.springframework.beans.factory.annotation.Autowired) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) HashSet(java.util.HashSet) OrderCriteria(org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Matchers.hasSize(org.hamcrest.Matchers.hasSize) User(org.hisp.dhis.user.User) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TrackedEntityCriteriaMapper(org.hisp.dhis.webapi.controller.event.mapper.TrackedEntityCriteriaMapper) UserService(org.hisp.dhis.user.UserService) OrderParam(org.hisp.dhis.webapi.controller.event.mapper.OrderParam) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) QueryOperator(org.hisp.dhis.common.QueryOperator) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) EventStatus(org.hisp.dhis.event.EventStatus) Sets(com.google.common.collect.Sets) DateUtils(org.apache.commons.lang3.time.DateUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Test(org.junit.jupiter.api.Test) ProgramStatus(org.hisp.dhis.program.ProgramStatus) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AssignedUserSelectionMode(org.hisp.dhis.common.AssignedUserSelectionMode) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) Collections(java.util.Collections) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.jupiter.api.Test)

Example 8 with TrackedEntityInstanceCriteria

use of org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria in project dhis2-core by dhis2.

the class TrackedEntityCriteriaMapperTest method verifyCriteriaMappingFailOnMissingTrackerEntityType.

@Test
void verifyCriteriaMappingFailOnMissingTrackerEntityType() {
    TrackedEntityInstanceCriteria criteria = new TrackedEntityInstanceCriteria();
    criteria.setTrackedEntityType(trackedEntityTypeA.getUid() + "A");
    IllegalQueryException e = assertThrows(IllegalQueryException.class, () -> trackedEntityCriteriaMapper.map(criteria));
    assertEquals("Tracked entity type does not exist: " + trackedEntityTypeA.getUid() + "A", e.getMessage());
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.jupiter.api.Test)

Example 9 with TrackedEntityInstanceCriteria

use of org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria 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 10 with TrackedEntityInstanceCriteria

use of org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria 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)

Aggregations

IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)11 TrackedEntityInstanceCriteria (org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria)11 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)8 Test (org.junit.jupiter.api.Test)8 HashSet (java.util.HashSet)4 AssignedUserSelectionMode (org.hisp.dhis.common.AssignedUserSelectionMode)4 OrganisationUnitSelectionMode (org.hisp.dhis.common.OrganisationUnitSelectionMode)4 QueryOperator (org.hisp.dhis.common.QueryOperator)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 OrganisationUnitService (org.hisp.dhis.organisationunit.OrganisationUnitService)4 Program (org.hisp.dhis.program.Program)4 ProgramService (org.hisp.dhis.program.ProgramService)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4 TrackedEntityAttributeService (org.hisp.dhis.trackedentity.TrackedEntityAttributeService)4 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)4 TrackedEntityType (org.hisp.dhis.trackedentity.TrackedEntityType)4 TrackedEntityTypeService (org.hisp.dhis.trackedentity.TrackedEntityTypeService)4 CurrentUserService (org.hisp.dhis.user.CurrentUserService)4 User (org.hisp.dhis.user.User)4 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)3