Account suggestion: Show suggestion with email that was matched

If an account is suggested because the query string matched on a
secondary email address, then the displayed suggestion should show
that secondary email in the suggestion (and not the preferred email).
This way the user can see why the account was suggested.

Change-Id: I86a9e5c099d4619e064aa52c68ed5b98c6faff13
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/AccountInfo.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/AccountInfo.java
index 830dcb3..7679799 100644
--- a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/AccountInfo.java
+++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/AccountInfo.java
@@ -17,6 +17,7 @@
 import com.google.gerrit.reviewdb.client.Account;
 import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.core.client.JsArray;
+import com.google.gwt.core.client.JsArrayString;
 import com.google.gwtjsonrpc.client.impl.ser.JavaSqlTimestamp_JsonSerializer;
 
 import java.sql.Timestamp;
@@ -29,6 +30,8 @@
   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 JsArrayString secondaryEmails()
+      /*-{ return this.secondary_emails; }-*/;
   public final native String username() /*-{ return this.username; }-*/;
 
   public final Timestamp registeredOn() {
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/AccountSuggestOracle.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/AccountSuggestOracle.java
index 60c23df..af8572b 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/AccountSuggestOracle.java
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/AccountSuggestOracle.java
@@ -35,7 +35,7 @@
           public void onSuccess(JsArray<AccountInfo> in) {
             List<AccountSuggestion> r = new ArrayList<>(in.length());
             for (AccountInfo p : Natives.asList(in)) {
-              r.add(new AccountSuggestion(p));
+              r.add(new AccountSuggestion(p, req.getQuery()));
             }
             cb.onSuggestionsReady(req, new Response(r));
           }
@@ -43,20 +43,34 @@
   }
 
   private static class AccountSuggestion implements SuggestOracle.Suggestion {
-    private final AccountInfo info;
+    private final String suggestion;
 
-    AccountSuggestion(final AccountInfo k) {
-      info = k;
+    AccountSuggestion(AccountInfo info, String query) {
+      String s = FormatUtil.nameEmail(info);
+      if (!s.toLowerCase().contains(query.toLowerCase())
+          && info.secondaryEmails() != null) {
+        for (String email : Natives.asList(info.secondaryEmails())) {
+          AccountInfo info2 = AccountInfo.create(info._accountId(), info.name(),
+              email, info.username());
+          String s2 = FormatUtil.nameEmail(info2);
+          if (s2.toLowerCase().contains(query.toLowerCase())) {
+            s = s2;
+            break;
+          }
+        }
+      }
+
+      this.suggestion = s;
     }
 
     @Override
     public String getDisplayString() {
-      return FormatUtil.nameEmail(info);
+      return suggestion;
     }
 
     @Override
     public String getReplacementString() {
-      return FormatUtil.nameEmail(info);
+      return suggestion;
     }
   }
 }