Keesun Baik 2014-08-19
Search: select not empty type
When a user search something with a keyword, this commit supports automatically select not-empty resource type.

If there are many not-empty types, select the most top resource type in side menu.
If all resource types are empty, select issue as a default.
@3b1e72415226efcb8937f63d07833967daa2397a
app/controllers/SearchApp.java
--- app/controllers/SearchApp.java
+++ app/controllers/SearchApp.java
@@ -57,8 +57,7 @@
         }
 
         SearchResult searchResult = getSearchResult(keyword, user, searchType);
-
-        switch (searchType) {
+        switch (searchResult.getSearchType()) {
             case ISSUE:
                 searchResult.setIssues(Search.findIssues(keyword, user, pageParam));
                 break;
@@ -99,6 +98,7 @@
         searchResult.setIssueCommentsCount(Search.countIssueComments(keyword, user));
         searchResult.setPostCommentsCount(Search.countPostComments(keyword, user));
         searchResult.setReviewsCount(Search.countReviews(keyword, user));
+        searchResult.updateSearchType();
         return searchResult;
     }
 
@@ -129,7 +129,7 @@
 
         SearchResult searchResult = getSearchResult(keyword, user, organization, searchType);
 
-        switch (searchType) {
+        switch (searchResult.getSearchType()) {
             case ISSUE:
                 searchResult.setIssues(Search.findIssues(keyword, user, organization, pageParam));
                 break;
@@ -171,6 +171,7 @@
         searchResult.setIssueCommentsCount(Search.countIssueComments(keyword, user, organization));
         searchResult.setPostCommentsCount(Search.countPostComments(keyword, user, organization));
         searchResult.setReviewsCount(Search.countReviews(keyword, user, organization));
+        searchResult.updateSearchType();
         return searchResult;
     }
 
@@ -203,7 +204,7 @@
 
         SearchResult searchResult = getSearchResult(keyword, user, project, searchType);
 
-        switch (searchType) {
+        switch (searchResult.getSearchType()) {
             case ISSUE:
                 searchResult.setIssues(Search.findIssues(keyword, user, project, pageParam));
                 break;
@@ -241,6 +242,7 @@
         searchResult.setIssueCommentsCount(Search.countIssueComments(keyword, user, project));
         searchResult.setPostCommentsCount(Search.countPostComments(keyword, user, project));
         searchResult.setReviewsCount(Search.countReviews(keyword, user, project));
+        searchResult.updateSearchType();
         return searchResult;
     }
 
app/models/SearchResult.java
--- app/models/SearchResult.java
+++ app/models/SearchResult.java
@@ -106,6 +106,54 @@
         return endIndex < contentLength ? endIndex : contentLength;
     }
 
+    public void updateSearchType() {
+        if(!(this.searchType == SearchType.AUTO)) {
+            return;
+        }
+
+        if (getIssuesCount() > 0) {
+            setSearchType(SearchType.ISSUE);
+            return;
+        }
+
+        if (getUsersCount() > 0) {
+            setSearchType(SearchType.USER);
+            return;
+        }
+
+        if (getProjectsCount() > 0) {
+            setSearchType(SearchType.PROJECT);
+            return;
+        }
+
+        if (getPostsCount() > 0) {
+            setSearchType(SearchType.POST);
+            return;
+        }
+
+        if (getMilestonesCount() > 0) {
+            setSearchType(SearchType.MILESTONE);
+            return;
+        }
+
+        if (getIssueCommentsCount() > 0) {
+            setSearchType(SearchType.ISSUE_COMMENT);
+            return;
+        }
+
+        if (getPostCommentsCount() > 0) {
+            setSearchType(SearchType.POST_COMMENT);
+            return;
+        }
+
+        if (getReviewsCount() > 0) {
+            setSearchType(SearchType.REVIEW);
+            return;
+        }
+
+        setSearchType(SearchType.ISSUE);
+    }
+
     private class BeginAndEnd {
         int beginIndex;
         int endIndex;
app/models/enumeration/SearchType.java
--- app/models/enumeration/SearchType.java
+++ app/models/enumeration/SearchType.java
@@ -5,7 +5,7 @@
  */
 public enum SearchType {
 
-    NA("not available"), USER("user"), PROJECT("project"), ISSUE("issue"), POST("post"),
+    AUTO("auto"), NA("not available"), USER("user"), PROJECT("project"), ISSUE("issue"), POST("post"),
     MILESTONE("milestone"), ISSUE_COMMENT("issue_comment"), POST_COMMENT("post_comment"), REVIEW("review");
 
     private String searchType;
app/views/common/navbar.scala.html
--- app/views/common/navbar.scala.html
+++ app/views/common/navbar.scala.html
@@ -65,7 +65,7 @@
             }
             <li>
                 <form action="@makeSearchLink()" class="input-prepend gnb-search-form" name="gnb-search-form">
-                <input type="hidden" name="searchType" value="issue">
+                <input type="hidden" name="searchType" value="auto">
                     @if(project != null || org !=null) {
                     
                     <div class="btn-group">
Add a comment
List