This refers to my decision to implement paging and sorting in a Driven Driven Design project with the intention of not polluting domain models and storage contracts ,
Base class for a REST request
public class AbstractQueryRequest {
...
private int startIndex;
private int offset;
...
}
Interceptor to retrieve request metadata and store it in a ThreadLocal container
public class QueryInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
...
QueryMetaData.instance().setPageMetaData();
}
}
Container for request metadata
public class QueryMetaData {
private static final ThreadLocal<QueryMetaData> instance = new ThreadLocal<>() {
protected QueryMetaData initialValue() {
return new QueryMetaData();
}
};
public static QueryMetaData instance() {
return instance.get();
}
private ThreadLocal<Pageable> pageMetadata = new ThreadLocal<>();
public void setPageMetaData(Pageable pageable) {
pageMetadata.set(pageable);
}
public Pageable getPageMetaData() {
return pageMetadata.get();
}
}
I intend to get this ThreadLocal value in the repository if it is available with queries in the data warehouse.
I hope this may not be a very dirty solution, but want to know if there is a more widely used template for this.