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