Skip to content

Commit

Permalink
KEYCLOAK 2538 - UI group pagination - fix duplicate result for search…
Browse files Browse the repository at this point in the history
… + sort result
  • Loading branch information
Levente NAGY committed Sep 12, 2017
1 parent 2c24b39 commit db56d82
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Expand Up @@ -18,6 +18,7 @@
package org.keycloak.models.jpa; package org.keycloak.models.jpa;


import com.sun.org.apache.xpath.internal.operations.Bool; import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.codec.binary.StringUtils;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.common.util.Time; import org.keycloak.common.util.Time;
import org.keycloak.connections.jpa.util.JpaUtils; import org.keycloak.connections.jpa.util.JpaUtils;
Expand Down Expand Up @@ -335,6 +336,7 @@ public List<GroupModel> getGroups(RealmModel realm) {


return ref.getGroups().stream() return ref.getGroups().stream()
.map(g -> session.realms().getGroupById(g.getId(), realm)) .map(g -> session.realms().getGroupById(g.getId(), realm))
.sorted(Comparator.comparing(GroupModel::getName))
.collect(Collectors.collectingAndThen( .collect(Collectors.collectingAndThen(
Collectors.toList(), Collections::unmodifiableList)); Collectors.toList(), Collections::unmodifiableList));
} }
Expand All @@ -354,12 +356,7 @@ public Long getGroupsCount(RealmModel realm, Boolean onlyTopGroups) {


@Override @Override
public Long getGroupsCountByNameContaining(RealmModel realm, String search) { public Long getGroupsCountByNameContaining(RealmModel realm, String search) {
Long count = em.createNamedQuery("getGroupCountByNameContaining", Long.class) return (long) searchForGroupByName(realm, search, null, null).size();
.setParameter("realm", realm.getId())
.setParameter("name", search)
.getSingleResult();

return count;
} }


@Override @Override
Expand All @@ -369,6 +366,7 @@ public List<GroupModel> getTopLevelGroups(RealmModel realm) {
return ref.getGroups().stream() return ref.getGroups().stream()
.filter(g -> g.getParent() == null) .filter(g -> g.getParent() == null)
.map(g -> session.realms().getGroupById(g.getId(), realm)) .map(g -> session.realms().getGroupById(g.getId(), realm))
.sorted(Comparator.comparing(GroupModel::getName))
.collect(Collectors.collectingAndThen( .collect(Collectors.collectingAndThen(
Collectors.toList(), Collections::unmodifiableList)); Collectors.toList(), Collections::unmodifiableList));
} }
Expand All @@ -388,6 +386,8 @@ public List<GroupModel> getTopLevelGroups(RealmModel realm, Integer first, Integ
} }
} }


list.sort(Comparator.comparing(GroupModel::getName));

return Collections.unmodifiableList(list); return Collections.unmodifiableList(list);
} }


Expand Down Expand Up @@ -582,18 +582,26 @@ public ClientTemplateModel getClientTemplateById(String id, RealmModel realm) {


@Override @Override
public List<GroupModel> searchForGroupByName(RealmModel realm, String search, Integer first, Integer max) { 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("realm", realm.getId())
.setParameter("search", search); .setParameter("search", search);
if(Objects.nonNull(first) && Objects.nonNull(max)) { if(Objects.nonNull(first) && Objects.nonNull(max)) {
query= query.setFirstResult(first).setMaxResults(max); query= query.setFirstResult(first).setMaxResults(max);
} }
List<String> groups = query.getResultList(); List<String> groups = query.getResultList();
if (Objects.isNull(groups)) return Collections.EMPTY_LIST; if (Objects.isNull(groups)) return Collections.EMPTY_LIST;
List<GroupModel> list = new LinkedList<>(); List<GroupModel> list = new ArrayList<>();
for (String id : groups) { 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); return Collections.unmodifiableList(list);
} }


Expand Down Expand Up @@ -645,7 +653,7 @@ public List<ClientInitialAccessModel> listClientInitialAccess(RealmModel realm)
List<ClientInitialAccessEntity> entities = query.getResultList(); List<ClientInitialAccessEntity> entities = query.getResultList();


return entities.stream() return entities.stream()
.map(entity -> entityToModel(entity)) .map(this::entityToModel)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }


Expand Down
Expand Up @@ -27,11 +27,10 @@
*/ */
@NamedQueries({ @NamedQueries({
@NamedQuery(name="getGroupIdsByParent", query="select u.id from GroupEntity u where u.parent = :parent"), @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="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="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="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,'%')"),
}) })
@Entity @Entity
@Table(name="KEYCLOAK_GROUP") @Table(name="KEYCLOAK_GROUP")
Expand Down
Expand Up @@ -48,7 +48,7 @@ public interface RealmProvider extends Provider {


List<GroupModel> getTopLevelGroups(RealmModel realm, Integer first, Integer max); 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); boolean removeGroup(RealmModel realm, GroupModel group);


Expand Down
Expand Up @@ -65,7 +65,7 @@ module.controller('GroupListCtrl', function($scope, $route, $q, realm, groups, g


$scope.$watch('currentPage', function(newValue, oldValue) { $scope.$watch('currentPage', function(newValue, oldValue) {
if(newValue !== oldValue) { if(newValue !== oldValue) {
refreshGroups(); refreshGroups($scope.searchTerms);
} }
}); });


Expand Down

0 comments on commit db56d82

Please sign in to comment.