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