Tue, 03 Jan 2023 17:04:39 +0100
add more possible 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 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.STATUS -> left.status.compareTo(right.status)
52 Field.CATEGORY -> left.category.compareTo(right.category)
53 Field.ETA -> left.compareEtaTo(right.eta)
54 Field.UPDATED -> left.updated.compareTo(right.updated)
55 Field.CREATED -> left.created.compareTo(right.created)
56 }
57 if (result != 0) {
58 return if (c.asc) result else -result
59 }
60 }
61 return 0
62 }
63 }
65 class IssueSummary {
66 var open = 0
67 var active = 0
68 var done = 0
70 val total get() = open + active + done
72 val openPercent get() = 100 - activePercent - donePercent
73 val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
74 val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
76 /**
77 * Adds the specified issue to the summary by incrementing the respective counter.
78 * @param issue the issue
79 */
80 fun add(issue: Issue) {
81 when (issue.status.phase) {
82 IssueStatusPhase.Open -> open++
83 IssueStatusPhase.WorkInProgress -> active++
84 IssueStatusPhase.Done -> done++
85 }
86 }
87 }
89 class IssueDetailView(
90 val issue: Issue,
91 val comments: List<IssueComment>,
92 val project: Project,
93 val version: Version?,
94 val component: Component?,
95 projectIssues: List<Issue>,
96 val currentRelations: List<IssueRelation>,
97 /**
98 * Optional resource key to an error message for the relation editor.
99 */
100 val relationError: String?
101 ) : View() {
102 val relationTypes = RelationType.values()
103 val linkableIssues = projectIssues.filterNot { it.id == issue.id }
105 private val parser: Parser
106 private val renderer: HtmlRenderer
108 init {
109 val options = MutableDataSet()
110 .set(SharedDataKeys.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
111 parser = Parser.builder(options).build()
112 renderer = HtmlRenderer.builder(options
113 .set(HtmlRenderer.ESCAPE_HTML, true)
114 ).build()
116 issue.description = formatMarkdown(issue.description ?: "")
117 for (comment in comments) {
118 comment.commentFormatted = formatMarkdown(comment.comment)
119 }
120 }
122 private fun formatEmojis(text: String) = text
123 .replace("(/)", "✅")
124 .replace("(x)", "❌")
125 .replace("(!)", "⚡")
127 private fun formatMarkdown(text: String) =
128 renderer.render(parser.parse(formatEmojis(text)))
129 }
131 class IssueEditView(
132 val issue: Issue,
133 val versions: List<Version>,
134 val components: List<Component>,
135 val users: List<User>,
136 val project: Project, // TODO: allow null values to create issues from the IssuesServlet
137 val version: Version? = null,
138 val component: Component? = null
139 ) : EditView() {
141 val versionsUpcoming: List<Version>
142 val versionsRecent: List<Version>
144 val issueStatus = IssueStatus.values()
145 val issueCategory = IssueCategory.values()
147 init {
148 val recent = mutableListOf<Version>()
149 issue.affected?.let { recent.add(it) }
150 val upcoming = mutableListOf<Version>()
151 issue.resolved?.let { upcoming.add(it) }
153 for (v in versions) {
154 if (v.status.isReleased) {
155 if (v.status != VersionStatus.Deprecated) recent.add(v)
156 } else {
157 upcoming.add(v)
158 }
159 }
160 versionsRecent = recent.distinct()
161 versionsUpcoming = upcoming.distinct()
162 }
163 }