src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt

Sat, 27 Nov 2021 13:03:57 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 27 Nov 2021 13:03:57 +0100
changeset 242
b7f3e972b13c
parent 232
296e12ff8d1c
child 247
e71ae69c68c0
permissions
-rw-r--r--

#109 add comment history

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.servlet
universe@184 27
universe@232 28 import de.uapcore.lightpit.*
universe@184 29 import de.uapcore.lightpit.dao.DataAccessObject
universe@184 30 import de.uapcore.lightpit.entities.*
universe@184 31 import de.uapcore.lightpit.types.IssueCategory
universe@184 32 import de.uapcore.lightpit.types.IssueStatus
universe@184 33 import de.uapcore.lightpit.types.VersionStatus
universe@184 34 import de.uapcore.lightpit.types.WebColor
universe@184 35 import de.uapcore.lightpit.util.AllFilter
universe@184 36 import de.uapcore.lightpit.util.IssueFilter
universe@193 37 import de.uapcore.lightpit.util.IssueSorter.Companion.DEFAULT_ISSUE_SORTER
universe@184 38 import de.uapcore.lightpit.util.SpecificFilter
universe@184 39 import de.uapcore.lightpit.viewmodel.*
universe@184 40 import java.sql.Date
universe@184 41 import javax.servlet.annotation.WebServlet
universe@184 42
universe@184 43 @WebServlet(urlPatterns = ["/projects/*"])
universe@184 44 class ProjectServlet : AbstractServlet() {
universe@184 45
universe@184 46 init {
universe@184 47 get("/", this::projects)
universe@184 48 get("/%project", this::project)
universe@184 49 get("/%project/issues/%version/%component/", this::project)
universe@184 50 get("/%project/edit", this::projectForm)
universe@184 51 get("/-/create", this::projectForm)
universe@184 52 post("/-/commit", this::projectCommit)
universe@184 53
universe@184 54 get("/%project/versions/", this::versions)
universe@184 55 get("/%project/versions/%version/edit", this::versionForm)
universe@184 56 get("/%project/versions/-/create", this::versionForm)
universe@184 57 post("/%project/versions/-/commit", this::versionCommit)
universe@184 58
universe@184 59 get("/%project/components/", this::components)
universe@184 60 get("/%project/components/%component/edit", this::componentForm)
universe@184 61 get("/%project/components/-/create", this::componentForm)
universe@184 62 post("/%project/components/-/commit", this::componentCommit)
universe@184 63
universe@184 64 get("/%project/issues/%version/%component/%issue", this::issue)
universe@184 65 get("/%project/issues/%version/%component/%issue/edit", this::issueForm)
universe@186 66 post("/%project/issues/%version/%component/%issue/comment", this::issueComment)
universe@184 67 get("/%project/issues/%version/%component/-/create", this::issueForm)
universe@186 68 post("/%project/issues/%version/%component/-/commit", this::issueCommit)
universe@184 69 }
universe@184 70
universe@193 71 private fun projects(http: HttpRequest, dao: DataAccessObject) {
universe@184 72 val projects = dao.listProjects()
universe@184 73 val projectInfos = projects.map {
universe@184 74 ProjectInfo(
universe@184 75 project = it,
universe@184 76 versions = dao.listVersions(it),
universe@184 77 components = emptyList(), // not required in this view
universe@184 78 issueSummary = dao.collectIssueSummary(it)
universe@184 79 )
universe@184 80 }
universe@184 81
universe@184 82 with(http) {
universe@184 83 view = ProjectsView(projectInfos)
universe@184 84 navigationMenu = projectNavMenu(projects)
universe@184 85 styleSheets = listOf("projects")
universe@184 86 render("projects")
universe@184 87 }
universe@184 88 }
universe@184 89
universe@184 90 private fun activeProjectNavMenu(
universe@184 91 projects: List<Project>,
universe@184 92 projectInfo: ProjectInfo,
universe@184 93 selectedVersion: Version? = null,
universe@184 94 selectedComponent: Component? = null
universe@184 95 ) =
universe@184 96 projectNavMenu(
universe@184 97 projects,
universe@184 98 projectInfo.versions,
universe@184 99 projectInfo.components,
universe@184 100 projectInfo.project,
universe@184 101 selectedVersion,
universe@184 102 selectedComponent
universe@184 103 )
universe@184 104
universe@210 105 private sealed interface LookupResult<T>
universe@210 106 private class NotFound<T> : LookupResult<T>
universe@210 107 private data class Found<T>(val elem: T?) : LookupResult<T>
universe@184 108
universe@184 109 private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> {
universe@184 110 val node = pathParams[paramName]
universe@184 111 return if (node == null || node == "-") {
universe@210 112 Found(null)
universe@184 113 } else {
universe@184 114 val result = list.find { it.node == node }
universe@184 115 if (result == null) {
universe@210 116 NotFound()
universe@184 117 } else {
universe@210 118 Found(result)
universe@184 119 }
universe@184 120 }
universe@184 121 }
universe@184 122
universe@184 123 private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? {
universe@184 124 val project = dao.findProjectByNode(http.pathParams["project"] ?: "") ?: return null
universe@184 125
universe@184 126 val versions: List<Version> = dao.listVersions(project)
universe@184 127 val components: List<Component> = dao.listComponents(project)
universe@184 128
universe@184 129 return ProjectInfo(
universe@184 130 project,
universe@184 131 versions,
universe@184 132 components,
universe@184 133 dao.collectIssueSummary(project)
universe@184 134 )
universe@184 135 }
universe@184 136
universe@184 137 private fun sanitizeNode(name: String): String {
universe@184 138 val san = name.replace(Regex("[/\\\\]"), "-")
universe@184 139 return if (san.startsWith(".")) {
universe@184 140 "v$san"
universe@184 141 } else {
universe@184 142 san
universe@184 143 }
universe@184 144 }
universe@184 145
universe@198 146 private fun feedPath(project: Project) = "feed/${project.node}/issues.rss"
universe@198 147
universe@210 148 private data class PathInfos(
universe@184 149 val projectInfo: ProjectInfo,
universe@184 150 val version: Version?,
universe@184 151 val component: Component?
universe@184 152 ) {
universe@198 153 val project = projectInfo.project
universe@198 154 val issuesHref by lazyOf("projects/${project.node}/issues/${version?.node ?: "-"}/${component?.node ?: "-"}/")
universe@184 155 }
universe@184 156
universe@184 157 private fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? {
universe@184 158 val projectInfo = obtainProjectInfo(http, dao)
universe@184 159 if (projectInfo == null) {
universe@184 160 http.response.sendError(404)
universe@184 161 return null
universe@184 162 }
universe@184 163
universe@184 164 val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) {
universe@210 165 is NotFound -> {
universe@184 166 http.response.sendError(404)
universe@184 167 return null
universe@184 168 }
universe@210 169 is Found -> {
universe@184 170 result.elem
universe@184 171 }
universe@184 172 }
universe@184 173 val component = when (val result = http.lookupPathParam("component", projectInfo.components)) {
universe@210 174 is NotFound -> {
universe@184 175 http.response.sendError(404)
universe@184 176 return null
universe@184 177 }
universe@210 178 is Found -> {
universe@184 179 result.elem
universe@184 180 }
universe@184 181 }
universe@184 182
universe@184 183 return PathInfos(projectInfo, version, component)
universe@184 184 }
universe@184 185
universe@193 186 private fun project(http: HttpRequest, dao: DataAccessObject) {
universe@184 187 withPathInfo(http, dao)?.run {
universe@193 188
universe@184 189 val issues = dao.listIssues(IssueFilter(
universe@198 190 project = SpecificFilter(project),
universe@184 191 version = version?.let { SpecificFilter(it) } ?: AllFilter(),
universe@184 192 component = component?.let { SpecificFilter(it) } ?: AllFilter()
universe@193 193 )).sortedWith(DEFAULT_ISSUE_SORTER)
universe@184 194
universe@184 195 with(http) {
universe@205 196 pageTitle = project.name
universe@184 197 view = ProjectDetails(projectInfo, issues, version, component)
universe@198 198 feedPath = feedPath(project)
universe@184 199 navigationMenu = activeProjectNavMenu(
universe@184 200 dao.listProjects(),
universe@184 201 projectInfo,
universe@184 202 version,
universe@184 203 component
universe@184 204 )
universe@184 205 styleSheets = listOf("projects")
universe@184 206 render("project-details")
universe@184 207 }
universe@184 208 }
universe@184 209 }
universe@184 210
universe@193 211 private fun projectForm(http: HttpRequest, dao: DataAccessObject) {
universe@200 212 if (!http.pathParams.containsKey("project")) {
universe@200 213 http.view = ProjectEditView(Project(-1), dao.listUsers())
universe@200 214 http.navigationMenu = projectNavMenu(dao.listProjects())
universe@200 215 } else {
universe@200 216 val projectInfo = obtainProjectInfo(http, dao)
universe@200 217 if (projectInfo == null) {
universe@200 218 http.response.sendError(404)
universe@200 219 return
universe@200 220 }
universe@200 221 http.view = ProjectEditView(projectInfo.project, dao.listUsers())
universe@200 222 http.navigationMenu = activeProjectNavMenu(
universe@184 223 dao.listProjects(),
universe@184 224 projectInfo
universe@184 225 )
universe@184 226 }
universe@200 227 http.styleSheets = listOf("projects")
universe@200 228 http.render("project-form")
universe@184 229 }
universe@184 230
universe@193 231 private fun projectCommit(http: HttpRequest, dao: DataAccessObject) {
universe@184 232 val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply {
universe@184 233 name = http.param("name") ?: ""
universe@184 234 node = http.param("node") ?: ""
universe@184 235 description = http.param("description") ?: ""
universe@184 236 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
universe@184 237 repoUrl = http.param("repoUrl") ?: ""
universe@184 238 owner = (http.param("owner")?.toIntOrNull() ?: -1).let {
universe@184 239 if (it < 0) null else dao.findUser(it)
universe@184 240 }
universe@184 241 // intentional defaults
universe@184 242 if (node.isBlank()) node = name
universe@184 243 // sanitizing
universe@184 244 node = sanitizeNode(node)
universe@184 245 }
universe@184 246
universe@184 247 if (project.id < 0) {
universe@184 248 dao.insertProject(project)
universe@184 249 } else {
universe@184 250 dao.updateProject(project)
universe@184 251 }
universe@184 252
universe@184 253 http.renderCommit("projects/${project.node}")
universe@184 254 }
universe@184 255
universe@193 256 private fun versions(http: HttpRequest, dao: DataAccessObject) {
universe@184 257 val projectInfo = obtainProjectInfo(http, dao)
universe@184 258 if (projectInfo == null) {
universe@184 259 http.response.sendError(404)
universe@184 260 return
universe@184 261 }
universe@184 262
universe@184 263 with(http) {
universe@205 264 pageTitle = "${projectInfo.project.name} - ${i18n("navmenu.versions")}"
universe@184 265 view = VersionsView(
universe@184 266 projectInfo,
universe@184 267 dao.listVersionSummaries(projectInfo.project)
universe@184 268 )
universe@198 269 feedPath = feedPath(projectInfo.project)
universe@184 270 navigationMenu = activeProjectNavMenu(
universe@184 271 dao.listProjects(),
universe@184 272 projectInfo
universe@184 273 )
universe@184 274 styleSheets = listOf("projects")
universe@184 275 render("versions")
universe@184 276 }
universe@184 277 }
universe@184 278
universe@193 279 private fun versionForm(http: HttpRequest, dao: DataAccessObject) {
universe@184 280 val projectInfo = obtainProjectInfo(http, dao)
universe@184 281 if (projectInfo == null) {
universe@184 282 http.response.sendError(404)
universe@184 283 return
universe@184 284 }
universe@184 285
universe@184 286 val version: Version
universe@184 287 when (val result = http.lookupPathParam("version", projectInfo.versions)) {
universe@210 288 is NotFound -> {
universe@184 289 http.response.sendError(404)
universe@184 290 return
universe@184 291 }
universe@210 292 is Found -> {
universe@184 293 version = result.elem ?: Version(-1, projectInfo.project.id)
universe@184 294 }
universe@184 295 }
universe@184 296
universe@184 297 with(http) {
universe@184 298 view = VersionEditView(projectInfo, version)
universe@198 299 feedPath = feedPath(projectInfo.project)
universe@184 300 navigationMenu = activeProjectNavMenu(
universe@184 301 dao.listProjects(),
universe@184 302 projectInfo,
universe@184 303 selectedVersion = version
universe@184 304 )
universe@184 305 styleSheets = listOf("projects")
universe@184 306 render("version-form")
universe@184 307 }
universe@184 308 }
universe@184 309
universe@210 310 private fun obtainIdAndProject(http: HttpRequest, dao:DataAccessObject): Pair<Int, Project>? {
universe@184 311 val id = http.param("id")?.toIntOrNull()
universe@184 312 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
universe@184 313 val project = dao.findProject(projectid)
universe@210 314 return if (id == null || project == null) {
universe@184 315 http.response.sendError(400)
universe@210 316 null
universe@210 317 } else {
universe@210 318 Pair(id, project)
universe@184 319 }
universe@210 320 }
universe@184 321
universe@210 322 private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
universe@210 323 val idParams = obtainIdAndProject(http, dao) ?: return
universe@210 324 val (id, project) = idParams
universe@210 325
universe@210 326 val version = Version(id, project.id).apply {
universe@184 327 name = http.param("name") ?: ""
universe@184 328 node = http.param("node") ?: ""
universe@184 329 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
universe@184 330 status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future
universe@225 331 // TODO: process error messages
universe@225 332 eol = http.param("eol", ::dateOptValidator, null, mutableListOf())
universe@225 333 release = http.param("release", ::dateOptValidator, null, mutableListOf())
universe@184 334 // intentional defaults
universe@184 335 if (node.isBlank()) node = name
universe@184 336 // sanitizing
universe@184 337 node = sanitizeNode(node)
universe@184 338 }
universe@184 339
universe@225 340 // sanitize eol and release date
universe@225 341 if (version.status.isEndOfLife) {
universe@225 342 if (version.eol == null) version.eol = Date(System.currentTimeMillis())
universe@225 343 } else if (version.status.isReleased) {
universe@225 344 if (version.release == null) version.release = Date(System.currentTimeMillis())
universe@225 345 }
universe@225 346
universe@184 347 if (id < 0) {
universe@184 348 dao.insertVersion(version)
universe@184 349 } else {
universe@184 350 dao.updateVersion(version)
universe@184 351 }
universe@184 352
universe@184 353 http.renderCommit("projects/${project.node}/versions/")
universe@184 354 }
universe@184 355
universe@193 356 private fun components(http: HttpRequest, dao: DataAccessObject) {
universe@184 357 val projectInfo = obtainProjectInfo(http, dao)
universe@184 358 if (projectInfo == null) {
universe@184 359 http.response.sendError(404)
universe@184 360 return
universe@184 361 }
universe@184 362
universe@184 363 with(http) {
universe@205 364 pageTitle = "${projectInfo.project.name} - ${i18n("navmenu.components")}"
universe@184 365 view = ComponentsView(
universe@184 366 projectInfo,
universe@184 367 dao.listComponentSummaries(projectInfo.project)
universe@184 368 )
universe@198 369 feedPath = feedPath(projectInfo.project)
universe@184 370 navigationMenu = activeProjectNavMenu(
universe@184 371 dao.listProjects(),
universe@184 372 projectInfo
universe@184 373 )
universe@184 374 styleSheets = listOf("projects")
universe@184 375 render("components")
universe@184 376 }
universe@184 377 }
universe@184 378
universe@193 379 private fun componentForm(http: HttpRequest, dao: DataAccessObject) {
universe@184 380 val projectInfo = obtainProjectInfo(http, dao)
universe@184 381 if (projectInfo == null) {
universe@184 382 http.response.sendError(404)
universe@184 383 return
universe@184 384 }
universe@184 385
universe@184 386 val component: Component
universe@184 387 when (val result = http.lookupPathParam("component", projectInfo.components)) {
universe@210 388 is NotFound -> {
universe@184 389 http.response.sendError(404)
universe@184 390 return
universe@184 391 }
universe@210 392 is Found -> {
universe@184 393 component = result.elem ?: Component(-1, projectInfo.project.id)
universe@184 394 }
universe@184 395 }
universe@184 396
universe@184 397 with(http) {
universe@184 398 view = ComponentEditView(projectInfo, component, dao.listUsers())
universe@198 399 feedPath = feedPath(projectInfo.project)
universe@184 400 navigationMenu = activeProjectNavMenu(
universe@184 401 dao.listProjects(),
universe@184 402 projectInfo,
universe@184 403 selectedComponent = component
universe@184 404 )
universe@184 405 styleSheets = listOf("projects")
universe@184 406 render("component-form")
universe@184 407 }
universe@184 408 }
universe@184 409
universe@193 410 private fun componentCommit(http: HttpRequest, dao: DataAccessObject) {
universe@210 411 val idParams = obtainIdAndProject(http, dao) ?: return
universe@210 412 val (id, project) = idParams
universe@184 413
universe@210 414 val component = Component(id, project.id).apply {
universe@184 415 name = http.param("name") ?: ""
universe@184 416 node = http.param("node") ?: ""
universe@184 417 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
universe@184 418 color = WebColor(http.param("color") ?: "#000000")
universe@184 419 description = http.param("description")
universe@227 420 // TODO: process error message
universe@227 421 active = http.param("active", ::boolValidator, true, mutableListOf())
universe@184 422 lead = (http.param("lead")?.toIntOrNull() ?: -1).let {
universe@184 423 if (it < 0) null else dao.findUser(it)
universe@184 424 }
universe@184 425 // intentional defaults
universe@184 426 if (node.isBlank()) node = name
universe@184 427 // sanitizing
universe@184 428 node = sanitizeNode(node)
universe@184 429 }
universe@184 430
universe@184 431 if (id < 0) {
universe@184 432 dao.insertComponent(component)
universe@184 433 } else {
universe@184 434 dao.updateComponent(component)
universe@184 435 }
universe@184 436
universe@184 437 http.renderCommit("projects/${project.node}/components/")
universe@184 438 }
universe@184 439
universe@193 440 private fun issue(http: HttpRequest, dao: DataAccessObject) {
universe@184 441 withPathInfo(http, dao)?.run {
universe@184 442 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
universe@184 443 if (issue == null) {
universe@184 444 http.response.sendError(404)
universe@184 445 return
universe@184 446 }
universe@184 447
universe@184 448 val comments = dao.listComments(issue)
universe@184 449
universe@184 450 with(http) {
universe@205 451 pageTitle = "${projectInfo.project.name}: #${issue.id} ${issue.subject}"
universe@198 452 view = IssueDetailView(issue, comments, project, version, component)
universe@198 453 feedPath = feedPath(projectInfo.project)
universe@184 454 navigationMenu = activeProjectNavMenu(
universe@184 455 dao.listProjects(),
universe@184 456 projectInfo,
universe@184 457 version,
universe@184 458 component
universe@184 459 )
universe@184 460 styleSheets = listOf("projects")
universe@207 461 javascript = "issue-editor"
universe@184 462 render("issue-view")
universe@184 463 }
universe@184 464 }
universe@184 465 }
universe@184 466
universe@193 467 private fun issueForm(http: HttpRequest, dao: DataAccessObject) {
universe@184 468 withPathInfo(http, dao)?.run {
universe@184 469 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) ?: Issue(
universe@184 470 -1,
universe@198 471 project,
universe@184 472 )
universe@184 473
universe@215 474 // for new issues set some defaults
universe@215 475 if (issue.id < 0) {
universe@215 476 // pre-select component, if available in the path info
universe@215 477 issue.component = component
universe@184 478
universe@215 479 // pre-select version, if available in the path info
universe@215 480 if (version != null) {
universe@215 481 if (version.status.isReleased) {
universe@231 482 issue.affected = version
universe@215 483 } else {
universe@231 484 issue.resolved = version
universe@215 485 }
universe@191 486 }
universe@191 487 }
universe@191 488
universe@184 489 with(http) {
universe@184 490 view = IssueEditView(
universe@184 491 issue,
universe@184 492 projectInfo.versions,
universe@184 493 projectInfo.components,
universe@184 494 dao.listUsers(),
universe@198 495 project,
universe@184 496 version,
universe@184 497 component
universe@184 498 )
universe@198 499 feedPath = feedPath(projectInfo.project)
universe@184 500 navigationMenu = activeProjectNavMenu(
universe@184 501 dao.listProjects(),
universe@184 502 projectInfo,
universe@184 503 version,
universe@184 504 component
universe@184 505 )
universe@184 506 styleSheets = listOf("projects")
universe@207 507 javascript = "issue-editor"
universe@184 508 render("issue-form")
universe@184 509 }
universe@184 510 }
universe@184 511 }
universe@184 512
universe@193 513 private fun issueComment(http: HttpRequest, dao: DataAccessObject) {
universe@184 514 withPathInfo(http, dao)?.run {
universe@184 515 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
universe@184 516 if (issue == null) {
universe@184 517 http.response.sendError(404)
universe@184 518 return
universe@184 519 }
universe@184 520
universe@207 521 val commentId = http.param("commentid")?.toIntOrNull() ?: -1
universe@207 522 if (commentId > 0) {
universe@207 523 val comment = dao.findComment(commentId)
universe@232 524 if (comment == null) {
universe@232 525 http.response.sendError(404)
universe@232 526 return
universe@232 527 }
universe@232 528 val originalAuthor = comment.author?.username
universe@207 529 if (originalAuthor != null && originalAuthor == http.remoteUser) {
universe@232 530 val newComment = http.param("comment")
universe@232 531 if (!newComment.isNullOrBlank()) {
universe@232 532 comment.comment = newComment
universe@232 533 dao.updateComment(comment)
universe@242 534 dao.insertHistoryEvent(issue, comment)
universe@232 535 } else {
universe@232 536 logger().debug("Not updating comment ${comment.id} because nothing changed.")
universe@232 537 }
universe@207 538 } else {
universe@207 539 http.response.sendError(403)
universe@207 540 return
universe@207 541 }
universe@207 542 } else {
universe@207 543 val comment = IssueComment(-1, issue.id).apply {
universe@207 544 author = http.remoteUser?.let { dao.findUserByName(it) }
universe@207 545 comment = http.param("comment") ?: ""
universe@207 546 }
universe@232 547 val newId = dao.insertComment(comment)
universe@242 548 dao.insertHistoryEvent(issue, comment, newId)
universe@184 549 }
universe@184 550
universe@184 551 http.renderCommit("${issuesHref}${issue.id}")
universe@184 552 }
universe@184 553 }
universe@184 554
universe@193 555 private fun issueCommit(http: HttpRequest, dao: DataAccessObject) {
universe@184 556 withPathInfo(http, dao)?.run {
universe@184 557 val issue = Issue(
universe@184 558 http.param("id")?.toIntOrNull() ?: -1,
universe@198 559 project
universe@184 560 ).apply {
universe@184 561 component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1)
universe@184 562 category = IssueCategory.valueOf(http.param("category") ?: "")
universe@184 563 status = IssueStatus.valueOf(http.param("status") ?: "")
universe@184 564 subject = http.param("subject") ?: ""
universe@184 565 description = http.param("description") ?: ""
universe@184 566 assignee = http.param("assignee")?.toIntOrNull()?.let {
universe@184 567 when (it) {
universe@184 568 -1 -> null
universe@184 569 -2 -> component?.lead
universe@184 570 else -> dao.findUser(it)
universe@184 571 }
universe@184 572 }
universe@225 573 // TODO: process error messages
universe@225 574 eta = http.param("eta", ::dateOptValidator, null, mutableListOf())
universe@184 575
universe@231 576 affected = http.param("affected")?.toIntOrNull()?.takeIf { it > 0 }?.let { Version(it, project.id) }
universe@231 577 resolved = http.param("resolved")?.toIntOrNull()?.takeIf { it > 0 }?.let { Version(it, project.id) }
universe@184 578 }
universe@184 579
universe@186 580 val openId = if (issue.id < 0) {
universe@232 581 val id = dao.insertIssue(issue)
universe@232 582 dao.insertHistoryEvent(issue, id)
universe@232 583 id
universe@186 584 } else {
universe@232 585 val reference = dao.findIssue(issue.id)
universe@232 586 if (reference == null) {
universe@232 587 http.response.sendError(404)
universe@232 588 return
universe@232 589 }
universe@232 590
universe@232 591 if (issue.hasChanged(reference)) {
universe@232 592 dao.updateIssue(issue)
universe@232 593 dao.insertHistoryEvent(issue)
universe@232 594 } else {
universe@232 595 logger().debug("Not updating issue ${issue.id} because nothing changed.")
universe@232 596 }
universe@232 597
universe@214 598 val newComment = http.param("comment")
universe@214 599 if (!newComment.isNullOrBlank()) {
universe@232 600 val comment = IssueComment(-1, issue.id).apply {
universe@214 601 author = http.remoteUser?.let { dao.findUserByName(it) }
universe@214 602 comment = newComment
universe@232 603 }
universe@232 604 val commentid = dao.insertComment(comment)
universe@242 605 dao.insertHistoryEvent(issue, comment, commentid)
universe@214 606 }
universe@186 607 issue.id
universe@186 608 }
universe@186 609
universe@186 610 if (http.param("more") != null) {
universe@185 611 http.renderCommit("${issuesHref}-/create")
universe@185 612 } else {
universe@186 613 http.renderCommit("${issuesHref}${openId}")
universe@185 614 }
universe@184 615 }
universe@184 616 }
universe@184 617 }

mercurial