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

Sun, 08 Jan 2023 17:07:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Jan 2023 17:07:26 +0100
changeset 268
ca5501d851fa
parent 267
d8ec2d8ffa82
child 271
f8f5e82944fa
permissions
-rw-r--r--

#15 add issue filters

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@268 34 import de.uapcore.lightpit.HttpRequest
universe@184 35 import de.uapcore.lightpit.entities.*
universe@263 36 import de.uapcore.lightpit.types.*
universe@184 37 import kotlin.math.roundToInt
universe@184 38
universe@249 39 class IssueSorter(private vararg val criteria: Criteria) : Comparator<Issue> {
universe@249 40 enum class Field {
universe@267 41 DONE, PHASE, STATUS, CATEGORY, ETA, UPDATED, CREATED
universe@249 42 }
universe@249 43
universe@249 44 data class Criteria(val field: Field, val asc: Boolean = true)
universe@249 45
universe@249 46 override fun compare(left: Issue, right: Issue): Int {
universe@249 47 if (left == right) {
universe@260 48 return 0
universe@249 49 }
universe@249 50 for (c in criteria) {
universe@249 51 val result = when (c.field) {
universe@267 52 Field.PHASE -> left.status.phase.compareTo(right.status.phase)
universe@267 53 Field.DONE -> (left.status.phase == IssueStatusPhase.Done).compareTo(right.status.phase == IssueStatusPhase.Done)
universe@265 54 Field.STATUS -> left.status.compareTo(right.status)
universe@265 55 Field.CATEGORY -> left.category.compareTo(right.category)
universe@265 56 Field.ETA -> left.compareEtaTo(right.eta)
universe@249 57 Field.UPDATED -> left.updated.compareTo(right.updated)
universe@265 58 Field.CREATED -> left.created.compareTo(right.created)
universe@249 59 }
universe@249 60 if (result != 0) {
universe@249 61 return if (c.asc) result else -result
universe@249 62 }
universe@249 63 }
universe@249 64 return 0
universe@249 65 }
universe@249 66 }
universe@249 67
universe@184 68 class IssueSummary {
universe@184 69 var open = 0
universe@184 70 var active = 0
universe@184 71 var done = 0
universe@184 72
universe@184 73 val total get() = open + active + done
universe@184 74
universe@184 75 val openPercent get() = 100 - activePercent - donePercent
universe@184 76 val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
universe@184 77 val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
universe@184 78
universe@184 79 /**
universe@184 80 * Adds the specified issue to the summary by incrementing the respective counter.
universe@184 81 * @param issue the issue
universe@184 82 */
universe@184 83 fun add(issue: Issue) {
universe@184 84 when (issue.status.phase) {
universe@184 85 IssueStatusPhase.Open -> open++
universe@184 86 IssueStatusPhase.WorkInProgress -> active++
universe@184 87 IssueStatusPhase.Done -> done++
universe@184 88 }
universe@184 89 }
universe@184 90 }
universe@184 91
universe@184 92 class IssueDetailView(
universe@184 93 val issue: Issue,
universe@184 94 val comments: List<IssueComment>,
universe@184 95 val project: Project,
universe@263 96 val version: Version?,
universe@263 97 val component: Component?,
universe@263 98 projectIssues: List<Issue>,
universe@263 99 val currentRelations: List<IssueRelation>,
universe@263 100 /**
universe@263 101 * Optional resource key to an error message for the relation editor.
universe@263 102 */
universe@263 103 val relationError: String?
universe@184 104 ) : View() {
universe@263 105 val relationTypes = RelationType.values()
universe@263 106 val linkableIssues = projectIssues.filterNot { it.id == issue.id }
universe@263 107
universe@234 108 private val parser: Parser
universe@234 109 private val renderer: HtmlRenderer
universe@184 110
universe@184 111 init {
universe@184 112 val options = MutableDataSet()
universe@234 113 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
universe@234 114 parser = Parser.builder(options).build()
universe@268 115 renderer = HtmlRenderer.builder(
universe@268 116 options
universe@268 117 .set(HtmlRenderer.ESCAPE_HTML, true)
universe@234 118 ).build()
universe@184 119
universe@234 120 issue.description = formatMarkdown(issue.description ?: "")
universe@184 121 for (comment in comments) {
universe@234 122 comment.commentFormatted = formatMarkdown(comment.comment)
universe@184 123 }
universe@184 124 }
universe@234 125
universe@234 126 private fun formatEmojis(text: String) = text
universe@234 127 .replace("(/)", "&#9989;")
universe@234 128 .replace("(x)", "&#10060;")
universe@234 129 .replace("(!)", "&#9889;")
universe@234 130
universe@234 131 private fun formatMarkdown(text: String) =
universe@234 132 renderer.render(parser.parse(formatEmojis(text)))
universe@184 133 }
universe@184 134
universe@184 135 class IssueEditView(
universe@184 136 val issue: Issue,
universe@184 137 val versions: List<Version>,
universe@184 138 val components: List<Component>,
universe@184 139 val users: List<User>,
universe@184 140 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
universe@184 141 val version: Version? = null,
universe@184 142 val component: Component? = null
universe@184 143 ) : EditView() {
universe@184 144
universe@184 145 val versionsUpcoming: List<Version>
universe@184 146 val versionsRecent: List<Version>
universe@184 147
universe@184 148 val issueStatus = IssueStatus.values()
universe@184 149 val issueCategory = IssueCategory.values()
universe@184 150
universe@184 151 init {
universe@184 152 val recent = mutableListOf<Version>()
universe@231 153 issue.affected?.let { recent.add(it) }
universe@184 154 val upcoming = mutableListOf<Version>()
universe@231 155 issue.resolved?.let { upcoming.add(it) }
universe@231 156
universe@184 157 for (v in versions) {
universe@184 158 if (v.status.isReleased) {
universe@184 159 if (v.status != VersionStatus.Deprecated) recent.add(v)
universe@184 160 } else {
universe@184 161 upcoming.add(v)
universe@184 162 }
universe@184 163 }
universe@186 164 versionsRecent = recent.distinct()
universe@186 165 versionsUpcoming = upcoming.distinct()
universe@184 166 }
universe@184 167 }
universe@184 168
universe@268 169 class IssueFilter(http: HttpRequest) {
universe@268 170
universe@268 171 val issueStatus = IssueStatus.values()
universe@268 172 val issueCategory = IssueCategory.values()
universe@268 173 val flagIncludeDone = "f.0"
universe@268 174 val flagMine = "f.1"
universe@268 175 val flagBlocker = "f.2"
universe@268 176
universe@268 177 val includeDone: Boolean = evalFlag(http, flagIncludeDone)
universe@268 178 val onlyMine: Boolean = evalFlag(http, flagMine)
universe@268 179 val onlyBlocker: Boolean = evalFlag(http, flagBlocker)
universe@268 180 val status: List<IssueStatus> = evalEnum(http, "s")
universe@268 181 val category: List<IssueCategory> = evalEnum(http, "c")
universe@268 182
universe@268 183 private fun evalFlag(http: HttpRequest, name: String): Boolean {
universe@268 184 val param = http.paramArray("filter")
universe@268 185 if (param.isNotEmpty()) {
universe@268 186 if (param.contains(name)) {
universe@268 187 http.session.setAttribute(name, true)
universe@268 188 } else {
universe@268 189 http.session.removeAttribute(name)
universe@268 190 }
universe@268 191 }
universe@268 192 return http.session.getAttribute(name) != null
universe@268 193 }
universe@268 194
universe@268 195 private inline fun <reified T : Enum<T>> evalEnum(http: HttpRequest, prefix: String): List<T> {
universe@268 196 val sattr = "f.${prefix}"
universe@268 197 val param = http.paramArray("filter")
universe@268 198 if (param.isNotEmpty()) {
universe@268 199 val list = param.filter { it.startsWith("${prefix}.") }
universe@268 200 .map { it.substring(prefix.length + 1) }
universe@268 201 .map {
universe@268 202 try {
universe@268 203 // quick and very dirty validation
universe@268 204 enumValueOf<T>(it)
universe@268 205 } catch (_: IllegalArgumentException) {
universe@268 206 // skip
universe@268 207 }
universe@268 208 }
universe@268 209 if (list.isEmpty()) {
universe@268 210 http.session.removeAttribute(sattr)
universe@268 211 } else {
universe@268 212 http.session.setAttribute(sattr, list.joinToString(","))
universe@268 213 }
universe@268 214 }
universe@268 215
universe@268 216 return http.session.getAttribute(sattr)
universe@268 217 ?.toString()
universe@268 218 ?.split(",")
universe@268 219 ?.map { enumValueOf(it) }
universe@268 220 ?: emptyList()
universe@268 221 }
universe@268 222 }

mercurial