Groups API: Add query methods
Change-Id: I8a47f281b1311675781ec520e278a5144d5b42c9 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -52,6 +52,25 @@ public interface Groups {
|
||||
/** @return new request for listing groups. */
|
||||
ListRequest list();
|
||||
|
||||
/**
|
||||
* Query groups.
|
||||
* <p>
|
||||
* Example code:
|
||||
* {@code query().withQuery("uuid:085178e5de6302324675715ca22f4027538253ba").get()}
|
||||
*
|
||||
* @return API for setting parameters and getting result.
|
||||
*/
|
||||
QueryRequest query();
|
||||
|
||||
/**
|
||||
* Query groups.
|
||||
* <p>
|
||||
* Shortcut API for {@code query().withQuery(String)}.
|
||||
*
|
||||
* @see #query()
|
||||
*/
|
||||
QueryRequest query(String query);
|
||||
|
||||
abstract class ListRequest {
|
||||
private final EnumSet<ListGroupsOption> options =
|
||||
EnumSet.noneOf(ListGroupsOption.class);
|
||||
@@ -181,6 +200,84 @@ public interface Groups {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API for setting parameters and getting result.
|
||||
* Used for {@code query()}.
|
||||
*
|
||||
* @see #query()
|
||||
*/
|
||||
abstract class QueryRequest {
|
||||
private String query;
|
||||
private int limit;
|
||||
private int start;
|
||||
private EnumSet<ListGroupsOption> options =
|
||||
EnumSet.noneOf(ListGroupsOption.class);
|
||||
|
||||
/**
|
||||
* Execute query and returns the matched groups as list.
|
||||
*/
|
||||
public abstract List<GroupInfo> get() throws RestApiException;
|
||||
|
||||
/**
|
||||
* Set query.
|
||||
*
|
||||
* @param query needs to be in human-readable form.
|
||||
*/
|
||||
public QueryRequest withQuery(String query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set limit for returned list of groups.
|
||||
* Optional; server-default is used when not provided.
|
||||
*/
|
||||
public QueryRequest withLimit(int limit) {
|
||||
this.limit = limit;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set number of groups to skip.
|
||||
* Optional; no groups are skipped when not provided.
|
||||
*/
|
||||
public QueryRequest withStart(int start) {
|
||||
this.start = start;
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryRequest withOption(ListGroupsOption options) {
|
||||
this.options.add(options);
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryRequest withOptions(ListGroupsOption... options) {
|
||||
this.options.addAll(Arrays.asList(options));
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryRequest withOptions(EnumSet<ListGroupsOption> options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public EnumSet<ListGroupsOption> getOptions() {
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A default implementation which allows source compatibility
|
||||
* when adding new methods to the interface.
|
||||
@@ -205,5 +302,15 @@ public interface Groups {
|
||||
public ListRequest list() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryRequest query() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryRequest query(String query) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import static com.google.gerrit.server.account.CapabilityUtils.checkRequiresCapa
|
||||
import com.google.gerrit.extensions.api.groups.GroupApi;
|
||||
import com.google.gerrit.extensions.api.groups.GroupInput;
|
||||
import com.google.gerrit.extensions.api.groups.Groups;
|
||||
import com.google.gerrit.extensions.client.ListGroupsOption;
|
||||
import com.google.gerrit.extensions.common.GroupInfo;
|
||||
import com.google.gerrit.extensions.restapi.BadRequestException;
|
||||
import com.google.gerrit.extensions.restapi.IdString;
|
||||
@@ -30,6 +31,7 @@ import com.google.gerrit.server.account.AccountsCollection;
|
||||
import com.google.gerrit.server.group.CreateGroup;
|
||||
import com.google.gerrit.server.group.GroupsCollection;
|
||||
import com.google.gerrit.server.group.ListGroups;
|
||||
import com.google.gerrit.server.group.QueryGroups;
|
||||
import com.google.gerrit.server.project.ProjectsCollection;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
@@ -37,6 +39,7 @@ import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.SortedMap;
|
||||
|
||||
@Singleton
|
||||
@@ -45,6 +48,7 @@ class GroupsImpl implements Groups {
|
||||
private final GroupsCollection groups;
|
||||
private final ProjectsCollection projects;
|
||||
private final Provider<ListGroups> listGroups;
|
||||
private final Provider<QueryGroups> queryGroups;
|
||||
private final Provider<CurrentUser> user;
|
||||
private final CreateGroup.Factory createGroup;
|
||||
private final GroupApiImpl.Factory api;
|
||||
@@ -55,6 +59,7 @@ class GroupsImpl implements Groups {
|
||||
GroupsCollection groups,
|
||||
ProjectsCollection projects,
|
||||
Provider<ListGroups> listGroups,
|
||||
Provider<QueryGroups> queryGroups,
|
||||
Provider<CurrentUser> user,
|
||||
CreateGroup.Factory createGroup,
|
||||
GroupApiImpl.Factory api) {
|
||||
@@ -62,6 +67,7 @@ class GroupsImpl implements Groups {
|
||||
this.groups = groups;
|
||||
this.projects = projects;
|
||||
this.listGroups = listGroups;
|
||||
this.queryGroups = queryGroups;
|
||||
this.user = user;
|
||||
this.createGroup = createGroup;
|
||||
this.api = api;
|
||||
@@ -145,4 +151,35 @@ class GroupsImpl implements Groups {
|
||||
throw new RestApiException("Cannot list groups", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryRequest query() {
|
||||
return new QueryRequest() {
|
||||
@Override
|
||||
public List<GroupInfo> get() throws RestApiException {
|
||||
return GroupsImpl.this.query(this);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryRequest query(String query) {
|
||||
return query().withQuery(query);
|
||||
}
|
||||
|
||||
private List<GroupInfo> query(QueryRequest r)
|
||||
throws RestApiException {
|
||||
try {
|
||||
QueryGroups myQueryGroups = queryGroups.get();
|
||||
myQueryGroups.setQuery(r.getQuery());
|
||||
myQueryGroups.setLimit(r.getLimit());
|
||||
myQueryGroups.setStart(r.getStart());
|
||||
for (ListGroupsOption option : r.getOptions()) {
|
||||
myQueryGroups.addOption(option);
|
||||
}
|
||||
return myQueryGroups.apply(TopLevelResource.INSTANCE);
|
||||
} catch (OrmException e) {
|
||||
throw new RestApiException("Cannot query groups", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user