KEYCLOAK 2538 - UI group pagination - fix duplicate result for search + sort result

This commit is contained in:
Levente NAGY 2017-09-12 11:45:37 +02:00
parent 2c24b39268
commit db56d82dbd
4 changed files with 22 additions and 15 deletions

View file

@ -18,6 +18,7 @@
package org.keycloak.models.jpa;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.codec.binary.StringUtils;
import org.jboss.logging.Logger;
import org.keycloak.common.util.Time;
import org.keycloak.connections.jpa.util.JpaUtils;
@ -335,6 +336,7 @@ public class JpaRealmProvider implements RealmProvider {
return ref.getGroups().stream()
.map(g -> session.realms().getGroupById(g.getId(), realm))
.sorted(Comparator.comparing(GroupModel::getName))
.collect(Collectors.collectingAndThen(
Collectors.toList(), Collections::unmodifiableList));
}
@ -354,12 +356,7 @@ public class JpaRealmProvider implements RealmProvider {
@Override
public Long getGroupsCountByNameContaining(RealmModel realm, String search) {
Long count = em.createNamedQuery("getGroupCountByNameContaining", Long.class)
.setParameter("realm", realm.getId())
.setParameter("name", search)
.getSingleResult();
return count;
return (long) searchForGroupByName(realm, search, null, null).size();
}
@Override
@ -369,6 +366,7 @@ public class JpaRealmProvider implements RealmProvider {
return ref.getGroups().stream()
.filter(g -> g.getParent() == null)
.map(g -> session.realms().getGroupById(g.getId(), realm))
.sorted(Comparator.comparing(GroupModel::getName))
.collect(Collectors.collectingAndThen(
Collectors.toList(), Collections::unmodifiableList));
}
@ -388,6 +386,8 @@ public class JpaRealmProvider implements RealmProvider {
}
}
list.sort(Comparator.comparing(GroupModel::getName));
return Collections.unmodifiableList(list);
}
@ -582,7 +582,7 @@ public class JpaRealmProvider implements RealmProvider {
@Override
public List<GroupModel> searchForGroupByName(RealmModel realm, String search, Integer first, Integer max) {
TypedQuery<String> query = em.createNamedQuery("getTopLevelGroupIdsByNameContaining", String.class)
TypedQuery<String> query = em.createNamedQuery("getGroupIdsByNameContaining", String.class)
.setParameter("realm", realm.getId())
.setParameter("search", search);
if(Objects.nonNull(first) && Objects.nonNull(max)) {
@ -590,10 +590,18 @@ public class JpaRealmProvider implements RealmProvider {
}
List<String> groups = query.getResultList();
if (Objects.isNull(groups)) return Collections.EMPTY_LIST;
List<GroupModel> list = new LinkedList<>();
List<GroupModel> list = new ArrayList<>();
for (String id : groups) {
list.add(session.realms().getGroupById(id, realm));
GroupModel groupById = session.realms().getGroupById(id, realm);
while(Objects.nonNull(groupById.getParentId())) {
groupById = session.realms().getGroupById(groupById.getParentId(), realm);
}
if(!list.contains(groupById)) {
list.add(groupById);
}
}
list.sort(Comparator.comparing(GroupModel::getName));
return Collections.unmodifiableList(list);
}
@ -645,7 +653,7 @@ public class JpaRealmProvider implements RealmProvider {
List<ClientInitialAccessEntity> entities = query.getResultList();
return entities.stream()
.map(entity -> entityToModel(entity))
.map(this::entityToModel)
.collect(Collectors.toList());
}

View file

@ -27,11 +27,10 @@ import java.util.Collection;
*/
@NamedQueries({
@NamedQuery(name="getGroupIdsByParent", query="select u.id from GroupEntity u where u.parent = :parent"),
@NamedQuery(name="getTopLevelGroupIdsByNameContaining", query="select u.id from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:search,'%') and u.parent is null order by u.name ASC"),
@NamedQuery(name="getGroupIdsByNameContaining", query="select u.id from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:search,'%') order by u.name ASC"),
@NamedQuery(name="getTopLevelGroupIds", query="select u.id from GroupEntity u where u.parent is null and u.realm.id = :realm"),
@NamedQuery(name="getGroupCount", query="select count(u) from GroupEntity u where u.realm.id = :realm"),
@NamedQuery(name="getTopLevelGroupCount", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.parent is null"),
@NamedQuery(name="getGroupCountByNameContaining", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:name,'%')"),
@NamedQuery(name="getTopLevelGroupCount", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.parent is null")
})
@Entity
@Table(name="KEYCLOAK_GROUP")

View file

@ -48,7 +48,7 @@ public interface RealmProvider extends Provider {
List<GroupModel> getTopLevelGroups(RealmModel realm, Integer first, Integer max);
List<GroupModel> searchForGroupByName(RealmModel realm, String search, Integer first, Integer max);
List searchForGroupByName(RealmModel realm, String search, Integer first, Integer max);
boolean removeGroup(RealmModel realm, GroupModel group);

View file

@ -65,7 +65,7 @@ module.controller('GroupListCtrl', function($scope, $route, $q, realm, groups, g
$scope.$watch('currentPage', function(newValue, oldValue) {
if(newValue !== oldValue) {
refreshGroups();
refreshGroups($scope.searchTerms);
}
});