Fix a bug where property mappings weren't being considered in sort parameter names.

This commit is contained in:
Jon Brisbin
2013-03-12 13:36:46 -05:00
committed by Jon Brisbin
parent 2d2a85e870
commit 34adf44c1f

View File

@@ -9,7 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.rest.config.RepositoryRestConfiguration;
import org.springframework.data.rest.config.ResourceMapping;
import org.springframework.data.rest.repository.PagingAndSorting;
import org.springframework.data.web.PageableDefaults;
import org.springframework.util.ClassUtils;
@@ -27,76 +29,84 @@ import org.springframework.web.method.support.ModelAndViewContainer;
*/
public class PagingAndSortingMethodArgumentResolver implements HandlerMethodArgumentResolver {
private static final int DEFAULT_PAGE = 1; // We're 1-based, not 0-based
@Autowired
private RepositoryRestConfiguration config;
private static final int DEFAULT_PAGE = 1; // We're 1-based, not 0-based
@Autowired
private RepositoryRestConfiguration config;
@Autowired
private RepositoryInformationHandlerMethodArgumentResolver repoInfoResolver;
public PagingAndSortingMethodArgumentResolver() {
}
public PagingAndSortingMethodArgumentResolver() {
}
public PagingAndSortingMethodArgumentResolver(RepositoryRestConfiguration config) {
this.config = config;
}
public PagingAndSortingMethodArgumentResolver(RepositoryRestConfiguration config) {
this.config = config;
}
@Override public boolean supportsParameter(MethodParameter parameter) {
return ClassUtils.isAssignable(parameter.getParameterType(), PagingAndSorting.class);
}
@Override public boolean supportsParameter(MethodParameter parameter) {
return ClassUtils.isAssignable(parameter.getParameterType(), PagingAndSorting.class);
}
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest)webRequest.getNativeRequest();
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
RepositoryInformation repoInfo = (RepositoryInformation)repoInfoResolver.resolveArgument(parameter,
mavContainer,
webRequest,
binderFactory);
ResourceMapping repoMapping = config.getResourceMappingForDomainType(repoInfo.getDomainType());
HttpServletRequest request = (HttpServletRequest)webRequest.getNativeRequest();
PageRequest pr = null;
for(Annotation annotation : parameter.getParameterAnnotations()) {
if(annotation instanceof PageableDefaults) {
PageableDefaults defaults = (PageableDefaults)annotation;
pr = new PageRequest(defaults.pageNumber(), defaults.value());
break;
}
}
if(null == pr) {
int page = DEFAULT_PAGE;
String sPage = request.getParameter(config.getPageParamName());
if(StringUtils.hasText(sPage)) {
try {
page = Integer.parseInt(sPage);
} catch(NumberFormatException ignored) {
}
}
int limit = config.getDefaultPageSize();
String sLimit = request.getParameter(config.getLimitParamName());
if(StringUtils.hasText(sLimit)) {
try {
limit = Math.min(Integer.parseInt(sLimit), config.getMaxPageSize());
} catch(NumberFormatException ignored) {
}
}
PageRequest pr = null;
for(Annotation annotation : parameter.getParameterAnnotations()) {
if(annotation instanceof PageableDefaults) {
PageableDefaults defaults = (PageableDefaults)annotation;
pr = new PageRequest(defaults.pageNumber(), defaults.value());
break;
}
}
if(null == pr) {
int page = DEFAULT_PAGE;
String sPage = request.getParameter(config.getPageParamName());
if(StringUtils.hasText(sPage)) {
try {
page = Integer.parseInt(sPage);
} catch(NumberFormatException ignored) {
}
}
int limit = config.getDefaultPageSize();
String sLimit = request.getParameter(config.getLimitParamName());
if(StringUtils.hasText(sLimit)) {
try {
limit = Math.min(Integer.parseInt(sLimit), config.getMaxPageSize());
} catch(NumberFormatException ignored) {
}
}
Sort sort = null;
List<Sort.Order> orders = new ArrayList<Sort.Order>();
String[] orderValues = request.getParameterValues(config.getSortParamName());
if(null != orderValues) {
for(String orderParam : orderValues) {
String sortDir = request.getParameter(orderParam + ".dir");
Sort.Direction dir = (null != sortDir ? Sort.Direction.valueOf(sortDir.toUpperCase()) : Sort.Direction.ASC);
orders.add(new Sort.Order(dir, orderParam));
}
if(!orders.isEmpty()) {
sort = new Sort(orders);
}
}
Sort sort = null;
List<Sort.Order> orders = new ArrayList<Sort.Order>();
String[] orderValues = request.getParameterValues(config.getSortParamName());
if(null != orderValues) {
for(String orderParam : orderValues) {
String name = repoMapping.getNameForPath(orderParam);
String sortDir = request.getParameter(orderParam + ".dir");
Sort.Direction dir = (null != sortDir ? Sort.Direction.valueOf(sortDir.toUpperCase()) : Sort.Direction.ASC);
orders.add(new Sort.Order(dir, name));
}
if(!orders.isEmpty()) {
sort = new Sort(orders);
}
}
if(null != sort) {
pr = new PageRequest(page - 1, limit, sort);
} else {
pr = new PageRequest(page - 1, limit);
}
}
if(null != sort) {
pr = new PageRequest(page - 1, limit, sort);
} else {
pr = new PageRequest(page - 1, limit);
}
}
return new PagingAndSorting(config, pr);
}
return new PagingAndSorting(config, pr);
}
}