src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt

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 234
d71bc6db42ef
child 250
ce6d539bb970
permissions
-rw-r--r--

move IssueSorter to viewmodel package

     1 /*
     2  * Copyright 2021 Mike Becker. All rights reserved.
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions are met:
     6  *
     7  * 1. Redistributions of source code must retain the above copyright
     8  * notice, this list of conditions and the following disclaimer.
     9  *
    10  * 2. Redistributions in binary form must reproduce the above copyright
    11  * notice, this list of conditions and the following disclaimer in the
    12  * documentation and/or other materials provided with the distribution.
    13  *
    14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    24  */
    26 package de.uapcore.lightpit.viewmodel
    28 import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
    29 import com.vladsch.flexmark.ext.tables.TablesExtension
    30 import com.vladsch.flexmark.html.HtmlRenderer
    31 import com.vladsch.flexmark.parser.Parser
    32 import com.vladsch.flexmark.util.data.MutableDataSet
    33 import com.vladsch.flexmark.util.data.SharedDataKeys
    34 import de.uapcore.lightpit.entities.*
    35 import de.uapcore.lightpit.types.IssueCategory
    36 import de.uapcore.lightpit.types.IssueStatus
    37 import de.uapcore.lightpit.types.IssueStatusPhase
    38 import de.uapcore.lightpit.types.VersionStatus
    39 import kotlin.math.roundToInt
    41 class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
    42     enum class Field {
    43         DONE, ETA, UPDATED
    44     }
    46     data class Criteria(val field: Field, val asc: Boolean = true)
    48     override fun compare(left: Issue, right: Issue): Int {
    49         if (left == right) {
    50             return 0;
    51         }
    52         for (c in criteria) {
    53             val result = when (c.field) {
    54                 Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
    55                 Field.ETA -> {
    56                     val l = left.eta
    57                     val r = right.eta
    58                     if (l == null && r == null) 0
    59                     else if (l == null) 1
    60                     else if (r == null) -1
    61                     else l.compareTo(r)
    62                 }
    63                 Field.UPDATED -> left.updated.compareTo(right.updated)
    64             }
    65             if (result != 0) {
    66                 return if (c.asc) result else -result
    67             }
    68         }
    69         return 0
    70     }
    71 }
    73 class IssueSummary {
    74     var open = 0
    75     var active = 0
    76     var done = 0
    78     val total get() = open + active + done
    80     val openPercent get() = 100 - activePercent - donePercent
    81     val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
    82     val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
    84     /**
    85      * Adds the specified issue to the summary by incrementing the respective counter.
    86      * @param issue the issue
    87      */
    88     fun add(issue: Issue) {
    89         when (issue.status.phase) {
    90             IssueStatusPhase.Open -> open++
    91             IssueStatusPhase.WorkInProgress -> active++
    92             IssueStatusPhase.Done -> done++
    93         }
    94     }
    95 }
    97 class IssueDetailView(
    98     val issue: Issue,
    99     val comments: List<IssueComment>,
   100     val project: Project,
   101     val version: Version? = null,
   102     val component: Component? = null
   103 ) : View() {
   104     private val parser: Parser
   105     private val renderer: HtmlRenderer
   107     init {
   108         val options = MutableDataSet()
   109             .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
   110         parser = Parser.builder(options).build()
   111         renderer = HtmlRenderer.builder(options
   112             .set(HtmlRenderer.ESCAPE_HTML, true)
   113         ).build()
   115         issue.description = formatMarkdown(issue.description ?: "")
   116         for (comment in comments) {
   117             comment.commentFormatted = formatMarkdown(comment.comment)
   118         }
   119     }
   121     private fun formatEmojis(text: String) = text
   122         .replace("(/)", "&#9989;")
   123         .replace("(x)", "&#10060;")
   124         .replace("(!)", "&#9889;")
   126     private fun formatMarkdown(text: String) =
   127         renderer.render(parser.parse(formatEmojis(text)))
   128 }
   130 class IssueEditView(
   131     val issue: Issue,
   132     val versions: List<Version>,
   133     val components: List<Component>,
   134     val users: List<User>,
   135     val project: Project, // TODO: allow null values to create issues from the IssuesServlet
   136     val version: Version? = null,
   137     val component: Component? = null
   138 ) : EditView() {
   140     val versionsUpcoming: List<Version>
   141     val versionsRecent: List<Version>
   143     val issueStatus = IssueStatus.values()
   144     val issueCategory = IssueCategory.values()
   146     init {
   147         val recent = mutableListOf<Version>()
   148         issue.affected?.let { recent.add(it) }
   149         val upcoming = mutableListOf<Version>()
   150         issue.resolved?.let { upcoming.add(it) }
   152         for (v in versions) {
   153             if (v.status.isReleased) {
   154                 if (v.status != VersionStatus.Deprecated) recent.add(v)
   155             } else {
   156                 upcoming.add(v)
   157             }
   158         }
   159         versionsRecent = recent.distinct()
   160         versionsUpcoming = upcoming.distinct()
   161     }
   162 }

mercurial