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

Thu, 29 Dec 2022 15:04:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 29 Dec 2022 15:04:21 +0100
changeset 260
fb2ae2d63a56
parent 250
ce6d539bb970
child 263
aa22103809cd
permissions
-rw-r--r--

some minor style fixes

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

mercurial