]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 refactor issue sorting
authorSimon Brandhof <simon.brandhof@gmail.com>
Sat, 4 May 2013 08:34:31 +0000 (10:34 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Sat, 4 May 2013 08:34:31 +0000 (10:34 +0200)
12 files changed:
sonar-core/src/main/java/org/sonar/core/issue/db/ChangeDtoConverter.java
sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java
sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java
sonar-core/src/main/java/org/sonar/core/source/HtmlSourceDecorator.java
sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml
sonar-core/src/test/java/org/sonar/core/issue/db/IssueDaoTest.java
sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueQuery.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java
sonar-plugin-api/src/test/java/org/sonar/api/issue/IssueQueryTest.java
sonar-server/src/main/java/org/sonar/server/issue/ServerIssueFinder.java
sonar-server/src/main/java/org/sonar/server/issue/WebIssuesApi.java
sonar-server/src/main/java/org/sonar/server/platform/UserSession.java

index 23015627eb74023a9672acb12e3156ab0dd8ef78..c5eaabf0b4df1f7370b80d9488c2461185aa56ee 100644 (file)
@@ -29,6 +29,10 @@ import java.util.List;
 
 class ChangeDtoConverter {
 
+  private ChangeDtoConverter() {
+    // only static methods
+  }
+
   static final String TYPE_FIELD_CHANGE = "change";
   static final String TYPE_COMMENT = "comment";
 
index f28fb0ac60fea787678a97b14c74d1b78c9368af..9e6bcf82f4c9b51c337ef9c4511b26c3a6a772e1 100644 (file)
@@ -43,13 +43,6 @@ public class IssueDao implements BatchComponent, ServerComponent {
 
   private final MyBatis mybatis;
 
-  private static final Map<String, String> SORTS = ImmutableMap.of(
-    "created", "i.issue_creation_date",
-    "updated", "i.issue_update_date",
-    "closed", "i.issue_close_date",
-    "assignee", "i.assignee"
-  );
-
   public IssueDao(MyBatis mybatis) {
     this.mybatis = mybatis;
   }
index 91394566a71f5c857205b3be4eff9703526bbfdd..8999a8f6f05696840ce6163696d3f4ee9c73acb2 100644 (file)
@@ -119,7 +119,7 @@ public class PluginClassloaders {
       } else {
         parent = new ResourcesClassloader(resources, baseClassloader);
       }
-      final ClassRealm realm;
+      ClassRealm realm;
       if (plugin.isUseChildFirstClassLoader()) {
         ClassRealm parentRealm = world.newRealm(plugin.getKey() + "-parent", parent);
         realm = parentRealm.createChildRealm(plugin.getKey());
index 16df96a30107310103388ecfc8b4c53b2633bd1c..eff897901d1f51a3738760e0e089074f44068af2 100644 (file)
@@ -45,7 +45,7 @@ public class HtmlSourceDecorator implements ServerComponent {
   }
 
   @VisibleForTesting
-  protected HtmlSourceDecorator(SnapshotSourceDao snapshotSourceDao, SnapshotDataDao snapshotDataDao) {
+  HtmlSourceDecorator(SnapshotSourceDao snapshotSourceDao, SnapshotDataDao snapshotDataDao) {
     this.snapshotSourceDao = snapshotSourceDao;
     this.snapshotDataDao= snapshotDataDao;
   }
index 4f0c14f67d2596e4ff8bb5398c8c46dbaf01a5bf..7b0ea92b70cc9ea6e9429272caa38187de3d01b7 100644 (file)
     <if test="sort != null">
       order by
       <choose>
-        <when test="'created'.equals(sort)">
+        <when test="'CREATION_DATE'.equals(sort.name())">
           i.issue_creation_date
         </when>
-        <when test="'updated'.equals(sort)">
+        <when test="'UPDATE_DATE'.equals(sort.name())">
           i.issue_update_date
         </when>
-        <when test="'closed'.equals(sort)">
+        <when test="'CLOSE_DATE'.equals(sort.name())">
           i.issue_close_date
         </when>
-        <when test="'assignee'.equals(sort)">
+        <when test="'ASSIGNEE'.equals(sort.name())">
           i.assignee_login
         </when>
       </choose>
index 7592dae2f0824f088348b9d04a9efe793e9ee8f3..7a9461e2a0dffa2e7752c464b4b82601abfa0512 100644 (file)
@@ -173,8 +173,8 @@ public class IssueDaoTest extends AbstractDaoTestCase {
   public void should_select_sort_by_assignee() {
     setupData("shared", "should_select_returned_sorted_result");
 
-    IssueQuery query = IssueQuery.builder().sort("assignee").asc(true).build();
-    List<IssueDto> results = newArrayList(dao.select(query));
+    IssueQuery query = IssueQuery.builder().sort(IssueQuery.Sort.ASSIGNEE).asc(true).build();
+      List < IssueDto > results = newArrayList(dao.select(query));
     assertThat(results).hasSize(3);
     assertThat(results.get(0).getAssignee()).isEqualTo("arthur");
     assertThat(results.get(1).getAssignee()).isEqualTo("henry");
index d3fedd09ac7de6e0bae9bc6ec6a3533ba0a02954..b80ea04b91e0071654c1866542e92d6723413651 100644 (file)
@@ -25,8 +25,6 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.sonar.api.rule.RuleKey;
 
 import javax.annotation.Nullable;
-
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 
@@ -37,6 +35,10 @@ import java.util.Date;
  */
 public class IssueQuery {
 
+  public static enum Sort {
+    CREATION_DATE, UPDATE_DATE, CLOSE_DATE, ASSIGNEE
+  }
+
   private final Collection<String> issueKeys;
   private final Collection<String> severities;
   private final Collection<String> statuses;
@@ -49,7 +51,7 @@ public class IssueQuery {
   private final Boolean assigned;
   private final Date createdAfter;
   private final Date createdBefore;
-  private final String sort;
+  private final Sort sort;
   private final boolean asc;
 
   // max results per page
@@ -125,7 +127,7 @@ public class IssueQuery {
     return createdBefore;
   }
 
-  public String sort() {
+  public Sort sort() {
     return sort;
   }
 
@@ -156,10 +158,6 @@ public class IssueQuery {
    */
   public static class Builder {
 
-    private enum Sort {
-      created, updated, closed, assignee
-    }
-
     private static final int DEFAULT_PAGE_SIZE = 100;
     private static final int MAX_PAGE_SIZE = 1000;
     private static final int DEFAULT_PAGE_INDEX = 1;
@@ -176,7 +174,7 @@ public class IssueQuery {
     private Boolean assigned = null;
     private Date createdAfter;
     private Date createdBefore;
-    private String sort;
+    private Sort sort;
     private boolean asc = false;
     private int pageSize = DEFAULT_PAGE_SIZE;
     private int pageIndex = DEFAULT_PAGE_INDEX;
@@ -248,14 +246,8 @@ public class IssueQuery {
       return this;
     }
 
-    public Builder sort(@Nullable String sort) {
-      if (sort != null) {
-        try {
-          this.sort = Sort.valueOf(sort).name();
-        } catch (IllegalArgumentException e){
-          throw new IllegalArgumentException("Sort should contain only : " + Arrays.toString(Sort.values()), e);
-        }
-      }
+    public Builder sort(@Nullable Sort sort) {
+      this.sort = sort;
       return this;
     }
 
index 3a5cc176be2fbbf4722caeb66ff5af8780792830..7bc4e18671cd20a8fdfd0a544133b6a73710c6cf 100644 (file)
@@ -422,8 +422,8 @@ public final class Rule {
   /**
    * @since 3.6
    */
-  public Rule setCreatedAt(Date created_at) {
-    this.createdAt = created_at;
+  public Rule setCreatedAt(Date d) {
+    this.createdAt = d;
     return this;
   }
 
index 5a9d955bac1ee701acd32f3713fd8ee081183b15..c7646729b99199e139a470055b6b26fdbf67d1a3 100644 (file)
@@ -46,7 +46,7 @@ public class IssueQueryTest {
       .assigned(true)
       .createdAfter(new Date())
       .createdBefore(new Date())
-      .sort("assignee")
+      .sort(IssueQuery.Sort.ASSIGNEE)
       .pageSize(10)
       .pageIndex(2)
       .build();
@@ -62,7 +62,7 @@ public class IssueQueryTest {
     assertThat(query.rules()).containsOnly(RuleKey.of("squid", "AvoidCycle"));
     assertThat(query.createdAfter()).isNotNull();
     assertThat(query.createdBefore()).isNotNull();
-    assertThat(query.sort()).isEqualTo("assignee");
+    assertThat(query.sort()).isEqualTo(IssueQuery.Sort.ASSIGNEE);
     assertThat(query.pageSize()).isEqualTo(10);
     assertThat(query.pageIndex()).isEqualTo(2);
   }
@@ -104,14 +104,8 @@ public class IssueQueryTest {
   }
 
   @Test
-  public void should_validate_sort() throws Exception {
-    try {
-      IssueQuery.builder()
-        .sort("INVALID SORT")
-        .build();
-      fail();
-    } catch (Exception e) {
-      assertThat(e).hasMessage("Sort should contain only : [created, updated, closed, assignee]").isInstanceOf(IllegalArgumentException.class);
-    }
+  public void should_accept_null_sort() throws Exception {
+    IssueQuery query = IssueQuery.builder().sort(null).build();
+    assertThat(query.sort()).isNull();
   }
 }
index 36d0985bc220fbfc6e517fc3e47863c640846441..41e95dabf5c58cd2ffc0319a7cef96efb0807252 100644 (file)
@@ -64,7 +64,9 @@ public class ServerIssueFinder implements IssueFinder {
   private final ResourceDao resourceDao;
   private final ActionPlanIssueDao actionPlanIssueDao;
 
-  public ServerIssueFinder(MyBatis myBatis, IssueDao issueDao, AuthorizationDao authorizationDao, DefaultRuleFinder ruleFinder, ResourceDao resourceDao, ActionPlanIssueDao actionPlanIssueDao) {
+  public ServerIssueFinder(MyBatis myBatis, IssueDao issueDao, AuthorizationDao authorizationDao,
+                           DefaultRuleFinder ruleFinder, ResourceDao resourceDao,
+                           ActionPlanIssueDao actionPlanIssueDao) {
     this.myBatis = myBatis;
     this.issueDao = issueDao;
     this.authorizationDao = authorizationDao;
index 35d9f60a4b5ad1ca47ed6bbc6700775d5ff5dc9d..bb1a29425e2d5fb6c3515bf6bb184fe8799f4bef 100644 (file)
@@ -27,14 +27,12 @@ import com.google.common.primitives.Ints;
 import org.sonar.api.issue.IssueFinder;
 import org.sonar.api.issue.IssueQuery;
 import org.sonar.api.issue.WebIssues;
-import org.sonar.api.issue.WebIssues;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.web.UserRole;
 import org.sonar.server.platform.UserSession;
 
 import javax.annotation.Nullable;
-
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -78,6 +76,10 @@ public class WebIssuesApi implements WebIssues {
     builder.createdBefore(toDate(props.get("createdBefore")));
     builder.pageSize(toInteger(props.get("pageSize")));
     builder.pageIndex(toInteger(props.get("pageIndex")));
+    String sort = (String) props.get("sort");
+    if (sort != null) {
+      builder.sort(IssueQuery.Sort.valueOf(sort));
+    }
     return builder.build();
   }
 
index 0639e884117316704b3d1bb1ed954069154e7ff0..c81409d11cdf9f5bf6ffbb83d2ef33e8b34ef781 100644 (file)
@@ -28,7 +28,7 @@ import java.util.Locale;
 
 public class UserSession {
 
-  private static final ThreadLocal<UserSession> threadLocal = new ThreadLocal<UserSession>();
+  private static final ThreadLocal<UserSession> THREAD_LOCAL = new ThreadLocal<UserSession>();
   private static final UserSession DEFAULT_ANONYMOUS = new UserSession(null, null, Locale.ENGLISH);
 
   private final Integer userId;
@@ -63,11 +63,11 @@ public class UserSession {
    * @return never null
    */
   public static UserSession get() {
-    return Objects.firstNonNull(threadLocal.get(), DEFAULT_ANONYMOUS);
+    return Objects.firstNonNull(THREAD_LOCAL.get(), DEFAULT_ANONYMOUS);
   }
 
   public static void set(@Nullable UserSession session) {
-    threadLocal.set(session);
+    THREAD_LOCAL.set(session);
   }
 
   public static void setSession(@Nullable Integer userId, @Nullable String login, @Nullable String localeRubyKey) {
@@ -75,11 +75,11 @@ public class UserSession {
   }
 
   public static void remove() {
-    threadLocal.remove();
+    THREAD_LOCAL.remove();
   }
 
   static boolean hasSession() {
-    return threadLocal.get() != null;
+    return THREAD_LOCAL.get() != null;
   }
 }