2020-03-23 15:35:58 +01:00
|
|
|
/*
|
2024-09-24 09:42:07 +02:00
|
|
|
* Copyright (c) 2020 - present Cloudogu GmbH
|
2020-03-23 15:35:58 +01:00
|
|
|
*
|
2024-09-24 09:42:07 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
|
* Software Foundation, version 3.
|
2020-03-23 15:35:58 +01:00
|
|
|
*
|
2024-09-24 09:42:07 +02:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
|
* details.
|
2020-03-23 15:35:58 +01:00
|
|
|
*
|
2024-09-24 09:42:07 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
2020-03-23 15:35:58 +01:00
|
|
|
*/
|
2022-01-11 16:17:57 +01:00
|
|
|
|
2019-04-01 13:51:34 +02:00
|
|
|
package sonia.scm;
|
|
|
|
|
|
|
|
|
|
import sonia.scm.search.SearchRequest;
|
|
|
|
|
import sonia.scm.search.SearchUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
Sorted autocomplete (#1918)
Users, groups, repositories and repository roles have been sorted in the rest layer by default if no other sort option was given. In the layers "below" (aka the manager classes or the dao), the collections have been unsorted. This led to the effect, that the autocomplete resource, which did not sort all values beforehand, returned unsorted results. As a sideeffect, direct matches for an input could occur at a random position or not at all (as reported in #1695), when there were enough other matches.
With this pull request the databases for users, groups, repositories and repository roles will use instances of TreeMap instead of LinkedHashMap internally, so that these values are sorted implicitly (by id respectively name for users, groups and repository roles and namespace/name for repositories).
Due to this change the default sort applied in the rest layer could be removed.
2022-01-18 09:46:10 +01:00
|
|
|
import static java.util.Optional.empty;
|
2019-04-01 13:51:34 +02:00
|
|
|
import static java.util.Optional.ofNullable;
|
|
|
|
|
|
|
|
|
|
public abstract class GenericDisplayManager<D, T extends ReducedModelObject> implements DisplayManager<T> {
|
|
|
|
|
|
|
|
|
|
private final GenericDAO<D> dao;
|
|
|
|
|
private final Function<D, T> transform;
|
|
|
|
|
|
|
|
|
|
protected GenericDisplayManager(GenericDAO<D> dao, Function<D, T> transform) {
|
|
|
|
|
this.dao = dao;
|
|
|
|
|
this.transform = transform;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Collection<T> autocomplete(String filter) {
|
|
|
|
|
checkPermission();
|
|
|
|
|
SearchRequest searchRequest = new SearchRequest(filter, true, DEFAULT_LIMIT);
|
|
|
|
|
return SearchUtil.search(
|
|
|
|
|
searchRequest,
|
|
|
|
|
dao.getAll(),
|
|
|
|
|
object -> matches(searchRequest, object)? transform.apply(object): null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void checkPermission();
|
|
|
|
|
|
|
|
|
|
protected abstract boolean matches(SearchRequest searchRequest, D object);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Optional<T> get(String id) {
|
Sorted autocomplete (#1918)
Users, groups, repositories and repository roles have been sorted in the rest layer by default if no other sort option was given. In the layers "below" (aka the manager classes or the dao), the collections have been unsorted. This led to the effect, that the autocomplete resource, which did not sort all values beforehand, returned unsorted results. As a sideeffect, direct matches for an input could occur at a random position or not at all (as reported in #1695), when there were enough other matches.
With this pull request the databases for users, groups, repositories and repository roles will use instances of TreeMap instead of LinkedHashMap internally, so that these values are sorted implicitly (by id respectively name for users, groups and repository roles and namespace/name for repositories).
Due to this change the default sort applied in the rest layer could be removed.
2022-01-18 09:46:10 +01:00
|
|
|
if (id == null) {
|
|
|
|
|
return empty();
|
|
|
|
|
}
|
2019-04-01 13:51:34 +02:00
|
|
|
return ofNullable(dao.get(id)).map(transform);
|
|
|
|
|
}
|
|
|
|
|
}
|