On client-side use AccountInfo instead of Account

AccountInfo is the client side representation of a user; Account is
the entity class for persisting a user.

The AccountInfo is now fetched via REST. As result the account
information in HostPageData is no longer used, and hence it is
removed.

Using AccountInfo instead of Account allows us in a next step to
remove some of the remaining old RPC's and replace them with REST
calls.

Change-Id: I20d72f62beec0e6a65cf792f67be04262be6fdb5
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-28 11:33:45 +02:00
parent cf77a6955d
commit 7c87d0f929
15 changed files with 113 additions and 72 deletions

View File

@@ -14,15 +14,52 @@
package com.google.gerrit.client.info;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwtjsonrpc.client.impl.ser.JavaSqlTimestamp_JsonSerializer;
import java.sql.Timestamp;
public class AccountInfo extends JavaScriptObject {
public final Account.Id getId() {
return new Account.Id(_accountId());
}
public final native int _accountId() /*-{ return this._account_id || 0; }-*/;
public final native String name() /*-{ return this.name; }-*/;
public final native String email() /*-{ return this.email; }-*/;
public final native String username() /*-{ return this.username; }-*/;
public final Timestamp registeredOn() {
Timestamp ts = _getRegisteredOn();
if (ts == null) {
ts = JavaSqlTimestamp_JsonSerializer.parseTimestamp(registeredOnRaw());
_setRegisteredOn(ts);
}
return ts;
}
private final native String registeredOnRaw() /*-{ return this.registered_on; }-*/;
private final native Timestamp _getRegisteredOn() /*-{ return this._cts; }-*/;
private final native void _setRegisteredOn(Timestamp ts) /*-{ this._cts = ts; }-*/;
public final Timestamp contactFiledOn() {
if (contactFiledOnRaw() != null) {
Timestamp ts = _getContactFiledOn();
if (ts == null) {
ts = JavaSqlTimestamp_JsonSerializer.parseTimestamp(contactFiledOnRaw());
_setContactFiledOn(ts);
}
return ts;
}
return null;
}
private final native String contactFiledOnRaw() /*-{ return this.contact_filed_on; }-*/;
private final native Timestamp _getContactFiledOn() /*-{ return this._cts; }-*/;
private final native void _setContactFiledOn(Timestamp ts) /*-{ this._cts = ts; }-*/;
/**
* @return true if the server supplied avatar information about this account.
* The information may be an empty list, indicating no avatars are
@@ -46,6 +83,10 @@ public class AccountInfo extends JavaScriptObject {
private final native JsArray<AvatarInfo> avatars()
/*-{ return this.avatars }-*/;
public final native void name(String n) /*-{ this.name = n }-*/;
public final native void email(String e) /*-{ this.email = e }-*/;
public final native void username(String n) /*-{ this.username = n }-*/;
public static native AccountInfo create(int id, String name,
String email, String username) /*-{
return {'_account_id': id, 'name': name, 'email': email,

View File

@@ -98,7 +98,7 @@ public class AvatarImage extends Image implements LoadHandler {
// TODO Kill /accounts/*/avatar URL.
String u = account.email();
if (Gerrit.isSignedIn()
&& u.equals(Gerrit.getUserAccount().getPreferredEmail())) {
&& u.equals(Gerrit.getUserAccount().email())) {
u = "self";
}
RestApi api = new RestApi("/accounts/").id(u).view("avatar");

View File

@@ -47,7 +47,6 @@ import com.google.gerrit.common.PageLinks;
import com.google.gerrit.common.data.HostPageData;
import com.google.gerrit.common.data.SystemInfoService;
import com.google.gerrit.extensions.client.GerritTopMenu;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountDiffPreference;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gwt.aria.client.Roles;
@@ -111,12 +110,12 @@ public class Gerrit implements EntryPoint {
private static String myHost;
private static ServerInfo myServerInfo;
private static AccountInfo myAccount;
private static AccountPreferencesInfo myPrefs;
private static UrlAliasMatcher urlAliasMatcher;
private static boolean hasDocumentation;
private static String docUrl;
private static HostPageData.Theme myTheme;
private static Account myAccount;
private static String defaultScreenToken;
private static AccountDiffPreference myAccountDiffPref;
private static String xGerritAuth;
@@ -304,14 +303,9 @@ public class Gerrit implements EntryPoint {
return myTheme;
}
/** @return the currently signed in user's account data; null if no account */
public static Account getUserAccount() {
return myAccount;
}
/** @return the currently signed in user's account data; empty account data if no account */
public static AccountInfo getUserAccountInfo() {
return FormatUtil.asInfo(myAccount);
public static AccountInfo getUserAccount() {
return myAccount;
}
/** @return access token to prove user identity during REST API calls. */
@@ -335,7 +329,7 @@ public class Gerrit implements EntryPoint {
/** @return true if the user is currently authenticated */
public static boolean isSignedIn() {
return getUserAccount() != null;
return xGerritAuth != null;
}
/** Sign the user into the application. */
@@ -394,7 +388,7 @@ public class Gerrit implements EntryPoint {
}
static void deleteSessionCookie() {
myAccount = null;
myAccount = AccountInfo.create(0, null, null, null);
myAccountDiffPref = null;
myPrefs = AccountPreferencesInfo.createDefault();
xGerritAuth = null;
@@ -466,23 +460,32 @@ public class Gerrit implements EntryPoint {
Document.get().getElementById("gerrit_hostpagedata").removeFromParent();
myTheme = result.theme;
isNoteDbEnabled = result.isNoteDbEnabled;
if (result.account != null) {
myAccount = result.account;
xGerritAuth = result.xGerritAuth;
}
if (result.accountDiffPref != null) {
myAccountDiffPref = result.accountDiffPref;
}
if (isSignedIn()) {
if (result.xGerritAuth != null) {
xGerritAuth = result.xGerritAuth;
// TODO: Support options on the GetDetail REST endpoint so that it can
// also return the preferences. Then we can fetch everything with a
// single request and we don't need the callback group anymore.
CallbackGroup cbg = new CallbackGroup();
AccountApi.self().view("detail")
.get(cbg.add(new GerritCallback<AccountInfo>() {
@Override
public void onSuccess(AccountInfo result) {
myAccount = result;
}
}));
AccountApi.self().view("preferences")
.get(new GerritCallback<AccountPreferencesInfo>() {
.get(cbg.addFinal(new GerritCallback<AccountPreferencesInfo>() {
@Override
public void onSuccess(AccountPreferencesInfo prefs) {
myPrefs = prefs;
onModuleLoad2(result);
}
});
}));
} else {
myAccount = AccountInfo.create(0, null, null, null);
myPrefs = AccountPreferencesInfo.createDefault();
onModuleLoad2(result);
}
@@ -911,7 +914,7 @@ public class Gerrit implements EntryPoint {
}
private static void whoAmI(boolean canLogOut) {
AccountInfo account = getUserAccountInfo();
AccountInfo account = getUserAccount();
final UserPopupPanel userPopup =
new UserPopupPanel(account, canLogOut, true);
final FlowPanel userSummaryPanel = new FlowPanel();

View File

@@ -15,8 +15,8 @@
package com.google.gerrit.client.account;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.ui.OnEditEnabler;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.ContactInformation;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
@@ -88,18 +88,18 @@ class ContactPanelFull extends ContactPanelShort {
}
@Override
protected void display(final Account userAccount) {
super.display(userAccount);
displayHasContact(userAccount);
protected void display(AccountInfo account) {
super.display(account);
displayHasContact(account);
addressTxt.setText("");
countryTxt.setText("");
phoneTxt.setText("");
faxTxt.setText("");
}
private void displayHasContact(final Account userAccount) {
if (userAccount.isContactFiled()) {
final Timestamp dt = userAccount.getContactFiledOn();
private void displayHasContact(AccountInfo account) {
if (account.contactFiledOn() != null) {
Timestamp dt = account.contactFiledOn();
hasContact.setText(Util.M.contactOnFile(new Date(dt.getTime())));
hasContact.setVisible(true);
} else {
@@ -108,9 +108,9 @@ class ContactPanelFull extends ContactPanelShort {
}
@Override
void onSaveSuccess(final Account userAccount) {
super.onSaveSuccess(userAccount);
displayHasContact(userAccount);
void onSaveSuccess(AccountInfo account) {
super.onSaveSuccess(account);
displayHasContact(account);
}
@Override

View File

@@ -15,7 +15,9 @@
package com.google.gerrit.client.account;
import com.google.gerrit.client.ErrorDialog;
import com.google.gerrit.client.FormatUtil;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.ui.OnEditEnabler;
@@ -195,11 +197,11 @@ class ContactPanelShort extends Composite {
Util.ACCOUNT_SVC.myAccount(new GerritCallback<Account>() {
@Override
public void onSuccess(final Account result) {
public void onSuccess(Account result) {
if (!isAttached()) {
return;
}
display(result);
display(FormatUtil.asInfo(result));
haveAccount = true;
postLoad();
}
@@ -237,9 +239,9 @@ class ContactPanelShort extends Composite {
info.getCellFormatter().addStyleName(row, 0, Gerrit.RESOURCES.css().header());
}
protected void display(final Account userAccount) {
currentEmail = userAccount.getPreferredEmail();
nameTxt.setText(userAccount.getFullName());
protected void display(AccountInfo account) {
currentEmail = account.email();
nameTxt.setText(account.name());
save.setEnabled(false);
new OnEditEnabler(save, nameTxt);
}
@@ -277,8 +279,8 @@ class ContactPanelShort extends Composite {
if (Gerrit.info().auth().isDev()) {
currentEmail = addr;
if (emailPick.getItemCount() == 0) {
final Account me = Gerrit.getUserAccount();
me.setPreferredEmail(addr);
AccountInfo me = Gerrit.getUserAccount();
me.email(addr);
onSaveSuccess(me);
} else {
save.setEnabled(true);
@@ -361,9 +363,9 @@ class ContactPanelShort extends Composite {
Util.ACCOUNT_SEC.updateContact(newName, newEmail, info,
new GerritCallback<Account>() {
@Override
public void onSuccess(final Account result) {
public void onSuccess(Account result) {
registerNewEmail.setEnabled(true);
onSaveSuccess(result);
onSaveSuccess(FormatUtil.asInfo(result));
if (onSave != null) {
onSave.onSuccess(result);
}
@@ -378,10 +380,10 @@ class ContactPanelShort extends Composite {
});
}
void onSaveSuccess(final Account result) {
final Account me = Gerrit.getUserAccount();
me.setFullName(result.getFullName());
me.setPreferredEmail(result.getPreferredEmail());
void onSaveSuccess(AccountInfo result) {
AccountInfo me = Gerrit.getUserAccount();
me.name(result.name());
me.email(result.email());
Gerrit.refreshMenuBar();
display(me);
}

View File

@@ -107,14 +107,14 @@ public class MyPasswordScreen extends SettingsScreen {
AccountApi.getUsername("self", new GerritCallback<NativeString>() {
@Override
public void onSuccess(NativeString user) {
Gerrit.getUserAccount().setUserName(user.asString());
Gerrit.getUserAccount().username(user.asString());
refreshHttpPassword();
}
@Override
public void onFailure(final Throwable caught) {
if (RestApi.isNotFound(caught)) {
Gerrit.getUserAccount().setUserName(null);
Gerrit.getUserAccount().username(null);
display();
} else {
super.onFailure(caught);
@@ -164,7 +164,7 @@ public class MyPasswordScreen extends SettingsScreen {
}
private void doGeneratePassword() {
if (Gerrit.getUserAccount().getUserName() != null) {
if (Gerrit.getUserAccount().username() != null) {
enableUI(false);
AccountApi.generateHttpPassword("self",
new GerritCallback<NativeString>() {
@@ -182,7 +182,7 @@ public class MyPasswordScreen extends SettingsScreen {
}
private void doClearPassword() {
if (Gerrit.getUserAccount().getUserName() != null) {
if (Gerrit.getUserAccount().username() != null) {
enableUI(false);
AccountApi.clearHttpPassword("self",
new GerritCallback<VoidResult>() {
@@ -200,7 +200,7 @@ public class MyPasswordScreen extends SettingsScreen {
}
private void enableUI(boolean on) {
on &= Gerrit.getUserAccount().getUserName() != null;
on &= Gerrit.getUserAccount().username() != null;
generatePassword.setEnabled(on);
clearPassword.setVisible(on && !"".equals(password.getText()));

View File

@@ -17,12 +17,11 @@ package com.google.gerrit.client.account;
import static com.google.gerrit.client.FormatUtil.mediumFormat;
import com.google.gerrit.client.AvatarImage;
import com.google.gerrit.client.FormatUtil;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.GerritUiExtensionPoint;
import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.rpc.NativeString;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
@@ -98,9 +97,9 @@ public class MyProfileScreen extends SettingsScreen {
Gerrit.RESOURCES.css().header());
}
void display(final Account account) {
void display(AccountInfo account) {
if (Gerrit.info().plugin().hasAvatars()) {
avatar.setAccount(FormatUtil.asInfo(account), 93, false);
avatar.setAccount(account, 93, false);
new RestApi("/accounts/").id("self").view("avatar.change.url")
.get(new AsyncCallback<NativeString>() {
@Override
@@ -119,9 +118,9 @@ public class MyProfileScreen extends SettingsScreen {
if (Gerrit.info().auth().siteHasUsernames()) {
info.setWidget(row++, fieldIdx, new UsernameField());
}
info.setText(row++, fieldIdx, account.getFullName());
info.setText(row++, fieldIdx, account.getPreferredEmail());
info.setText(row++, fieldIdx, mediumFormat(account.getRegisteredOn()));
info.setText(row++, fieldIdx, account.name());
info.setText(row++, fieldIdx, account.email());
info.setText(row++, fieldIdx, mediumFormat(account.registeredOn()));
info.setText(row, fieldIdx, account.getId().toString());
}
}

View File

@@ -15,11 +15,11 @@
package com.google.gerrit.client.account;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.ui.AccountScreen;
import com.google.gerrit.client.ui.InlineHyperlink;
import com.google.gerrit.client.ui.SmallHeading;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Account.FieldName;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.ui.FlowPanel;
@@ -56,8 +56,8 @@ public class RegisterScreen extends AccountScreen {
contactGroup.add(whereFrom);
contactGroup.add(new ContactPanelShort() {
@Override
protected void display(final Account userAccount) {
super.display(userAccount);
protected void display(AccountInfo account) {
super.display(account);
if ("".equals(nameTxt.getText())) {
// No name? Encourage the user to provide us something.
@@ -69,7 +69,7 @@ public class RegisterScreen extends AccountScreen {
});
formBody.add(contactGroup);
if (Gerrit.getUserAccount().getUserName() == null
if (Gerrit.getUserAccount().username() == null
&& Gerrit.info().auth().canEdit(FieldName.USER_NAME)) {
final FlowPanel fp = new FlowPanel();
fp.setStyleName(Gerrit.RESOURCES.css().registerScreenSection());

View File

@@ -91,7 +91,7 @@ public abstract class SettingsScreen extends MenuScreen {
GerritUiExtensionPoint extensionPoint) {
ExtensionPanel extensionPanel = new ExtensionPanel(extensionPoint);
extensionPanel.putObject(GerritUiExtensionPoint.Key.ACCOUNT_INFO,
Gerrit.getUserAccountInfo());
Gerrit.getUserAccount());
return extensionPanel;
}
}

View File

@@ -42,7 +42,7 @@ class UsernameField extends Composite {
private Button setUserName;
UsernameField() {
String user = Gerrit.getUserAccount().getUserName();
String user = Gerrit.getUserAccount().username();
userNameLbl = new CopyableLabel(user != null ? user : "");
userNameLbl.setStyleName(Gerrit.RESOURCES.css().accountUsername());
@@ -117,8 +117,8 @@ class UsernameField extends Composite {
Util.ACCOUNT_SEC.changeUserName(newUserName,
new GerritCallback<VoidResult>() {
@Override
public void onSuccess(final VoidResult result) {
Gerrit.getUserAccount().setUserName(newUserName);
public void onSuccess(VoidResult result) {
Gerrit.getUserAccount().username(newUserName);
userNameLbl.setText(newUserName);
userNameLbl.setVisible(true);
userNameTxt.setVisible(false);

View File

@@ -261,7 +261,7 @@ public class ApiGlue {
}
private static final AccountInfo getCurrentUser() {
return Gerrit.getUserAccountInfo();
return Gerrit.getUserAccount();
}
private static final AccountPreferencesInfo getUserPreferences() {

View File

@@ -889,7 +889,7 @@ public class ChangeScreen extends Screen {
static Timestamp myLastReply(ChangeInfo info) {
if (Gerrit.isSignedIn() && info.messages() != null) {
int self = Gerrit.getUserAccountInfo()._accountId();
int self = Gerrit.getUserAccount()._accountId();
for (int i = info.messages().length() - 1; i >= 0; i--) {
MessageInfo m = info.messages().get(i);
if (m.author() != null && m.author()._accountId() == self) {

View File

@@ -125,7 +125,7 @@ public class Reviewers extends Composite {
@UiHandler("addMe")
void onAddMe(@SuppressWarnings("unused") ClickEvent e) {
String accountId = String.valueOf(Gerrit.getUserAccountInfo()._accountId());
String accountId = String.valueOf(Gerrit.getUserAccount()._accountId());
addReviewer(accountId, false);
}
@@ -227,7 +227,7 @@ public class Reviewers extends Composite {
reviewersText.setInnerSafeHtml(rHtml);
ccText.setInnerSafeHtml(ccHtml);
if (Gerrit.isSignedIn()) {
int currentUser = Gerrit.getUserAccountInfo()._accountId();
int currentUser = Gerrit.getUserAccount()._accountId();
boolean showAddMeButton = info.owner()._accountId() != currentUser
&& !cc.containsKey(currentUser)
&& !r.containsKey(currentUser);

View File

@@ -71,7 +71,7 @@ public class CommentEditorPanel extends CommentPanel implements ClickHandler,
comment = plc;
addStyleName(Gerrit.RESOURCES.css().commentEditorPanel());
setAuthorNameText(Gerrit.getUserAccountInfo(), PatchUtil.C.draft());
setAuthorNameText(Gerrit.getUserAccount(), PatchUtil.C.draft());
setMessageText(plc.getMessage());
addDoubleClickHandler(this);

View File

@@ -182,10 +182,6 @@ public class HostPageServlet extends HttpServlet {
final StringWriter w = new StringWriter();
final CurrentUser user = currentUser.get();
if (user.isIdentifiedUser()) {
w.write(HPD_ID + ".account=");
json(((IdentifiedUser) user).getAccount(), w);
w.write(";");
w.write(HPD_ID + ".xGerritAuth=");
json(session.get().getXGerritAuth(), w);
w.write(";");