--- /dev/null
+/*
+ * 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;
+ }
+}
private Date createdAt;
private Date updatedAt;
- private DefaultActionPlan(){
+ public DefaultActionPlan(){
}
}
}
- public Collection<ActionPlanDto> findByKeys(Collection<String> keys) {
+ public List<ActionPlanDto> findByKeys(Collection<String> keys) {
if (keys.isEmpty()) {
return Collections.emptyList();
}
}
}
- public Collection<ActionPlanDto> findOpenByProjectId(Long projectId) {
+ public List<ActionPlanDto> findOpenByProjectId(Long projectId) {
SqlSession session = mybatis.openSession();
try {
return session.getMapper(ActionPlanMapper.class).findOpenByProjectId(projectId);
}
}
- 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);
}
public DefaultActionPlan toActionPlan() {
- return DefaultActionPlan.create(name)
+ return new DefaultActionPlan()
+ .setName(name)
.setKey(kee)
.setProjectKey(projectKey)
.setDescription(description)
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);
}
and ap.status='OPEN'
and ap.project_id=p.id
</where>
- order by deadline
</select>
<select id="findByNameAndProjectId" parameterType="long" resultType="ActionPlanIssue">
--- /dev/null
+/*
+ * 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);
+ }
+}
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;
import javax.annotation.CheckForNull;
import java.util.Collection;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
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) {
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) {