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