[KEYCLOAK-2538] - groups count for pagination limits

This commit is contained in:
Levente NAGY 2017-06-07 20:52:22 +02:00
parent c4da7637d6
commit f377a45c4e
9 changed files with 102 additions and 1 deletions

View file

@ -67,6 +67,29 @@ public interface GroupsResource {
@QueryParam("first") Integer first,
@QueryParam("max") Integer max);
/**
* Counts all groups.
* @return The number of groups.
*/
@GET
@NoCache
@Path("/count")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Response count();
/**
* Counts groups by name search.
* @param search max number of occurrences
* @return The number of group containing search therm.
*/
@GET
@NoCache
@Path("/count")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
Response count(@QueryParam("search") String search);
/**
* create or add a top level realm groupSet or create child. This will update the group and set the parent if it exists. Create it and set the parent
* if the group doesn't exist.

View file

@ -1200,6 +1200,16 @@ public class RealmAdapter implements CachedRealmModel {
return cacheSession.getGroups(this);
}
@Override
public Long getGroupsCount() {
return cacheSession.getGroupsCount(this);
}
@Override
public Long getGroupsCountByNameContaining(String search) {
return cacheSession.getGroupsCountByNameContaining(this, search);
}
@Override
public List<GroupModel> getTopLevelGroups() {
return cacheSession.getTopLevelGroups(this);

View file

@ -841,6 +841,16 @@ public class RealmCacheSession implements CacheRealmProvider {
return list;
}
@Override
public Long getGroupsCount(RealmModel realm) {
return getDelegate().getGroupsCount(realm);
}
@Override
public Long getGroupsCountByNameContaining(RealmModel realm, String search) {
return getDelegate().getGroupsCountByNameContaining(realm, search);
}
@Override
public List<GroupModel> getTopLevelGroups(RealmModel realm) {
String cacheKey = getTopGroupsQueryCacheKey(realm.getId());

View file

@ -322,6 +322,25 @@ public class JpaRealmProvider implements RealmProvider {
Collectors.toList(), Collections::unmodifiableList));
}
@Override
public Long getGroupsCount(RealmModel realm) {
Long count = em.createNamedQuery("getGroupCount", Long.class)
.setParameter("realm", realm.getId())
.getSingleResult();
return count;
}
@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;
}
@Override
public List<GroupModel> getTopLevelGroups(RealmModel realm) {
RealmEntity ref = em.getReference(RealmEntity.class, realm.getId());

View file

@ -1674,6 +1674,16 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
return session.realms().getGroups(this);
}
@Override
public Long getGroupsCount() {
return session.realms().getGroupsCount(this);
}
@Override
public Long getGroupsCountByNameContaining(String search) {
return session.realms().getGroupsCountByNameContaining(this, search);
}
@Override
public List<GroupModel> getTopLevelGroups() {
return session.realms().getTopLevelGroups(this);

View file

@ -28,7 +28,9 @@ import java.util.Collection;
@NamedQueries({
@NamedQuery(name="getGroupIdsByParent", query="select u.id from GroupEntity u where u.parent = :parent"),
@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="getGroupCountByNameContaining", query="select count(u) from GroupEntity u where u.realm.id = :realm and u.name like concat('%',:name,'%')")
})
@Entity
@Table(name="KEYCLOAK_GROUP")

View file

@ -397,6 +397,8 @@ public interface RealmModel extends RoleContainerModel {
GroupModel getGroupById(String id);
List<GroupModel> getGroups();
Long getGroupsCount();
Long getGroupsCountByNameContaining(String search);
List<GroupModel> getTopLevelGroups();
List<GroupModel> getTopLevelGroups(Integer first, Integer max);
List<GroupModel> searchForGroupByName(String search, Integer first, Integer max);

View file

@ -40,6 +40,10 @@ public interface RealmProvider extends Provider {
List<GroupModel> getGroups(RealmModel realm);
Long getGroupsCount(RealmModel realm);
Long getGroupsCountByNameContaining(RealmModel realm, String search);
List<GroupModel> getTopLevelGroups(RealmModel realm);
List<GroupModel> getTopLevelGroups(RealmModel realm, Integer first, Integer max);
@ -89,6 +93,8 @@ public interface RealmProvider extends Provider {
ClientTemplateModel getClientTemplateById(String id, RealmModel realm);
GroupModel getGroupById(String id, RealmModel realm);
List<RealmModel> getRealms();
boolean removeRealm(String id);
void close();

View file

@ -101,6 +101,25 @@ public class GroupsResource {
return resource;
}
/**
* Returns the groups counts.
*
* @return
*/
@GET
@NoCache
@Path("/count")
public Response getGroupCount(@QueryParam("search") String search) {
auth.requireView();
Long results;
if (Objects.nonNull(search)) {
results = realm.getGroupsCountByNameContaining(search);
} else {
results = realm.getGroupsCount();
}
return Response.ok(results).build();
}
/**
* create or add a top level realm groupSet or create child. This will update the group and set the parent if it exists. Create it and set the parent
* if the group doesn't exist.