Removing all RPC calls for watching projects
This change migrates the GWT UI towards using the new REST endpoints for
all requests related to watching projects. This will remove unnecessary
RPC requests.
Change-Id: Ib59fe170dcd6c22aceab9ccb6434f065319a31f7
diff --git a/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountProjectWatchInfo.java b/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountProjectWatchInfo.java
deleted file mode 100644
index 581a09b..0000000
--- a/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountProjectWatchInfo.java
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright (C) 2008 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.gerrit.common.data;
-
-import com.google.gerrit.reviewdb.client.AccountProjectWatch;
-import com.google.gerrit.reviewdb.client.Project;
-
-public final class AccountProjectWatchInfo {
- protected AccountProjectWatch watch;
- protected Project project;
-
- protected AccountProjectWatchInfo() {
- }
-
- public AccountProjectWatchInfo(final AccountProjectWatch w, final Project p) {
- watch = w;
- project = p;
- }
-
- public AccountProjectWatch getWatch() {
- return watch;
- }
-
- public Project getProject() {
- return project;
- }
-}
diff --git a/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountService.java b/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountService.java
index 55073d5..22482c7 100644
--- a/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountService.java
+++ b/gerrit-common/src/main/java/com/google/gerrit/common/data/AccountService.java
@@ -14,38 +14,14 @@
package com.google.gerrit.common.data;
-import com.google.gerrit.common.audit.Audit;
import com.google.gerrit.common.auth.SignInRequired;
-import com.google.gerrit.reviewdb.client.AccountProjectWatch;
import com.google.gwtjsonrpc.common.AsyncCallback;
import com.google.gwtjsonrpc.common.RemoteJsonService;
import com.google.gwtjsonrpc.common.RpcImpl;
import com.google.gwtjsonrpc.common.RpcImpl.Version;
-import com.google.gwtjsonrpc.common.VoidResult;
-
-import java.util.List;
-import java.util.Set;
@RpcImpl(version = Version.V2_0)
public interface AccountService extends RemoteJsonService {
@SignInRequired
- void myProjectWatch(AsyncCallback<List<AccountProjectWatchInfo>> callback);
-
- @Audit
- @SignInRequired
- void addProjectWatch(String projectName, String filter,
- AsyncCallback<AccountProjectWatchInfo> callback);
-
- @Audit
- @SignInRequired
- void updateProjectWatch(AccountProjectWatch watch,
- AsyncCallback<VoidResult> callback);
-
- @Audit
- @SignInRequired
- void deleteProjectWatches(Set<AccountProjectWatch.Key> keys,
- AsyncCallback<VoidResult> callback);
-
- @SignInRequired
void myAgreements(AsyncCallback<AgreementInfo> callback);
}
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/AccountApi.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/AccountApi.java
index f67ce98..a822cfc 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/AccountApi.java
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/AccountApi.java
@@ -27,6 +27,7 @@
import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.user.client.rpc.AsyncCallback;
+import java.util.HashSet;
import java.util.Set;
/**
@@ -108,6 +109,54 @@
.post(sshPublicKey, cb);
}
+ /** Retrieve Watched Projects */
+ public static void getWatchedProjects(String account,
+ AsyncCallback<JsArray<ProjectWatchInfo>> cb) {
+ new RestApi("/accounts/").id(account).view("watched.projects").get(cb);
+ }
+
+ /** Create/Update Watched Project */
+ public static void updateWatchedProject(
+ String account,
+ ProjectWatchInfo watchedProjectInfo,
+ AsyncCallback<JsArray<ProjectWatchInfo>> cb) {
+ Set<ProjectWatchInfo> watchedProjectInfos = new HashSet<>();
+ watchedProjectInfos.add(watchedProjectInfo);
+ updateWatchedProjects(account, watchedProjectInfos, cb);
+ }
+
+ /** Create/Update Watched Projects */
+ public static void updateWatchedProjects(
+ String account,
+ Set<ProjectWatchInfo> watchedProjectInfos,
+ AsyncCallback<JsArray<ProjectWatchInfo>> cb) {
+ new RestApi("/accounts/")
+ .id(account)
+ .view("watched.projects")
+ .post(projectWatchArrayFromSet(watchedProjectInfos), cb);
+ }
+
+ /** Delete Watched Project */
+ public static void deleteWatchedProject(
+ String account,
+ ProjectWatchInfo watchedProjectInfo,
+ AsyncCallback<JsArray<ProjectWatchInfo>> cb) {
+ Set<ProjectWatchInfo> watchedProjectInfos = new HashSet<>();
+ watchedProjectInfos.add(watchedProjectInfo);
+ deleteWatchedProjects(account, watchedProjectInfos, cb);
+ }
+
+ /** Delete Watched Projects */
+ public static void deleteWatchedProjects(
+ String account,
+ Set<ProjectWatchInfo> watchedProjectInfos,
+ AsyncCallback<JsArray<ProjectWatchInfo>> cb) {
+ new RestApi("/accounts/")
+ .id(account)
+ .view("watched.projects:delete")
+ .post(projectWatchArrayFromSet(watchedProjectInfos), cb);
+ }
+
/**
* Delete SSH keys. For each key to be deleted a separate DELETE request is
* fired to the server. The {@code onSuccess} method of the provided callback
@@ -146,6 +195,15 @@
new RestApi("/accounts/").id(account).view("password.http").delete(cb);
}
+ private static JsArray<ProjectWatchInfo> projectWatchArrayFromSet(
+ Set<ProjectWatchInfo> set) {
+ JsArray<ProjectWatchInfo> jsArray = JsArray.createArray().cast();
+ for (ProjectWatchInfo p : set) {
+ jsArray.push(p);
+ }
+ return jsArray;
+ }
+
private static class HttpPasswordInput extends JavaScriptObject {
final native void generate(boolean g) /*-{ if(g)this.generate=g; }-*/;
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchedProjectsScreen.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchedProjectsScreen.java
index a8bf3e5..ce5cfd3 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchedProjectsScreen.java
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchedProjectsScreen.java
@@ -16,13 +16,13 @@
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.GerritCallback;
-import com.google.gerrit.client.rpc.ScreenLoadCallback;
import com.google.gerrit.client.ui.HintTextBox;
import com.google.gerrit.client.ui.ProjectListPopup;
import com.google.gerrit.client.ui.ProjectNameSuggestOracle;
import com.google.gerrit.client.ui.RemoteSuggestBox;
import com.google.gerrit.common.PageLinks;
-import com.google.gerrit.common.data.AccountProjectWatchInfo;
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
@@ -36,8 +36,6 @@
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HorizontalPanel;
-import java.util.List;
-
public class MyWatchedProjectsScreen extends SettingsScreen {
private Button addNew;
private RemoteSuggestBox nameBox;
@@ -188,35 +186,40 @@
nameBox.setEnabled(false);
filterTxt.setEnabled(false);
- Util.ACCOUNT_SVC.addProjectWatch(projectName, filter,
- new GerritCallback<AccountProjectWatchInfo>() {
+ final ProjectWatchInfo projectWatchInfo = JavaScriptObject
+ .createObject().cast();
+ projectWatchInfo.project(projectName);
+ projectWatchInfo.filter(filterTxt.getText());
+
+ AccountApi.updateWatchedProject("self", projectWatchInfo,
+ new GerritCallback<JsArray<ProjectWatchInfo>>() {
@Override
- public void onSuccess(final AccountProjectWatchInfo result) {
+ public void onSuccess(JsArray<ProjectWatchInfo> watchedProjects) {
addNew.setEnabled(true);
nameBox.setEnabled(true);
filterTxt.setEnabled(true);
nameBox.setText("");
- watchesTab.insertWatch(result);
+ watchesTab.insertWatch(projectWatchInfo);
}
@Override
- public void onFailure(final Throwable caught) {
+ public void onFailure(Throwable caught) {
addNew.setEnabled(true);
nameBox.setEnabled(true);
filterTxt.setEnabled(true);
-
super.onFailure(caught);
}
});
}
protected void populateWatches() {
- Util.ACCOUNT_SVC.myProjectWatch(
- new ScreenLoadCallback<List<AccountProjectWatchInfo>>(this) {
+ AccountApi.getWatchedProjects("self",
+ new GerritCallback<JsArray<ProjectWatchInfo>>() {
@Override
- public void preDisplay(final List<AccountProjectWatchInfo> result) {
- watchesTab.display(result);
+ public void onSuccess(JsArray<ProjectWatchInfo> watchedProjects) {
+ display();
+ watchesTab.display(watchedProjects);
}
});
}
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchesTable.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchesTable.java
index 08effdc..3647baf 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchesTable.java
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/MyWatchesTable.java
@@ -16,23 +16,22 @@
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.GerritCallback;
+import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.ui.FancyFlexTable;
import com.google.gerrit.client.ui.ProjectLink;
-import com.google.gerrit.common.data.AccountProjectWatchInfo;
-import com.google.gerrit.reviewdb.client.AccountProjectWatch;
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
-import com.google.gwtjsonrpc.common.VoidResult;
import java.util.HashSet;
-import java.util.List;
import java.util.Set;
-public class MyWatchesTable extends FancyFlexTable<AccountProjectWatchInfo> {
+public class MyWatchesTable extends FancyFlexTable<ProjectWatchInfo> {
public MyWatchesTable() {
table.setWidth("");
@@ -63,22 +62,22 @@
}
public void deleteChecked() {
- final Set<AccountProjectWatch.Key> ids = getCheckedIds();
- if (!ids.isEmpty()) {
- Util.ACCOUNT_SVC.deleteProjectWatches(ids,
- new GerritCallback<VoidResult>() {
+ final Set<ProjectWatchInfo> infos = getCheckedProjectWatchInfos();
+ if (!infos.isEmpty()) {
+ AccountApi.deleteWatchedProjects("self", infos,
+ new GerritCallback<JsArray<ProjectWatchInfo>>() {
@Override
- public void onSuccess(final VoidResult result) {
- remove(ids);
+ public void onSuccess(JsArray<ProjectWatchInfo> watchedProjects) {
+ remove(infos);
}
});
}
}
- protected void remove(Set<AccountProjectWatch.Key> ids) {
+ protected void remove(Set<ProjectWatchInfo> infos) {
for (int row = 1; row < table.getRowCount();) {
- final AccountProjectWatchInfo k = getRowItem(row);
- if (k != null && ids.contains(k.getWatch().getKey())) {
+ final ProjectWatchInfo k = getRowItem(row);
+ if (k != null && infos.contains(k)) {
table.removeRow(row);
} else {
row++;
@@ -86,23 +85,23 @@
}
}
- protected Set<AccountProjectWatch.Key> getCheckedIds() {
- final Set<AccountProjectWatch.Key> ids = new HashSet<>();
+ protected Set<ProjectWatchInfo> getCheckedProjectWatchInfos() {
+ final Set<ProjectWatchInfo> infos = new HashSet<>();
for (int row = 1; row < table.getRowCount(); row++) {
- final AccountProjectWatchInfo k = getRowItem(row);
+ final ProjectWatchInfo k = getRowItem(row);
if (k != null && ((CheckBox) table.getWidget(row, 1)).getValue()) {
- ids.add(k.getWatch().getKey());
+ infos.add(k);
}
}
- return ids;
+ return infos;
}
- public void insertWatch(final AccountProjectWatchInfo k) {
- final String newName = k.getProject().getName();
+ public void insertWatch(final ProjectWatchInfo k) {
+ final String newName = k.project();
int row = 1;
for (; row < table.getRowCount(); row++) {
- final AccountProjectWatchInfo i = getRowItem(row);
- if (i != null && i.getProject().getName().compareTo(newName) >= 0) {
+ final ProjectWatchInfo i = getRowItem(row);
+ if (i != null && i.project().compareTo(newName) >= 0) {
break;
}
}
@@ -112,24 +111,25 @@
populate(row, k);
}
- public void display(final List<AccountProjectWatchInfo> result) {
+ public void display(final JsArray<ProjectWatchInfo> result) {
while (2 < table.getRowCount()) {
table.removeRow(table.getRowCount() - 1);
}
- for (final AccountProjectWatchInfo k : result) {
+ for (ProjectWatchInfo info : Natives.asList(result)) {
final int row = table.getRowCount();
table.insertRow(row);
applyDataRowStyle(row);
- populate(row, k);
+ populate(row, info);
}
}
- protected void populate(final int row, final AccountProjectWatchInfo info) {
+ protected void populate(final int row, final ProjectWatchInfo info) {
final FlowPanel fp = new FlowPanel();
- fp.add(new ProjectLink(info.getProject().getNameKey()));
- if (info.getWatch().getFilter() != null) {
- Label filter = new Label(info.getWatch().getFilter());
+ fp.add(new ProjectLink(info.project(),
+ new Project.NameKey(info.project())));
+ if (info.filter() != null) {
+ Label filter = new Label(info.filter());
filter.setStyleName(Gerrit.RESOURCES.css().watchedProjectFilter());
fp.add(filter);
}
@@ -137,11 +137,11 @@
table.setWidget(row, 1, new CheckBox());
table.setWidget(row, 2, fp);
- addNotifyButton(AccountProjectWatch.NotifyType.NEW_CHANGES, info, row, 3);
- addNotifyButton(AccountProjectWatch.NotifyType.NEW_PATCHSETS, info, row, 4);
- addNotifyButton(AccountProjectWatch.NotifyType.ALL_COMMENTS, info, row, 5);
- addNotifyButton(AccountProjectWatch.NotifyType.SUBMITTED_CHANGES, info, row, 6);
- addNotifyButton(AccountProjectWatch.NotifyType.ABANDONED_CHANGES, info, row, 7);
+ addNotifyButton(ProjectWatchInfo.Type.NEW_CHANGES, info, row, 3);
+ addNotifyButton(ProjectWatchInfo.Type.NEW_PATCHSETS, info, row, 4);
+ addNotifyButton(ProjectWatchInfo.Type.ALL_COMMENTS, info, row, 5);
+ addNotifyButton(ProjectWatchInfo.Type.SUBMITTED_CHANGES, info, row, 6);
+ addNotifyButton(ProjectWatchInfo.Type.ABANDONED_CHANGES, info, row, 7);
final FlexCellFormatter fmt = table.getFlexCellFormatter();
fmt.addStyleName(row, 1, Gerrit.RESOURCES.css().iconCell());
@@ -155,27 +155,28 @@
setRowItem(row, info);
}
- protected void addNotifyButton(final AccountProjectWatch.NotifyType type,
- final AccountProjectWatchInfo info, final int row, final int col) {
+ protected void addNotifyButton(final ProjectWatchInfo.Type type,
+ final ProjectWatchInfo info, final int row, final int col) {
final CheckBox cbox = new CheckBox();
cbox.addClickHandler(new ClickHandler() {
@Override
public void onClick(final ClickEvent event) {
- final boolean oldVal = info.getWatch().isNotify(type);
- info.getWatch().setNotify(type, cbox.getValue());
+ final Boolean oldVal = info.notify(type);
+ info.notify(type, cbox.getValue());
cbox.setEnabled(false);
- Util.ACCOUNT_SVC.updateProjectWatch(info.getWatch(),
- new GerritCallback<VoidResult>() {
+
+ AccountApi.updateWatchedProject("self", info,
+ new GerritCallback<JsArray<ProjectWatchInfo>>() {
@Override
- public void onSuccess(final VoidResult result) {
+ public void onSuccess(JsArray<ProjectWatchInfo> watchedProjects) {
cbox.setEnabled(true);
}
@Override
- public void onFailure(final Throwable caught) {
+ public void onFailure(Throwable caught) {
cbox.setEnabled(true);
- info.getWatch().setNotify(type, oldVal);
+ info.notify(type, oldVal);
cbox.setValue(oldVal);
super.onFailure(caught);
}
@@ -183,7 +184,7 @@
}
});
- cbox.setValue(info.getWatch().isNotify(type));
+ cbox.setValue(info.notify(type));
table.setWidget(row, col, cbox);
}
}
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchInfo.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchInfo.java
new file mode 100644
index 0000000..e43ec0c
--- /dev/null
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchInfo.java
@@ -0,0 +1,79 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.gerrit.client.account;
+
+import com.google.gwt.core.client.JavaScriptObject;
+
+public class ProjectWatchInfo extends JavaScriptObject {
+
+ public enum Type {
+ NEW_CHANGES,
+ NEW_PATCHSETS,
+ ALL_COMMENTS,
+ SUBMITTED_CHANGES,
+ ABANDONED_CHANGES
+ }
+
+ public final native String project() /*-{ return this.project; }-*/;
+ public final native String filter() /*-{ return this.filter; }-*/;
+
+ public final native void project(String s) /*-{ this.project = s; }-*/;
+ public final native void filter(String s) /*-{ this.filter = s; }-*/;
+
+ public final void notify(ProjectWatchInfo.Type t, Boolean b) {
+ if (t == ProjectWatchInfo.Type.NEW_CHANGES) {
+ notifyNewChanges(b.booleanValue());
+ } else if (t == Type.NEW_PATCHSETS) {
+ notifyNewPatchSets(b.booleanValue());
+ } else if (t == Type.ALL_COMMENTS) {
+ notifyAllComments(b.booleanValue());
+ } else if (t == Type.SUBMITTED_CHANGES) {
+ notifySubmittedChanges(b.booleanValue());
+ } else if (t == Type.ABANDONED_CHANGES) {
+ notifyAbandonedChanges(b.booleanValue());
+ }
+ }
+
+ public final Boolean notify(ProjectWatchInfo.Type t) {
+ boolean b = false;
+ if (t == ProjectWatchInfo.Type.NEW_CHANGES) {
+ b = notifyNewChanges();
+ } else if (t == Type.NEW_PATCHSETS) {
+ b = notifyNewPatchSets();
+ } else if (t == Type.ALL_COMMENTS) {
+ b = notifyAllComments();
+ } else if (t == Type.SUBMITTED_CHANGES) {
+ b = notifySubmittedChanges();
+ } else if (t == Type.ABANDONED_CHANGES) {
+ b = notifyAbandonedChanges();
+ }
+ return Boolean.valueOf(b);
+ }
+
+ private native boolean notifyNewChanges() /*-{ return this['notify_new_changes'] ? true : false; }-*/;
+ private native boolean notifyNewPatchSets() /*-{ return this['notify_new_patch_sets'] ? true : false; }-*/;
+ private native boolean notifyAllComments() /*-{ return this['notify_all_comments'] ? true : false; }-*/;
+ private native boolean notifySubmittedChanges() /*-{ return this['notify_submitted_changes'] ? true : false; }-*/;
+ private native boolean notifyAbandonedChanges() /*-{ return this['notify_abandoned_changes'] ? true : false; }-*/;
+
+ private native void notifyNewChanges(boolean b) /*-{ this['notify_new_changes'] = b ? true : null; }-*/;
+ private native void notifyNewPatchSets(boolean b) /*-{ this['notify_new_patch_sets'] = b ? true : null; }-*/;
+ private native void notifyAllComments(boolean b) /*-{ this['notify_all_comments'] = b ? true : null; }-*/;
+ private native void notifySubmittedChanges(boolean b) /*-{ this['notify_submitted_changes'] = b ? true : null; }-*/;
+ private native void notifyAbandonedChanges(boolean b) /*-{ this['notify_abandoned_changes'] = b ? true : null; }-*/;
+
+ protected ProjectWatchInfo() {
+
+ }
+}
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountServiceImpl.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountServiceImpl.java
index ce47161..8fba47d 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountServiceImpl.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/rpc/account/AccountServiceImpl.java
@@ -14,149 +14,25 @@
package com.google.gerrit.httpd.rpc.account;
-import com.google.gerrit.common.data.AccountProjectWatchInfo;
import com.google.gerrit.common.data.AccountService;
import com.google.gerrit.common.data.AgreementInfo;
-import com.google.gerrit.common.errors.InvalidQueryException;
-import com.google.gerrit.common.errors.NoSuchEntityException;
import com.google.gerrit.httpd.rpc.BaseServiceImplementation;
-import com.google.gerrit.reviewdb.client.Account;
-import com.google.gerrit.reviewdb.client.AccountProjectWatch;
-import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.IdentifiedUser;
-import com.google.gerrit.server.project.NoSuchProjectException;
-import com.google.gerrit.server.project.ProjectControl;
-import com.google.gerrit.server.query.QueryParseException;
-import com.google.gerrit.server.query.change.ChangeQueryBuilder;
import com.google.gwtjsonrpc.common.AsyncCallback;
-import com.google.gwtjsonrpc.common.VoidResult;
-import com.google.gwtorm.server.OrmDuplicateKeyException;
-import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
-import java.util.Set;
-
class AccountServiceImpl extends BaseServiceImplementation implements
AccountService {
- private final ProjectControl.Factory projectControlFactory;
private final AgreementInfoFactory.Factory agreementInfoFactory;
- private final Provider<ChangeQueryBuilder> queryBuilder;
@Inject
AccountServiceImpl(final Provider<ReviewDb> schema,
final Provider<IdentifiedUser> identifiedUser,
- final ProjectControl.Factory projectControlFactory,
- final AgreementInfoFactory.Factory agreementInfoFactory,
- final Provider<ChangeQueryBuilder> queryBuilder) {
+ final AgreementInfoFactory.Factory agreementInfoFactory) {
super(schema, identifiedUser);
- this.projectControlFactory = projectControlFactory;
this.agreementInfoFactory = agreementInfoFactory;
- this.queryBuilder = queryBuilder;
- }
-
- @Override
- public void myProjectWatch(
- final AsyncCallback<List<AccountProjectWatchInfo>> callback) {
- run(callback, new Action<List<AccountProjectWatchInfo>>() {
- @Override
- public List<AccountProjectWatchInfo> run(ReviewDb db) throws OrmException {
- List<AccountProjectWatchInfo> r = new ArrayList<>();
-
- for (final AccountProjectWatch w : db.accountProjectWatches()
- .byAccount(getAccountId()).toList()) {
- final ProjectControl ctl;
- try {
- ctl = projectControlFactory.validateFor(w.getProjectNameKey());
- } catch (NoSuchProjectException e) {
- db.accountProjectWatches().delete(Collections.singleton(w));
- continue;
- }
- r.add(new AccountProjectWatchInfo(w, ctl.getProject()));
- }
- Collections.sort(r, new Comparator<AccountProjectWatchInfo>() {
- @Override
- public int compare(final AccountProjectWatchInfo a,
- final AccountProjectWatchInfo b) {
- return a.getProject().getName().compareTo(b.getProject().getName());
- }
- });
- return r;
- }
- });
- }
-
- @Override
- public void addProjectWatch(final String projectName, final String filter,
- final AsyncCallback<AccountProjectWatchInfo> callback) {
- run(callback, new Action<AccountProjectWatchInfo>() {
- @Override
- public AccountProjectWatchInfo run(ReviewDb db) throws OrmException,
- NoSuchProjectException, InvalidQueryException {
- final Project.NameKey nameKey = new Project.NameKey(projectName);
- final ProjectControl ctl = projectControlFactory.validateFor(nameKey);
-
- if (filter != null) {
- try {
- queryBuilder.get().parse(filter);
- } catch (QueryParseException badFilter) {
- throw new InvalidQueryException(badFilter.getMessage(), filter);
- }
- }
-
- AccountProjectWatch watch =
- new AccountProjectWatch(new AccountProjectWatch.Key(
- ctl.getUser().getAccountId(),
- nameKey, filter));
- try {
- db.accountProjectWatches().insert(Collections.singleton(watch));
- } catch (OrmDuplicateKeyException alreadyHave) {
- watch = db.accountProjectWatches().get(watch.getKey());
- }
- return new AccountProjectWatchInfo(watch, ctl.getProject());
- }
- });
- }
-
- @Override
- public void updateProjectWatch(final AccountProjectWatch watch,
- final AsyncCallback<VoidResult> callback) {
- if (!getAccountId().equals(watch.getAccountId())) {
- callback.onFailure(new NoSuchEntityException());
- return;
- }
-
- run(callback, new Action<VoidResult>() {
- @Override
- public VoidResult run(ReviewDb db) throws OrmException {
- db.accountProjectWatches().update(Collections.singleton(watch));
- return VoidResult.INSTANCE;
- }
- });
- }
-
- @Override
- public void deleteProjectWatches(final Set<AccountProjectWatch.Key> keys,
- final AsyncCallback<VoidResult> callback) {
- run(callback, new Action<VoidResult>() {
- @Override
- public VoidResult run(final ReviewDb db) throws OrmException, Failure {
- final Account.Id me = getAccountId();
- for (final AccountProjectWatch.Key keyId : keys) {
- if (!me.equals(keyId.getParentKey())) {
- throw new Failure(new NoSuchEntityException());
- }
- }
-
- db.accountProjectWatches().deleteKeys(keys);
- return VoidResult.INSTANCE;
- }
- });
}
@Override