Sat, 09 Oct 2021 20:05:39 +0200
change rss feed to display the issue history
TODO: diffs and comments
universe@195 | 1 | /* |
universe@195 | 2 | * Copyright 2021 Mike Becker. All rights reserved. |
universe@195 | 3 | * |
universe@195 | 4 | * Redistribution and use in source and binary forms, with or without |
universe@195 | 5 | * modification, are permitted provided that the following conditions are met: |
universe@195 | 6 | * |
universe@195 | 7 | * 1. Redistributions of source code must retain the above copyright |
universe@195 | 8 | * notice, this list of conditions and the following disclaimer. |
universe@195 | 9 | * |
universe@195 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@195 | 11 | * notice, this list of conditions and the following disclaimer in the |
universe@195 | 12 | * documentation and/or other materials provided with the distribution. |
universe@195 | 13 | * |
universe@195 | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@195 | 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@195 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
universe@195 | 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
universe@195 | 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
universe@195 | 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
universe@195 | 20 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
universe@195 | 21 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
universe@195 | 22 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
universe@195 | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
universe@195 | 24 | */ |
universe@195 | 25 | |
universe@195 | 26 | package de.uapcore.lightpit.servlet |
universe@195 | 27 | |
universe@195 | 28 | import de.uapcore.lightpit.AbstractServlet |
universe@195 | 29 | import de.uapcore.lightpit.HttpRequest |
universe@195 | 30 | import de.uapcore.lightpit.dao.DataAccessObject |
universe@235 | 31 | import de.uapcore.lightpit.entities.IssueHistoryData |
universe@235 | 32 | import de.uapcore.lightpit.entities.IssueHistoryEntry |
universe@235 | 33 | import de.uapcore.lightpit.viewmodel.IssueDiff |
universe@195 | 34 | import de.uapcore.lightpit.viewmodel.IssueFeed |
universe@235 | 35 | import de.uapcore.lightpit.viewmodel.IssueFeedEntry |
universe@235 | 36 | import java.text.SimpleDateFormat |
universe@195 | 37 | import javax.servlet.annotation.WebServlet |
universe@195 | 38 | |
universe@195 | 39 | @WebServlet(urlPatterns = ["/feed/*"]) |
universe@195 | 40 | class FeedServlet : AbstractServlet() { |
universe@195 | 41 | |
universe@195 | 42 | init { |
universe@198 | 43 | get("/%project/issues.rss", this::issues) |
universe@198 | 44 | } |
universe@198 | 45 | |
universe@235 | 46 | private fun String.convertLF() = replace("\r", "").replace("\n", "<br>") |
universe@235 | 47 | |
universe@235 | 48 | private fun fullContent(issue: IssueHistoryData) = IssueDiff( |
universe@235 | 49 | issue.id, |
universe@235 | 50 | issue.subject, |
universe@235 | 51 | issue.component, |
universe@235 | 52 | issue.status.name, |
universe@235 | 53 | issue.category.name, |
universe@235 | 54 | issue.subject, |
universe@235 | 55 | issue.description.convertLF(), |
universe@235 | 56 | issue.assignee, |
universe@235 | 57 | issue.eta?.let { SimpleDateFormat("dd.MM.yyyy").format(it) } ?: "", |
universe@235 | 58 | issue.affected, |
universe@235 | 59 | issue.resolved |
universe@235 | 60 | ) |
universe@235 | 61 | |
universe@235 | 62 | private fun diffContent(cur: IssueHistoryData, next: IssueHistoryData): IssueDiff { |
universe@235 | 63 | val prev = fullContent(next) |
universe@235 | 64 | val diff = fullContent(cur) |
universe@235 | 65 | // TODO: compute and apply diff |
universe@235 | 66 | return diff |
universe@235 | 67 | } |
universe@235 | 68 | |
universe@235 | 69 | /** |
universe@235 | 70 | * Generates the feed entries. |
universe@235 | 71 | * Assumes that [historyEntry] is already sorted by timestamp (descending). |
universe@235 | 72 | */ |
universe@235 | 73 | private fun generateFeedEntries(historyEntry: List<IssueHistoryEntry>) = |
universe@235 | 74 | if (historyEntry.isEmpty()) emptyList() |
universe@235 | 75 | else historyEntry.zipWithNext().map { (cur, next) -> |
universe@235 | 76 | IssueFeedEntry( |
universe@235 | 77 | cur.time, cur.type, diffContent(cur.data, next.data) |
universe@235 | 78 | ) |
universe@235 | 79 | }.plus( |
universe@235 | 80 | historyEntry.last().let { IssueFeedEntry(it.time, it.type, fullContent(it.data)) } |
universe@235 | 81 | ) |
universe@235 | 82 | |
universe@195 | 83 | private fun issues(http: HttpRequest, dao: DataAccessObject) { |
universe@198 | 84 | val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) } |
universe@198 | 85 | if (project == null) { |
universe@198 | 86 | http.response.sendError(404) |
universe@198 | 87 | return |
universe@198 | 88 | } |
universe@195 | 89 | |
universe@235 | 90 | val days = http.param("days")?.toIntOrNull() ?: 30 |
universe@195 | 91 | |
universe@235 | 92 | val issueHistory = dao.listIssueHistory(project.id, days) |
universe@235 | 93 | // TODO: add comment history depending on parameter |
universe@235 | 94 | |
universe@235 | 95 | http.view = IssueFeed(project, generateFeedEntries(issueHistory)) |
universe@198 | 96 | http.renderFeed("issues-feed") |
universe@195 | 97 | } |
universe@195 | 98 | } |