From 37d5b7cda27ae7da5690f0b0ce0dee4421624810 Mon Sep 17 00:00:00 2001 From: Alfred Schmid Date: Thu, 13 Feb 2014 09:47:53 +0100 Subject: Show "Displayname (username)" in palettes for edit team and repository pages --- src/main/java/com/gitblit/models/UserChoice.java | 112 +++++++++++++++++++++ .../gitblit/wicket/pages/EditRepositoryPage.java | 30 ++++-- .../com/gitblit/wicket/pages/EditTeamPage.java | 36 +++++-- 3 files changed, 166 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/gitblit/models/UserChoice.java (limited to 'src/main/java/com/gitblit') diff --git a/src/main/java/com/gitblit/models/UserChoice.java b/src/main/java/com/gitblit/models/UserChoice.java new file mode 100644 index 00000000..845ab7c7 --- /dev/null +++ b/src/main/java/com/gitblit/models/UserChoice.java @@ -0,0 +1,112 @@ +/* + * Copyright 2014 gitblit.com. + * + * 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.gitblit.models; + +import java.io.Serializable; + +import com.gitblit.utils.StringUtils; + +/** + * @author Alfred Schmid + * @author James Moger + * + */ +public class UserChoice implements Serializable { + + private static final long serialVersionUID = 1L; + + private final String displayName; + private final String userId; + private final String email; + + /** + * Create a UserChoice without email and displayName. + * + * @param userId + * the unique id of the user (in most cases the unique username + * from user store). Can never be null or empty string. + * + */ + public UserChoice(String userId) { + this(null, userId, null); + } + + /** + * Create a UserChoice without email. + * + * @param displayName + * the display name for the user. Can be null or empty string. + * @param userId + * the unique id of the user (in most cases the unique username + * from user store). Can never be null or empty string. + * + */ + public UserChoice(String displayName, String userId) { + this(displayName, userId, null); + } + + /** + * Create a UserChoice with email and displayName. + * + * @param displayName + * the display name for the user. Can be null or empty string. + * @param userId + * the unique id of the user (in most cases the unique username + * from user store). Can never be null or empty string. + * @param email + * the email from the user. Can be null or empty string. + * + */ + public UserChoice(String displayName, String userId, String email) { + if (userId == null) { + throw new IllegalArgumentException("The argument userId can't be null!"); + } + if ("".equals(userId)) { + throw new IllegalArgumentException("The argument userId can't be an empty String!"); + } + this.displayName = displayName; + this.userId = userId; + this.email = email; + } + + public String getDisplayName() { + return displayName; + } + + public String getDisplayNameOrUserId() { + if (StringUtils.isEmpty(displayName)) { + return userId; + } + return displayName; + } + + public String getUserId() { + return userId; + } + + public String getEmail() { + return email; + } + + @Override + public String toString() { + String dn = getDisplayNameOrUserId(); + if (dn.equals(userId)) { + return dn; + } + return dn + " (" + userId + ")"; + } +} diff --git a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java index a986fd53..bb166101 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -36,6 +36,7 @@ import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; +import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.IChoiceRenderer; @@ -59,6 +60,7 @@ import com.gitblit.GitBlitException; import com.gitblit.Keys; import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.RepositoryModel; +import com.gitblit.models.UserChoice; import com.gitblit.models.UserModel; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.StringUtils; @@ -172,10 +174,26 @@ public class EditRepositoryPage extends RootSubPage { RegistrantType.TEAM, app().users().getAllTeamNames(), repositoryTeams, getAccessPermissions()); // owners palette - List owners = new ArrayList(repositoryModel.owners); - List persons = app().users().getAllUsernames(); - final Palette ownersPalette = new Palette("owners", new ListModel(owners), new CollectionModel( - persons), new StringChoiceRenderer(), 12, true); + List owners = new ArrayList(); + for (String owner : repositoryModel.owners) { + UserModel o = app().users().getUserModel(owner); + if (o != null) { + owners.add(new UserChoice(o.getDisplayName(), o.username, o.emailAddress)); + } else { + owners.add(new UserChoice(owner)); + } + } + List persons = new ArrayList(); + for (String person : app().users().getAllUsernames()) { + UserModel o = app().users().getUserModel(person); + if (o != null) { + persons.add(new UserChoice(o.getDisplayName(), o.username, o.emailAddress)); + } else { + persons.add(new UserChoice(person)); + } + } + final Palette ownersPalette = new Palette("owners", new ListModel(owners), new CollectionModel( + persons), new ChoiceRenderer(null, "userId"), 12, true); // indexed local branches palette List allLocalBranches = new ArrayList(); @@ -358,9 +376,9 @@ public class EditRepositoryPage extends RootSubPage { // owners repositoryModel.owners.clear(); - Iterator owners = ownersPalette.getSelectedChoices(); + Iterator owners = ownersPalette.getSelectedChoices(); while (owners.hasNext()) { - repositoryModel.addOwner(owners.next()); + repositoryModel.addOwner(owners.next().getUserId()); } // pre-receive scripts diff --git a/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java b/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java index 82b28fc4..a0d11a05 100644 --- a/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java +++ b/src/main/java/com/gitblit/wicket/pages/EditTeamPage.java @@ -18,6 +18,7 @@ package com.gitblit.wicket.pages; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -28,6 +29,7 @@ import org.apache.wicket.behavior.SimpleAttributeModifier; import org.apache.wicket.extensions.markup.html.form.palette.Palette; import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.CheckBox; +import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.Form; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.model.CompoundPropertyModel; @@ -41,6 +43,8 @@ import com.gitblit.GitBlitException; import com.gitblit.Keys; import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.TeamModel; +import com.gitblit.models.UserChoice; +import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.RequiresAdminRole; import com.gitblit.wicket.StringChoiceRenderer; @@ -97,7 +101,6 @@ public class EditTeamPage extends RootSubPage { List repos = getAccessRestrictedRepositoryList(true, null); List teamUsers = new ArrayList(teamModel.users); - Collections.sort(teamUsers); List preReceiveScripts = new ArrayList(); List postReceiveScripts = new ArrayList(); @@ -105,9 +108,8 @@ public class EditTeamPage extends RootSubPage { final List permissions = teamModel.getRepositoryPermissions(); // users palette - final Palette users = new Palette("users", new ListModel( - new ArrayList(teamUsers)), new CollectionModel(app().users() - .getAllUsernames()), new StringChoiceRenderer(), 10, false); + final Palette users = new Palette("users", new ListModel( + getTeamUsers(teamUsers)), new CollectionModel(sortByDisplayName(getTeamUsers(app().users().getAllUsernames()))), new ChoiceRenderer(null, "userId"), 10, false); // pre-receive palette if (teamModel.preReceiveScripts != null) { @@ -155,10 +157,10 @@ public class EditTeamPage extends RootSubPage { teamModel.setRepositoryPermission(repositoryPermission.registrant, repositoryPermission.permission); } - Iterator selectedUsers = users.getSelectedChoices(); + Iterator selectedUsers = users.getSelectedChoices(); List members = new ArrayList(); while (selectedUsers.hasNext()) { - members.add(selectedUsers.next().toLowerCase()); + members.add(selectedUsers.next().getUserId().toLowerCase()); } teamModel.users.clear(); teamModel.users.addAll(members); @@ -255,4 +257,26 @@ public class EditTeamPage extends RootSubPage { add(form); } + + private List getTeamUsers(List teamUserIds) { + List teamUsers = new ArrayList(); + for (String teamUserId : teamUserIds) { + UserModel userModel = app().users().getUserModel(teamUserId); + if (userModel!=null) { + teamUsers.add(new UserChoice(userModel.displayName, userModel.username, userModel.emailAddress)); + } + } + return sortByDisplayName(teamUsers); + } + + private List sortByDisplayName(List teamUsers) { + Collections.sort(teamUsers, new Comparator() { + + @Override + public int compare(UserChoice o1, UserChoice o2) { + return o1.getDisplayNameOrUserId().compareTo(o2.getDisplayNameOrUserId()); + } + }); + return teamUsers; + } } -- cgit v1.2.3