doortts doortts 2018-02-08
issue: Change search by labels to exclusive search
When to search by labels, results are exclusive by labels.

See: Yona Github issue #325 #197
@28ac7bf76961bb1cd8151077aeeb32d39520e5de
app/models/support/SearchCondition.java
--- app/models/support/SearchCondition.java
+++ app/models/support/SearchCondition.java
@@ -422,10 +422,7 @@
 
         setIssueState(el);
 
-        if (CollectionUtils.isNotEmpty(labelIds)) {
-            Set<IssueLabel> labels = IssueLabel.finder.where().idIn(new ArrayList<>(labelIds)).findSet();
-            el.in("id", Issue.finder.where().in("labels", labels).findIds());
-        }
+        setLabelsIfExist(project, el);
 
         setOrderByIfExist(el);
 
@@ -440,6 +437,40 @@
         return el;
     }
 
+    private void setLabelsIfExist(Project project, ExpressionList<Issue> el) {
+        if (CollectionUtils.isNotEmpty(labelIds)) {
+            Set<IssueLabel> labels = IssueLabel.finder.where().idIn(new ArrayList<>(labelIds)).findSet();
+
+            List<Issue> issues = Issue.finder.where()
+                    .eq("project", project)
+                    .in("labels", labels).findList();
+
+            for (IssueLabel issueLabel : labels) {
+                issues = findIssueByLabel(issues, issueLabel);
+            }
+
+            el.in("id", extractIssueIds(issues));
+        }
+    }
+
+    private Set<Long> extractIssueIds(List<Issue> issues) {
+        Set<Long> ids = new HashSet<>();
+        for (Issue issue : issues) {
+            ids.add(issue.id);
+        }
+        return ids;
+    }
+
+    private List<Issue> findIssueByLabel(List<Issue> issues, IssueLabel label) {
+        List<Issue> result = new ArrayList<>();
+        for (Issue issue : issues) {
+            if(issue.labels.contains(label)){
+                result.add(issue);
+            }
+        }
+        return result;
+    }
+
     public String getDueDateString() {
         if (dueDate == null) {
             return null;
Add a comment
List