Sat, 05 Jun 2021 09:04:13 +0200
fixes project creation not working
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 private fun feedPath(project: Project) = "feed/${project.node}/issues.rss"
150 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 LookupResult.NotFound -> {
168 http.response.sendError(404)
169 return null
170 }
171 is LookupResult.Found -> {
172 result.elem
173 }
174 }
175 val component = when (val result = http.lookupPathParam("component", projectInfo.components)) {
176 is LookupResult.NotFound -> {
177 http.response.sendError(404)
178 return null
179 }
180 is LookupResult.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 view = ProjectDetails(projectInfo, issues, version, component)
199 feedPath = feedPath(project)
200 navigationMenu = activeProjectNavMenu(
201 dao.listProjects(),
202 projectInfo,
203 version,
204 component
205 )
206 styleSheets = listOf("projects")
207 render("project-details")
208 }
209 }
210 }
212 private fun projectForm(http: HttpRequest, dao: DataAccessObject) {
213 if (!http.pathParams.containsKey("project")) {
214 http.view = ProjectEditView(Project(-1), dao.listUsers())
215 http.navigationMenu = projectNavMenu(dao.listProjects())
216 } else {
217 val projectInfo = obtainProjectInfo(http, dao)
218 if (projectInfo == null) {
219 http.response.sendError(404)
220 return
221 }
222 http.view = ProjectEditView(projectInfo.project, dao.listUsers())
223 http.navigationMenu = activeProjectNavMenu(
224 dao.listProjects(),
225 projectInfo
226 )
227 }
228 http.styleSheets = listOf("projects")
229 http.render("project-form")
230 }
232 private fun projectCommit(http: HttpRequest, dao: DataAccessObject) {
233 // TODO: replace defaults with throwing validator exceptions
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 view = VersionsView(
267 projectInfo,
268 dao.listVersionSummaries(projectInfo.project)
269 )
270 feedPath = feedPath(projectInfo.project)
271 navigationMenu = activeProjectNavMenu(
272 dao.listProjects(),
273 projectInfo
274 )
275 styleSheets = listOf("projects")
276 render("versions")
277 }
278 }
280 private fun versionForm(http: HttpRequest, dao: DataAccessObject) {
281 val projectInfo = obtainProjectInfo(http, dao)
282 if (projectInfo == null) {
283 http.response.sendError(404)
284 return
285 }
287 val version: Version
288 when (val result = http.lookupPathParam("version", projectInfo.versions)) {
289 is LookupResult.NotFound -> {
290 http.response.sendError(404)
291 return
292 }
293 is LookupResult.Found -> {
294 version = result.elem ?: Version(-1, projectInfo.project.id)
295 }
296 }
298 with(http) {
299 view = VersionEditView(projectInfo, version)
300 feedPath = feedPath(projectInfo.project)
301 navigationMenu = activeProjectNavMenu(
302 dao.listProjects(),
303 projectInfo,
304 selectedVersion = version
305 )
306 styleSheets = listOf("projects")
307 render("version-form")
308 }
309 }
311 private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
312 val id = http.param("id")?.toIntOrNull()
313 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
314 val project = dao.findProject(projectid)
315 if (id == null || project == null) {
316 http.response.sendError(400)
317 return
318 }
320 // TODO: replace defaults with throwing validator exceptions
321 val version = Version(id, projectid).apply {
322 name = http.param("name") ?: ""
323 node = http.param("node") ?: ""
324 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
325 status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future
326 // intentional defaults
327 if (node.isBlank()) node = name
328 // sanitizing
329 node = sanitizeNode(node)
330 }
332 if (id < 0) {
333 dao.insertVersion(version)
334 } else {
335 dao.updateVersion(version)
336 }
338 http.renderCommit("projects/${project.node}/versions/")
339 }
341 private fun components(http: HttpRequest, dao: DataAccessObject) {
342 val projectInfo = obtainProjectInfo(http, dao)
343 if (projectInfo == null) {
344 http.response.sendError(404)
345 return
346 }
348 with(http) {
349 view = ComponentsView(
350 projectInfo,
351 dao.listComponentSummaries(projectInfo.project)
352 )
353 feedPath = feedPath(projectInfo.project)
354 navigationMenu = activeProjectNavMenu(
355 dao.listProjects(),
356 projectInfo
357 )
358 styleSheets = listOf("projects")
359 render("components")
360 }
361 }
363 private fun componentForm(http: HttpRequest, dao: DataAccessObject) {
364 val projectInfo = obtainProjectInfo(http, dao)
365 if (projectInfo == null) {
366 http.response.sendError(404)
367 return
368 }
370 val component: Component
371 when (val result = http.lookupPathParam("component", projectInfo.components)) {
372 is LookupResult.NotFound -> {
373 http.response.sendError(404)
374 return
375 }
376 is LookupResult.Found -> {
377 component = result.elem ?: Component(-1, projectInfo.project.id)
378 }
379 }
381 with(http) {
382 view = ComponentEditView(projectInfo, component, dao.listUsers())
383 feedPath = feedPath(projectInfo.project)
384 navigationMenu = activeProjectNavMenu(
385 dao.listProjects(),
386 projectInfo,
387 selectedComponent = component
388 )
389 styleSheets = listOf("projects")
390 render("component-form")
391 }
392 }
394 private fun componentCommit(http: HttpRequest, dao: DataAccessObject) {
395 val id = http.param("id")?.toIntOrNull()
396 val projectid = http.param("projectid")?.toIntOrNull() ?: -1
397 val project = dao.findProject(projectid)
398 if (id == null || project == null) {
399 http.response.sendError(400)
400 return
401 }
403 // TODO: replace defaults with throwing validator exceptions
404 val component = Component(id, projectid).apply {
405 name = http.param("name") ?: ""
406 node = http.param("node") ?: ""
407 ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
408 color = WebColor(http.param("color") ?: "#000000")
409 description = http.param("description")
410 lead = (http.param("lead")?.toIntOrNull() ?: -1).let {
411 if (it < 0) null else dao.findUser(it)
412 }
413 // intentional defaults
414 if (node.isBlank()) node = name
415 // sanitizing
416 node = sanitizeNode(node)
417 }
419 if (id < 0) {
420 dao.insertComponent(component)
421 } else {
422 dao.updateComponent(component)
423 }
425 http.renderCommit("projects/${project.node}/components/")
426 }
428 private fun issue(http: HttpRequest, dao: DataAccessObject) {
429 withPathInfo(http, dao)?.run {
430 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
431 if (issue == null) {
432 http.response.sendError(404)
433 return
434 }
436 val comments = dao.listComments(issue)
438 with(http) {
439 view = IssueDetailView(issue, comments, project, version, component)
440 // TODO: feed path for this particular issue
441 feedPath = feedPath(projectInfo.project)
442 navigationMenu = activeProjectNavMenu(
443 dao.listProjects(),
444 projectInfo,
445 version,
446 component
447 )
448 styleSheets = listOf("projects")
449 render("issue-view")
450 }
451 }
452 }
454 private fun issueForm(http: HttpRequest, dao: DataAccessObject) {
455 withPathInfo(http, dao)?.run {
456 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1) ?: Issue(
457 -1,
458 project,
459 )
461 // pre-select component, if available in the path info
462 issue.component = component
464 // pre-select version, if available in the path info
465 if (version != null) {
466 if (version.status.isReleased) {
467 issue.affectedVersions = listOf(version)
468 } else {
469 issue.resolvedVersions = listOf(version)
470 }
471 }
473 with(http) {
474 view = IssueEditView(
475 issue,
476 projectInfo.versions,
477 projectInfo.components,
478 dao.listUsers(),
479 project,
480 version,
481 component
482 )
483 feedPath = feedPath(projectInfo.project)
484 navigationMenu = activeProjectNavMenu(
485 dao.listProjects(),
486 projectInfo,
487 version,
488 component
489 )
490 styleSheets = listOf("projects")
491 render("issue-form")
492 }
493 }
494 }
496 private fun issueComment(http: HttpRequest, dao: DataAccessObject) {
497 withPathInfo(http, dao)?.run {
498 val issue = dao.findIssue(http.pathParams["issue"]?.toIntOrNull() ?: -1)
499 if (issue == null) {
500 http.response.sendError(404)
501 return
502 }
504 // TODO: throw validator exception instead of using a default
505 val comment = IssueComment(-1, issue.id).apply {
506 author = http.remoteUser?.let { dao.findUserByName(it) }
507 comment = http.param("comment") ?: ""
508 }
510 dao.insertComment(comment)
512 http.renderCommit("${issuesHref}${issue.id}")
513 }
514 }
516 private fun issueCommit(http: HttpRequest, dao: DataAccessObject) {
517 withPathInfo(http, dao)?.run {
518 // TODO: throw validator exception instead of using defaults
519 val issue = Issue(
520 http.param("id")?.toIntOrNull() ?: -1,
521 project
522 ).apply {
523 component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1)
524 category = IssueCategory.valueOf(http.param("category") ?: "")
525 status = IssueStatus.valueOf(http.param("status") ?: "")
526 subject = http.param("subject") ?: ""
527 description = http.param("description") ?: ""
528 assignee = http.param("assignee")?.toIntOrNull()?.let {
529 when (it) {
530 -1 -> null
531 -2 -> component?.lead
532 else -> dao.findUser(it)
533 }
534 }
535 eta = http.param("eta")?.let { if (it.isBlank()) null else Date.valueOf(it) }
537 affectedVersions = http.paramArray("affected")
538 .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, project.id) } }
539 resolvedVersions = http.paramArray("resolved")
540 .mapNotNull { param -> param.toIntOrNull()?.let { Version(it, project.id) } }
541 }
543 val openId = if (issue.id < 0) {
544 dao.insertIssue(issue)
545 } else {
546 dao.updateIssue(issue)
547 issue.id
548 }
550 if (http.param("more") != null) {
551 http.renderCommit("${issuesHref}-/create")
552 } else {
553 http.renderCommit("${issuesHref}${openId}")
554 }
555 }
556 }
557 }