Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute-based filtering to organization count API #321

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Stream<OrganizationModel> searchForOrganizationStream(
Integer maxResults,
Optional<UserModel> member);

Long getOrganizationsCount(RealmModel realm, String search);
Long getOrganizationsCount(RealmModel realm, String search, Map<String, String> attributes);

boolean removeOrganization(RealmModel realm, String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,25 @@ public Stream<OrganizationModel> searchForOrganizationStream(
}

@Override
public Long getOrganizationsCount(RealmModel realm, String search) {
TypedQuery<Long> query = em.createNamedQuery("countOrganizationsByRealmIdAndName", Long.class);
query.setParameter("realmId", realm.getId());
search = createSearchString(search);
query.setParameter("search", search);
public Long getOrganizationsCount(RealmModel realm, String search, Map<String, String> attributes) {
if (attributes == null) {
attributes = ImmutableMap.of();
}

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Long> queryBuilder = builder.createQuery(Long.class);
Root<ExtOrganizationEntity> root = queryBuilder.from(ExtOrganizationEntity.class);

List<Predicate> predicates = attributePredicates(attributes, root);
predicates.add(builder.equal(root.get("realmId"), realm.getId()));

if (search != null && !search.trim().isEmpty()) {
String searchPattern = "%" + search.toLowerCase() + "%";
predicates.add(builder.like(builder.lower(root.get("name")), searchPattern));
}

queryBuilder.select(builder.count(root)).where(predicates.toArray(new Predicate[0]));
TypedQuery<Long> query = em.createQuery(queryBuilder);
return query.getSingleResult();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,24 @@ public Stream<Organization> listOrgs(
@GET
@Path("count")
@Produces(MediaType.APPLICATION_JSON)
public Long countOrgs(@QueryParam("search") String searchQuery) {
log.debugf("countOrgs %s %s", realm.getName(), searchQuery);
public Long countOrgs(
@QueryParam("search") String searchQuery,
@QueryParam("q") String searchAttributes) {

log.debugf("countOrgs realm: %s, search: %s, query: %s", realm.getName(), searchQuery, searchAttributes);

if (!auth.hasViewOrgs()) {
throw new NotAuthorizedException("Insufficient permission to count organizations.");
}
return orgs.getOrganizationsCount(realm, searchQuery);

Map<String, String> attributes =
(searchAttributes == null) ? Maps.newHashMap() : SearchQueryUtils.getFields(searchAttributes);

if (searchQuery != null) {
attributes.put("name", searchQuery.trim());
}

return orgs.getOrganizationsCount(realm, searchQuery, attributes);
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ void testSearchOrganizations() throws IOException {
assertThat(orgs, notNullValue());
assertThat(cnt, is(6L));

// orgs count with attribute filter
response = givenSpec("orgs", "count").when().queryParam("q", "foo:bar").get().andReturn();
assertThat(response.statusCode(), is(Status.OK.getStatusCode()));
cnt = objectMapper().readValue(response.getBody().asString(), Long.class);
assertThat(cnt, is(2L));

// orgs count with name + attribute filter
response = givenSpec("orgs", "count").when().queryParam("search", "qu").queryParam("q", "foo:bar").get().andReturn();
assertThat(response.statusCode(), is(Status.OK.getStatusCode()));
cnt = objectMapper().readValue(response.getBody().asString(), Long.class);
assertThat(cnt, is(1L));

for (String id : ids) {
deleteOrganization(id);
}
Expand Down