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

changeset 184
e8eecee6aadf
child 186
05eec764facd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/kotlin/de/uapcore/lightpit/viewmodel/Issues.kt	Fri Apr 02 11:59:14 2021 +0200
     1.3 @@ -0,0 +1,118 @@
     1.4 +/*
     1.5 + * Copyright 2021 Mike Becker. All rights reserved.
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions are met:
     1.9 + *
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + * notice, this list of conditions and the following disclaimer.
    1.12 + *
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + * notice, this list of conditions and the following disclaimer in the
    1.15 + * documentation and/or other materials provided with the distribution.
    1.16 + *
    1.17 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.18 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.19 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    1.20 + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    1.21 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.22 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.23 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    1.24 + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.25 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.26 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.27 + */
    1.28 +
    1.29 +package de.uapcore.lightpit.viewmodel
    1.30 +
    1.31 +import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughExtension
    1.32 +import com.vladsch.flexmark.ext.tables.TablesExtension
    1.33 +import com.vladsch.flexmark.html.HtmlRenderer
    1.34 +import com.vladsch.flexmark.parser.Parser
    1.35 +import com.vladsch.flexmark.util.data.MutableDataSet
    1.36 +import de.uapcore.lightpit.entities.*
    1.37 +import de.uapcore.lightpit.types.IssueCategory
    1.38 +import de.uapcore.lightpit.types.IssueStatus
    1.39 +import de.uapcore.lightpit.types.IssueStatusPhase
    1.40 +import de.uapcore.lightpit.types.VersionStatus
    1.41 +import kotlin.math.roundToInt
    1.42 +
    1.43 +class IssueSummary {
    1.44 +    var open = 0
    1.45 +    var active = 0
    1.46 +    var done = 0
    1.47 +
    1.48 +    val total get() = open + active + done
    1.49 +
    1.50 +    val openPercent get() = 100 - activePercent - donePercent
    1.51 +    val activePercent get() = if (total > 0) (100f * active / total).roundToInt() else 0
    1.52 +    val donePercent get() = if (total > 0) (100f * done / total).roundToInt() else 100
    1.53 +
    1.54 +    /**
    1.55 +     * Adds the specified issue to the summary by incrementing the respective counter.
    1.56 +     * @param issue the issue
    1.57 +     */
    1.58 +    fun add(issue: Issue) {
    1.59 +        when (issue.status.phase) {
    1.60 +            IssueStatusPhase.Open -> open++
    1.61 +            IssueStatusPhase.WorkInProgress -> active++
    1.62 +            IssueStatusPhase.Done -> done++
    1.63 +        }
    1.64 +    }
    1.65 +}
    1.66 +
    1.67 +class IssueDetailView(
    1.68 +    val issue: Issue,
    1.69 +    val comments: List<IssueComment>,
    1.70 +    val project: Project,
    1.71 +    val version: Version? = null,
    1.72 +    val component: Component? = null
    1.73 +) : View() {
    1.74 +
    1.75 +    init {
    1.76 +        val options = MutableDataSet()
    1.77 +            .set(Parser.EXTENSIONS, listOf(TablesExtension.create(), StrikethroughExtension.create()))
    1.78 +        val parser = Parser.builder(options).build()
    1.79 +        val renderer = HtmlRenderer.builder(options).build()
    1.80 +        val process = fun(it: String) = renderer.render(parser.parse(it))
    1.81 +
    1.82 +        issue.description = process(issue.description)
    1.83 +        for (comment in comments) {
    1.84 +            comment.comment = process(comment.comment)
    1.85 +        }
    1.86 +    }
    1.87 +}
    1.88 +
    1.89 +class IssueEditView(
    1.90 +    val issue: Issue,
    1.91 +    val versions: List<Version>,
    1.92 +    val components: List<Component>,
    1.93 +    val users: List<User>,
    1.94 +    val project: Project, // TODO: allow null values to create issues from the IssuesServlet
    1.95 +    val version: Version? = null,
    1.96 +    val component: Component? = null
    1.97 +) : EditView() {
    1.98 +
    1.99 +    val versionsUpcoming: List<Version>
   1.100 +    val versionsRecent: List<Version>
   1.101 +
   1.102 +    val issueStatus = IssueStatus.values()
   1.103 +    val issueCategory = IssueCategory.values()
   1.104 +
   1.105 +    init {
   1.106 +        val recent = mutableListOf<Version>()
   1.107 +        val upcoming = mutableListOf<Version>()
   1.108 +        recent.addAll(issue.affectedVersions)
   1.109 +        upcoming.addAll(issue.resolvedVersions)
   1.110 +        for (v in versions) {
   1.111 +            if (v.status.isReleased) {
   1.112 +                if (v.status != VersionStatus.Deprecated) recent.add(v)
   1.113 +            } else {
   1.114 +                upcoming.add(v)
   1.115 +            }
   1.116 +        }
   1.117 +        versionsRecent = recent
   1.118 +        versionsUpcoming = upcoming
   1.119 +    }
   1.120 +}
   1.121 +

mercurial