org.apache.deltaspike.data.api.QueryResult Java Examples

The following examples show how to use org.apache.deltaspike.data.api.QueryResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public <X> QueryResult<T> changeOrder(final SingularAttribute<T, X> attribute)
{
    changeOrder(new ChangeOrder()
    {
        @Override
        public boolean matches(OrderByQueryStringPostProcessor orderBy)
        {
            return orderBy.matches(attribute);
        }

        @Override
        public void addDefault()
        {
            orderAsc(attribute);
        }
    });
    return this;
}
 
Example #2
Source File: QueryProcessorFactory.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public QueryProcessor build(RepositoryMethodMetadata methodMetadata)
{
    if (ClassUtils.returns(methodMetadata.getMethod(), QueryResult.class))
    {
        return noOp;
    }
    
    if (ClassUtils.returns(methodMetadata.getMethod(), List.class))
    {
        return listResult;
    }
    
    if (methodMetadata.isReturnsStream())
    {
        return streamResult;
    }
    
    if (isModifying(methodMetadata))
    {
        return executeUpdate;
    }

    return singleResult;
}
 
Example #3
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public QueryResult<T> changeOrder(final String attribute)
{
    changeOrder(new ChangeOrder()
    {
        @Override
        public boolean matches(OrderByQueryStringPostProcessor orderBy)
        {
            return orderBy.matches(attribute);
        }

        @Override
        public void addDefault()
        {
            orderAsc(attribute);
        }
    });
    return this;
}
 
Example #4
Source File: QueryResultTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void should_paginate_with_orderby()
{
    // given
    SimpleStringIdBuilder builder = new SimpleStringIdBuilder(getEntityManager());


    final String name = "should_paginate_with_orderby";
    final String name2 = "should_paginate_with_orderby2";
    builder.createSimple("a", name);
    builder.createSimple("b", name2);

    // when
    QueryResult<SimpleStringId> allOrderByNamePaginate = repo.findAllOrderByIdPaginate(0, 10);

    // then
    assertNotNull(allOrderByNamePaginate);

    List<SimpleStringId> resultList = allOrderByNamePaginate.getResultList();
    assertEquals(2, resultList.size());
    assertEquals("a", resultList.get(0).getId());
    assertEquals("b", resultList.get(1).getId());
}
 
Example #5
Source File: QueryResultTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void should_change_sort_order()
{
    // given
    final String name = "testChangeSortOrder";
    builder.createSimple(name, Integer.valueOf(99));
    builder.createSimple(name, Integer.valueOf(22));
    builder.createSimple(name, Integer.valueOf(229));

    // when
    QueryResult<Simple> query = repo.findByName(name);
    List<Simple> result1 = query
            .changeOrder(Simple_.counter)
            .getResultList();
    List<Simple> result2 = query
            .changeOrder(Simple_.counter)
            .getResultList();

    // then
    assertEquals(22, result1.get(0).getCounter().intValue());
    assertEquals(229, result2.get(0).getCounter().intValue());
}
 
Example #6
Source File: QueryResultTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void should_page_with_page_api()
{
    // given
    final String name = "testPageAPI";
    builder.createSimple(name, Integer.valueOf(22));
    builder.createSimple(name, Integer.valueOf(56));
    builder.createSimple(name, Integer.valueOf(99));
    builder.createSimple(name, Integer.valueOf(123));
    builder.createSimple(name, Integer.valueOf(229));
    builder.createSimple(name, Integer.valueOf(299));
    builder.createSimple(name, Integer.valueOf(389));

    // when
    QueryResult<Simple> pagedQuery = repo
            .findByName(name)
            .withPageSize(2);
    List<Simple> result1 = pagedQuery.getResultList();
    List<Simple> result2 = pagedQuery.nextPage().nextPage().getResultList();
    int current = pagedQuery.currentPage();
    List<Simple> result3 = pagedQuery.toPage(1).getResultList();
    int total = pagedQuery.countPages();
    int pageSize = pagedQuery.pageSize();

    // then
    assertEquals(2, result1.size());
    assertEquals(2, result2.size());
    assertEquals(2, result3.size());
    assertEquals(2, current);
    assertEquals(4, total);
    assertEquals(2, pageSize);

    assertEquals(22, result1.get(0).getCounter().intValue());
    assertEquals(229, result2.get(0).getCounter().intValue());
    assertEquals(99, result3.get(0).getCounter().intValue());

}
 
Example #7
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResult<T> orderDesc(String attribute, boolean appendEntityName)
{
    context.addQueryStringPostProcessor(new OrderByQueryStringPostProcessor(attribute, OrderDirection.DESC,
            appendEntityName));
    return this;
}
 
Example #8
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public <X> QueryResult<T> orderDesc(SingularAttribute<T, X> attribute, boolean appendEntityName)
{
    context.addQueryStringPostProcessor(new OrderByQueryStringPostProcessor(attribute, OrderDirection.DESC,
            appendEntityName));
    return this;
}
 
Example #9
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResult<T> orderAsc(String attribute, boolean appendEntityName)
{
    context.addQueryStringPostProcessor(new OrderByQueryStringPostProcessor(attribute, OrderDirection.ASC,
            appendEntityName));
    return this;
}
 
Example #10
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public <X> QueryResult<T> orderAsc(SingularAttribute<T, X> attribute, boolean appendEntityName)
{
    context.addQueryStringPostProcessor(new OrderByQueryStringPostProcessor(attribute, OrderDirection.ASC,
            appendEntityName));
    return this;
}
 
Example #11
Source File: QueryResultTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void should_clear_sort_order()
{
    // given
    final String name = "testClearSortOrder";
    builder.createSimple(name, Integer.valueOf(99));
    builder.createSimple(name, Integer.valueOf(22));
    builder.createSimple(name, Integer.valueOf(229));

    // when
    QueryResult<Simple> query = repo.findByName(name);
    List<Simple> result1 = query
            .changeOrder(Simple_.counter)
            .getResultList();
    List<Simple> result2 = query
            .clearOrder()
            .getResultList();

    // then
    assertEquals(result1.size(), result2.size());
    for (int i = 0; i < result1.size(); i++)
    {
        int count1 = result1.get(i).getCounter().intValue();
        int count2 = result2.get(i).getCounter().intValue();
        if (count1 != count2)
        {
            return;
        }
    }
    fail("Both collections sorted: " + result1 + "," + result2);
}
 
Example #12
Source File: QueryBuilderFactory.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public QueryBuilder build(RepositoryMethodMetadata methodMetadata, CdiQueryInvocationContext context)
{
    QueryBuilder builder = getQueryBuilder(context.getRepositoryMethodMetadata().getMethodType());

    if (QueryResult.class.equals(methodMetadata.getMethod().getReturnType()))
    {
        return new WrappedQueryBuilder(builder);
    }

    return builder;
}
 
Example #13
Source File: PhoneBookService.java    From jpa-addressbook with The Unlicense 5 votes vote down vote up
/**
 * Finds a set of entries from database with given filter, starting from
 * given row. The "page size" (aka max results limit passed for the query)
 * is 45.
 *
 * @param filter the filters string
 * @param firstRow the first row to be fetched
 * @param maxresults maximum number of results
 * @return
 */
public List<PhoneBookEntry> getEntriesPaged(String filter, int firstRow, int maxresults, List<QuerySortOrder> sortOrder) {
    QueryResult<PhoneBookEntry> qr = entryRepo.findByNameLikeIgnoreCase("%" + filter + "%");
    for (QuerySortOrder qso : sortOrder) {
        if(qso.getDirection() == SortDirection.ASCENDING) {
            qr = qr.orderAsc(qso.getSorted());
        } else {
            qr = qr.orderDesc(qso.getSorted());
        }
    }
    return qr
            .firstResult(firstRow).maxResults(maxresults)
            .getResultList();
}
 
Example #14
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResult<T> clearOrder()
{
    for (Iterator<QueryStringPostProcessor> it = context.getQueryStringPostProcessors().iterator(); it.hasNext();)
    {
        if (it.next() instanceof OrderByQueryStringPostProcessor)
        {
            it.remove();
        }
    }
    return this;
}
 
Example #15
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResult<T> maxResults(int max)
{
    context.addJpaQueryPostProcessor(new MaxResultPostProcessor(max));
    pageSize = max;
    return this;
}
 
Example #16
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> toPage(int page)
{
    this.page = page;
    return firstResult(pageSize * page);
}
 
Example #17
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> nextPage()
{
    page = page + 1;
    return firstResult(pageSize * page);
}
 
Example #18
Source File: SimpleMappedRepository.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Query("select e from Simple e where e.name = ?1")
QueryResult<SimpleDto> findByNameToo(String name);
 
Example #19
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> previousPage()
{
    page = page > 0 ? page - 1 : page;
    return firstResult(pageSize * page);
}
 
Example #20
Source File: SimpleRepository.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Query("select s from Simple s where s.name = ?1 order by s.counter desc")
public abstract QueryResult<Simple> findByQueryWithOrderBy(String name);
 
Example #21
Source File: SimpleRepository.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Query(named = Simple.BY_NAME_LIKE)
public abstract QueryResult<Simple> queryResultWithNamed(String name);
 
Example #22
Source File: SimpleRepository.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Query("select s from Simple s")
public abstract QueryResult<Simple> queryAll();
 
Example #23
Source File: SimpleRepository.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Query(named = SimpleStringId.FIND_ALL_ORDER_BY_ID)
public abstract QueryResult<SimpleStringId> findAllOrderByIdPaginate(@FirstResult int start, @MaxResults int pageSize);
 
Example #24
Source File: ContactRepository.java    From database-rider with Apache License 2.0 4 votes vote down vote up
public QueryResult findByCompanyAndNameLikeIgnoreCase(Company company,
String string);
 
Example #25
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> withPageSize(int pageSize)
{
    return maxResults(pageSize);
}
 
Example #26
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> firstResult(int first)
{
    context.addJpaQueryPostProcessor(new FirstResultPostProcessor(first));
    return this;
}
 
Example #27
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> orderDesc(String attribute)
{
    return orderDesc(attribute, true);
}
 
Example #28
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public <X> QueryResult<T> orderDesc(SingularAttribute<T, X> attribute)
{
    return orderDesc(attribute, true);
}
 
Example #29
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public QueryResult<T> orderAsc(String attribute)
{
    return orderAsc(attribute, true);
}
 
Example #30
Source File: DefaultQueryResult.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public <X> QueryResult<T> orderAsc(SingularAttribute<T, X> attribute)
{
    return orderAsc(attribute, true);
}