]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4290 order action plans by chronological deadline
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 24 May 2013 12:12:09 +0000 (14:12 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 24 May 2013 12:12:17 +0000 (14:12 +0200)
sonar-core/src/main/java/org/sonar/core/issue/ActionPlanDeadlineComparator.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/issue/DefaultActionPlan.java
sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanDao.java
sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanDto.java
sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanMapper.java
sonar-core/src/main/resources/org/sonar/core/issue/db/ActionPlanMapper.xml
sonar-core/src/test/java/org/sonar/core/issue/ActionPlanDeadlineComparatorTest.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/issue/ActionPlanService.java

diff --git a/sonar-core/src/main/java/org/sonar/core/issue/ActionPlanDeadlineComparator.java b/sonar-core/src/main/java/org/sonar/core/issue/ActionPlanDeadlineComparator.java
new file mode 100644 (file)
index 0000000..0deb183
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.core.issue;
+
+import org.sonar.api.issue.ActionPlan;
+
+import java.util.Comparator;
+import java.util.Date;
+
+/**
+ * Sort action plans by chronological deadlines. Plans without deadline are
+ * located after plans with deadline.
+ */
+public class ActionPlanDeadlineComparator implements Comparator<ActionPlan> {
+
+  @Override
+  public int compare(ActionPlan a1, ActionPlan a2) {
+    Date d1 = a1.deadLine();
+    Date d2 = a2.deadLine();
+    if (d1 != null && d2 != null) {
+      return d1.compareTo(d2);
+    }
+    if (d1 != null) {
+      return -1;
+    }
+    return 1;
+  }
+}
index be6fb18bd63285288ff2fdf5b8a6b62669fabb5b..4c5bac02e0c94638467de6bad9a752c518024912 100644 (file)
@@ -40,7 +40,7 @@ public class DefaultActionPlan implements ActionPlan {
   private Date createdAt;
   private Date updatedAt;
 
-  private DefaultActionPlan(){
+  public DefaultActionPlan(){
 
   }
 
index 37544cd0c60aa57c668fcf281d6a742528305f1b..5e00190c152490f0c5023a5e9aadb1fa8d105dfb 100644 (file)
@@ -82,7 +82,7 @@ public class ActionPlanDao implements BatchComponent, ServerComponent {
     }
   }
 
-  public Collection<ActionPlanDto> findByKeys(Collection<String> keys) {
+  public List<ActionPlanDto> findByKeys(Collection<String> keys) {
     if (keys.isEmpty()) {
       return Collections.emptyList();
     }
@@ -95,7 +95,7 @@ public class ActionPlanDao implements BatchComponent, ServerComponent {
     }
   }
 
-  public Collection<ActionPlanDto> findOpenByProjectId(Long projectId) {
+  public List<ActionPlanDto> findOpenByProjectId(Long projectId) {
     SqlSession session = mybatis.openSession();
     try {
       return session.getMapper(ActionPlanMapper.class).findOpenByProjectId(projectId);
@@ -104,7 +104,7 @@ public class ActionPlanDao implements BatchComponent, ServerComponent {
     }
   }
 
-  public Collection<ActionPlanDto> findByNameAndProjectId(String name, Long projectId) {
+  public List<ActionPlanDto> findByNameAndProjectId(String name, Long projectId) {
     SqlSession session = mybatis.openSession();
     try {
       return session.getMapper(ActionPlanMapper.class).findByNameAndProjectId(name, projectId);
index d736e68715bb69fa6932200729a5e98ab79d7cab..88ace09db2bdccbd3dc525ad5b5ea8e0bc1ced53 100644 (file)
@@ -177,7 +177,8 @@ public class ActionPlanDto {
   }
 
   public DefaultActionPlan toActionPlan() {
-    return DefaultActionPlan.create(name)
+    return new DefaultActionPlan()
+      .setName(name)
       .setKey(kee)
       .setProjectKey(projectKey)
       .setDescription(description)
index 245bb1a9eefa36544a1db35c50c806974203e6dc..8ca67acbaaaa0238ae995171e8fe512365eccb26 100644 (file)
@@ -36,11 +36,11 @@ public interface ActionPlanMapper {
 
   void delete(@Param("key") String key);
 
-  Collection<ActionPlanDto> findByKeys(@Param("keys") List <List<String>> keys);
+  List<ActionPlanDto> findByKeys(@Param("keys") List <List<String>> keys);
 
   ActionPlanDto findByKey(@Param("key") String key);
 
-  Collection<ActionPlanDto> findOpenByProjectId(@Param("projectId") Long projectId);
+  List<ActionPlanDto> findOpenByProjectId(@Param("projectId") Long projectId);
 
-  Collection<ActionPlanDto> findByNameAndProjectId(@Param("name")String name, @Param("projectId") Long projectId);
+  List<ActionPlanDto> findByNameAndProjectId(@Param("name")String name, @Param("projectId") Long projectId);
 }
index 2e091afd2ba3f4052495b4e42fa05a4c15efb92e..663284e21ac89f8bcbfd7c0bcfe14e751b2139d0 100644 (file)
@@ -78,7 +78,6 @@
       and ap.status='OPEN'
       and ap.project_id=p.id
     </where>
-    order by deadline
   </select>
 
   <select id="findByNameAndProjectId" parameterType="long" resultType="ActionPlanIssue">
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/ActionPlanDeadlineComparatorTest.java b/sonar-core/src/test/java/org/sonar/core/issue/ActionPlanDeadlineComparatorTest.java
new file mode 100644 (file)
index 0000000..93a7e69
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.core.issue;
+
+import org.junit.Test;
+import org.sonar.api.issue.ActionPlan;
+import org.sonar.api.utils.DateUtils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class ActionPlanDeadlineComparatorTest {
+
+  ActionPlan shortTerm = new DefaultActionPlan().setDeadLine(DateUtils.parseDate("2013-12-01"));
+  ActionPlan longTerm = new DefaultActionPlan().setDeadLine(DateUtils.parseDate("2018-05-05"));
+  ActionPlan noDeadline = new DefaultActionPlan().setDeadLine(null);
+
+  @Test
+  public void compare_plans_with_deadlines() throws Exception {
+    List<ActionPlan> plans = Arrays.asList(shortTerm, longTerm);
+    Collections.sort(plans, new ActionPlanDeadlineComparator());
+    assertThat(plans).containsSequence(shortTerm, longTerm);
+
+    plans = Arrays.asList(longTerm, shortTerm);
+    Collections.sort(plans, new ActionPlanDeadlineComparator());
+    assertThat(plans).containsSequence(shortTerm, longTerm);
+  }
+
+  @Test
+  public void end_with_plans_without_deadline() throws Exception {
+    List<ActionPlan> plans = Arrays.asList(noDeadline, longTerm, shortTerm);
+    Collections.sort(plans, new ActionPlanDeadlineComparator());
+    assertThat(plans).containsSequence(shortTerm, longTerm, noDeadline);
+
+    plans = Arrays.asList(longTerm, noDeadline, shortTerm);
+    Collections.sort(plans, new ActionPlanDeadlineComparator());
+    assertThat(plans).containsSequence(shortTerm, longTerm, noDeadline);
+  }
+}
index 370e51cc5d16b44eb04acfe4b2af6682e8add477..2a51fe5d06e8e44e5e473073ef68b049ef859a10 100644 (file)
@@ -24,6 +24,7 @@ import com.google.common.base.Function;
 import com.google.common.collect.Iterables;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.issue.ActionPlan;
+import org.sonar.core.issue.ActionPlanDeadlineComparator;
 import org.sonar.core.issue.ActionPlanStats;
 import org.sonar.core.issue.db.ActionPlanDao;
 import org.sonar.core.issue.db.ActionPlanDto;
@@ -36,6 +37,7 @@ import org.sonar.core.resource.ResourceQuery;
 import javax.annotation.CheckForNull;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
@@ -90,14 +92,16 @@ public class ActionPlanService implements ServerComponent {
     return actionPlanDto.toActionPlan();
   }
 
-  public Collection<ActionPlan> findByKeys(Collection<String> keys) {
-    Collection<ActionPlanDto> actionPlanDtos = actionPlanDao.findByKeys(keys);
+  public List<ActionPlan> findByKeys(Collection<String> keys) {
+    List<ActionPlanDto> actionPlanDtos = actionPlanDao.findByKeys(keys);
     return toActionPlans(actionPlanDtos);
   }
 
   public Collection<ActionPlan> findOpenByComponentKey(String componentKey) {
-    Collection<ActionPlanDto> actionPlanDtos = actionPlanDao.findOpenByProjectId(findRootProject(componentKey).getId());
-    return toActionPlans(actionPlanDtos);
+    List<ActionPlanDto> dtos = actionPlanDao.findOpenByProjectId(findRootProject(componentKey).getId());
+    List<ActionPlan> plans = toActionPlans(dtos);
+    Collections.sort(plans, new ActionPlanDeadlineComparator());
+    return plans;
   }
 
   public List<ActionPlanStats> findActionPlanStats(String projectKey) {
@@ -114,7 +118,7 @@ public class ActionPlanService implements ServerComponent {
     return !actionPlanDao.findByNameAndProjectId(name, findComponent(projectKey).getId()).isEmpty();
   }
 
-  private Collection<ActionPlan> toActionPlans(Collection<ActionPlanDto> actionPlanDtos) {
+  private List<ActionPlan> toActionPlans(List<ActionPlanDto> actionPlanDtos) {
     return newArrayList(Iterables.transform(actionPlanDtos, new Function<ActionPlanDto, ActionPlan>() {
       @Override
       public ActionPlan apply(ActionPlanDto actionPlanDto) {