[Notice] Announcing the End of Demo Server [Read me]
채수원 2015-01-12
Merge branch 'issue-1849' of dlab/hive
from pull-request 1412

* refs/heads/issue-1849:
  fixes `Assign to me` and `Create by me` on issue view.

Reviewed-by: 채수원 
@f5a4d3d6c27dc38e824a48fa67e4e9d6270f0d58
app/models/Project.java
--- app/models/Project.java
+++ app/models/Project.java
@@ -23,7 +23,6 @@
 import com.avaje.ebean.Ebean;
 import com.avaje.ebean.ExpressionList;
 import com.avaje.ebean.Page;
-import controllers.UserApp;
 import models.enumeration.ProjectScope;
 import models.enumeration.RequestState;
 import models.enumeration.ResourceType;
@@ -468,31 +467,7 @@
      * If the project has a group and is protected or public, it returns all project and group members.
      */
     public List<User> getAssignableUsers() {
-        Set<User> users = new HashSet<>();
-
-        // member of this project.
-        List<ProjectUser> pus = members();
-        for(ProjectUser pu : pus) {
-            users.add(pu.user);
-        }
-
-        // member of the group
-        if(hasGroup()) {
-            List<OrganizationUser> ous = (isPublic() || isProtected()) ? this.organization.users : this.organization.getAdmins();
-            for(OrganizationUser ou : ous) {
-                users.add(ou.user);
-            }
-        }
-
-        // sorting
-        List<User> result = new ArrayList<>(users);
-        Collections.sort(result, User.USER_NAME_COMPARATOR);
-
-        if (UserApp.currentUser().isSiteManager()) {
-            result.add(UserApp.currentUser());
-        }
-
-        return result;
+        return User.findUsersByProjectAndOrganization(this);
     }
 
     public List<User> getAssignableUsersAndAssignee(Issue issue) {
@@ -505,6 +480,10 @@
         return users;
     }
 
+    public boolean isProjectOrOrganizationUser(User user) {
+        return User.findUsersByProjectAndOrganization(this).contains(user);
+    }
+
     public boolean isAssignableUser(User user) {
         return getAssignableUsers().contains(user);
     }
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -24,7 +24,10 @@
 import java.util.*;
 
 import javax.persistence.*;
+import javax.persistence.OrderBy;
+import javax.persistence.criteria.Expression;
 
+import com.avaje.ebean.*;
 import controllers.UserApp;
 import models.enumeration.*;
 import models.resource.GlobalResource;
@@ -39,14 +42,11 @@
 import play.data.validation.Constraints.*;
 import play.db.ebean.Model;
 import play.db.ebean.Transactional;
+import scala.reflect.internal.Trees;
 import utils.JodaDateUtil;
 import utils.ReservedWordsValidator;
 
-import com.avaje.ebean.Ebean;
-import com.avaje.ebean.ExpressionList;
-import com.avaje.ebean.Page;
-import com.avaje.ebean.RawSql;
-import com.avaje.ebean.RawSqlBuilder;
+import static com.avaje.ebean.Expr.eq;
 
 @Table(name = "n4user")
 @Entity
@@ -327,6 +327,34 @@
                 .findList();
     }
 
+    public static List<User> findUsersByProjectAndOrganization(Project project) {
+        Set<User> users = new HashSet<>();
+
+        // member of this project.
+        List<ProjectUser> pus = project.members();
+        for(ProjectUser pu : pus) {
+            users.add(pu.user);
+        }
+
+        // member of the group
+        if(project.hasGroup()) {
+            List<OrganizationUser> ous = (project.isPublic() || project.isProtected()) ? project.organization.users : project.organization.getAdmins();
+            for(OrganizationUser ou : ous) {
+                users.add(ou.user);
+            }
+        }
+
+        // sorting
+        List<User> result = new ArrayList<>(users);
+        Collections.sort(result, USER_NAME_COMPARATOR);
+
+        if (UserApp.currentUser().isSiteManager()) {
+            result.add(UserApp.currentUser());
+        }
+
+        return result;
+    }
+
     @Transient
     public Long avatarId() {
         List<Attachment> attachments = Attachment.findByContainer(avatarAsResource());
@@ -567,13 +595,16 @@
     }
 
     /**
-     * All user post a issue at a project whose id is {@code projectId}
+     * Find issue authors and {@code currentUser}
      *
+     * Issue authors are found in a project whose id is {@code projectId}.
+     *
+     * @param currentUser
      * @param projectId
      * @return
      */
-    public static List<User> findIssueAuthorsByProjectId(long projectId) {
-        String sql = "select distinct user.id, user.name, user.login_id from issue issue, n4user user where issue.author_id = user.id";
+    public static List<User> findIssueAuthorsByProjectIdAndMe(User currentUser, long projectId) {
+        String sql = "select distinct user.id, user.name, user.login_id from issue issue, n4user user where issue.author_id = user.id or user.id = " + currentUser.id;
         return createUserSearchQueryWithRawSql(sql).where()
                 .eq("issue.project_id", projectId)
                 .orderBy("user.name ASC")
@@ -586,8 +617,11 @@
      * @param projectId
      * @return
      */
-    public static List<Assignee> findIssueAssigneeByProjectId(long projectId) {
-        return Assignee.finder.where().eq("project.id", projectId).orderBy("user.name ASC").findList();
+    public static List<User> findIssueAssigneeByProjectIdAndMe(User currentUser, long projectId) {
+        String sql = "SELECT id, name FROM n4user WHERE id = " + currentUser.id + " or id IN (SELECT DISTINCT user_id FROM assignee WHERE id IN (SELECT DISTINCT assignee_id FROM issue WHERE project_id = " + projectId + "))";
+        return User.find.setRawSql(RawSqlBuilder.parse(sql).create())
+                .orderBy("name ASC")
+                .findList();
     }
 
     /**
app/views/issue/partial_searchform.scala.html
--- app/views/issue/partial_searchform.scala.html
+++ app/views/issue/partial_searchform.scala.html
@@ -41,12 +41,12 @@
                 <select id="authorId" name="authorId" data-search="authorId"
                         data-toggle="select2" data-format="user" data-container-css-class="fullsize">
                     <option value="">@Messages("common.order.all")</option>
-                    @if(ProjectUser.isMember(UserApp.currentUser().id, project.id)){
+                    @if(project.isProjectOrOrganizationUser(UserApp.currentUser())) {
                     <option value="@UserApp.currentUser().id">@Messages("issue.list.authoredByMe")</option>
                     }
-                    @for(author <- User.findIssueAuthorsByProjectId(project.id)) {
+                    @for(author <- User.findIssueAuthorsByProjectIdAndMe(UserApp.currentUser(), project.id)) {
                         <option value="@author.id"
-                        @if(param.authorId != null && param.authorId == author.id){
+                        @if(param.authorId != null && param.authorId == author.id) {
                             selected
                         }
                         data-avatar-url="@author.avatarUrl"
@@ -68,15 +68,15 @@
                     @if(ProjectUser.isMember(UserApp.currentUser().id, project.id)){
                     <option value="@UserApp.currentUser().id">@Messages("issue.list.assignedToMe")</a></option>
                     }
-                    @for(assignee <- User.findIssueAssigneeByProjectId(project.id)) {
-                        <option value="@assignee.user.id"
+                    @for(user <- User.findIssueAssigneeByProjectIdAndMe(UserApp.currentUser(), project.id)) {
+                        <option value="@user.id"
                         @if(param.assigneeId != null
-                            && param.assigneeId == assignee.user.id){
+                            && param.assigneeId == user.id){
                             selected
                         }
-                        data-avatar-url="@assignee.user.avatarUrl"
-                        data-login-id="@assignee.user.loginId">
-                            @assignee.user.name
+                        data-avatar-url="@user.avatarUrl"
+                        data-login-id="@user.loginId">
+                            @user.name
                         </option>
                     }
                 </select>
Add a comment
List