move IssueSorter to viewmodel package

Sat, 04 Jun 2022 18:29:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 04 Jun 2022 18:29:58 +0200
changeset 249
6bded7090719
parent 248
90dc13c78b5d
child 250
ce6d539bb970

move IssueSorter to viewmodel package

src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt file | annotate | diff | comparison | revisions
src/main/kotlin/de/uapcore/lightpit/util/Issues.kt file | annotate | diff | comparison | revisions
src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt file | annotate | diff | comparison | revisions
--- a/src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt	Sat Jun 04 18:02:25 2022 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt	Sat Jun 04 18:29:58 2022 +0200
@@ -35,7 +35,6 @@
 import de.uapcore.lightpit.types.IssueStatus
 import de.uapcore.lightpit.types.VersionStatus
 import de.uapcore.lightpit.types.WebColor
-import de.uapcore.lightpit.util.IssueSorter.Companion.DEFAULT_ISSUE_SORTER
 import de.uapcore.lightpit.viewmodel.*
 import java.sql.Date
 import javax.servlet.annotation.WebServlet
@@ -187,7 +186,13 @@
         withPathInfo(http, dao)?.run {
 
             val issues = dao.listIssues(project, version, component)
-                .sortedWith(DEFAULT_ISSUE_SORTER)
+                .sortedWith(
+                    IssueSorter(
+                        IssueSorter.Criteria(IssueSorter.Field.DONE),
+                        IssueSorter.Criteria(IssueSorter.Field.ETA),
+                        IssueSorter.Criteria(IssueSorter.Field.UPDATED, false)
+                    )
+                )
 
             with(http) {
                 pageTitle = project.name
@@ -304,7 +309,7 @@
         }
     }
 
-    private fun obtainIdAndProject(http: HttpRequest, dao:DataAccessObject): Pair<Int, Project>? {
+    private fun obtainIdAndProject(http: HttpRequest, dao: DataAccessObject): Pair<Int, Project>? {
         val id = http.param("id")?.toIntOrNull()
         val projectid = http.param("projectid")?.toIntOrNull() ?: -1
         val project = dao.findProject(projectid)
@@ -326,8 +331,8 @@
             ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
             status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future
             // TODO: process error messages
-            eol =  http.param("eol", ::dateOptValidator, null, mutableListOf())
-            release =  http.param("release", ::dateOptValidator, null, mutableListOf())
+            eol = http.param("eol", ::dateOptValidator, null, mutableListOf())
+            release = http.param("release", ::dateOptValidator, null, mutableListOf())
             // intentional defaults
             if (node.isBlank()) node = name
             // sanitizing
--- a/src/main/kotlin/de/uapcore/lightpit/util/Issues.kt	Sat Jun 04 18:02:25 2022 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,70 +0,0 @@
-/*
- * Copyright 2021 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package de.uapcore.lightpit.util
-
-import de.uapcore.lightpit.entities.Issue
-import de.uapcore.lightpit.types.IssueStatusPhase
-
-data class IssueSorter(val criteria: List<Criteria>) : Comparator<Issue> {
-    enum class Field {
-        DONE, ETA, UPDATED
-    }
-
-    data class Criteria(val field: Field, val asc: Boolean = true)
-
-    companion object {
-        val DEFAULT_ISSUE_SORTER = IssueSorter(listOf(
-            Criteria(Field.DONE),
-            Criteria(Field.ETA),
-            Criteria(Field.UPDATED, false)
-        ))
-    }
-
-    override fun compare(left: Issue, right: Issue): Int {
-        if (left == right) {
-            return 0;
-        }
-        for (c in criteria) {
-            val result = when (c.field) {
-                Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
-                Field.ETA -> {
-                    val l = left.eta
-                    val r = right.eta
-                    if (l == null && r == null) 0
-                    else if (l == null) 1
-                    else if (r == null) -1
-                    else l.compareTo(r)
-                }
-                Field.UPDATED -> left.updated.compareTo(right.updated)
-            }
-            if (result != 0) {
-                return if (c.asc) result else -result
-            }
-        }
-        return 0
-    }
-}
-
--- a/src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt	Sat Jun 04 18:02:25 2022 +0200
+++ b/src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt	Sat Jun 04 18:29:58 2022 +0200
@@ -38,6 +38,38 @@
 import de.uapcore.lightpit.types.VersionStatus
 import kotlin.math.roundToInt
 
+class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
+    enum class Field {
+        DONE, ETA, UPDATED
+    }
+
+    data class Criteria(val field: Field, val asc: Boolean = true)
+
+    override fun compare(left: Issue, right: Issue): Int {
+        if (left == right) {
+            return 0;
+        }
+        for (c in criteria) {
+            val result = when (c.field) {
+                Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
+                Field.ETA -> {
+                    val l = left.eta
+                    val r = right.eta
+                    if (l == null && r == null) 0
+                    else if (l == null) 1
+                    else if (r == null) -1
+                    else l.compareTo(r)
+                }
+                Field.UPDATED -> left.updated.compareTo(right.updated)
+            }
+            if (result != 0) {
+                return if (c.asc) result else -result
+            }
+        }
+        return 0
+    }
+}
+
 class IssueSummary {
     var open = 0
     var active = 0

mercurial