Thu, 13 May 2021 10:16:57 +0200
fixes missing issue sorting
1 /*
2 * Copyright 2021 Mike Becker. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
26 package de.uapcore.lightpit.servlet
28 import de.uapcore.lightpit.AbstractServlet
29 import de.uapcore.lightpit.HttpRequest
30 import de.uapcore.lightpit.dao.DataAccessObject
31 import de.uapcore.lightpit.entities.*
32 import de.uapcore.lightpit.types.IssueCategory
33 import de.uapcore.lightpit.types.IssueStatus
34 import de.uapcore.lightpit.types.VersionStatus
35 import de.uapcore.lightpit.types.WebColor
36 import de.uapcore.lightpit.util.AllFilter
37 import de.uapcore.lightpit.util.IssueFilter
38 import de.uapcore.lightpit.util.IssueSorter.Companion.DEFAULT_ISSUE_SORTER
39 import de.uapcore.lightpit.util.SpecificFilter
40 import de.uapcore.lightpit.viewmodel.*
41 import java.sql.Date
42 import javax.servlet.annotation.WebServlet
44 @WebServlet(urlPatterns = ["/projects/*"])
45 class ProjectServlet : AbstractServlet() {
47 init {
48 get("/", this::projects)
49 get("/%project", this::project)
50 get("/%project/issues/%version/%component/", this::project)
51 get("/%project/edit", this::projectForm)
52 get("/-/create", this::projectForm)
53 post("/-/commit", this::projectCommit)
55 get("/%project/versions/", this::versions)
56 get("/%project/versions/%version/edit", this::versionForm)
57 get("/%project/versions/-/create", this::versionForm)
58 post("/%project/versions/-/commit", this::versionCommit)
60 get("/%project/components/", this::components)
61 get("/%project/components/%component/edit", this::componentForm)
62 get("/%project/components/-/create", this::componentForm)
63 post("/%project/components/-/commit", this::componentCommit)
65 get("/%project/issues/%version/%component/%issue", this::issue)
66 get("/%project/issues/%version/%component/%issue/edit", this::issueForm)
67 post("/%project/issues/%version/%component/%issue/comment", this::issueComment)
68 get("/%project/issues/%version/%component/-/create", this::issueForm)
69 post("/%project/issues/%version/%component/-/commit", this::issueCommit)
70 }
72 private fun projects(http: HttpRequest, dao: DataAccessObject) {
73 val projects = dao.listProjects()
74 val projectInfos = projects.map {
75 ProjectInfo(
76 project = it,
77 versions = dao.listVersions(it),
78 components = emptyList(), // not required in this view
79 issueSummary = dao.collectIssueSummary(it)
80 )
81 }
83 with(http) {
84 view = ProjectsView(projectInfos)
85 navigationMenu = projectNavMenu(projects)
86 styleSheets = listOf("projects")
87 render("projects")
88 }
89 }
91 private fun activeProjectNavMenu(
92 projects: List<Project>,
93 projectInfo: ProjectInfo,
94 selectedVersion: Version? = null,
95 selectedComponent: Component? = null
96 ) =
97 projectNavMenu(
98 projects,
99 projectInfo.versions,
100 projectInfo.components,
101 projectInfo.project,
102 selectedVersion,
103 selectedComponent
104 )
106 sealed class LookupResult<T> {
107 class NotFound<T> : LookupResult<T>()
108 data class Found<T>(val elem: T?) : LookupResult<T>()
109 }
111 private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> {
112 val node = pathParams[paramName]
113 return if (node == null || node == "-") {
114 LookupResult.Found(null)
115 } else {
116 val result = list.find { it.node == node }
117 if (result == null) {
118 LookupResult.NotFound()
119 } else {
120 LookupResult.Found(result)
121 }
122 }
123 }
125 private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? {
126 val project = dao.findProjectByNode(http.pathParams["project"] ?: "") ?: return null
128 val versions: List<Version> = dao.listVersions(project)
129 val components: List<Component> = dao.listComponents(project)
131 return ProjectInfo(
132 project,
133 versions,
134 components,
135 dao.collectIssueSummary(project)
136 )
137 }
139 private fun sanitizeNode(name: String): String {
140 val san = name.replace(Regex("[/\\\\]"), "-")
141 return if (san.startsWith(".")) {
142 "v$san"
143 } else {
144 san
145 }
146 }
148 data class PathInfos(
149 val projectInfo: ProjectInfo,
150 val version: Version?,
151 val component: Component?
152 ) {
153 val issuesHref by lazyOf("projects/${projectInfo.project.node}/issues/${version?.node ?: "-"}/${component?.node ?: "-"}/")
154 }
156 private fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? {
157 val projectInfo = obtainProjectInfo(http, dao)
158 if (projectInfo == null) {
159 http.response.sendError(404)
160 return null
161 }
163 val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) {
164 is LookupResult.NotFound -> {
165 http.response.sendError(404)
166 return null
167 }
168 is LookupResult.Found -> {
169 result.elem
170 }
171 }
172 val component = when (val result = http.lookupPathParam("component", projectInfo.components)) {
173 is LookupResult.NotFound -> {
174 http.response.sendError(404)
175 return null
176 }
177 is LookupResult.Found -> {
178 result.elem
179 }
180 }
182 return PathInfos(projectInfo, version, component)
183 }
185 private fun project(http: HttpRequest, dao: DataAccessObject) {
186 withPathInfo(http, dao)?.run {
188 val issues = dao.listIssues(IssueFilter(
189 project = SpecificFilter(projectInfo.project),
190 version = version?.let { SpecificFilter(it) } ?: AllFilter(),
191 component = component?.let { SpecificFilter(it) } ?: AllFilter()
192 )).sortedWith(DEFAULT_ISSUE_SORTER)
194 with(http) {
195 view = ProjectDetails(projectInfo, issues, version, component)
196 navigationMenu = activeProjectNavMenu(
197 dao.listProjects(),
198 projectInfo,
199 version,
200 component
201 )
202 styleSheets = listOf("projects")
203 render("project-details")
204 }
205 }
206 }
208 private fun projectForm(http: HttpRequest, dao: DataAccessObject) {
209 val projectInfo = obtainProjectInfo(http, dao)
210 if (projectInfo == null) {
211 http.response.sendError(404)
212 return
213 }
215 with(http) {
216 view = ProjectEditView(projectInfo.project, dao.listUsers())
217 navigationMenu = activeProjectNavMenu(
218 dao.listProjects(),
219 projectInfo
220 )
221 styleSheets = listOf("projects")
222 render("project-form")
223 }
224 }
226 private fun projectCommit(http: HttpRequest, dao: DataAccessObject) {
227 // TODO: replace defaults with throwing validator exceptions
228 val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply {
229 name = http.param("name") ?: ""
230 node = http.param("node") ?: ""
231 description = http.param("description") ?: ""
232 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
233 repoUrl = http.param("repoUrl") ?: ""
234 owner = (http.param("owner")?.toIntOrNull() ?: -1).let {
235 if (it < 0) null else dao.findUser(it)
236 }
237 // intentional defaults
238 if (node.isBlank()) node = name
239 // sanitizing
240 node = sanitizeNode(node)
241 }
243 if (project.id < 0) {
244 dao.insertProject(project)
245 } else {
246 dao.updateProject(project)
247 }
249 http.renderCommit("projects/${project.node}")
250 }
252 private fun versions(http: HttpRequest, dao: DataAccessObject) {
253 val projectInfo = obtainProjectInfo(http, dao)
254 if (projectInfo == null) {
255 http.response.sendError(404)
256 return
257 }
259 with(http) {
260 view = VersionsView(
261 projectInfo,
262 dao.listVersionSummaries(projectInfo.project)
263 )
264 navigationMenu = activeProjectNavMenu(
265 dao.listProjects(),
266 projectInfo
267 )
268 styleSheets = listOf("projects")
269 render("versions")
270 }
271 }
273 private fun versionForm(http: HttpRequest, dao: DataAccessObject) {
274 val projectInfo = obtainProjectInfo(http, dao)
275 if (projectInfo == null) {
276 http.response.sendError(404)
277 return
278 }
280 val version: Version
281 when (val result = http.lookupPathParam("version", projectInfo.versions)) {
282 is LookupResult.NotFound -> {
283 http.response.sendError(404)
284 return
285 }
286 is LookupResult.Found -> {
287 version = result.elem ?: Version(-1, projectInfo.project.id)
288 }
289 }
291 with(http) {
292 view = VersionEditView(projectInfo, version)
293 navigationMenu = activeProjectNavMenu(
294 dao.listProjects(),
295 projectInfo,
296 selectedVersion = version
297 )
298 styleSheets = listOf("projects")
299 render("version-form")
300 }
301 }
303 private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
304 val id = http.param("id")?.toIntOrNull()
305 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
306 val project = dao.findProject(projectid)
307 if (id == null || project == null) {
308 http.response.sendError(400)
309 return
310 }
312 // TODO: replace defaults with throwing validator exceptions
313 val version = Version(id, projectid).apply {
314 name = http.param("name") ?: ""
315 node = http.param("node") ?: ""
316 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
317 status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future
318 // intentional defaults
319 if (node.isBlank()) node = name
320 // sanitizing
321 node = sanitizeNode(node)
322 }
324 if (id < 0) {
325 dao.insertVersion(version)
326 } else {
327 dao.updateVersion(version)
328 }
330 http.renderCommit("projects/${project.node}/versions/")
331 }
333 private fun components(http: HttpRequest, dao: DataAccessObject) {
334 val projectInfo = obtainProjectInfo(http, dao)
335 if (projectInfo == null) {
336 http.response.sendError(404)
337 return
338 }
340 with(http) {
341 view = ComponentsView(
342 projectInfo,
343 dao.listComponentSummaries(projectInfo.project)
344 )
345 navigationMenu = activeProjectNavMenu(
346 dao.listProjects(),
347 projectInfo
348 )
349 styleSheets = listOf("projects")
350 render("components")
351 }
352 }
354 private fun componentForm(http: HttpRequest, dao: DataAccessObject) {
355 val projectInfo = obtainProjectInfo(http, dao)
356 if (projectInfo == null) {
357 http.response.sendError(404)
358 return
359 }
361 val component: Component
362 when (val result = http.lookupPathParam("component", projectInfo.components)) {
363 is LookupResult.NotFound -> {
364 http.response.sendError(404)
365 return
366 }
367 is LookupResult.Found -> {
368 component = result.elem ?: Component(-1, projectInfo.project.id)
369 }
370 }
372 with(http) {
373 view = ComponentEditView(projectInfo, component, dao.listUsers())
374 navigationMenu = activeProjectNavMenu(
375 dao.listProjects(),
376 projectInfo,
377 selectedComponent = component
378 )
379 styleSheets = listOf("projects")
380 render("component-form")
381 }
382 }
384 private fun componentCommit(http: HttpRequest, dao: DataAccessObject) {
385 val id = http.param("id")?.toIntOrNull()
386 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
387 val project = dao.findProject(projectid)
388 if (id == null || project == null) {
389 http.response.sendError(400)
390 return
391 }
393 // TODO: replace defaults with throwing validator exceptions
394 val component = Component(id, projectid).apply {
395 name = http.param("name") ?: ""
396 node = http.param("node") ?: ""
397 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
398 color = WebColor(http.param("color") ?: "#000000")
399 description = http.param("description")
400 lead = (http.param("lead")?.toIntOrNull() ?: -1).let {
401 if (it < 0) null else dao.findUser(it)
402 }
403 // intentional defaults
404 if (node.isBlank()) node = name
405 // sanitizing
406 node = sanitizeNode(node)
407 }
409 if (id < 0) {
410 dao.insertComponent(component)
411 } else {
412 dao.updateComponent(component)
413 }
415 http.renderCommit("projects/${project.node}/components/")
416 }
418 private fun issue(http: HttpRequest, dao: DataAccessObject) {
419 withPathInfo(http, dao)?.run {
420 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
421 if (issue == null) {
422 http.response.sendError(404)
423 return
424 }
426 val comments = dao.listComments(issue)
428 with(http) {
429 view = IssueDetailView(issue, comments, projectInfo.project, version, component)
430 navigationMenu = activeProjectNavMenu(
431 dao.listProjects(),
432 projectInfo,
433 version,
434 component
435 )
436 styleSheets = listOf("projects")
437 render("issue-view")
438 }
439 }
440 }
442 private fun issueForm(http: HttpRequest, dao: DataAccessObject) {
443 withPathInfo(http, dao)?.run {
444 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) ?: Issue(
445 -1,
446 projectInfo.project,
447 )
449 // pre-select component, if available in the path info
450 issue.component = component
452 // pre-select version, if available in the path info
453 if (version != null) {
454 if (version.status.isReleased) {
455 issue.affectedVersions = listOf(version)
456 } else {
457 issue.resolvedVersions = listOf(version)
458 }
459 }
461 with(http) {
462 view = IssueEditView(
463 issue,
464 projectInfo.versions,
465 projectInfo.components,
466 dao.listUsers(),
467 projectInfo.project,
468 version,
469 component
470 )
471 navigationMenu = activeProjectNavMenu(
472 dao.listProjects(),
473 projectInfo,
474 version,
475 component
476 )
477 styleSheets = listOf("projects")
478 render("issue-form")
479 }
480 }
481 }
483 private fun issueComment(http: HttpRequest, dao: DataAccessObject) {
484 withPathInfo(http, dao)?.run {
485 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
486 if (issue == null) {
487 http.response.sendError(404)
488 return
489 }
491 // TODO: throw validator exception instead of using a default
492 val comment = IssueComment(-1, issue.id).apply {
493 author = http.remoteUser?.let { dao.findUserByName(it) }
494 comment = http.param("comment") ?: ""
495 }
497 dao.insertComment(comment)
499 http.renderCommit("${issuesHref}${issue.id}")
500 }
501 }
503 private fun issueCommit(http: HttpRequest, dao: DataAccessObject) {
504 withPathInfo(http, dao)?.run {
505 // TODO: throw validator exception instead of using defaults
506 val issue = Issue(
507 http.param("id")?.toIntOrNull() ?: -1,
508 projectInfo.project
509 ).apply {
510 component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1)
511 category = IssueCategory.valueOf(http.param("category") ?: "")
512 status = IssueStatus.valueOf(http.param("status") ?: "")
513 subject = http.param("subject") ?: ""
514 description = http.param("description") ?: ""
515 assignee = http.param("assignee")?.toIntOrNull()?.let {
516 when (it) {
517 -1 -> null
518 -2 -> component?.lead
519 else -> dao.findUser(it)
520 }
521 }
522 eta = http.param("eta")?.let { if (it.isBlank()) null else Date.valueOf(it) }
524 affectedVersions = http.paramArray("affected")
525 .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, projectInfo.project.id) } }
526 resolvedVersions = http.paramArray("resolved")
527 .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, projectInfo.project.id) } }
528 }
530 val openId = if (issue.id < 0) {
531 dao.insertIssue(issue)
532 } else {
533 dao.updateIssue(issue)
534 issue.id
535 }
537 if (http.param("more") != null) {
538 http.renderCommit("${issuesHref}-/create")
539 } else {
540 http.renderCommit("${issuesHref}${openId}")
541 }
542 }
543 }
544 }