use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class DefaultFindSliceInterceptor method intercept.
@SuppressWarnings("unchecked")
@Override
public R intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, R> context) {
if (context.hasAnnotation(Query.class)) {
PreparedQuery<?, ?> preparedQuery = prepareQuery(methodKey, context);
Pageable pageable = preparedQuery.getPageable();
Iterable<R> iterable = (Iterable<R>) operations.findAll(preparedQuery);
Slice<R> slice = Slice.of(CollectionUtils.iterableToList(iterable), pageable);
return convertOrFail(context, slice);
} else {
PagedQuery<Object> pagedQuery = getPagedQuery(context);
Iterable iterable = operations.findAll(pagedQuery);
Slice<R> slice = Slice.of(CollectionUtils.iterableToList(iterable), pagedQuery.getPageable());
return convertOrFail(context, slice);
}
}
use of io.micronaut.data.model.Pageable in project kestra by kestra-io.
the class ElasticSearchExecutionRepository method findTaskRun.
@Override
public ArrayListTotal<TaskRun> findTaskRun(String query, Pageable pageable, @Nullable List<State.Type> state) {
BoolQueryBuilder filterAggQuery = QueryBuilders.boolQuery().filter(QueryBuilders.queryStringQuery(query));
if (state != null) {
filterAggQuery = filterAggQuery.must(QueryBuilders.termsQuery("taskRunList.state.current", stateConvert(state)));
}
NestedAggregationBuilder nestedAgg = AggregationBuilders.nested("NESTED", "taskRunList").subAggregation(AggregationBuilders.filter("FILTER", filterAggQuery).subAggregation(AggregationBuilders.topHits("TOPHITS").size(pageable.getSize()).sorts(defaultSorts(pageable, true)).from(Math.toIntExact(pageable.getOffset() - pageable.getSize()))));
BoolQueryBuilder mainQuery = this.defaultFilter().filter(QueryBuilders.nestedQuery("taskRunList", QueryBuilders.queryStringQuery(query).field("*.fulltext"), ScoreMode.Total));
SearchSourceBuilder sourceBuilder = this.searchSource(mainQuery, Optional.of(List.of(nestedAgg)), null).fetchSource(false);
SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
ParsedNested pn = searchResponse.getAggregations().get("NESTED");
Filter fa = pn.getAggregations().get("FILTER");
long docCount = fa.getDocCount();
TopHits th = fa.getAggregations().get("TOPHITS");
List<TaskRun> collect = Arrays.stream(th.getHits().getHits()).map(documentFields -> {
try {
return MAPPER.readValue(documentFields.getSourceAsString(), TaskRun.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
return new ArrayListTotal<>(collect, docCount);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class HibernateJpaOperations method findAll.
@NonNull
@Override
public <T, R> Iterable<R> findAll(@NonNull PreparedQuery<T, R> preparedQuery) {
return transactionOperations.executeRead(status -> {
Session entityManager = sessionFactory.getCurrentSession();
String queryStr = preparedQuery.getQuery();
Pageable pageable = preparedQuery.getPageable();
if (pageable != Pageable.UNPAGED) {
Sort sort = pageable.getSort();
if (sort.isSorted()) {
queryStr += QUERY_BUILDER.buildOrderBy(queryStr, getEntity(preparedQuery.getRootEntity()), sort).getQuery();
}
}
if (preparedQuery.isDtoProjection()) {
Query<Tuple> q;
if (preparedQuery.isNative()) {
q = entityManager.createNativeQuery(queryStr, Tuple.class);
} else if (queryStr.toLowerCase(Locale.ENGLISH).startsWith("select new ")) {
Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
Query<R> query = entityManager.createQuery(queryStr, wrapperType);
bindPreparedQuery(query, preparedQuery, entityManager, queryStr);
return query.list();
} else {
q = entityManager.createQuery(queryStr, Tuple.class);
}
bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
return q.stream().map(tuple -> {
Set<String> properties = tuple.getElements().stream().map(TupleElement::getAlias).collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
return (new BeanIntrospectionMapper<Tuple, R>() {
@Override
public Object read(Tuple tuple1, String alias) {
if (!properties.contains(alias)) {
return null;
}
return tuple1.get(alias);
}
@Override
public ConversionService<?> getConversionService() {
return dataConversionService;
}
}).map(tuple, preparedQuery.getResultType());
}).collect(Collectors.toList());
} else {
Class<R> wrapperType = ReflectionUtils.getWrapperType(preparedQuery.getResultType());
Query<R> q;
if (preparedQuery.isNative()) {
if (DataType.ENTITY.equals(preparedQuery.getResultDataType())) {
q = entityManager.createNativeQuery(queryStr, wrapperType);
} else {
q = entityManager.createNativeQuery(queryStr);
}
} else {
q = entityManager.createQuery(queryStr, wrapperType);
}
bindPreparedQuery(q, preparedQuery, entityManager, queryStr);
return q.list();
}
});
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class FindPageSpecificationInterceptor method intercept.
@Override
public Page intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
if (context.getParameterValues().length != 2) {
throw new IllegalStateException("Expected exactly 2 arguments to method");
}
Specification specification = getSpecification(context);
final EntityManager entityManager = jpaOperations.getCurrentEntityManager();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
Class<Object> rootEntity = getRequiredRootEntity(context);
final CriteriaQuery<Object> query = criteriaBuilder.createQuery(rootEntity);
final Root<Object> root = query.from(rootEntity);
final Predicate predicate = specification.toPredicate(root, query, criteriaBuilder);
if (predicate != null) {
query.where(predicate);
}
query.select(root);
Pageable pageable = getPageable(context);
final Sort sort = pageable.getSort();
if (sort.isSorted()) {
query.orderBy(getOrders(sort, root, criteriaBuilder));
}
final TypedQuery<Object> typedQuery = entityManager.createQuery(query);
if (pageable.isUnpaged()) {
List<Object> resultList = typedQuery.getResultList();
return Page.of(resultList, pageable, resultList.size());
} else {
typedQuery.setFirstResult((int) pageable.getOffset());
typedQuery.setMaxResults(pageable.getSize());
final List<Object> results = typedQuery.getResultList();
final CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
final Root<?> countRoot = countQuery.from(rootEntity);
final Predicate countPredicate = specification.toPredicate(countRoot, countQuery, criteriaBuilder);
if (countPredicate != null) {
countQuery.where(countPredicate);
}
countQuery.select(criteriaBuilder.count(countRoot));
Long singleResult = entityManager.createQuery(countQuery).getSingleResult();
return Page.of(results, pageable, singleResult);
}
}
use of io.micronaut.data.model.Pageable in project micronaut-data by micronaut-projects.
the class PageableRequestArgumentBinder method bind.
@Override
public BindingResult<Pageable> bind(ArgumentConversionContext<Pageable> context, HttpRequest<?> source) {
HttpParameters parameters = source.getParameters();
int page = Math.max(parameters.getFirst(configuration.getPageParameterName(), Integer.class).orElse(0), 0);
final int configuredMaxSize = configuration.getMaxPageSize();
final int defaultSize = configuration.getDefaultPageSize();
int size = Math.min(parameters.getFirst(configuration.getSizeParameterName(), Integer.class).orElse(defaultSize), configuredMaxSize);
String sortParameterName = configuration.getSortParameterName();
boolean hasSort = parameters.contains(sortParameterName);
Pageable pageable;
Sort sort = null;
if (hasSort) {
List<String> sortParams = parameters.getAll(sortParameterName);
List<Sort.Order> orders = sortParams.stream().map(sortMapper).collect(Collectors.toList());
sort = Sort.of(orders);
}
if (size < 1) {
if (page == 0 && configuredMaxSize < 1 && sort == null) {
pageable = Pageable.UNPAGED;
} else {
pageable = Pageable.from(page, defaultSize, sort);
}
} else {
pageable = Pageable.from(page, size, sort);
}
return () -> Optional.of(pageable);
}
Aggregations