src/main/kotlin/de/uapcore/lightpit/servlet/ProjectServlet.kt

Sun, 08 Jan 2023 19:32:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Jan 2023 19:32:11 +0100
changeset 271
f8f5e82944fa
parent 268
ca5501d851fa
child 284
671c1c8fbf1c
permissions
-rw-r--r--

#15 add sort options

     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.boolValidator
    31 import de.uapcore.lightpit.dao.DataAccessObject
    32 import de.uapcore.lightpit.dateOptValidator
    33 import de.uapcore.lightpit.entities.*
    34 import de.uapcore.lightpit.types.*
    35 import de.uapcore.lightpit.viewmodel.*
    36 import jakarta.servlet.annotation.WebServlet
    37 import java.sql.Date
    39 @WebServlet(urlPatterns = ["/projects/*"])
    40 class ProjectServlet : AbstractServlet() {
    42     init {
    43         get("/", this::projects)
    44         get("/%project", this::project)
    45         get("/%project/issues/%version/%component/", this::project)
    46         get("/%project/edit", this::projectForm)
    47         get("/-/create", this::projectForm)
    48         post("/-/commit", this::projectCommit)
    50         get("/%project/versions/", this::versions)
    51         get("/%project/versions/%version/edit", this::versionForm)
    52         get("/%project/versions/-/create", this::versionForm)
    53         post("/%project/versions/-/commit", this::versionCommit)
    55         get("/%project/components/", this::components)
    56         get("/%project/components/%component/edit", this::componentForm)
    57         get("/%project/components/-/create", this::componentForm)
    58         post("/%project/components/-/commit", this::componentCommit)
    60         get("/%project/issues/%version/%component/%issue", this::issue)
    61         get("/%project/issues/%version/%component/%issue/edit", this::issueForm)
    62         post("/%project/issues/%version/%component/%issue/comment", this::issueComment)
    63         post("/%project/issues/%version/%component/%issue/relation", this::issueRelation)
    64         get("/%project/issues/%version/%component/%issue/removeRelation", this::issueRemoveRelation)
    65         get("/%project/issues/%version/%component/-/create", this::issueForm)
    66         post("/%project/issues/%version/%component/-/commit", this::issueCommit)
    67     }
    69     private fun projects(http: HttpRequest, dao: DataAccessObject) {
    70         val projects = dao.listProjects()
    71         val projectInfos = projects.map {
    72             ProjectInfo(
    73                 project = it,
    74                 versions = dao.listVersions(it),
    75                 components = emptyList(), // not required in this view
    76                 issueSummary = dao.collectIssueSummary(it)
    77             )
    78         }
    80         with(http) {
    81             view = ProjectsView(projectInfos)
    82             navigationMenu = projectNavMenu(projects)
    83             styleSheets = listOf("projects")
    84             render("projects")
    85         }
    86     }
    88     private fun activeProjectNavMenu(
    89         projects: List<Project>,
    90         projectInfo: ProjectInfo,
    91         selectedVersion: Version? = null,
    92         selectedComponent: Component? = null
    93     ) =
    94         projectNavMenu(
    95             projects,
    96             projectInfo.versions,
    97             projectInfo.components,
    98             projectInfo.project,
    99             selectedVersion,
   100             selectedComponent
   101         )
   103     private sealed interface LookupResult<T>
   104     private class NotFound<T> : LookupResult<T>
   105     private data class Found<T>(val elem: T?) : LookupResult<T>
   107     private fun <T : HasNode> HttpRequest.lookupPathParam(paramName: String, list: List<T>): LookupResult<T> {
   108         val node = pathParams[paramName]
   109         return if (node == null || node == "-") {
   110             Found(null)
   111         } else {
   112             val result = list.find { it.node == node }
   113             if (result == null) {
   114                 NotFound()
   115             } else {
   116                 Found(result)
   117             }
   118         }
   119     }
   121     private fun obtainProjectInfo(http: HttpRequest, dao: DataAccessObject): ProjectInfo? {
   122         val project = dao.findProjectByNode(http.pathParams["project"] ?: "") ?: return null
   124         val versions: List<Version> = dao.listVersions(project)
   125         val components: List<Component> = dao.listComponents(project)
   127         return ProjectInfo(
   128             project,
   129             versions,
   130             components,
   131             dao.collectIssueSummary(project)
   132         )
   133     }
   135     private fun sanitizeNode(name: String): String {
   136         val san = name.replace(Regex("[/\\\\]"), "-")
   137         return if (san.startsWith(".")) {
   138             "v$san"
   139         } else {
   140             san
   141         }
   142     }
   144     private fun feedPath(project: Project) = "feed/${project.node}/issues.rss"
   146     private data class PathInfos(
   147         val projectInfo: ProjectInfo,
   148         val version: Version?,
   149         val component: Component?
   150     ) {
   151         val project = projectInfo.project
   152         val issuesHref by lazyOf("projects/${project.node}/issues/${version?.node ?: "-"}/${component?.node ?: "-"}/")
   153     }
   155     private fun withPathInfo(http: HttpRequest, dao: DataAccessObject): PathInfos? {
   156         val projectInfo = obtainProjectInfo(http, dao)
   157         if (projectInfo == null) {
   158             http.response.sendError(404)
   159             return null
   160         }
   162         val version = when (val result = http.lookupPathParam("version", projectInfo.versions)) {
   163             is NotFound -> {
   164                 http.response.sendError(404)
   165                 return null
   166             }
   167             is Found -> {
   168                 result.elem
   169             }
   170         }
   171         val component = when (val result = http.lookupPathParam("component", projectInfo.components)) {
   172             is NotFound -> {
   173                 http.response.sendError(404)
   174                 return null
   175             }
   176             is Found -> {
   177                 result.elem
   178             }
   179         }
   181         return PathInfos(projectInfo, version, component)
   182     }
   184     private fun project(http: HttpRequest, dao: DataAccessObject) {
   185         withPathInfo(http, dao)?.run {
   187             val filter = IssueFilter(http)
   189             val needRelationsMap = filter.onlyBlocker
   191             val relationsMap = if (needRelationsMap) dao.getIssueRelationMap(project, filter.includeDone) else emptyMap()
   193             val issues = dao.listIssues(project, filter.includeDone, version, component)
   194                 .sortedWith(IssueSorter(filter.sortPrimary, filter.sortSecondary, filter.sortTertiary))
   195                 .filter {
   196                     (!filter.onlyMine || (it.assignee?.username ?: "") == (http.remoteUser ?: "<Anonymous>")) &&
   197                     (!filter.onlyBlocker || (relationsMap[it.id]?.any { (_,type) -> type.blocking }?:false)) &&
   198                     (filter.status.isEmpty() || filter.status.contains(it.status)) &&
   199                     (filter.category.isEmpty() || filter.category.contains(it.category))
   200                 }
   202             with(http) {
   203                 pageTitle = project.name
   204                 view = ProjectDetails(projectInfo, issues, filter, version, component)
   205                 feedPath = feedPath(project)
   206                 navigationMenu = activeProjectNavMenu(
   207                     dao.listProjects(),
   208                     projectInfo,
   209                     version,
   210                     component
   211                 )
   212                 styleSheets = listOf("projects")
   213                 javascript = "project-details"
   214                 render("project-details")
   215             }
   216         }
   217     }
   219     private fun projectForm(http: HttpRequest, dao: DataAccessObject) {
   220         if (!http.pathParams.containsKey("project")) {
   221             http.view = ProjectEditView(Project(-1), dao.listUsers())
   222             http.navigationMenu = projectNavMenu(dao.listProjects())
   223         } else {
   224             val projectInfo = obtainProjectInfo(http, dao)
   225             if (projectInfo == null) {
   226                 http.response.sendError(404)
   227                 return
   228             }
   229             http.view = ProjectEditView(projectInfo.project, dao.listUsers())
   230             http.navigationMenu = activeProjectNavMenu(
   231                 dao.listProjects(),
   232                 projectInfo
   233             )
   234         }
   235         http.styleSheets = listOf("projects")
   236         http.render("project-form")
   237     }
   239     private fun projectCommit(http: HttpRequest, dao: DataAccessObject) {
   240         val project = Project(http.param("id")?.toIntOrNull() ?: -1).apply {
   241             name = http.param("name") ?: ""
   242             node = http.param("node") ?: ""
   243             description = http.param("description") ?: ""
   244             ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
   245             repoUrl = http.param("repoUrl") ?: ""
   246             owner = (http.param("owner")?.toIntOrNull() ?: -1).let {
   247                 if (it < 0) null else dao.findUser(it)
   248             }
   249             // intentional defaults
   250             if (node.isBlank()) node = name
   251             // sanitizing
   252             node = sanitizeNode(node)
   253         }
   255         if (project.id < 0) {
   256             dao.insertProject(project)
   257         } else {
   258             dao.updateProject(project)
   259         }
   261         http.renderCommit("projects/${project.node}")
   262     }
   264     private fun versions(http: HttpRequest, dao: DataAccessObject) {
   265         val projectInfo = obtainProjectInfo(http, dao)
   266         if (projectInfo == null) {
   267             http.response.sendError(404)
   268             return
   269         }
   271         with(http) {
   272             pageTitle = "${projectInfo.project.name} - ${i18n("navmenu.versions")}"
   273             view = VersionsView(
   274                 projectInfo,
   275                 dao.listVersionSummaries(projectInfo.project)
   276             )
   277             feedPath = feedPath(projectInfo.project)
   278             navigationMenu = activeProjectNavMenu(
   279                 dao.listProjects(),
   280                 projectInfo
   281             )
   282             styleSheets = listOf("projects")
   283             javascript = "project-details"
   284             render("versions")
   285         }
   286     }
   288     private fun versionForm(http: HttpRequest, dao: DataAccessObject) {
   289         val projectInfo = obtainProjectInfo(http, dao)
   290         if (projectInfo == null) {
   291             http.response.sendError(404)
   292             return
   293         }
   295         val version: Version
   296         when (val result = http.lookupPathParam("version", projectInfo.versions)) {
   297             is NotFound -> {
   298                 http.response.sendError(404)
   299                 return
   300             }
   301             is Found -> {
   302                 version = result.elem ?: Version(-1, projectInfo.project.id)
   303             }
   304         }
   306         with(http) {
   307             view = VersionEditView(projectInfo, version)
   308             feedPath = feedPath(projectInfo.project)
   309             navigationMenu = activeProjectNavMenu(
   310                 dao.listProjects(),
   311                 projectInfo,
   312                 selectedVersion = version
   313             )
   314             styleSheets = listOf("projects")
   315             render("version-form")
   316         }
   317     }
   319     private fun obtainIdAndProject(http: HttpRequest, dao: DataAccessObject): Pair<Int, Project>? {
   320         val id = http.param("id")?.toIntOrNull()
   321         val projectid = http.param("projectid")?.toIntOrNull() ?: -1
   322         val project = dao.findProject(projectid)
   323         return if (id == null || project == null) {
   324             http.response.sendError(400)
   325             null
   326         } else {
   327             Pair(id, project)
   328         }
   329     }
   331     private fun versionCommit(http: HttpRequest, dao: DataAccessObject) {
   332         val idParams = obtainIdAndProject(http, dao) ?: return
   333         val (id, project) = idParams
   335         val version = Version(id, project.id).apply {
   336             name = http.param("name") ?: ""
   337             node = http.param("node") ?: ""
   338             ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
   339             status = http.param("status")?.let(VersionStatus::valueOf) ?: VersionStatus.Future
   340             // TODO: process error messages
   341             eol = http.param("eol", ::dateOptValidator, null, mutableListOf())
   342             release = http.param("release", ::dateOptValidator, null, mutableListOf())
   343             // intentional defaults
   344             if (node.isBlank()) node = name
   345             // sanitizing
   346             node = sanitizeNode(node)
   347         }
   349         // sanitize eol and release date
   350         if (version.status.isEndOfLife) {
   351             if (version.eol == null) version.eol = Date(System.currentTimeMillis())
   352         } else if (version.status.isReleased) {
   353             if (version.release == null) version.release = Date(System.currentTimeMillis())
   354         }
   356         if (id < 0) {
   357             dao.insertVersion(version)
   358         } else {
   359             dao.updateVersion(version)
   360         }
   362         http.renderCommit("projects/${project.node}/versions/")
   363     }
   365     private fun components(http: HttpRequest, dao: DataAccessObject) {
   366         val projectInfo = obtainProjectInfo(http, dao)
   367         if (projectInfo == null) {
   368             http.response.sendError(404)
   369             return
   370         }
   372         with(http) {
   373             pageTitle = "${projectInfo.project.name} - ${i18n("navmenu.components")}"
   374             view = ComponentsView(
   375                 projectInfo,
   376                 dao.listComponentSummaries(projectInfo.project)
   377             )
   378             feedPath = feedPath(projectInfo.project)
   379             navigationMenu = activeProjectNavMenu(
   380                 dao.listProjects(),
   381                 projectInfo
   382             )
   383             styleSheets = listOf("projects")
   384             javascript = "project-details"
   385             render("components")
   386         }
   387     }
   389     private fun componentForm(http: HttpRequest, dao: DataAccessObject) {
   390         val projectInfo = obtainProjectInfo(http, dao)
   391         if (projectInfo == null) {
   392             http.response.sendError(404)
   393             return
   394         }
   396         val component: Component
   397         when (val result = http.lookupPathParam("component", projectInfo.components)) {
   398             is NotFound -> {
   399                 http.response.sendError(404)
   400                 return
   401             }
   402             is Found -> {
   403                 component = result.elem ?: Component(-1, projectInfo.project.id)
   404             }
   405         }
   407         with(http) {
   408             view = ComponentEditView(projectInfo, component, dao.listUsers())
   409             feedPath = feedPath(projectInfo.project)
   410             navigationMenu = activeProjectNavMenu(
   411                 dao.listProjects(),
   412                 projectInfo,
   413                 selectedComponent = component
   414             )
   415             styleSheets = listOf("projects")
   416             render("component-form")
   417         }
   418     }
   420     private fun componentCommit(http: HttpRequest, dao: DataAccessObject) {
   421         val idParams = obtainIdAndProject(http, dao) ?: return
   422         val (id, project) = idParams
   424         val component = Component(id, project.id).apply {
   425             name = http.param("name") ?: ""
   426             node = http.param("node") ?: ""
   427             ordinal = http.param("ordinal")?.toIntOrNull() ?: 0
   428             color = WebColor(http.param("color") ?: "#000000")
   429             description = http.param("description")
   430             // TODO: process error message
   431             active = http.param("active", ::boolValidator, true, mutableListOf())
   432             lead = (http.param("lead")?.toIntOrNull() ?: -1).let {
   433                 if (it < 0) null else dao.findUser(it)
   434             }
   435             // intentional defaults
   436             if (node.isBlank()) node = name
   437             // sanitizing
   438             node = sanitizeNode(node)
   439         }
   441         if (id < 0) {
   442             dao.insertComponent(component)
   443         } else {
   444             dao.updateComponent(component)
   445         }
   447         http.renderCommit("projects/${project.node}/components/")
   448     }
   450     private fun issue(http: HttpRequest, dao: DataAccessObject) {
   451         val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue)
   452         if (issue == null) {
   453             http.response.sendError(404)
   454             return
   455         }
   456         renderIssueView(http, dao, issue)
   457     }
   459     private fun renderIssueView(
   460         http: HttpRequest,
   461         dao: DataAccessObject,
   462         issue: Issue,
   463         relationError: String? = null
   464     ) {
   465         withPathInfo(http, dao)?.run {
   466             val comments = dao.listComments(issue)
   468             with(http) {
   469                 pageTitle = "${projectInfo.project.name}: #${issue.id} ${issue.subject}"
   470                 view = IssueDetailView(
   471                     issue,
   472                     comments,
   473                     project,
   474                     version,
   475                     component,
   476                     dao.listIssues(project, true),
   477                     dao.listIssueRelations(issue),
   478                     relationError
   479                 )
   480                 feedPath = feedPath(projectInfo.project)
   481                 navigationMenu = activeProjectNavMenu(
   482                     dao.listProjects(),
   483                     projectInfo,
   484                     version,
   485                     component
   486                 )
   487                 styleSheets = listOf("projects")
   488                 javascript = "issue-editor"
   489                 render("issue-view")
   490             }
   491         }
   492     }
   494     private fun issueForm(http: HttpRequest, dao: DataAccessObject) {
   495         withPathInfo(http, dao)?.run {
   496             val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue) ?: Issue(
   497                 -1,
   498                 project,
   499             )
   501             // for new issues set some defaults
   502             if (issue.id < 0) {
   503                 // pre-select component, if available in the path info
   504                 issue.component = component
   506                 // pre-select version, if available in the path info
   507                 if (version != null) {
   508                     if (version.status.isReleased) {
   509                         issue.affected = version
   510                     } else {
   511                         issue.resolved = version
   512                     }
   513                 }
   514             }
   516             with(http) {
   517                 view = IssueEditView(
   518                     issue,
   519                     projectInfo.versions,
   520                     projectInfo.components,
   521                     dao.listUsers(),
   522                     project,
   523                     version,
   524                     component
   525                 )
   526                 feedPath = feedPath(projectInfo.project)
   527                 navigationMenu = activeProjectNavMenu(
   528                     dao.listProjects(),
   529                     projectInfo,
   530                     version,
   531                     component
   532                 )
   533                 styleSheets = listOf("projects")
   534                 javascript = "issue-editor"
   535                 render("issue-form")
   536             }
   537         }
   538     }
   540     private fun issueComment(http: HttpRequest, dao: DataAccessObject) {
   541         withPathInfo(http, dao)?.run {
   542             val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue)
   543             if (issue == null) {
   544                 http.response.sendError(404)
   545                 return
   546             }
   548             val commentId = http.param("commentid")?.toIntOrNull() ?: -1
   549             if (commentId > 0) {
   550                 val comment = dao.findComment(commentId)
   551                 if (comment == null) {
   552                     http.response.sendError(404)
   553                     return
   554                 }
   555                 val originalAuthor = comment.author?.username
   556                 if (originalAuthor != null && originalAuthor == http.remoteUser) {
   557                     val newComment = http.param("comment")
   558                     if (!newComment.isNullOrBlank()) {
   559                         comment.comment = newComment
   560                         dao.updateComment(comment)
   561                         dao.insertHistoryEvent(issue, comment)
   562                     } else {
   563                         logger.debug("Not updating comment ${comment.id} because nothing changed.")
   564                     }
   565                 } else {
   566                     http.response.sendError(403)
   567                     return
   568                 }
   569             } else {
   570                 val comment = IssueComment(-1, issue.id).apply {
   571                     author = http.remoteUser?.let { dao.findUserByName(it) }
   572                     comment = http.param("comment") ?: ""
   573                 }
   574                 val newId = dao.insertComment(comment)
   575                 dao.insertHistoryEvent(issue, comment, newId)
   576             }
   578             http.renderCommit("${issuesHref}${issue.id}")
   579         }
   580     }
   582     private fun issueCommit(http: HttpRequest, dao: DataAccessObject) {
   583         withPathInfo(http, dao)?.run {
   584             val issue = Issue(
   585                 http.param("id")?.toIntOrNull() ?: -1,
   586                 project
   587             ).apply {
   588                 component = dao.findComponent(http.param("component")?.toIntOrNull() ?: -1)
   589                 category = IssueCategory.valueOf(http.param("category") ?: "")
   590                 status = IssueStatus.valueOf(http.param("status") ?: "")
   591                 subject = http.param("subject") ?: ""
   592                 description = http.param("description") ?: ""
   593                 assignee = http.param("assignee")?.toIntOrNull()?.let {
   594                     when (it) {
   595                         -1 -> null
   596                         -2 -> component?.lead
   597                         else -> dao.findUser(it)
   598                     }
   599                 }
   600                 // TODO: process error messages
   601                 eta = http.param("eta", ::dateOptValidator, null, mutableListOf())
   603                 affected = http.param("affected")?.toIntOrNull()?.takeIf { it > 0 }?.let { Version(it, project.id) }
   604                 resolved = http.param("resolved")?.toIntOrNull()?.takeIf { it > 0 }?.let { Version(it, project.id) }
   605             }
   607             val openId = if (issue.id < 0) {
   608                 val id = dao.insertIssue(issue)
   609                 dao.insertHistoryEvent(issue, id)
   610                 id
   611             } else {
   612                 val reference = dao.findIssue(issue.id)
   613                 if (reference == null) {
   614                     http.response.sendError(404)
   615                     return
   616                 }
   618                 if (issue.hasChanged(reference)) {
   619                     dao.updateIssue(issue)
   620                     dao.insertHistoryEvent(issue)
   621                 } else {
   622                     logger.debug("Not updating issue ${issue.id} because nothing changed.")
   623                 }
   625                 val newComment = http.param("comment")
   626                 if (!newComment.isNullOrBlank()) {
   627                     val comment = IssueComment(-1, issue.id).apply {
   628                         author = http.remoteUser?.let { dao.findUserByName(it) }
   629                         comment = newComment
   630                     }
   631                     val commentid = dao.insertComment(comment)
   632                     dao.insertHistoryEvent(issue, comment, commentid)
   633                 }
   634                 issue.id
   635             }
   637             if (http.param("more") != null) {
   638                 http.renderCommit("${issuesHref}-/create")
   639             } else {
   640                 http.renderCommit("${issuesHref}${openId}")
   641             }
   642         }
   643     }
   645     private fun issueRelation(http: HttpRequest, dao: DataAccessObject) {
   646         withPathInfo(http, dao)?.run {
   647             val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue)
   648             if (issue == null) {
   649                 http.response.sendError(404)
   650                 return
   651             }
   653             // determine the relation type
   654             val type: Pair<RelationType, Boolean>? = http.param("type")?.let {
   655                 try {
   656                     if (it.startsWith("!")) {
   657                         Pair(RelationType.valueOf(it.substring(1)), true)
   658                     } else {
   659                         Pair(RelationType.valueOf(it), false)
   660                     }
   661                 } catch (_: IllegalArgumentException) {
   662                     null
   663                 }
   664             }
   666             // if the relation type was invalid, send HTTP 500
   667             if (type == null) {
   668                 http.response.sendError(500)
   669                 return
   670             }
   672             // determine the target issue
   673             val targetIssue: Issue? = http.param("issue")?.let {
   674                 if (it.startsWith("#") && it.length > 1) {
   675                     it.substring(1).split(" ", limit = 2)[0].toIntOrNull()
   676                         ?.let(dao::findIssue)
   677                         ?.takeIf { target -> target.project.id == issue.project.id }
   678                 } else {
   679                     null
   680                 }
   681             }
   683             // check if the target issue is valid
   684             if (targetIssue == null) {
   685                 renderIssueView(http, dao, issue, "issue.relations.target.invalid")
   686                 return
   687             }
   689             // commit the result
   690             dao.insertIssueRelation(IssueRelation(issue, targetIssue, type.first, type.second))
   691             http.renderCommit("${issuesHref}${issue.id}")
   692         }
   693     }
   695     private fun issueRemoveRelation(http: HttpRequest, dao: DataAccessObject) {
   696         withPathInfo(http, dao)?.run {
   697             val issue = http.pathParams["issue"]?.toIntOrNull()?.let(dao::findIssue)
   698             if (issue == null) {
   699                 http.response.sendError(404)
   700                 return
   701             }
   703             // determine relation
   704             val type = http.param("type")?.let {
   705                 try {RelationType.valueOf(it)}
   706                 catch (_:IllegalArgumentException) {null}
   707             }
   708             if (type == null) {
   709                 http.response.sendError(500)
   710                 return
   711             }
   712             val rel = http.param("to")?.toIntOrNull()?.let(dao::findIssue)?.let {
   713                 IssueRelation(
   714                     issue,
   715                     it,
   716                     type,
   717                     http.param("reverse")?.toBoolean() ?: false
   718                 )
   719             }
   721             // execute removal, if there is something to remove
   722             rel?.run(dao::deleteIssueRelation)
   724             // always pretend that the operation was successful - if there was nothing to remove, it's okay
   725             http.renderCommit("${issuesHref}${issue.id}")
   726         }
   727     }
   728 }

mercurial