use of io.micronaut.data.model.Page in project micronaut-data by micronaut-projects.
the class RepositoryTypeElementVisitor method processMethodInfo.
private void processMethodInfo(MethodMatchContext methodMatchContext, MethodMatchInfo methodInfo) {
QueryBuilder queryEncoder = methodMatchContext.getQueryBuilder();
MethodElement element = methodMatchContext.getMethodElement();
SourcePersistentEntity entity = methodMatchContext.getRootEntity();
ParameterElement[] parameters = methodMatchContext.getParameters();
// populate parameter roles
for (Map.Entry<String, Element> entry : methodMatchContext.getParametersInRole().entrySet()) {
methodInfo.addParameterRole(entry.getKey(), entry.getValue().getName());
}
List<QueryParameterBinding> parameterBinding = null;
boolean encodeEntityParameters = false;
boolean supportsImplicitQueries = methodMatchContext.supportsImplicitQueries();
QueryResult queryResult = methodInfo.getQueryResult();
if (queryResult != null) {
if (methodInfo.isRawQuery()) {
// no need to annotation since already annotated, just replace the
// the computed parameter names
parameterBinding = queryResult.getParameterBindings();
element.annotate(Query.class, (builder) -> builder.member(DataMethod.META_MEMBER_RAW_QUERY, element.stringValue(Query.class).map(q -> addRawQueryParameterPlaceholders(queryEncoder, queryResult.getQuery(), queryResult.getQueryParts())).orElse(null)));
ClassElement genericReturnType = methodMatchContext.getReturnType();
if (methodMatchContext.isTypeInRole(genericReturnType, TypeRole.PAGE) || element.isPresent(Query.class, "countQuery")) {
QueryResult countQueryResult = methodInfo.getCountQueryResult();
if (countQueryResult == null) {
throw new MatchFailedException("Query returns a Page and does not specify a 'countQuery' member.", element);
} else {
element.annotate(Query.class, (builder) -> builder.member(DataMethod.META_MEMBER_RAW_COUNT_QUERY, addRawQueryParameterPlaceholders(queryEncoder, countQueryResult.getQuery(), countQueryResult.getQueryParts())));
}
}
encodeEntityParameters = methodInfo.isEncodeEntityParameters();
} else {
encodeEntityParameters = methodInfo.isEncodeEntityParameters();
parameterBinding = queryResult.getParameterBindings();
bindAdditionalParameters(methodMatchContext, entity, parameterBinding, parameters, queryResult.getAdditionalRequiredParameters());
QueryResult preparedCount = methodInfo.getCountQueryResult();
if (preparedCount != null) {
element.annotate(Query.class, annotationBuilder -> {
annotationBuilder.value(queryResult.getQuery());
annotationBuilder.member(DataMethod.META_MEMBER_COUNT_QUERY, preparedCount.getQuery());
});
} else {
element.annotate(Query.class, annotationBuilder -> {
annotationBuilder.value(queryResult.getQuery());
String update = queryResult.getUpdate();
if (StringUtils.isNotEmpty(update)) {
annotationBuilder.member("update", update);
}
});
}
}
}
ClassElement runtimeInterceptor = methodInfo.getRuntimeInterceptor();
if (runtimeInterceptor == null) {
throw new MatchFailedException("Unable to implement Repository method: " + currentRepository.getSimpleName() + "." + element.getName() + "(..). No possible runtime implementations found.", element);
}
boolean finalEncodeEntityParameters = encodeEntityParameters;
List<QueryParameterBinding> finalParameterBinding = parameterBinding;
element.annotate(DataMethod.class, annotationBuilder -> {
annotationBuilder.member(DataMethod.META_MEMBER_OPERATION_TYPE, methodInfo.getOperationType());
annotationBuilder.member(DataMethod.META_MEMBER_ROOT_ENTITY, new AnnotationClassValue<>(entity.getName()));
// include the roles
methodInfo.getParameterRoles().forEach(annotationBuilder::member);
if (methodInfo.isDto()) {
annotationBuilder.member(DataMethod.META_MEMBER_DTO, true);
}
if (methodInfo.isOptimisticLock()) {
annotationBuilder.member(DataMethod.META_MEMBER_OPTIMISTIC_LOCK, true);
}
TypedElement resultType = methodInfo.getResultType();
if (resultType != null) {
annotationBuilder.member(DataMethod.META_MEMBER_RESULT_TYPE, new AnnotationClassValue<>(resultType.getName()));
ClassElement type = resultType.getType();
if (!TypeUtils.isVoid(type)) {
annotationBuilder.member(DataMethod.META_MEMBER_RESULT_DATA_TYPE, TypeUtils.resolveDataType(type, dataTypes));
}
}
String idType = resolveIdType(entity);
if (idType != null) {
annotationBuilder.member(DataMethod.META_MEMBER_ID_TYPE, idType);
}
annotationBuilder.member(DataMethod.META_MEMBER_INTERCEPTOR, new AnnotationClassValue<>(runtimeInterceptor.getName()));
if (queryResult != null) {
if (finalParameterBinding.stream().anyMatch(QueryParameterBinding::isExpandable)) {
annotationBuilder.member(DataMethod.META_MEMBER_EXPANDABLE_QUERY, queryResult.getQueryParts().toArray(new String[0]));
QueryResult preparedCount = methodInfo.getCountQueryResult();
if (preparedCount != null) {
annotationBuilder.member(DataMethod.META_MEMBER_EXPANDABLE_COUNT_QUERY, preparedCount.getQueryParts().toArray(new String[0]));
}
}
int max = queryResult.getMax();
if (max > -1) {
annotationBuilder.member(DataMethod.META_MEMBER_PAGE_SIZE, max);
}
long offset = queryResult.getOffset();
if (offset > 0) {
annotationBuilder.member(DataMethod.META_MEMBER_PAGE_INDEX, offset);
}
}
Arrays.stream(parameters).filter(p -> p.getGenericType().isAssignable(entity.getName())).findFirst().ifPresent(parameterElement -> annotationBuilder.member(DataMethod.META_MEMBER_ENTITY, parameterElement.getName()));
if (CollectionUtils.isNotEmpty(finalParameterBinding)) {
bindParameters(supportsImplicitQueries, finalParameterBinding, finalEncodeEntityParameters, annotationBuilder);
}
});
}
use of io.micronaut.data.model.Page in project micronaut-data by micronaut-projects.
the class FindPageSpecificationInterceptor method intercept.
@Override
public Object intercept(RepositoryMethodKey methodKey, MethodInvocationContext<Object, Object> context) {
if (context.getParameterValues().length != 2) {
throw new IllegalStateException("Expected exactly 2 arguments to method");
}
Pageable pageable = getPageable(context);
if (pageable.isUnpaged()) {
PreparedQuery<?, ?> preparedQuery = preparedQueryForCriteria(methodKey, context, Type.FIND_PAGE);
Iterable<?> iterable = operations.findAll(preparedQuery);
List<Object> resultList = (List<Object>) CollectionUtils.iterableToList(iterable);
return Page.of(resultList, pageable, resultList.size());
}
PreparedQuery<?, ?> preparedQuery = preparedQueryForCriteria(methodKey, context, Type.FIND_PAGE);
PreparedQuery<?, Number> countQuery = preparedQueryForCriteria(methodKey, context, Type.COUNT);
Iterable<?> iterable = operations.findAll(preparedQuery);
List<Object> resultList = (List<Object>) CollectionUtils.iterableToList(iterable);
Number count = operations.findOne(countQuery);
Page page = Page.of(resultList, getPageable(context), count != null ? count.longValue() : 0);
Class<Object> rt = context.getReturnType().getType();
if (rt.isInstance(page)) {
return page;
}
return operations.getConversionService().convert(page, rt).orElseThrow(() -> new IllegalStateException("Unsupported page interface type " + rt));
}
use of io.micronaut.data.model.Page in project micronaut-data by micronaut-projects.
the class DefaultFindPageInterceptor method intercept.
@Override
public R intercept(RepositoryMethodKey methodKey, MethodInvocationContext<T, R> context) {
Class<R> returnType = context.getReturnType().getType();
if (context.hasAnnotation(Query.class)) {
PreparedQuery<?, ?> preparedQuery = prepareQuery(methodKey, context);
PreparedQuery<?, Number> countQuery = prepareCountQuery(methodKey, context);
Iterable<?> iterable = operations.findAll(preparedQuery);
List<R> resultList = (List<R>) CollectionUtils.iterableToList(iterable);
Number n = operations.findOne(countQuery);
Long result = n != null ? n.longValue() : 0;
Page<R> page = Page.of(resultList, getPageable(context), result);
if (returnType.isInstance(page)) {
return (R) page;
} else {
return operations.getConversionService().convert(page, returnType).orElseThrow(() -> new IllegalStateException("Unsupported page interface type " + returnType));
}
} else {
Page page = operations.findPage(getPagedQuery(context));
if (returnType.isInstance(page)) {
return (R) page;
} else {
return operations.getConversionService().convert(page, returnType).orElseThrow(() -> new IllegalStateException("Unsupported page interface type " + returnType));
}
}
}
Aggregations