Search in sources :

Example 1 with PageRange

use of org.hisp.dhis.commons.util.PageRange in project dhis2-core by dhis2.

the class PageRangeTest method testGetPages.

@Test
public void testGetPages() {
    PageRange range = new PageRange(12).setPageSize(5);
    List<int[]> pages = range.getPages();
    assertEquals(3, pages.size());
    assertEquals(0, pages.get(0)[0]);
    assertEquals(5, pages.get(0)[1]);
    assertEquals(5, pages.get(1)[0]);
    assertEquals(10, pages.get(1)[1]);
    assertEquals(10, pages.get(2)[0]);
    assertEquals(12, pages.get(2)[1]);
}
Also used : PageRange(org.hisp.dhis.commons.util.PageRange) Test(org.junit.Test)

Example 2 with PageRange

use of org.hisp.dhis.commons.util.PageRange in project dhis2-core by dhis2.

the class DefaultMaintenanceService method removeExpiredInvitations.

@Override
@Transactional
public int removeExpiredInvitations() {
    UserQueryParams params = new UserQueryParams();
    params.setInvitationStatus(UserInvitationStatus.EXPIRED);
    int userCount = userService.getUserCount(params);
    int removeCount = 0;
    PageRange range = new PageRange(userCount).setPageSize(200);
    List<int[]> pages = range.getPages();
    // Iterate from end since users are deleted
    Collections.reverse(pages);
    log.debug("Pages: " + pages);
    for (int[] page : pages) {
        params.setFirst(page[0]);
        params.setMax(range.getPageSize());
        List<User> users = userService.getUsers(params);
        for (User user : users) {
            try {
                userService.deleteUser(user);
                removeCount++;
            } catch (DeleteNotAllowedException ex) {
                log.warn("Could not delete user " + user.getUsername());
            }
        }
    }
    log.info("Removed expired invitations: " + removeCount);
    return removeCount;
}
Also used : DeleteNotAllowedException(org.hisp.dhis.common.DeleteNotAllowedException) PageRange(org.hisp.dhis.commons.util.PageRange) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with PageRange

use of org.hisp.dhis.commons.util.PageRange in project dhis2-core by dhis2.

the class PageRangeTest method testPages.

@Test
public void testPages() {
    PageRange range = new PageRange(11).setPages(3);
    assertTrue(range.nextPage());
    assertEquals(0, range.getFromIndex());
    assertEquals(4, range.getToIndex());
    assertTrue(range.nextPage());
    assertEquals(4, range.getFromIndex());
    assertEquals(8, range.getToIndex());
    assertTrue(range.nextPage());
    assertEquals(8, range.getFromIndex());
    assertEquals(11, range.getToIndex());
    assertFalse(range.nextPage());
}
Also used : PageRange(org.hisp.dhis.commons.util.PageRange) Test(org.junit.Test)

Example 4 with PageRange

use of org.hisp.dhis.commons.util.PageRange in project dhis2-core by dhis2.

the class PageRangeTest method testPageSize.

@Test
public void testPageSize() {
    PageRange range = new PageRange(12).setPageSize(5);
    assertTrue(range.nextPage());
    assertEquals(0, range.getFromIndex());
    assertEquals(5, range.getToIndex());
    assertTrue(range.nextPage());
    assertEquals(5, range.getFromIndex());
    assertEquals(10, range.getToIndex());
    assertTrue(range.nextPage());
    assertEquals(10, range.getFromIndex());
    assertEquals(12, range.getToIndex());
    assertFalse(range.nextPage());
}
Also used : PageRange(org.hisp.dhis.commons.util.PageRange) Test(org.junit.Test)

Aggregations

PageRange (org.hisp.dhis.commons.util.PageRange)4 Test (org.junit.Test)3 DeleteNotAllowedException (org.hisp.dhis.common.DeleteNotAllowedException)1 Transactional (org.springframework.transaction.annotation.Transactional)1