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

Sun, 08 Jan 2023 19:32:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Jan 2023 19:32:11 +0100
changeset 271
f8f5e82944fa
parent 268
ca5501d851fa
child 284
671c1c8fbf1c
permissions
-rw-r--r--

#15 add sort options

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

mercurial