Skip to content

Commit

Permalink
Add user display formatting to AccountInfo
Browse files Browse the repository at this point in the history
Since FormatUtil is in a client package it is not
available to be used server side, but it does allow
a user to be easily displayed by their fullname and
to fallback to their email.  It would be convenient
for the server to be able to format users this way
when it needs to return an error message mentioning
the user.  By adding the getName() and getNameEmail()
methods to AccountInfo, it can easily be accessed by
classes in both the client and server packages.

This change does not introduce any callers to these
methods yet, it simply makes them available.

Change-Id: Ia44009e118988c6c87a43c572d9d410f00b032b4
  • Loading branch information
mfick committed Jun 22, 2012
1 parent 95ff3f0 commit 6355d91
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,4 +65,51 @@ public String getPreferredEmail() {
public void setPreferredEmail(final String email) { public void setPreferredEmail(final String email) {
preferredEmail = email; preferredEmail = email;
} }

/**
* Formats an account name.
* <p>
* If the account has a full name, it returns only the full name. Otherwise it
* returns a longer form that includes the email address.
*/
public String getName(String anonymousCowardName) {
if (getFullName() != null) {
return getFullName();
}
if (getPreferredEmail() != null) {
return getPreferredEmail();
}
return getNameEmail(anonymousCowardName);
}

/**
* Formats an account as an name and an email address.
* <p>
* Example output:
* <ul>
* <li><code>A U. Thor &lt;author@example.com&gt;</code>: full populated</li>
* <li><code>A U. Thor (12)</code>: missing email address</li>
* <li><code>Anonymous Coward &lt;author@example.com&gt;</code>: missing name</li>
* <li><code>Anonymous Coward (12)</code>: missing name and email address</li>
* </ul>
*/
public String getNameEmail(String anonymousCowardName) {
String name = getFullName();
if (name == null) {
name = anonymousCowardName;
}

final StringBuilder b = new StringBuilder();
b.append(name);
if (getPreferredEmail() != null) {
b.append(" <");
b.append(getPreferredEmail());
b.append(">");
} else if (getId() != null) {
b.append(" (");
b.append(getId().get());
b.append(")");
}
return b.toString();
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.gerrit.server; package com.google.gerrit.server;


import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.gerrit.common.data.AccountInfo;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountDiffPreference; import com.google.gerrit.reviewdb.client.AccountDiffPreference;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
Expand Down Expand Up @@ -249,6 +250,14 @@ public Set<String> getEmailAddresses() {
return emailAddresses; return emailAddresses;
} }


public String getName() {
return new AccountInfo(getAccount()).getName(anonymousCowardName);
}

public String getNameEmail() {
return new AccountInfo(getAccount()).getNameEmail(anonymousCowardName);
}

@Override @Override
public GroupMembership getEffectiveGroups() { public GroupMembership getEffectiveGroups() {
if (effectiveGroups == null) { if (effectiveGroups == null) {
Expand Down

0 comments on commit 6355d91

Please sign in to comment.