adds application level issue sorting (fixes #19)

Fri, 09 Oct 2020 15:35:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 09 Oct 2020 15:35:48 +0200
changeset 121
428dca747d6b
parent 120
5d4c12338fe0
child 122
b2edd3d14135

adds application level issue sorting (fixes #19)

src/main/java/de/uapcore/lightpit/dao/postgres/PGIssueDao.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/viewmodel/util/IssueSorter.java file | annotate | diff | comparison | revisions
--- a/src/main/java/de/uapcore/lightpit/dao/postgres/PGIssueDao.java	Fri Oct 09 11:57:05 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/dao/postgres/PGIssueDao.java	Fri Oct 09 15:35:48 2020 +0200
@@ -56,8 +56,7 @@
                         "from lpit_issue i " +
                         "join lpit_project p on project = projectid " +
                         "left join lpit_user on userid = assignee " +
-                        "where project = ? "+
-                        "order by eta asc, updated desc");
+                        "where project = ? ");
 
         listForVersion = connection.prepareStatement(
                 "with issue_version as ( "+
@@ -70,8 +69,7 @@
                         "join lpit_project p on project = projectid " +
                         "left join issue_version using (issueid) "+
                         "left join lpit_user on userid = assignee " +
-                        "where coalesce(versionid,-1) = ? "+
-                        "order by eta asc, updated desc"
+                        "where coalesce(versionid,-1) = ? "
         );
 
         find = connection.prepareStatement(
--- a/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Fri Oct 09 11:57:05 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java	Fri Oct 09 15:35:48 2020 +0200
@@ -33,6 +33,7 @@
 import de.uapcore.lightpit.dao.DataAccessObjects;
 import de.uapcore.lightpit.entities.*;
 import de.uapcore.lightpit.viewmodel.*;
+import de.uapcore.lightpit.viewmodel.util.IssueSorter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -197,6 +198,11 @@
         final var detailView = viewModel.getProjectDetails();
         final var issues = issueDao.list(version);
         for (var issue : issues) issueDao.joinVersionInformation(issue);
+        issues.sort(new IssueSorter(
+                new IssueSorter.Criteria(IssueSorter.Field.PHASE, true),
+                new IssueSorter.Criteria(IssueSorter.Field.ETA, true),
+                new IssueSorter.Criteria(IssueSorter.Field.UPDATED, false)
+        ));
         detailView.updateDetails(issues, version);
 
         return forwardView(req, viewModel, "project-details");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/uapcore/lightpit/viewmodel/util/IssueSorter.java	Fri Oct 09 15:35:48 2020 +0200
@@ -0,0 +1,78 @@
+package de.uapcore.lightpit.viewmodel.util;
+
+import de.uapcore.lightpit.entities.Issue;
+import de.uapcore.lightpit.entities.IssueStatus;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+public class IssueSorter implements Comparator<Issue> {
+
+    public enum Field {
+        PHASE, ETA, UPDATED
+    }
+
+    public static class Criteria {
+        private Field field;
+        private boolean asc;
+
+        public Criteria(Field field, boolean asc) {
+            this.field = field;
+            this.asc = asc;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == null || !obj.getClass().equals(Criteria.class))
+                return false;
+            final var other = (Criteria)obj;
+            return other.field.equals(field) && other.asc == asc;
+        }
+    }
+
+    private final Criteria[] criteria;
+
+    public IssueSorter(Criteria ... criteria) {
+        this.criteria = criteria;
+    }
+
+    private int compare(Issue left, Issue right, Criteria criteria) {
+        int result;
+        switch (criteria.field) {
+            case PHASE:
+                result = Boolean.compare(
+                        left.getPhase() == IssueStatus.PHASE_DONE,
+                        right.getPhase() == IssueStatus.PHASE_DONE);
+                break;
+            case ETA:
+                if (left.getEta() != null && right.getEta() != null)
+                    result = left.getEta().compareTo(right.getEta());
+                else
+                    result = left != null ? -1 : 1;
+                break;
+            case UPDATED:
+                result = left.getUpdated().compareTo(right.getUpdated());
+                break;
+            default:
+                throw new UnsupportedOperationException();
+        }
+        return criteria.asc ? result : -result;
+    }
+
+    @Override
+    public int compare(Issue left, Issue right) {
+        for (var c : criteria) {
+            int r = compare(left, right, c);
+            if (r != 0) return r;
+        }
+        return 0;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (o == null || !o.getClass().equals(IssueSorter.class))
+            return false;
+        final var other = (IssueSorter) o;
+        return Arrays.equals(criteria, other.criteria);
+    }
+}

mercurial