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

Wed, 18 Aug 2021 14:57:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 18 Aug 2021 14:57:45 +0200
changeset 225
87328572e36f
parent 215
028792eda9b7
child 227
f0ede8046b59
permissions
-rw-r--r--

#159 adds release and eol dates

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

mercurial