From 3744e10406d684a3605350f59aa7d93a73eb2b02 Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 28 Mar 2022 16:37:13 +0200 Subject: [PATCH] fixed filter --- Jenkinsfile | 2 +- .../gui/content/monitoring/FinishedExam.java | 9 +- .../servicelayer/PaginationService.java | 12 +- .../servicelayer/dao/FilterMap.java | 8 ++ .../api/ClientConnectionController.java | 131 ++++++++++++++---- 5 files changed, 127 insertions(+), 35 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index df7145c8..0814052d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -23,7 +23,7 @@ pipeline { stage('Reporting') { steps { withMaven(maven: 'Maven', options: [findbugsPublisher(disabled: true)]) { - sh "mvn --batch-mode -V -U -e pmd:pmd pmd:cpd findbugs:findbugs spotbugs:spotbugs" + sh "mvn --batch-mode -V -U -e -P let_reporting pmd:pmd pmd:cpd findbugs:findbugs spotbugs:spotbugs" } } } diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/monitoring/FinishedExam.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/monitoring/FinishedExam.java index 19f173d9..dbe2ddfc 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/monitoring/FinishedExam.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/monitoring/FinishedExam.java @@ -129,20 +129,23 @@ public class FinishedExam implements TemplateComposer { Domain.CLIENT_CONNECTION.ATTR_EXAM_USER_SESSION_ID, TABLE_COLUMN_NAME, c -> c.clientConnection.getUserSessionId()) - .withFilter(this.nameFilter)) + .withFilter(this.nameFilter) + .sortable()) .withColumn(new ColumnDefinition( ClientConnection.ATTR_INFO, TABLE_COLUMN_INFO, c -> c.clientConnection.getInfo()) - .withFilter(this.infoFilter)) + .withFilter(this.infoFilter) + .sortable()) .withColumn(new ColumnDefinition( Domain.CLIENT_CONNECTION.ATTR_STATUS, TABLE_COLUMN_STATUS, row -> this.pageService.getResourceService() .localizedClientConnectionStatusName(row.clientConnection.getStatus())) - .withFilter(this.statusFilter)) + .withFilter(this.statusFilter) + .sortable()) .withDefaultAction(t -> actionBuilder .newAction(ActionDefinition.VIEW_FINISHED_EXAM_CLIENT_CONNECTION) 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 f35d938c..6212f1d6 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 @@ -108,16 +108,17 @@ public interface PaginationService { * @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 + * @param pageFunction a function that filter and 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 Function, List> pageFunction) { + + final List sorted = pageFunction.apply(all); - final List sorted = sorter.apply(all); final int _pageNumber = getPageNumber(pageNumber); final int _pageSize = getPageSize(pageSize); final int start = (_pageNumber - 1) * _pageSize; @@ -125,7 +126,10 @@ public interface PaginationService { if (sorted.size() < end) { end = sorted.size(); } - final int numberOfPages = sorted.size() / _pageSize; + int numberOfPages = sorted.size() / _pageSize; + if (sorted.size() % _pageSize > 0) { + numberOfPages++; + } return new Page<>( (numberOfPages > 0) ? numberOfPages : 1, diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/FilterMap.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/FilterMap.java index 06e74fb9..1c9243bd 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/FilterMap.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/FilterMap.java @@ -9,6 +9,7 @@ package ch.ethz.seb.sebserver.webservice.servicelayer.dao; import java.util.Arrays; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; @@ -333,4 +334,11 @@ public class FilterMap extends POSTMapper { } } + public boolean containsAny(final Set extFilter) { + return extFilter.stream() + .filter(this.params::containsKey) + .findFirst() + .isPresent(); + } + } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientConnectionController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientConnectionController.java index 3bd7ec26..f4458849 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientConnectionController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ClientConnectionController.java @@ -8,8 +8,14 @@ package ch.ethz.seb.sebserver.webservice.weblayer.api; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; @@ -27,13 +33,16 @@ import org.springframework.web.bind.annotation.RestController; import ch.ethz.seb.sebserver.gbl.api.API; import ch.ethz.seb.sebserver.gbl.api.API.BulkActionType; import ch.ethz.seb.sebserver.gbl.api.EntityType; +import ch.ethz.seb.sebserver.gbl.model.Domain; import ch.ethz.seb.sebserver.gbl.model.EntityDependency; import ch.ethz.seb.sebserver.gbl.model.Page; +import ch.ethz.seb.sebserver.gbl.model.PageSortOrder; import ch.ethz.seb.sebserver.gbl.model.session.ClientConnection; import ch.ethz.seb.sebserver.gbl.model.session.ClientConnectionData; import ch.ethz.seb.sebserver.gbl.model.user.UserRole; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; import ch.ethz.seb.sebserver.gbl.util.Result; +import ch.ethz.seb.sebserver.gbl.util.Utils; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ClientConnectionRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService; import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationService; @@ -52,6 +61,8 @@ public class ClientConnectionController extends ReadonlyEntityController EXT_FILTER = new HashSet<>(Arrays.asList(ClientConnection.FILTER_ATTR_INFO)); + protected ClientConnectionController( final AuthorizationService authorization, final BulkActionService bulkActionService, @@ -93,15 +104,27 @@ public class ClientConnectionController extends ReadonlyEntityController page = this.paginationService.getPage( - pageNumber, - pageSize, - sort, - getSQLTableOfEntity().name(), - () -> getAllData(filterMap)) - .getOrThrow(); + if (StringUtils.isNotBlank(sort) || filterMap.containsAny(EXT_FILTER)) { - return page; + final Collection allConnections = getAllData(filterMap) + .getOrThrow(); + + return this.paginationService.buildPageFromList( + pageNumber, + pageSize, + sort, + allConnections, + pageFunction(filterMap, sort)); + } else { + + return this.paginationService.getPage( + pageNumber, + pageSize, + sort, + getSQLTableOfEntity().name(), + () -> getAllData(filterMap)) + .getOrThrow(); + } } @RequestMapping( @@ -115,25 +138,17 @@ public class ClientConnectionController extends ReadonlyEntityController> getAllData(final FilterMap filterMap) { - return getAll(filterMap) - .map(connection -> connection.stream() - .map(this.sebClientConnectionService::getIndicatorValues) - .flatMap(Result::onErrorLogAndSkip) - .collect(Collectors.toList())); - } - - @Override - protected Result> getAll(final FilterMap filterMap) { - final String infoFilter = filterMap.getString(ClientConnection.FILTER_ATTR_INFO); - if (StringUtils.isNotBlank(infoFilter)) { - return super.getAll(filterMap) - .map(all -> all.stream().filter(c -> c.getInfo() == null || c.getInfo().contains(infoFilter)) - .collect(Collectors.toList())); - } - - return super.getAll(filterMap); - } +// @Override +// protected Result> getAll(final FilterMap filterMap) { +// final String infoFilter = filterMap.getString(ClientConnection.FILTER_ATTR_INFO); +// if (StringUtils.isNotBlank(infoFilter)) { +// return super.getAll(filterMap) +// .map(all -> all.stream().filter(c -> c.getInfo() == null || c.getInfo().contains(infoFilter)) +// .collect(Collectors.toList())); +// } +// +// return super.getAll(filterMap); +// } @Override public Collection getDependencies( @@ -174,4 +189,66 @@ public class ClientConnectionController extends ReadonlyEntityController> getAllData(final FilterMap filterMap) { + return getAll(filterMap) + .map(connections -> connections.stream() + .map(this.sebClientConnectionService::getIndicatorValues) + .flatMap(Result::onErrorLogAndSkip) + .collect(Collectors.toList())); + } + + private Function, List> pageFunction( + final FilterMap filterMap, + final String sort) { + + return connections -> { + + final List filtered = connections.stream() + .filter(getFilter(filterMap)) + .collect(Collectors.toList()); + if (StringUtils.isNotBlank(sort)) { + filtered.sort(new ClientConnectionDataComparator(sort)); + } + return filtered; + }; + } + + private Predicate getFilter(final FilterMap filterMap) { + final String infoFilter = filterMap.getString(ClientConnection.FILTER_ATTR_INFO); + Predicate filter = Utils.truePredicate(); + if (StringUtils.isNotBlank(infoFilter)) { + filter = c -> c.clientConnection.getInfo() == null || c.clientConnection.getInfo().contains(infoFilter); + } + return filter; + } + + private static final class ClientConnectionDataComparator implements Comparator { + + final String sortColumn; + final boolean descending; + + ClientConnectionDataComparator(final String sort) { + this.sortColumn = PageSortOrder.decode(sort); + this.descending = PageSortOrder.getSortOrder(sort) == PageSortOrder.DESCENDING; + } + + @Override + public int compare(final ClientConnectionData cc1, final ClientConnectionData cc2) { + int result = 0; + if (Domain.CLIENT_CONNECTION.ATTR_EXAM_USER_SESSION_ID.equals(this.sortColumn)) { + result = cc1.clientConnection.userSessionId + .compareTo(cc2.clientConnection.userSessionId); + } else if (ClientConnection.ATTR_INFO.equals(this.sortColumn)) { + result = cc1.clientConnection.getInfo().compareTo(cc2.clientConnection.getInfo()); + } else if (Domain.CLIENT_CONNECTION.ATTR_STATUS.equals(this.sortColumn)) { + result = cc1.clientConnection.getStatus() + .compareTo(cc2.clientConnection.getStatus()); + } else { + result = cc1.clientConnection.userSessionId + .compareTo(cc2.clientConnection.userSessionId); + } + return (this.descending) ? -result : result; + } + } }