REST API /projects/$SUGGEST

The /projects/ URL now accepts a prefix string as part of the URL,
making it usable by the suggestion client UI to limit results.
With this change, the project suggestion service can be removed
and replaced by the REST API.

"GET /projects/platform/" will list all projects that start with
the specified platform/ prefix.

Change-Id: I6f8b9302166f59d03d58a85292cbf052e8419a78
This commit is contained in:
Shawn O. Pearce
2012-04-07 13:47:18 -07:00
parent e96071a099
commit 5cd0577828
9 changed files with 78 additions and 68 deletions

View File

@@ -16,8 +16,11 @@ package com.google.gerrit.client.projects;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.SuggestOracle;
public class ProjectInfo extends JavaScriptObject {
public class ProjectInfo
extends JavaScriptObject
implements SuggestOracle.Suggestion {
public final Project.NameKey name_key() {
return new Project.NameKey(name());
}
@@ -25,6 +28,19 @@ public class ProjectInfo extends JavaScriptObject {
public final native String name() /*-{ return this.name; }-*/;
public final native String description() /*-{ return this.description; }-*/;
@Override
public final String getDisplayString() {
if (description() != null) {
return name() + " (" + description() + ")";
}
return name();
}
@Override
public final String getReplacementString() {
return name();
}
protected ProjectInfo() {
}
}

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.client.projects;
import com.google.gerrit.client.rpc.NativeMap;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gwtjsonrpc.common.AsyncCallback;
import com.google.gwt.http.client.URL;
/** Projects available from {@code /projects/}. */
public class ProjectMap extends NativeMap<ProjectInfo> {
@@ -36,6 +37,14 @@ public class ProjectMap extends NativeMap<ProjectInfo> {
.send(NativeMap.copyKeysIntoChildren(callback));
}
public static void suggest(String prefix, int limit, AsyncCallback<ProjectMap> cb) {
new RestApi("/projects/" + URL.encode(prefix).replaceAll("[?]", "%3F"))
.addParameterRaw("type", "ALL")
.addParameter("n", limit)
.addParameterTrue("d") // description
.send(NativeMap.copyKeysIntoChildren(cb));
}
protected ProjectMap() {
}
}

View File

@@ -15,49 +15,25 @@
package com.google.gerrit.client.ui;
import com.google.gerrit.client.RpcStatus;
import com.google.gerrit.client.projects.ProjectMap;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwtexpui.safehtml.client.HighlightSuggestOracle;
import java.util.ArrayList;
import java.util.List;
/** Suggestion Oracle for Project.NameKey entities. */
public class ProjectNameSuggestOracle extends HighlightSuggestOracle {
@Override
public void onRequestSuggestions(final Request req, final Callback callback) {
RpcStatus.hide(new Runnable() {
@Override
public void run() {
SuggestUtil.SVC.suggestProjectNameKey(req.getQuery(), req.getLimit(),
new GerritCallback<List<Project.NameKey>>() {
public void onSuccess(final List<Project.NameKey> result) {
final ArrayList<ProjectNameSuggestion> r =
new ArrayList<ProjectNameSuggestion>(result.size());
for (final Project.NameKey p : result) {
r.add(new ProjectNameSuggestion(p));
}
callback.onSuggestionsReady(req, new Response(r));
ProjectMap.suggest(req.getQuery(), req.getLimit(),
new GerritCallback<ProjectMap>() {
@Override
public void onSuccess(ProjectMap map) {
callback.onSuggestionsReady(req, new Response(map.values().asList()));
}
});
}
});
}
private static class ProjectNameSuggestion implements
SuggestOracle.Suggestion {
private final Project.NameKey key;
ProjectNameSuggestion(final Project.NameKey k) {
key = k;
}
public String getDisplayString() {
return key.get();
}
public String getReplacementString() {
return key.get();
}
}
}