Use REST API for loading/saving general account preferences

This migrates the MyPreferencesScreen to the REST API.

Change-Id: I3ba3130d97e342507aa705b51ca8e159da91870b
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2014-03-26 01:49:11 +01:00
parent 18a45e805a
commit 6b0894dd44
3 changed files with 216 additions and 35 deletions
gerrit-gwtui/src/main/java/com/google/gerrit/client/account
gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client

@ -22,7 +22,6 @@ 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.OnEditEnabler;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.CommentVisibilityStrategy;
import com.google.gwt.event.dom.client.ClickEvent;
@ -34,7 +33,6 @@ import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwtjsonrpc.common.VoidResult;
import java.util.Date;
@ -223,9 +221,11 @@ public class MyPreferencesScreen extends SettingsScreen {
@Override
protected void onLoad() {
super.onLoad();
Util.ACCOUNT_SVC.myAccount(new ScreenLoadCallback<Account>(this) {
public void preDisplay(final Account result) {
display(result.getGeneralPreferences());
AccountApi.self().view("preferences")
.get(new ScreenLoadCallback<Preferences>(this) {
@Override
public void preDisplay(Preferences prefs) {
display(prefs);
}
});
}
@ -246,28 +246,28 @@ public class MyPreferencesScreen extends SettingsScreen {
diffView.setEnabled(on);
}
private void display(final AccountGeneralPreferences p) {
showSiteHeader.setValue(p.isShowSiteHeader());
useFlashClipboard.setValue(p.isUseFlashClipboard());
copySelfOnEmails.setValue(p.isCopySelfOnEmails());
reversePatchSetOrder.setValue(p.isReversePatchSetOrder());
showUsernameInReviewCategory.setValue(p.isShowUsernameInReviewCategory());
setListBox(maximumPageSize, DEFAULT_PAGESIZE, p.getMaximumPageSize());
private void display(Preferences p) {
showSiteHeader.setValue(p.showSiteHeader());
useFlashClipboard.setValue(p.useFlashClipboard());
copySelfOnEmails.setValue(p.copySelfOnEmail());
reversePatchSetOrder.setValue(p.reversePatchSetOrder());
showUsernameInReviewCategory.setValue(p.showUsernameInReviewCategory());
setListBox(maximumPageSize, DEFAULT_PAGESIZE, p.changesPerPage());
setListBox(dateFormat, AccountGeneralPreferences.DateFormat.STD, //
p.getDateFormat());
p.dateFormat());
setListBox(timeFormat, AccountGeneralPreferences.TimeFormat.HHMM_12, //
p.getTimeFormat());
relativeDateInChangeTable.setValue(p.isRelativeDateInChangeTable());
sizeBarInChangeTable.setValue(p.isSizeBarInChangeTable());
p.timeFormat());
relativeDateInChangeTable.setValue(p.relativeDateInChangeTable());
sizeBarInChangeTable.setValue(p.sizeBarInChangeTable());
setListBox(commentVisibilityStrategy,
AccountGeneralPreferences.CommentVisibilityStrategy.EXPAND_RECENT,
p.getCommentVisibilityStrategy());
p.commentVisibilityStrategy());
setListBox(changeScreen,
null,
p.getChangeScreen());
p.changeScreen());
setListBox(diffView,
AccountGeneralPreferences.DiffView.SIDE_BY_SIDE,
p.getDiffView());
p.diffView());
}
private void setListBox(final ListBox f, final short defaultValue,
@ -350,22 +350,24 @@ public class MyPreferencesScreen extends SettingsScreen {
enable(false);
save.setEnabled(false);
Util.ACCOUNT_SVC.changePreferences(p, new GerritCallback<VoidResult>() {
@Override
public void onSuccess(final VoidResult result) {
Gerrit.getUserAccount().setGeneralPreferences(p);
Gerrit.applyUserPreferences();
Dispatcher.changeScreen2 = false;
enable(true);
}
AccountApi.self().view("preferences")
.post(Preferences.create(p), new GerritCallback<Preferences>() {
@Override
public void onSuccess(Preferences prefs) {
Gerrit.getUserAccount().setGeneralPreferences(p);
Gerrit.applyUserPreferences();
Dispatcher.changeScreen2 = false;
enable(true);
display(prefs);
}
@Override
public void onFailure(final Throwable caught) {
enable(true);
save.setEnabled(true);
super.onFailure(caught);
}
});
@Override
public void onFailure(Throwable caught) {
enable(true);
save.setEnabled(true);
super.onFailure(caught);
}
});
}
private static String getLabel(AccountGeneralPreferences.ChangeScreen ui) {

@ -15,12 +15,185 @@
package com.google.gerrit.client.account;
import com.google.gerrit.client.extensions.TopMenuItem;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.ChangeScreen;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.CommentVisibilityStrategy;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DateFormat;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DiffView;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.TimeFormat;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
public class Preferences extends JavaScriptObject {
public static Preferences create(AccountGeneralPreferences in) {
Preferences p = createObject().cast();
if (in == null) {
in = AccountGeneralPreferences.createDefault();
}
p.changesPerPage(in.getMaximumPageSize());
p.showSiteHeader(in.isShowSiteHeader());
p.useFlashClipboard(in.isUseFlashClipboard());
p.downloadScheme(in.getDownloadUrl());
p.downloadCommand(in.getDownloadCommand());
p.copySelfOnEmail(in.isCopySelfOnEmails());
p.dateFormat(in.getDateFormat());
p.timeFormat(in.getTimeFormat());
p.reversePatchSetOrder(in.isReversePatchSetOrder());
p.showUsernameInReviewCategory(in.isShowUsernameInReviewCategory());
p.relativeDateInChangeTable(in.isRelativeDateInChangeTable());
p.sizeBarInChangeTable(in.isSizeBarInChangeTable());
p.commentVisibilityStrategy(in.getCommentVisibilityStrategy());
p.diffView(in.getDiffView());
p.changeScreen(in.getChangeScreen());
return p;
}
public final native JsArray<TopMenuItem> my() /*-{ return this.my; }-*/;
public final short changesPerPage() {
return get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE);
}
private final native short get(String n, int d)
/*-{ return this.hasOwnProperty(n) ? this[n] : d }-*/;
public final native boolean showSiteHeader()
/*-{ return this.show_site_header || false }-*/;
public final native boolean useFlashClipboard()
/*-{ return this.use_flash_clipboard || false }-*/;
public final DownloadScheme downloadScheme() {
String s = downloadSchemeRaw();
return s != null ? DownloadScheme.valueOf(s) : null;
}
private final native String downloadSchemeRaw()
/*-{ return this.download_scheme }-*/;
public final DownloadCommand downloadCommand() {
String s = downloadCommandRaw();
return s != null ? DownloadCommand.valueOf(s) : null;
}
private final native String downloadCommandRaw()
/*-{ return this.download_command }-*/;
public final native boolean copySelfOnEmail()
/*-{ return this.copy_self_on_email || false }-*/;
public final DateFormat dateFormat() {
String s = dateFormatRaw();
return s != null ? DateFormat.valueOf(s) : null;
}
private final native String dateFormatRaw()
/*-{ return this.date_format }-*/;
public final TimeFormat timeFormat() {
String s = timeFormatRaw();
return s != null ? TimeFormat.valueOf(s) : null;
}
private final native String timeFormatRaw()
/*-{ return this.time_format }-*/;
public final native boolean reversePatchSetOrder()
/*-{ return this.reverse_patch_set_order || false }-*/;
public final native boolean showUsernameInReviewCategory()
/*-{ return this.show_username_in_review_category || false }-*/;
public final native boolean relativeDateInChangeTable()
/*-{ return this.relative_date_in_change_table || false }-*/;
public final native boolean sizeBarInChangeTable()
/*-{ return this.size_bar_in_change_table || false }-*/;
public final CommentVisibilityStrategy commentVisibilityStrategy() {
String s = commentVisibilityStrategyRaw();
return s != null ? CommentVisibilityStrategy.valueOf(s) : null;
}
private final native String commentVisibilityStrategyRaw()
/*-{ return this.comment_visibility_strategy }-*/;
public final DiffView diffView() {
String s = diffViewRaw();
return s != null ? DiffView.valueOf(s) : null;
}
private final native String diffViewRaw()
/*-{ return this.diff_view }-*/;
public final ChangeScreen changeScreen() {
String s = changeScreenRaw();
return s != null ? ChangeScreen.valueOf(s) : null;
}
private final native String changeScreenRaw()
/*-{ return this.change_screen }-*/;
public final native JsArray<TopMenuItem> my()
/*-{ return this.my; }-*/;
public final native void changesPerPage(short n)
/*-{ this.changes_per_page = n }-*/;
public final native void showSiteHeader(boolean s)
/*-{ this.show_site_header = s }-*/;
public final native void useFlashClipboard(boolean u)
/*-{ this.use_flash_clipboard = u }-*/;
public final void downloadScheme(DownloadScheme d) {
downloadSchemeRaw(d != null ? d.toString() : null);
}
private final native void downloadSchemeRaw(String d)
/*-{ this.download_scheme = d }-*/;
public final void downloadCommand(DownloadCommand d) {
downloadCommandRaw(d != null ? d.toString() : null);
}
public final native void downloadCommandRaw(String d)
/*-{ this.download_command = d }-*/;
public final native void copySelfOnEmail(boolean c)
/*-{ this.copy_self_on_email = c }-*/;
public final void dateFormat(DateFormat f) {
dateFormatRaw(f != null ? f.toString() : null);
}
private final native void dateFormatRaw(String f)
/*-{ this.date_format = f }-*/;
public final void timeFormat(TimeFormat f) {
timeFormatRaw(f != null ? f.toString() : null);
}
private final native void timeFormatRaw(String f)
/*-{ this.time_format = f }-*/;
public final native void reversePatchSetOrder(boolean r)
/*-{ this.reverse_patch_set_order = r }-*/;
public final native void showUsernameInReviewCategory(boolean s)
/*-{ this.show_username_in_review_category = s }-*/;
public final native void relativeDateInChangeTable(boolean d)
/*-{ this.relative_date_in_change_table = d }-*/;
public final native void sizeBarInChangeTable(boolean s)
/*-{ this.size_bar_in_change_table = s }-*/;
public final void commentVisibilityStrategy(CommentVisibilityStrategy s) {
commentVisibilityStrategyRaw(s != null ? s.toString() : null);
}
private final native void commentVisibilityStrategyRaw(String s)
/*-{ this.comment_visibility_strategy = s }-*/;
public final void diffView(DiffView d) {
diffViewRaw(d != null ? d.toString() : null);
}
private final native void diffViewRaw(String d)
/*-{ this.diff_view = d }-*/;
public final void changeScreen(ChangeScreen s) {
changeScreenRaw(s != null ? s.toString() : null);
}
private final native void changeScreenRaw(String s)
/*-{ this.change_screen = s }-*/;
protected Preferences() {
}

@ -103,6 +103,12 @@ public final class AccountGeneralPreferences {
}
}
public static AccountGeneralPreferences createDefault() {
AccountGeneralPreferences p = new AccountGeneralPreferences();
p.resetToDefaults();
return p;
}
/** Number of changes to show in a screen. */
@Column(id = 2)
protected short maximumPageSize;