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

Tue, 03 Jan 2023 18:25:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 03 Jan 2023 18:25:51 +0100
changeset 267
d8ec2d8ffa82
parent 265
6a21bb926e02
child 268
ca5501d851fa
permissions
-rw-r--r--

fix default sort criteria

     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.*
    36 import kotlin.math.roundToInt
    38 class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
    39     enum class Field {
    40         DONE, PHASE, STATUS, CATEGORY, ETA, UPDATED, CREATED
    41     }
    43     data class Criteria(val field: Field, val asc: Boolean = true)
    45     override fun compare(left: Issue, right: Issue): Int {
    46         if (left == right) {
    47             return 0
    48         }
    49         for (c in criteria) {
    50             val result = when (c.field) {
    51                 Field.PHASE -> left.status.phase.compareTo(right.status.phase)
    52                 Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
    53                 Field.STATUS -> left.status.compareTo(right.status)
    54                 Field.CATEGORY -> left.category.compareTo(right.category)
    55                 Field.ETA -> left.compareEtaTo(right.eta)
    56                 Field.UPDATED -> left.updated.compareTo(right.updated)
    57                 Field.CREATED -> left.created.compareTo(right.created)
    58             }
    59             if (result != 0) {
    60                 return if (c.asc) result else -result
    61             }
    62         }
    63         return 0
    64     }
    65 }
    67 class IssueSummary {
    68     var open = 0
    69     var active = 0
    70     var done = 0
    72     val total get() = open + active + done
    74     val openPercent get() = 100 - activePercent - donePercent
    75     val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
    76     val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
    78     /**
    79      * Adds the specified issue to the summary by incrementing the respective counter.
    80      * @param issue the issue
    81      */
    82     fun add(issue: Issue) {
    83         when (issue.status.phase) {
    84             IssueStatusPhase.Open -> open++
    85             IssueStatusPhase.WorkInProgress -> active++
    86             IssueStatusPhase.Done -> done++
    87         }
    88     }
    89 }
    91 class IssueDetailView(
    92     val issue: Issue,
    93     val comments: List<IssueComment>,
    94     val project: Project,
    95     val version: Version?,
    96     val component: Component?,
    97     projectIssues: List<Issue>,
    98     val currentRelations: List<IssueRelation>,
    99     /**
   100      * Optional resource key to an error message for the relation editor.
   101      */
   102     val relationError: String?
   103 ) : View() {
   104     val relationTypes = RelationType.values()
   105     val linkableIssues = projectIssues.filterNot { it.id == issue.id }
   107     private val parser: Parser
   108     private val renderer: HtmlRenderer
   110     init {
   111         val options = MutableDataSet()
   112             .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
   113         parser = Parser.builder(options).build()
   114         renderer = HtmlRenderer.builder(options
   115             .set(HtmlRenderer.ESCAPE_HTML, true)
   116         ).build()
   118         issue.description = formatMarkdown(issue.description ?: "")
   119         for (comment in comments) {
   120             comment.commentFormatted = formatMarkdown(comment.comment)
   121         }
   122     }
   124     private fun formatEmojis(text: String) = text
   125         .replace("(/)", "&#9989;")
   126         .replace("(x)", "&#10060;")
   127         .replace("(!)", "&#9889;")
   129     private fun formatMarkdown(text: String) =
   130         renderer.render(parser.parse(formatEmojis(text)))
   131 }
   133 class IssueEditView(
   134     val issue: Issue,
   135     val versions: List<Version>,
   136     val components: List<Component>,
   137     val users: List<User>,
   138     val project: Project, // TODO: allow null values to create issues from the IssuesServlet
   139     val version: Version? = null,
   140     val component: Component? = null
   141 ) : EditView() {
   143     val versionsUpcoming: List<Version>
   144     val versionsRecent: List<Version>
   146     val issueStatus = IssueStatus.values()
   147     val issueCategory = IssueCategory.values()
   149     init {
   150         val recent = mutableListOf<Version>()
   151         issue.affected?.let { recent.add(it) }
   152         val upcoming = mutableListOf<Version>()
   153         issue.resolved?.let { upcoming.add(it) }
   155         for (v in versions) {
   156             if (v.status.isReleased) {
   157                 if (v.status != VersionStatus.Deprecated) recent.add(v)
   158             } else {
   159                 upcoming.add(v)
   160             }
   161         }
   162         versionsRecent = recent.distinct()
   163         versionsUpcoming = upcoming.distinct()
   164     }
   165 }

mercurial