diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationService.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationService.java index 8799ab75..1993dfa7 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationService.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationService.java @@ -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>> delegate); + /** Use this to build a current Page from a given list of objects. + * + * @param 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 Page buildPageFromList( + final Integer pageNumber, + final Integer pageSize, + final String sort, + final Collection all, + final Function, List> sorter) { + + final List 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)); + } + } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java index d48bf3aa..f9ff8024 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java @@ -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 { EntityType.EXAM, institutionId); - final List exams = new ArrayList<>( - this.examDAO - .allMatching(new FilterMap(allRequestParams, request.getQueryString()), this::hasReadAccess) - .getOrThrow()); + final Collection 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 { } } - public static Page buildSortedExamPage( - final Integer pageNumber, - final Integer pageSize, - final String sort, - final List exams) { + static Function, List> pageSort(final String sort) { + + final String sortBy = PageSortOrder.decode(sort); + return exams -> { + final List 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; + }; } } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java index ea3af18a..2302d637 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamMonitoringController.java @@ -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 exams = new ArrayList<>(this.examSessionService + final Collection 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(