refactor create page form list (noSQL)

This commit is contained in:
anhefti 2021-04-20 16:21:21 +02:00
parent 813b1c84ba
commit 33e6fab629
3 changed files with 70 additions and 43 deletions

View file

@ -9,6 +9,8 @@
package ch.ethz.seb.sebserver.webservice.servicelayer;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import org.mybatis.dynamic.sql.SqlTable;
@ -61,7 +63,7 @@ public interface PaginationService {
* So we recommend to apply as much of the filtering as possible on the SQL level and only if necessary and
* not avoidable, apply a additional filter on software-level that eventually filter one or two entities
* for a page.
*
*
* @param pageNumber the current page number
* @param pageSize the (full) size of the page
* @param sort the name of the sort column with a leading '-' for descending sort order
@ -75,4 +77,36 @@ public interface PaginationService {
final String tableName,
final Supplier<Result<Collection<T>>> delegate);
/** Use this to build a current Page from a given list of objects.
*
* @param <T> the Type if list entities
* @param pageNumber the number of the current page
* @param pageSize the size of a page
* @param sort the page sort flag
* @param all list of all entities, unsorted
* @param sorter a sorter function that sorts the list for specific type of entries
* @return current page of objects from the sorted list of entities */
default <T> Page<T> buildPageFromList(
final Integer pageNumber,
final Integer pageSize,
final String sort,
final Collection<T> all,
final Function<Collection<T>, List<T>> sorter) {
final List<T> sorted = sorter.apply(all);
final int _pageNumber = getPageNumber(pageNumber);
final int _pageSize = getPageSize(pageSize);
final int start = (_pageNumber - 1) * _pageSize;
int end = start + _pageSize;
if (sorted.size() < end) {
end = sorted.size();
}
return new Page<>(
sorted.size() / _pageSize,
_pageNumber,
sort,
sorted.subList(start, end));
}
}

View file

@ -9,12 +9,12 @@
package ch.ethz.seb.sebserver.webservice.weblayer.api;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.ServletOutputStream;
@ -158,16 +158,19 @@ public class ExamAdministrationController extends EntityController<Exam, Exam> {
EntityType.EXAM,
institutionId);
final List<Exam> exams = new ArrayList<>(
this.examDAO
.allMatching(new FilterMap(allRequestParams, request.getQueryString()), this::hasReadAccess)
.getOrThrow());
final Collection<Exam> exams = this.examDAO
.allMatching(new FilterMap(
allRequestParams,
request.getQueryString()),
this::hasReadAccess)
.getOrThrow();
return buildSortedExamPage(
this.paginationService.getPageNumber(pageNumber),
this.paginationService.getPageSize(pageSize),
return this.paginationService.buildPageFromList(
pageNumber,
pageSize,
sort,
exams);
exams,
pageSort(sort));
}
}
@ -543,39 +546,30 @@ public class ExamAdministrationController extends EntityController<Exam, Exam> {
}
}
public static Page<Exam> buildSortedExamPage(
final Integer pageNumber,
final Integer pageSize,
final String sort,
final List<Exam> exams) {
static Function<Collection<Exam>, List<Exam>> pageSort(final String sort) {
final String sortBy = PageSortOrder.decode(sort);
return exams -> {
final List<Exam> list = exams.stream().collect(Collectors.toList());
if (StringUtils.isBlank(sort)) {
return list;
}
if (!StringUtils.isBlank(sort)) {
final String sortBy = PageSortOrder.decode(sort);
if (sortBy.equals(Exam.FILTER_ATTR_NAME)) {
exams.sort(Comparator.comparing(exam -> exam.name));
list.sort(Comparator.comparing(exam -> exam.name));
}
if (sortBy.equals(Exam.FILTER_ATTR_TYPE)) {
exams.sort(Comparator.comparing(exam -> exam.type));
list.sort(Comparator.comparing(exam -> exam.type));
}
if (sortBy.equals(QuizData.FILTER_ATTR_START_TIME)) {
exams.sort(Comparator.comparing(exam -> exam.startTime));
list.sort(Comparator.comparing(exam -> exam.startTime));
}
}
if (PageSortOrder.DESCENDING == PageSortOrder.getSortOrder(sort)) {
Collections.reverse(exams);
}
final int start = (pageNumber - 1) * pageSize;
int end = start + pageSize;
if (exams.size() < end) {
end = exams.size();
}
return new Page<>(
exams.size() / pageSize,
pageNumber,
sort,
exams.subList(start, end));
if (PageSortOrder.DESCENDING == PageSortOrder.getSortOrder(sort)) {
Collections.reverse(list);
}
return list;
};
}
}

View file

@ -8,10 +8,8 @@
package ch.ethz.seb.sebserver.webservice.weblayer.api;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
@ -141,17 +139,18 @@ public class ExamMonitoringController {
filterMap.putIfAbsent(API.PARAM_INSTITUTION_ID, String.valueOf(institutionId));
}
final List<Exam> exams = new ArrayList<>(this.examSessionService
final Collection<Exam> exams = this.examSessionService
.getFilteredRunningExams(
filterMap,
exam -> this.hasRunningExamPrivilege(exam, institutionId))
.getOrThrow());
.getOrThrow();
return ExamAdministrationController.buildSortedExamPage(
this.paginationService.getPageNumber(pageNumber),
this.paginationService.getPageSize(pageSize),
return this.paginationService.buildPageFromList(
pageNumber,
pageSize,
sort,
exams);
exams,
ExamAdministrationController.pageSort(sort));
}
@RequestMapping(