Fri, 02 Apr 2021 11:59:14 +0200
completes kotlin migration
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@184 | 31 | import de.uapcore.lightpit.entities.* |
universe@184 | 32 | import de.uapcore.lightpit.types.IssueCategory |
universe@184 | 33 | import de.uapcore.lightpit.types.IssueStatus |
universe@184 | 34 | import de.uapcore.lightpit.types.VersionStatus |
universe@184 | 35 | import de.uapcore.lightpit.types.WebColor |
universe@184 | 36 | import de.uapcore.lightpit.util.AllFilter |
universe@184 | 37 | import de.uapcore.lightpit.util.IssueFilter |
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@184 | 66 | get("/%project/issues/%version/%component/%issue/comment", this::issueComment) |
universe@184 | 67 | get("/%project/issues/%version/%component/-/create", this::issueForm) |
universe@184 | 68 | get("/%project/issues/%version/%component/-/commit", this::issueCommit) |
universe@184 | 69 | } |
universe@184 | 70 | |
universe@184 | 71 | 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@184 | 105 | sealed class LookupResult<T> { |
universe@184 | 106 | class NotFound<T> : LookupResult<T>() |
universe@184 | 107 | data class Found<T>(val elem: T?) : LookupResult<T>() |
universe@184 | 108 | } |
universe@184 | 109 | |
universe@184 | 110 | private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> { |
universe@184 | 111 | val node = pathParams[paramName] |
universe@184 | 112 | return if (node == null || node == "-") { |
universe@184 | 113 | LookupResult.Found(null) |
universe@184 | 114 | } else { |
universe@184 | 115 | val result = list.find { it.node == node } |
universe@184 | 116 | if (result == null) { |
universe@184 | 117 | LookupResult.NotFound() |
universe@184 | 118 | } else { |
universe@184 | 119 | LookupResult.Found(result) |
universe@184 | 120 | } |
universe@184 | 121 | } |
universe@184 | 122 | } |
universe@184 | 123 | |
universe@184 | 124 | private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? { |
universe@184 | 125 | val project = dao.findProjectByNode(http.pathParams["project"] ?: "") ?: return null |
universe@184 | 126 | |
universe@184 | 127 | val versions: List<Version> = dao.listVersions(project) |
universe@184 | 128 | val components: List<Component> = dao.listComponents(project) |
universe@184 | 129 | |
universe@184 | 130 | return ProjectInfo( |
universe@184 | 131 | project, |
universe@184 | 132 | versions, |
universe@184 | 133 | components, |
universe@184 | 134 | dao.collectIssueSummary(project) |
universe@184 | 135 | ) |
universe@184 | 136 | } |
universe@184 | 137 | |
universe@184 | 138 | private fun sanitizeNode(name: String): String { |
universe@184 | 139 | val san = name.replace(Regex("[/\\\\]"), "-") |
universe@184 | 140 | return if (san.startsWith(".")) { |
universe@184 | 141 | "v$san" |
universe@184 | 142 | } else { |
universe@184 | 143 | san |
universe@184 | 144 | } |
universe@184 | 145 | } |
universe@184 | 146 | |
universe@184 | 147 | data class PathInfos( |
universe@184 | 148 | val projectInfo: ProjectInfo, |
universe@184 | 149 | val version: Version?, |
universe@184 | 150 | val component: Component? |
universe@184 | 151 | ) { |
universe@184 | 152 | val issuesHref by lazyOf("projects/${projectInfo.project.node}/issues/${version?.node ?: "-"}/${component?.node ?: "-"}/") |
universe@184 | 153 | } |
universe@184 | 154 | |
universe@184 | 155 | private fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? { |
universe@184 | 156 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 157 | if (projectInfo == null) { |
universe@184 | 158 | http.response.sendError(404) |
universe@184 | 159 | return null |
universe@184 | 160 | } |
universe@184 | 161 | |
universe@184 | 162 | val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) { |
universe@184 | 163 | is LookupResult.NotFound -> { |
universe@184 | 164 | http.response.sendError(404) |
universe@184 | 165 | return null |
universe@184 | 166 | } |
universe@184 | 167 | is LookupResult.Found -> { |
universe@184 | 168 | result.elem |
universe@184 | 169 | } |
universe@184 | 170 | } |
universe@184 | 171 | val component = when (val result = http.lookupPathParam("component", projectInfo.components)) { |
universe@184 | 172 | is LookupResult.NotFound -> { |
universe@184 | 173 | http.response.sendError(404) |
universe@184 | 174 | return null |
universe@184 | 175 | } |
universe@184 | 176 | is LookupResult.Found -> { |
universe@184 | 177 | result.elem |
universe@184 | 178 | } |
universe@184 | 179 | } |
universe@184 | 180 | |
universe@184 | 181 | return PathInfos(projectInfo, version, component) |
universe@184 | 182 | } |
universe@184 | 183 | |
universe@184 | 184 | fun project(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 185 | withPathInfo(http, dao)?.run { |
universe@184 | 186 | val issues = dao.listIssues(IssueFilter( |
universe@184 | 187 | project = SpecificFilter(projectInfo.project), |
universe@184 | 188 | version = version?.let { SpecificFilter(it) } ?: AllFilter(), |
universe@184 | 189 | component = component?.let { SpecificFilter(it) } ?: AllFilter() |
universe@184 | 190 | )) |
universe@184 | 191 | |
universe@184 | 192 | with(http) { |
universe@184 | 193 | view = ProjectDetails(projectInfo, issues, version, component) |
universe@184 | 194 | navigationMenu = activeProjectNavMenu( |
universe@184 | 195 | dao.listProjects(), |
universe@184 | 196 | projectInfo, |
universe@184 | 197 | version, |
universe@184 | 198 | component |
universe@184 | 199 | ) |
universe@184 | 200 | styleSheets = listOf("projects") |
universe@184 | 201 | render("project-details") |
universe@184 | 202 | } |
universe@184 | 203 | } |
universe@184 | 204 | } |
universe@184 | 205 | |
universe@184 | 206 | fun projectForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 207 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 208 | if (projectInfo == null) { |
universe@184 | 209 | http.response.sendError(404) |
universe@184 | 210 | return |
universe@184 | 211 | } |
universe@184 | 212 | |
universe@184 | 213 | with(http) { |
universe@184 | 214 | view = ProjectEditView(projectInfo.project, dao.listUsers()) |
universe@184 | 215 | navigationMenu = activeProjectNavMenu( |
universe@184 | 216 | dao.listProjects(), |
universe@184 | 217 | projectInfo |
universe@184 | 218 | ) |
universe@184 | 219 | styleSheets = listOf("projects") |
universe@184 | 220 | render("project-form") |
universe@184 | 221 | } |
universe@184 | 222 | } |
universe@184 | 223 | |
universe@184 | 224 | fun projectCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 225 | // TODO: replace defaults with throwing validator exceptions |
universe@184 | 226 | val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply { |
universe@184 | 227 | name = http.param("name") ?: "" |
universe@184 | 228 | node = http.param("node") ?: "" |
universe@184 | 229 | description = http.param("description") ?: "" |
universe@184 | 230 | ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 |
universe@184 | 231 | repoUrl = http.param("repoUrl") ?: "" |
universe@184 | 232 | owner = (http.param("owner")?.toIntOrNull() ?: -1).let { |
universe@184 | 233 | if (it < 0) null else dao.findUser(it) |
universe@184 | 234 | } |
universe@184 | 235 | // intentional defaults |
universe@184 | 236 | if (node.isBlank()) node = name |
universe@184 | 237 | // sanitizing |
universe@184 | 238 | node = sanitizeNode(node) |
universe@184 | 239 | } |
universe@184 | 240 | |
universe@184 | 241 | if (project.id < 0) { |
universe@184 | 242 | dao.insertProject(project) |
universe@184 | 243 | } else { |
universe@184 | 244 | dao.updateProject(project) |
universe@184 | 245 | } |
universe@184 | 246 | |
universe@184 | 247 | http.renderCommit("projects/${project.node}") |
universe@184 | 248 | } |
universe@184 | 249 | |
universe@184 | 250 | fun versions(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 251 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 252 | if (projectInfo == null) { |
universe@184 | 253 | http.response.sendError(404) |
universe@184 | 254 | return |
universe@184 | 255 | } |
universe@184 | 256 | |
universe@184 | 257 | with(http) { |
universe@184 | 258 | view = VersionsView( |
universe@184 | 259 | projectInfo, |
universe@184 | 260 | dao.listVersionSummaries(projectInfo.project) |
universe@184 | 261 | ) |
universe@184 | 262 | navigationMenu = activeProjectNavMenu( |
universe@184 | 263 | dao.listProjects(), |
universe@184 | 264 | projectInfo |
universe@184 | 265 | ) |
universe@184 | 266 | styleSheets = listOf("projects") |
universe@184 | 267 | render("versions") |
universe@184 | 268 | } |
universe@184 | 269 | } |
universe@184 | 270 | |
universe@184 | 271 | fun versionForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 272 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 273 | if (projectInfo == null) { |
universe@184 | 274 | http.response.sendError(404) |
universe@184 | 275 | return |
universe@184 | 276 | } |
universe@184 | 277 | |
universe@184 | 278 | val version: Version |
universe@184 | 279 | when (val result = http.lookupPathParam("version", projectInfo.versions)) { |
universe@184 | 280 | is LookupResult.NotFound -> { |
universe@184 | 281 | http.response.sendError(404) |
universe@184 | 282 | return |
universe@184 | 283 | } |
universe@184 | 284 | is LookupResult.Found -> { |
universe@184 | 285 | version = result.elem ?: Version(-1, projectInfo.project.id) |
universe@184 | 286 | } |
universe@184 | 287 | } |
universe@184 | 288 | |
universe@184 | 289 | with(http) { |
universe@184 | 290 | view = VersionEditView(projectInfo, version) |
universe@184 | 291 | navigationMenu = activeProjectNavMenu( |
universe@184 | 292 | dao.listProjects(), |
universe@184 | 293 | projectInfo, |
universe@184 | 294 | selectedVersion = version |
universe@184 | 295 | ) |
universe@184 | 296 | styleSheets = listOf("projects") |
universe@184 | 297 | render("version-form") |
universe@184 | 298 | } |
universe@184 | 299 | } |
universe@184 | 300 | |
universe@184 | 301 | fun versionCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 302 | val id = http.param("id")?.toIntOrNull() |
universe@184 | 303 | val projectid = http.param("projectid")?.toIntOrNull() ?: -1 |
universe@184 | 304 | val project = dao.findProject(projectid) |
universe@184 | 305 | if (id == null || project == null) { |
universe@184 | 306 | http.response.sendError(400) |
universe@184 | 307 | return |
universe@184 | 308 | } |
universe@184 | 309 | |
universe@184 | 310 | // TODO: replace defaults with throwing validator exceptions |
universe@184 | 311 | val version = Version(id, projectid).apply { |
universe@184 | 312 | name = http.param("name") ?: "" |
universe@184 | 313 | node = http.param("node") ?: "" |
universe@184 | 314 | ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 |
universe@184 | 315 | status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future |
universe@184 | 316 | // intentional defaults |
universe@184 | 317 | if (node.isBlank()) node = name |
universe@184 | 318 | // sanitizing |
universe@184 | 319 | node = sanitizeNode(node) |
universe@184 | 320 | } |
universe@184 | 321 | |
universe@184 | 322 | if (id < 0) { |
universe@184 | 323 | dao.insertVersion(version) |
universe@184 | 324 | } else { |
universe@184 | 325 | dao.updateVersion(version) |
universe@184 | 326 | } |
universe@184 | 327 | |
universe@184 | 328 | http.renderCommit("projects/${project.node}/versions/") |
universe@184 | 329 | } |
universe@184 | 330 | |
universe@184 | 331 | fun components(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 332 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 333 | if (projectInfo == null) { |
universe@184 | 334 | http.response.sendError(404) |
universe@184 | 335 | return |
universe@184 | 336 | } |
universe@184 | 337 | |
universe@184 | 338 | with(http) { |
universe@184 | 339 | view = ComponentsView( |
universe@184 | 340 | projectInfo, |
universe@184 | 341 | dao.listComponentSummaries(projectInfo.project) |
universe@184 | 342 | ) |
universe@184 | 343 | navigationMenu = activeProjectNavMenu( |
universe@184 | 344 | dao.listProjects(), |
universe@184 | 345 | projectInfo |
universe@184 | 346 | ) |
universe@184 | 347 | styleSheets = listOf("projects") |
universe@184 | 348 | render("components") |
universe@184 | 349 | } |
universe@184 | 350 | } |
universe@184 | 351 | |
universe@184 | 352 | fun componentForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 353 | val projectInfo = obtainProjectInfo(http, dao) |
universe@184 | 354 | if (projectInfo == null) { |
universe@184 | 355 | http.response.sendError(404) |
universe@184 | 356 | return |
universe@184 | 357 | } |
universe@184 | 358 | |
universe@184 | 359 | val component: Component |
universe@184 | 360 | when (val result = http.lookupPathParam("component", projectInfo.components)) { |
universe@184 | 361 | is LookupResult.NotFound -> { |
universe@184 | 362 | http.response.sendError(404) |
universe@184 | 363 | return |
universe@184 | 364 | } |
universe@184 | 365 | is LookupResult.Found -> { |
universe@184 | 366 | component = result.elem ?: Component(-1, projectInfo.project.id) |
universe@184 | 367 | } |
universe@184 | 368 | } |
universe@184 | 369 | |
universe@184 | 370 | with(http) { |
universe@184 | 371 | view = ComponentEditView(projectInfo, component, dao.listUsers()) |
universe@184 | 372 | navigationMenu = activeProjectNavMenu( |
universe@184 | 373 | dao.listProjects(), |
universe@184 | 374 | projectInfo, |
universe@184 | 375 | selectedComponent = component |
universe@184 | 376 | ) |
universe@184 | 377 | styleSheets = listOf("projects") |
universe@184 | 378 | render("component-form") |
universe@184 | 379 | } |
universe@184 | 380 | } |
universe@184 | 381 | |
universe@184 | 382 | fun componentCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 383 | val id = http.param("id")?.toIntOrNull() |
universe@184 | 384 | val projectid = http.param("projectid")?.toIntOrNull() ?: -1 |
universe@184 | 385 | val project = dao.findProject(projectid) |
universe@184 | 386 | if (id == null || project == null) { |
universe@184 | 387 | http.response.sendError(400) |
universe@184 | 388 | return |
universe@184 | 389 | } |
universe@184 | 390 | |
universe@184 | 391 | // TODO: replace defaults with throwing validator exceptions |
universe@184 | 392 | val component = Component(id, projectid).apply { |
universe@184 | 393 | name = http.param("name") ?: "" |
universe@184 | 394 | node = http.param("node") ?: "" |
universe@184 | 395 | ordinal = http.param("ordinal")?.toIntOrNull() ?: 0 |
universe@184 | 396 | color = WebColor(http.param("color") ?: "#000000") |
universe@184 | 397 | description = http.param("description") |
universe@184 | 398 | lead = (http.param("lead")?.toIntOrNull() ?: -1).let { |
universe@184 | 399 | if (it < 0) null else dao.findUser(it) |
universe@184 | 400 | } |
universe@184 | 401 | // intentional defaults |
universe@184 | 402 | if (node.isBlank()) node = name |
universe@184 | 403 | // sanitizing |
universe@184 | 404 | node = sanitizeNode(node) |
universe@184 | 405 | } |
universe@184 | 406 | |
universe@184 | 407 | if (id < 0) { |
universe@184 | 408 | dao.insertComponent(component) |
universe@184 | 409 | } else { |
universe@184 | 410 | dao.updateComponent(component) |
universe@184 | 411 | } |
universe@184 | 412 | |
universe@184 | 413 | http.renderCommit("projects/${project.node}/components/") |
universe@184 | 414 | } |
universe@184 | 415 | |
universe@184 | 416 | fun issue(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 417 | withPathInfo(http, dao)?.run { |
universe@184 | 418 | val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) |
universe@184 | 419 | if (issue == null) { |
universe@184 | 420 | http.response.sendError(404) |
universe@184 | 421 | return |
universe@184 | 422 | } |
universe@184 | 423 | |
universe@184 | 424 | val comments = dao.listComments(issue) |
universe@184 | 425 | |
universe@184 | 426 | with(http) { |
universe@184 | 427 | view = IssueDetailView(issue, comments, projectInfo.project, version, component) |
universe@184 | 428 | navigationMenu = activeProjectNavMenu( |
universe@184 | 429 | dao.listProjects(), |
universe@184 | 430 | projectInfo, |
universe@184 | 431 | version, |
universe@184 | 432 | component |
universe@184 | 433 | ) |
universe@184 | 434 | styleSheets = listOf("projects") |
universe@184 | 435 | render("issue-view") |
universe@184 | 436 | } |
universe@184 | 437 | } |
universe@184 | 438 | } |
universe@184 | 439 | |
universe@184 | 440 | fun issueForm(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 441 | withPathInfo(http, dao)?.run { |
universe@184 | 442 | val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) ?: Issue( |
universe@184 | 443 | -1, |
universe@184 | 444 | projectInfo.project, |
universe@184 | 445 | ) |
universe@184 | 446 | |
universe@184 | 447 | // pre-select component, if available in the path info |
universe@184 | 448 | issue.component = component |
universe@184 | 449 | |
universe@184 | 450 | with(http) { |
universe@184 | 451 | view = IssueEditView( |
universe@184 | 452 | issue, |
universe@184 | 453 | projectInfo.versions, |
universe@184 | 454 | projectInfo.components, |
universe@184 | 455 | dao.listUsers(), |
universe@184 | 456 | projectInfo.project, |
universe@184 | 457 | version, |
universe@184 | 458 | component |
universe@184 | 459 | ) |
universe@184 | 460 | navigationMenu = activeProjectNavMenu( |
universe@184 | 461 | dao.listProjects(), |
universe@184 | 462 | projectInfo, |
universe@184 | 463 | version, |
universe@184 | 464 | component |
universe@184 | 465 | ) |
universe@184 | 466 | styleSheets = listOf("projects") |
universe@184 | 467 | render("issue-form") |
universe@184 | 468 | } |
universe@184 | 469 | } |
universe@184 | 470 | } |
universe@184 | 471 | |
universe@184 | 472 | fun issueComment(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 473 | withPathInfo(http, dao)?.run { |
universe@184 | 474 | val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) |
universe@184 | 475 | if (issue == null) { |
universe@184 | 476 | http.response.sendError(404) |
universe@184 | 477 | return |
universe@184 | 478 | } |
universe@184 | 479 | |
universe@184 | 480 | // TODO: throw validator exception instead of using a default |
universe@184 | 481 | val comment = IssueComment(-1, issue.id).apply { |
universe@184 | 482 | author = http.remoteUser?.let { dao.findUserByName(it) } |
universe@184 | 483 | comment = http.param("comment") ?: "" |
universe@184 | 484 | } |
universe@184 | 485 | |
universe@184 | 486 | dao.insertComment(comment) |
universe@184 | 487 | |
universe@184 | 488 | http.renderCommit("${issuesHref}${issue.id}") |
universe@184 | 489 | } |
universe@184 | 490 | } |
universe@184 | 491 | |
universe@184 | 492 | fun issueCommit(http: HttpRequest, dao: DataAccessObject) { |
universe@184 | 493 | withPathInfo(http, dao)?.run { |
universe@184 | 494 | // TODO: throw validator exception instead of using defaults |
universe@184 | 495 | val issue = Issue( |
universe@184 | 496 | http.param("id")?.toIntOrNull() ?: -1, |
universe@184 | 497 | projectInfo.project |
universe@184 | 498 | ).apply { |
universe@184 | 499 | component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1) |
universe@184 | 500 | category = IssueCategory.valueOf(http.param("category") ?: "") |
universe@184 | 501 | status = IssueStatus.valueOf(http.param("status") ?: "") |
universe@184 | 502 | subject = http.param("subject") ?: "" |
universe@184 | 503 | description = http.param("description") ?: "" |
universe@184 | 504 | assignee = http.param("assignee")?.toIntOrNull()?.let { |
universe@184 | 505 | when (it) { |
universe@184 | 506 | -1 -> null |
universe@184 | 507 | -2 -> component?.lead |
universe@184 | 508 | else -> dao.findUser(it) |
universe@184 | 509 | } |
universe@184 | 510 | } |
universe@184 | 511 | eta = http.param("eta")?.let { Date.valueOf(it) } |
universe@184 | 512 | |
universe@184 | 513 | affectedVersions = http.paramArray("affected") |
universe@184 | 514 | .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, projectInfo.project.id) } } |
universe@184 | 515 | resolvedVersions = http.paramArray("resolved") |
universe@184 | 516 | .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, projectInfo.project.id) } } |
universe@184 | 517 | } |
universe@184 | 518 | |
universe@184 | 519 | http.renderCommit("${issuesHref}${issue.id}") |
universe@184 | 520 | } |
universe@184 | 521 | } |
universe@184 | 522 | } |