fix grid sorting

This commit is contained in:
f43nd1r 2019-01-28 01:18:16 +01:00
parent e4b0bc527b
commit 1ef06a57c0
2 changed files with 17 additions and 28 deletions

View file

@ -24,11 +24,8 @@ import com.vaadin.flow.data.provider.AbstractBackEndDataProvider;
import com.vaadin.flow.data.provider.Query; import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.data.provider.QuerySortOrder; import com.vaadin.flow.data.provider.QuerySortOrder;
import com.vaadin.flow.data.provider.SortDirection; import com.vaadin.flow.data.provider.SortDirection;
import org.springframework.lang.NonNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -37,10 +34,8 @@ import java.util.stream.Stream;
* @since 30.05.18 * @since 30.05.18
*/ */
public class QueryDslDataProvider<T> extends AbstractBackEndDataProvider<T, Void> { public class QueryDslDataProvider<T> extends AbstractBackEndDataProvider<T, Void> {
private final List<SizeListener> sizeListeners;
private final Supplier<JPAQuery<T>> fetchProvider; private final Supplier<JPAQuery<T>> fetchProvider;
private final Supplier<JPAQuery<?>> countProvider; private final Supplier<JPAQuery<?>> countProvider;
private final Map<String, Expression<? extends Comparable>> sortOptions;
public QueryDslDataProvider(JPAQuery<T> base) { public QueryDslDataProvider(JPAQuery<T> base) {
this(base, base); this(base, base);
@ -53,27 +48,14 @@ public class QueryDslDataProvider<T> extends AbstractBackEndDataProvider<T, Void
public QueryDslDataProvider(Supplier<JPAQuery<T>> fetchProvider, Supplier<JPAQuery<?>> countProvider){ public QueryDslDataProvider(Supplier<JPAQuery<T>> fetchProvider, Supplier<JPAQuery<?>> countProvider){
this.fetchProvider = fetchProvider; this.fetchProvider = fetchProvider;
this.countProvider = countProvider; this.countProvider = countProvider;
sizeListeners = new ArrayList<>();
sortOptions = new HashMap<>();
}
public void addSizeListener(SizeListener listener) {
sizeListeners.add(listener);
}
public String addSortable(Expression<? extends Comparable> expression) {
String id = String.valueOf(sortOptions.size());
sortOptions.put(id, expression);
return id;
} }
@Override @Override
protected Stream<T> fetchFromBackEnd(Query<T, Void> query) { protected Stream<T> fetchFromBackEnd(Query<T, Void> query) {
JPAQuery<T> q = fetchProvider.get().offset(query.getOffset()).limit(query.getLimit()); JPAQuery<T> q = fetchProvider.get().offset(query.getOffset()).limit(query.getLimit());
for (QuerySortOrder order : query.getSortOrders()) { for (QuerySortOrder order : query.getSortOrders()) {
Expression<? extends Comparable> sort = sortOptions.get(order.getSorted()); if(order instanceof QueryDslSortOrder) {
if (sort != null) { q = q.orderBy(((QueryDslSortOrder) order).toSpecifier());
q = q.orderBy(new OrderSpecifier<>(order.getDirection() == SortDirection.ASCENDING ? Order.ASC : Order.DESC, sort));
} }
} }
return q.fetch().stream(); return q.fetch().stream();
@ -81,13 +63,19 @@ public class QueryDslDataProvider<T> extends AbstractBackEndDataProvider<T, Void
@Override @Override
protected int sizeInBackEnd(Query<T, Void> query) { protected int sizeInBackEnd(Query<T, Void> query) {
int result = Math.toIntExact(countProvider.get().fetchCount()); return Math.toIntExact(countProvider.get().fetchCount());
sizeListeners.forEach(listener -> listener.sizeChanged(result));
return result;
} }
@FunctionalInterface public static class QueryDslSortOrder extends QuerySortOrder {
public interface SizeListener { private final Expression<? extends Comparable> expression;
void sizeChanged(int size);
public QueryDslSortOrder(@NonNull Expression<? extends Comparable> expression, @NonNull SortDirection direction) {
super(expression.toString(), direction);
this.expression = expression;
}
public OrderSpecifier<?> toSpecifier() {
return new OrderSpecifier<>(getDirection() == SortDirection.ASCENDING ? Order.ASC : Order.DESC, expression);
}
} }
} }

View file

@ -40,6 +40,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream;
/** /**
* @author lukas * @author lukas
@ -91,7 +92,7 @@ public class MyGrid<T> extends Composite<Grid<T>> implements LocaleChangeObserve
} }
private Grid.Column<T> setupSortableColumn(@NonNull Grid.Column<T> column, @NonNull Expression<? extends Comparable> sort) { private Grid.Column<T> setupSortableColumn(@NonNull Grid.Column<T> column, @NonNull Expression<? extends Comparable> sort) {
column.setId(dataProvider.addSortable(sort)); column.setSortOrderProvider(direction -> Stream.of(new QueryDslDataProvider.QueryDslSortOrder(sort, direction)));
column.setSortable(true); column.setSortable(true);
return column; return column;
} }