Search in sources :

Example 1 with Page

use of org.candlepin.common.paging.Page in project candlepin by candlepin.

the class CandlepinQueryInterceptor method postProcess.

@Override
public void postProcess(ServerResponse response) {
    Object entity = response.getEntity();
    if (entity instanceof CandlepinQuery) {
        final PageRequest pageRequest = ResteasyProviderFactory.getContextData(PageRequest.class);
        final Session session = this.openSession();
        final CandlepinQuery query = (CandlepinQuery) entity;
        final ObjectMapper mapper = this.jsonProvider.locateMapper(Object.class, MediaType.APPLICATION_JSON_TYPE);
        // Use a separate session so we aren't at risk of lazy loading or interceptors closing
        // our cursor mid-stream.
        query.useSession(session);
        // Apply any paging config we may have
        if (pageRequest != null) {
            // Impl note:
            // Sorting will always be required (for consistency) if a page request object is
            // present -- either isPaging() will be true, or we'll have ordering config.
            String sortField = pageRequest.getSortBy() != null ? pageRequest.getSortBy() : AbstractHibernateObject.DEFAULT_SORT_FIELD;
            PageRequest.Order order = pageRequest.getOrder() != null ? pageRequest.getOrder() : PageRequest.DEFAULT_ORDER;
            query.addOrder(order == PageRequest.Order.DESCENDING ? Order.desc(sortField) : Order.asc(sortField));
            if (pageRequest.isPaging()) {
                query.setFirstResult((pageRequest.getPage() - 1) * pageRequest.getPerPage());
                query.setMaxResults(pageRequest.getPerPage());
                // Create a page object for the link header response
                Page page = new Page();
                // This is expensive :(
                page.setMaxRecords(query.getRowCount());
                page.setPageRequest(pageRequest);
                // Note: we don't need to store the page data in the page
                ResteasyProviderFactory.pushContext(Page.class, page);
            }
        }
        // Set the output streamer that will stream our query result
        response.setEntity(new StreamingOutput() {

            @Override
            public void write(OutputStream stream) throws IOException, WebApplicationException {
                JsonGenerator generator = null;
                ResultIterator<Object> iterator = null;
                try {
                    generator = mapper.getJsonFactory().createGenerator(stream);
                    iterator = query.iterate();
                    generator.writeStartArray();
                    while (iterator.hasNext()) {
                        mapper.writeValue(generator, iterator.next());
                    }
                    generator.writeEndArray();
                } finally {
                    if (generator != null) {
                        generator.flush();
                        generator.close();
                    }
                    if (iterator != null) {
                        iterator.close();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            }
        });
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) ResultIterator(org.candlepin.model.ResultIterator) CandlepinQuery(org.candlepin.model.CandlepinQuery) Page(org.candlepin.common.paging.Page) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) PageRequest(org.candlepin.common.paging.PageRequest) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractHibernateObject(org.candlepin.model.AbstractHibernateObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Session(org.hibernate.Session)

Example 2 with Page

use of org.candlepin.common.paging.Page in project candlepin by candlepin.

the class LinkHeaderResponseFilterTest method testGetLastPage.

@Test
public void testGetLastPage() {
    Page<Object> p = new Page<>();
    p.setMaxRecords(55);
    PageRequest pr = new PageRequest();
    p.setPageRequest(pr);
    pr.setPerPage(10);
    pr.setPage(1);
    assertEquals(Integer.valueOf(6), interceptor.getLastPage(p));
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Page(org.candlepin.common.paging.Page) Test(org.junit.Test)

Example 3 with Page

use of org.candlepin.common.paging.Page in project candlepin by candlepin.

the class LinkHeaderResponseFilterTest method testGetNextPageWhenNoNextAvailable.

@Test
public void testGetNextPageWhenNoNextAvailable() {
    Page<Object> p = new Page<>();
    p.setMaxRecords(55);
    PageRequest pr = new PageRequest();
    p.setPageRequest(pr);
    pr.setPerPage(10);
    pr.setPage(6);
    assertNull(interceptor.getNextPage(p));
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Page(org.candlepin.common.paging.Page) Test(org.junit.Test)

Example 4 with Page

use of org.candlepin.common.paging.Page in project candlepin by candlepin.

the class LinkHeaderResponseFilterTest method testPagesWithOutOfBoundsInitialPage.

@Test
public void testPagesWithOutOfBoundsInitialPage() {
    Page<Object> p = new Page<>();
    p.setMaxRecords(8);
    PageRequest pr = new PageRequest();
    p.setPageRequest(pr);
    pr.setPerPage(10);
    pr.setPage(2);
    assertNull(interceptor.getPrevPage(p));
    assertNull(interceptor.getNextPage(p));
    assertEquals(Integer.valueOf(1), interceptor.getLastPage(p));
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Page(org.candlepin.common.paging.Page) Test(org.junit.Test)

Example 5 with Page

use of org.candlepin.common.paging.Page in project candlepin by candlepin.

the class LinkHeaderResponseFilterTest method testGetLastPageWhenMaxRecordsLessThanLimit.

@Test
public void testGetLastPageWhenMaxRecordsLessThanLimit() {
    Page<Object> p = new Page<>();
    p.setMaxRecords(8);
    PageRequest pr = new PageRequest();
    p.setPageRequest(pr);
    pr.setPerPage(10);
    pr.setPage(1);
    assertEquals(Integer.valueOf(1), interceptor.getLastPage(p));
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Page(org.candlepin.common.paging.Page) Test(org.junit.Test)

Aggregations

Page (org.candlepin.common.paging.Page)20 PageRequest (org.candlepin.common.paging.PageRequest)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)10 List (java.util.List)10 LinkedList (java.util.LinkedList)6 Consumer (org.candlepin.model.Consumer)6 Entitlement (org.candlepin.model.Entitlement)6 Pool (org.candlepin.model.Pool)6 CandlepinQuery (org.candlepin.model.CandlepinQuery)5 Transactional (com.google.inject.persist.Transactional)4 Date (java.util.Date)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 EntitlementDTO (org.candlepin.dto.api.v1.EntitlementDTO)4 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)4 Product (org.candlepin.model.Product)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)3 ConsumerType (org.candlepin.model.ConsumerType)3 EntitlementFilterBuilder (org.candlepin.model.EntitlementFilterBuilder)3