Search in sources :

Example 1 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class DataItemServiceFacade method retrieveDataItemEntities.

/**
 * This method will iterate through the list of target entities, and query
 * each one of them using the filters and params provided. The result list
 * will bring together the results of all target entities queried.
 *
 * @param targetEntities the list of entities to be retrieved
 * @param orderParams request ordering params
 * @param filters request filters
 * @param options request options
 * @return the consolidated collection of entities found.
 */
List<DataItem> retrieveDataItemEntities(final Set<Class<? extends BaseIdentifiableObject>> targetEntities, final Set<String> filters, final WebOptions options, final OrderParams orderParams) {
    final List<DataItem> dataItems = new ArrayList<>();
    final User currentUser = currentUserService.getCurrentUser();
    if (isNotEmpty(targetEntities)) {
        // Defining the query params map, and setting the common params.
        final MapSqlParameterSource paramsMap = new MapSqlParameterSource().addValue("userUid", currentUser.getUid());
        setFilteringParams(filters, options, paramsMap, currentUser);
        setOrderingParams(orderParams, paramsMap);
        setMaxResultsWhenPaging(options, paramsMap);
        dataItems.addAll(queryExecutor.find(targetEntities, paramsMap));
        // In memory pagination.
        return paginate(options, dataItems);
    }
    return dataItems;
}
Also used : User(org.hisp.dhis.user.User) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DataItem(org.hisp.dhis.dataitem.DataItem) ArrayList(java.util.ArrayList)

Example 2 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class PaginationHelperTest method testPaginateWhenIntermediatePage.

@Test
void testPaginateWhenIntermediatePage() {
    // Given
    final int pageSize = 5;
    final int secondPage = 2;
    final int totalOfItems = 13;
    final WebOptions theWebOptions = mockWebOptions(pageSize, secondPage);
    final List<DataItem> anyDimensionalItems = mockDimensionalItems(totalOfItems);
    // When
    final List<DataItem> resultingList = paginate(theWebOptions, anyDimensionalItems);
    // Then
    assertThat(resultingList, hasSize(5));
}
Also used : DataItem(org.hisp.dhis.dataitem.DataItem) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 3 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class PaginationHelperTest method testPaginateWhenLastPage.

@Test
void testPaginateWhenLastPage() {
    // Given
    final int pageSize = 5;
    final int lastPage = 3;
    final int totalOfItems = 13;
    final WebOptions theWebOptions = mockWebOptions(pageSize, lastPage);
    final List<DataItem> anyDimensionalItems = mockDimensionalItems(totalOfItems);
    // When
    final List<DataItem> resultingList = paginate(theWebOptions, anyDimensionalItems);
    // Then
    assertThat(resultingList, hasSize(3));
}
Also used : DataItem(org.hisp.dhis.dataitem.DataItem) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 4 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class PaginationHelperTest method testPaginateWhenPageSizeIsZero.

@Test
void testPaginateWhenPageSizeIsZero() {
    // Given
    final int pageSize = 0;
    final int lastPage = 3;
    final int totalOfItems = 13;
    final WebOptions theWebOptions = mockWebOptions(pageSize, lastPage);
    final List<DataItem> anyDimensionalItems = mockDimensionalItems(totalOfItems);
    // When
    assertThrows(IllegalStateException.class, () -> paginate(theWebOptions, anyDimensionalItems), "Page size must be greater than zero.");
}
Also used : DataItem(org.hisp.dhis.dataitem.DataItem) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 5 with DataItem

use of org.hisp.dhis.dataitem.DataItem in project dhis2-core by dhis2.

the class PaginationHelperTest method mockDimensionalItems.

private List<DataItem> mockDimensionalItems(final int totalOfItems) {
    final List<DataItem> dataItemEntities = new ArrayList<>(0);
    for (int i = 0; i < totalOfItems; i++) {
        final DataItem dataItem = DataItem.builder().name("d-" + i).id("d-" + i).build();
        dataItemEntities.add(dataItem);
    }
    return dataItemEntities;
}
Also used : DataItem(org.hisp.dhis.dataitem.DataItem) ArrayList(java.util.ArrayList)

Aggregations

DataItem (org.hisp.dhis.dataitem.DataItem)14 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)11 Test (org.junit.jupiter.api.Test)11 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)5 HashSet (java.util.HashSet)4 OrderParams (org.hisp.dhis.dxf2.common.OrderParams)4 RootNode (org.hisp.dhis.node.types.RootNode)4 User (org.hisp.dhis.user.User)4 Indicator (org.hisp.dhis.indicator.Indicator)3 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 CollectionNode (org.hisp.dhis.node.types.CollectionNode)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 ResponseEntity (org.springframework.http.ResponseEntity)1