Fri, 18 Dec 2020 16:16:54 +0100
Add mailto link to the display name above comments - fixes #112
universe@41 | 1 | /* |
universe@41 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@41 | 3 | * |
universe@41 | 4 | * Copyright 2018 Mike Becker. All rights reserved. |
universe@41 | 5 | * |
universe@41 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@41 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@41 | 8 | * |
universe@41 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@41 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@41 | 11 | * |
universe@41 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@41 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@41 | 14 | * documentation and/or other materials provided with the distribution. |
universe@41 | 15 | * |
universe@41 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@41 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@41 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@41 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@41 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@41 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@41 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@41 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@41 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@41 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@41 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@41 | 27 | * |
universe@41 | 28 | */ |
universe@41 | 29 | package de.uapcore.lightpit.modules; |
universe@41 | 30 | |
universe@41 | 31 | |
universe@41 | 32 | import de.uapcore.lightpit.*; |
mike@159 | 33 | import de.uapcore.lightpit.dao.DaoProvider; |
universe@64 | 34 | import de.uapcore.lightpit.entities.*; |
universe@134 | 35 | import de.uapcore.lightpit.types.WebColor; |
universe@86 | 36 | import de.uapcore.lightpit.viewmodel.*; |
universe@121 | 37 | import de.uapcore.lightpit.viewmodel.util.IssueSorter; |
universe@59 | 38 | import org.slf4j.Logger; |
universe@59 | 39 | import org.slf4j.LoggerFactory; |
universe@41 | 40 | |
universe@157 | 41 | import javax.servlet.ServletException; |
universe@41 | 42 | import javax.servlet.annotation.WebServlet; |
universe@41 | 43 | import javax.servlet.http.HttpServletRequest; |
universe@59 | 44 | import javax.servlet.http.HttpServletResponse; |
universe@59 | 45 | import java.io.IOException; |
universe@75 | 46 | import java.sql.Date; |
universe@47 | 47 | import java.sql.SQLException; |
universe@134 | 48 | import java.util.List; |
universe@86 | 49 | import java.util.NoSuchElementException; |
universe@136 | 50 | import java.util.Optional; |
universe@83 | 51 | import java.util.stream.Collectors; |
universe@83 | 52 | import java.util.stream.Stream; |
universe@41 | 53 | |
universe@41 | 54 | @WebServlet( |
universe@41 | 55 | name = "ProjectsModule", |
universe@41 | 56 | urlPatterns = "/projects/*" |
universe@41 | 57 | ) |
universe@41 | 58 | public final class ProjectsModule extends AbstractLightPITServlet { |
universe@41 | 59 | |
universe@59 | 60 | private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); |
universe@59 | 61 | |
universe@78 | 62 | @Override |
universe@78 | 63 | protected String getResourceBundleName() { |
universe@78 | 64 | return "localization.projects"; |
universe@78 | 65 | } |
universe@71 | 66 | |
universe@158 | 67 | private static int parseIntOrZero(String str) { |
universe@158 | 68 | try { |
universe@158 | 69 | return Integer.parseInt(str); |
universe@158 | 70 | } catch (NumberFormatException ex) { |
universe@158 | 71 | return 0; |
universe@158 | 72 | } |
universe@158 | 73 | } |
universe@158 | 74 | |
universe@162 | 75 | private void populate(ProjectView viewModel, PathParameters pathParameters, DaoProvider dao) { |
universe@99 | 76 | final var projectDao = dao.getProjectDao(); |
universe@99 | 77 | final var versionDao = dao.getVersionDao(); |
universe@129 | 78 | final var componentDao = dao.getComponentDao(); |
universe@99 | 79 | |
universe@99 | 80 | projectDao.list().stream().map(ProjectInfo::new).forEach(viewModel.getProjectList()::add); |
universe@99 | 81 | |
universe@131 | 82 | if (pathParameters == null) |
universe@131 | 83 | return; |
universe@131 | 84 | |
universe@99 | 85 | // Select Project |
universe@138 | 86 | final var project = projectDao.findByNode(pathParameters.get("project")); |
universe@138 | 87 | if (project == null) |
universe@138 | 88 | return; |
universe@138 | 89 | |
universe@138 | 90 | final var info = new ProjectInfo(project); |
universe@138 | 91 | info.setVersions(versionDao.list(project)); |
universe@138 | 92 | info.setComponents(componentDao.list(project)); |
universe@138 | 93 | info.setIssueSummary(projectDao.getIssueSummary(project)); |
universe@138 | 94 | viewModel.setProjectInfo(info); |
universe@99 | 95 | |
universe@99 | 96 | // Select Version |
universe@138 | 97 | final var versionNode = pathParameters.get("version"); |
universe@161 | 98 | if (versionNode != null) { |
universe@161 | 99 | if ("no-version".equals(versionNode)) { |
universe@161 | 100 | viewModel.setVersionFilter(ProjectView.NO_VERSION); |
universe@161 | 101 | } else if ("all-versions".equals(versionNode)) { |
universe@161 | 102 | viewModel.setVersionFilter(ProjectView.ALL_VERSIONS); |
universe@161 | 103 | } else { |
universe@161 | 104 | viewModel.setVersionFilter(versionDao.findByNode(project, versionNode)); |
universe@161 | 105 | } |
universe@131 | 106 | } |
universe@131 | 107 | |
universe@131 | 108 | // Select Component |
universe@138 | 109 | final var componentNode = pathParameters.get("component"); |
universe@161 | 110 | if (componentNode != null) { |
universe@161 | 111 | if ("no-component".equals(componentNode)) { |
universe@161 | 112 | viewModel.setComponentFilter(ProjectView.NO_COMPONENT); |
universe@161 | 113 | } else if ("all-components".equals(componentNode)) { |
universe@161 | 114 | viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS); |
universe@161 | 115 | } else { |
universe@161 | 116 | viewModel.setComponentFilter(componentDao.findByNode(project, componentNode)); |
universe@161 | 117 | } |
universe@138 | 118 | } |
universe@138 | 119 | } |
universe@138 | 120 | |
universe@138 | 121 | private static String sanitizeNode(String node, String defaultValue) { |
universe@138 | 122 | String result = node == null || node.isBlank() ? defaultValue : node; |
universe@138 | 123 | result = result.replace('/', '-'); |
universe@138 | 124 | if (result.equals(".") || result.equals("..")) { |
universe@138 | 125 | return "_"+result; |
universe@138 | 126 | } else { |
universe@138 | 127 | return result; |
universe@129 | 128 | } |
universe@99 | 129 | } |
universe@99 | 130 | |
universe@157 | 131 | private void forwardView(HttpServletRequest req, HttpServletResponse resp, ProjectView viewModel, String name) throws ServletException, IOException { |
universe@99 | 132 | setViewModel(req, viewModel); |
universe@99 | 133 | setContentPage(req, name); |
universe@99 | 134 | setStylesheet(req, "projects"); |
universe@109 | 135 | setNavigationMenu(req, "project-navmenu"); |
universe@157 | 136 | renderSite(req, resp); |
universe@64 | 137 | } |
universe@64 | 138 | |
universe@61 | 139 | @RequestMapping(method = HttpMethod.GET) |
mike@159 | 140 | public void index(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException { |
universe@99 | 141 | final var viewModel = new ProjectView(); |
universe@131 | 142 | populate(viewModel, null, dao); |
universe@86 | 143 | |
universe@86 | 144 | final var projectDao = dao.getProjectDao(); |
universe@86 | 145 | final var versionDao = dao.getVersionDao(); |
universe@86 | 146 | |
universe@99 | 147 | for (var info : viewModel.getProjectList()) { |
universe@99 | 148 | info.setVersions(versionDao.list(info.getProject())); |
universe@99 | 149 | info.setIssueSummary(projectDao.getIssueSummary(info.getProject())); |
universe@86 | 150 | } |
universe@86 | 151 | |
universe@157 | 152 | forwardView(req, resp, viewModel, "projects"); |
universe@45 | 153 | } |
universe@45 | 154 | |
mike@159 | 155 | private void configureProjectEditor(ProjectEditView viewModel, Project project, DaoProvider dao) throws SQLException { |
universe@99 | 156 | viewModel.setProject(project); |
universe@86 | 157 | viewModel.setUsers(dao.getUserDao().list()); |
universe@71 | 158 | } |
universe@71 | 159 | |
universe@131 | 160 | @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET) |
mike@159 | 161 | public void edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@99 | 162 | final var viewModel = new ProjectEditView(); |
universe@131 | 163 | populate(viewModel, pathParams, dao); |
universe@47 | 164 | |
universe@134 | 165 | if (!viewModel.isProjectInfoPresent()) { |
universe@131 | 166 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 167 | return; |
universe@131 | 168 | } |
universe@47 | 169 | |
universe@131 | 170 | configureProjectEditor(viewModel, viewModel.getProjectInfo().getProject(), dao); |
universe@157 | 171 | forwardView(req, resp, viewModel, "project-form"); |
universe@131 | 172 | } |
universe@131 | 173 | |
universe@131 | 174 | @RequestMapping(requestPath = "create", method = HttpMethod.GET) |
mike@159 | 175 | public void create(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws SQLException, ServletException, IOException { |
universe@131 | 176 | final var viewModel = new ProjectEditView(); |
universe@131 | 177 | populate(viewModel, null, dao); |
universe@131 | 178 | configureProjectEditor(viewModel, new Project(-1), dao); |
universe@157 | 179 | forwardView(req, resp, viewModel, "project-form"); |
universe@47 | 180 | } |
universe@47 | 181 | |
universe@47 | 182 | @RequestMapping(requestPath = "commit", method = HttpMethod.POST) |
mike@159 | 183 | public void commit(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@47 | 184 | |
universe@47 | 185 | try { |
universe@131 | 186 | final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@47 | 187 | project.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@138 | 188 | |
universe@138 | 189 | final var node = getParameter(req, String.class, "node").orElse(null); |
universe@138 | 190 | project.setNode(sanitizeNode(node, project.getName())); |
universe@138 | 191 | |
universe@47 | 192 | getParameter(req, String.class, "description").ifPresent(project::setDescription); |
universe@47 | 193 | getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); |
universe@47 | 194 | getParameter(req, Integer.class, "owner").map( |
universe@47 | 195 | ownerId -> ownerId >= 0 ? new User(ownerId) : null |
universe@47 | 196 | ).ifPresent(project::setOwner); |
universe@47 | 197 | |
mike@159 | 198 | final var projectDao = dao.getProjectDao(); |
mike@159 | 199 | if (project.getId() > 0) { |
mike@159 | 200 | // TODO: unused return value |
mike@159 | 201 | projectDao.update(project); |
mike@159 | 202 | } else { |
mike@159 | 203 | projectDao.save(project); |
mike@159 | 204 | } |
universe@47 | 205 | |
universe@118 | 206 | setRedirectLocation(req, "./projects/"); |
universe@74 | 207 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@59 | 208 | LOG.debug("Successfully updated project {}", project.getName()); |
universe@99 | 209 | |
universe@157 | 210 | renderSite(req, resp); |
mike@159 | 211 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 212 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 213 | // TODO: implement - fix issue #21 |
universe@47 | 214 | } |
universe@47 | 215 | } |
universe@47 | 216 | |
universe@134 | 217 | @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET) |
mike@159 | 218 | public void issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DaoProvider dao) throws SQLException, IOException, ServletException { |
universe@99 | 219 | final var viewModel = new ProjectDetailsView(); |
universe@131 | 220 | populate(viewModel, pathParams, dao); |
universe@86 | 221 | |
universe@134 | 222 | if (!viewModel.isEveryFilterValid()) { |
universe@131 | 223 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 224 | return; |
universe@80 | 225 | } |
universe@47 | 226 | |
universe@134 | 227 | final var project = viewModel.getProjectInfo().getProject(); |
universe@134 | 228 | final var version = viewModel.getVersionFilter(); |
universe@134 | 229 | final var component = viewModel.getComponentFilter(); |
universe@134 | 230 | |
universe@86 | 231 | final var issueDao = dao.getIssueDao(); |
universe@70 | 232 | |
universe@134 | 233 | final List<Issue> issues; |
universe@134 | 234 | if (version.equals(ProjectView.NO_VERSION)) { |
universe@134 | 235 | if (component.equals(ProjectView.ALL_COMPONENTS)) { |
universe@134 | 236 | issues = issueDao.list(project, (Version) null); |
universe@134 | 237 | } else if (component.equals(ProjectView.NO_COMPONENT)) { |
universe@134 | 238 | issues = issueDao.list(project, null, null); |
universe@134 | 239 | } else { |
universe@134 | 240 | issues = issueDao.list(project, component, null); |
universe@134 | 241 | } |
universe@134 | 242 | } else if (version.equals(ProjectView.ALL_VERSIONS)) { |
universe@134 | 243 | if (component.equals(ProjectView.ALL_COMPONENTS)) { |
universe@134 | 244 | issues = issueDao.list(project); |
universe@134 | 245 | } else if (component.equals(ProjectView.NO_COMPONENT)) { |
universe@134 | 246 | issues = issueDao.list(project, (Component)null); |
universe@134 | 247 | } else { |
universe@134 | 248 | issues = issueDao.list(project, component); |
universe@134 | 249 | } |
universe@134 | 250 | } else { |
universe@134 | 251 | if (component.equals(ProjectView.ALL_COMPONENTS)) { |
universe@134 | 252 | issues = issueDao.list(project, version); |
universe@134 | 253 | } else if (component.equals(ProjectView.NO_COMPONENT)) { |
universe@134 | 254 | issues = issueDao.list(project, null, version); |
universe@134 | 255 | } else { |
universe@134 | 256 | issues = issueDao.list(project, component, version); |
universe@134 | 257 | } |
universe@134 | 258 | } |
universe@134 | 259 | |
universe@100 | 260 | for (var issue : issues) issueDao.joinVersionInformation(issue); |
universe@121 | 261 | issues.sort(new IssueSorter( |
universe@141 | 262 | new IssueSorter.Criteria(IssueSorter.Field.DONE, true), |
universe@121 | 263 | new IssueSorter.Criteria(IssueSorter.Field.ETA, true), |
universe@121 | 264 | new IssueSorter.Criteria(IssueSorter.Field.UPDATED, false) |
universe@121 | 265 | )); |
universe@134 | 266 | |
universe@134 | 267 | |
universe@134 | 268 | viewModel.getProjectDetails().updateDetails(issues); |
universe@134 | 269 | if (version.getId() > 0) |
universe@134 | 270 | viewModel.getProjectDetails().updateVersionInfo(version); |
universe@80 | 271 | |
universe@157 | 272 | forwardView(req, resp, viewModel, "project-details"); |
universe@71 | 273 | } |
universe@71 | 274 | |
universe@131 | 275 | @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET) |
mike@159 | 276 | public void versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@109 | 277 | final var viewModel = new VersionsView(); |
universe@131 | 278 | populate(viewModel, pathParameters, dao); |
universe@109 | 279 | |
universe@109 | 280 | final var projectInfo = viewModel.getProjectInfo(); |
universe@109 | 281 | if (projectInfo == null) { |
universe@131 | 282 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 283 | return; |
universe@109 | 284 | } |
universe@109 | 285 | |
universe@109 | 286 | final var issueDao = dao.getIssueDao(); |
universe@109 | 287 | final var issues = issueDao.list(projectInfo.getProject()); |
universe@109 | 288 | for (var issue : issues) issueDao.joinVersionInformation(issue); |
universe@109 | 289 | viewModel.update(projectInfo.getVersions(), issues); |
universe@109 | 290 | |
universe@157 | 291 | forwardView(req, resp, viewModel, "versions"); |
universe@109 | 292 | } |
universe@109 | 293 | |
universe@131 | 294 | @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET) |
mike@159 | 295 | public void editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@99 | 296 | final var viewModel = new VersionEditView(); |
universe@131 | 297 | populate(viewModel, pathParameters, dao); |
universe@99 | 298 | |
universe@131 | 299 | if (viewModel.getProjectInfo() == null || viewModel.getVersionFilter() == null) { |
universe@131 | 300 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 301 | return; |
universe@110 | 302 | } |
universe@110 | 303 | |
universe@131 | 304 | viewModel.setVersion(viewModel.getVersionFilter()); |
universe@59 | 305 | |
universe@157 | 306 | forwardView(req, resp, viewModel, "version-form"); |
universe@59 | 307 | } |
universe@59 | 308 | |
universe@131 | 309 | @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET) |
mike@159 | 310 | public void createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@131 | 311 | final var viewModel = new VersionEditView(); |
universe@131 | 312 | populate(viewModel, pathParameters, dao); |
universe@59 | 313 | |
universe@131 | 314 | if (viewModel.getProjectInfo() == null) { |
universe@131 | 315 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 316 | return; |
universe@131 | 317 | } |
universe@131 | 318 | |
universe@152 | 319 | viewModel.setVersion(new Version(-1)); |
universe@131 | 320 | |
universe@157 | 321 | forwardView(req, resp, viewModel, "version-form"); |
universe@131 | 322 | } |
universe@131 | 323 | |
universe@131 | 324 | @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST) |
mike@159 | 325 | public void commitVersion(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@131 | 326 | |
universe@59 | 327 | try { |
universe@138 | 328 | final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@138 | 329 | if (project == null) { |
universe@138 | 330 | // TODO: improve error handling, because not found is not correct for this POST request |
universe@138 | 331 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 332 | return; |
universe@138 | 333 | } |
universe@131 | 334 | final var version = new Version(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@59 | 335 | version.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@138 | 336 | |
universe@138 | 337 | final var node = getParameter(req, String.class, "node").orElse(null); |
universe@138 | 338 | version.setNode(sanitizeNode(node, version.getName())); |
universe@138 | 339 | |
universe@59 | 340 | getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); |
universe@59 | 341 | version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); |
mike@159 | 342 | |
mike@159 | 343 | final var versionDao = dao.getVersionDao(); |
mike@159 | 344 | if (version.getId() > 0) { |
mike@159 | 345 | // TODO: use return value |
mike@159 | 346 | versionDao.update(version); |
mike@159 | 347 | } else { |
mike@159 | 348 | versionDao.save(version, project); |
mike@159 | 349 | } |
universe@59 | 350 | |
universe@138 | 351 | setRedirectLocation(req, "./projects/" + project.getNode() + "/versions/"); |
universe@74 | 352 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@157 | 353 | |
universe@157 | 354 | renderSite(req, resp); |
mike@159 | 355 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 356 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 357 | // TODO: implement - fix issue #21 |
universe@59 | 358 | } |
universe@41 | 359 | } |
universe@64 | 360 | |
universe@134 | 361 | @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET) |
mike@159 | 362 | public void components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@134 | 363 | final var viewModel = new ComponentsView(); |
universe@134 | 364 | populate(viewModel, pathParameters, dao); |
universe@134 | 365 | |
universe@134 | 366 | final var projectInfo = viewModel.getProjectInfo(); |
universe@134 | 367 | if (projectInfo == null) { |
universe@134 | 368 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 369 | return; |
universe@134 | 370 | } |
universe@134 | 371 | |
universe@134 | 372 | final var issueDao = dao.getIssueDao(); |
universe@134 | 373 | final var issues = issueDao.list(projectInfo.getProject()); |
universe@134 | 374 | viewModel.update(projectInfo.getComponents(), issues); |
universe@134 | 375 | |
universe@157 | 376 | forwardView(req, resp, viewModel, "components"); |
universe@134 | 377 | } |
universe@134 | 378 | |
universe@134 | 379 | @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET) |
mike@159 | 380 | public void editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@134 | 381 | final var viewModel = new ComponentEditView(); |
universe@134 | 382 | populate(viewModel, pathParameters, dao); |
universe@134 | 383 | |
universe@134 | 384 | if (viewModel.getProjectInfo() == null || viewModel.getComponentFilter() == null) { |
universe@134 | 385 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 386 | return; |
universe@134 | 387 | } |
universe@134 | 388 | |
universe@134 | 389 | viewModel.setComponent(viewModel.getComponentFilter()); |
universe@134 | 390 | viewModel.setUsers(dao.getUserDao().list()); |
universe@134 | 391 | |
universe@157 | 392 | forwardView(req, resp, viewModel, "component-form"); |
universe@134 | 393 | } |
universe@134 | 394 | |
universe@134 | 395 | @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET) |
mike@159 | 396 | public void createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@134 | 397 | final var viewModel = new ComponentEditView(); |
universe@134 | 398 | populate(viewModel, pathParameters, dao); |
universe@134 | 399 | |
universe@134 | 400 | if (viewModel.getProjectInfo() == null) { |
universe@134 | 401 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 402 | return; |
universe@134 | 403 | } |
universe@134 | 404 | |
universe@134 | 405 | viewModel.setComponent(new Component(-1)); |
universe@134 | 406 | viewModel.setUsers(dao.getUserDao().list()); |
universe@134 | 407 | |
universe@157 | 408 | forwardView(req, resp, viewModel, "component-form"); |
universe@134 | 409 | } |
universe@134 | 410 | |
universe@134 | 411 | @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST) |
mike@159 | 412 | public void commitComponent(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@134 | 413 | |
universe@134 | 414 | try { |
universe@138 | 415 | final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@138 | 416 | if (project == null) { |
universe@138 | 417 | // TODO: improve error handling, because not found is not correct for this POST request |
universe@138 | 418 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 419 | return; |
universe@138 | 420 | } |
universe@134 | 421 | final var component = new Component(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@134 | 422 | component.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@138 | 423 | |
universe@138 | 424 | final var node = getParameter(req, String.class, "node").orElse(null); |
universe@138 | 425 | component.setNode(sanitizeNode(node, component.getName())); |
universe@138 | 426 | |
universe@134 | 427 | component.setColor(getParameter(req, WebColor.class, "color").orElseThrow()); |
universe@134 | 428 | getParameter(req, Integer.class, "ordinal").ifPresent(component::setOrdinal); |
universe@134 | 429 | getParameter(req, Integer.class, "lead").map( |
universe@134 | 430 | userid -> userid >= 0 ? new User(userid) : null |
universe@134 | 431 | ).ifPresent(component::setLead); |
universe@134 | 432 | getParameter(req, String.class, "description").ifPresent(component::setDescription); |
universe@134 | 433 | |
mike@159 | 434 | final var componentDao = dao.getComponentDao(); |
mike@159 | 435 | if (component.getId() > 0) { |
mike@159 | 436 | // TODO: use return value |
mike@159 | 437 | componentDao.update(component); |
mike@159 | 438 | } else { |
mike@159 | 439 | componentDao.save(component, project); |
mike@159 | 440 | } |
universe@134 | 441 | |
universe@138 | 442 | setRedirectLocation(req, "./projects/" + project.getNode() + "/components/"); |
universe@134 | 443 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@157 | 444 | |
universe@157 | 445 | renderSite(req, resp); |
mike@159 | 446 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@134 | 447 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@134 | 448 | // TODO: implement - fix issue #21 |
universe@134 | 449 | } |
universe@134 | 450 | } |
universe@134 | 451 | |
mike@159 | 452 | private void configureIssueEditor(IssueEditView viewModel, Issue issue, DaoProvider dao) throws SQLException { |
universe@134 | 453 | final var project = viewModel.getProjectInfo().getProject(); |
universe@146 | 454 | issue.setProject(project); // automatically set current project for new issues |
universe@99 | 455 | viewModel.setIssue(issue); |
universe@99 | 456 | viewModel.configureVersionSelectors(viewModel.getProjectInfo().getVersions()); |
universe@86 | 457 | viewModel.setUsers(dao.getUserDao().list()); |
universe@134 | 458 | viewModel.setComponents(dao.getComponentDao().list(project)); |
universe@71 | 459 | } |
universe@71 | 460 | |
universe@146 | 461 | @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET) |
mike@159 | 462 | public void viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@146 | 463 | final var viewModel = new IssueDetailView(); |
universe@146 | 464 | populate(viewModel, pathParameters, dao); |
universe@146 | 465 | |
universe@146 | 466 | final var projectInfo = viewModel.getProjectInfo(); |
universe@146 | 467 | if (projectInfo == null) { |
universe@146 | 468 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 469 | return; |
universe@146 | 470 | } |
universe@146 | 471 | |
universe@146 | 472 | final var issueDao = dao.getIssueDao(); |
universe@158 | 473 | final var issue = issueDao.find(parseIntOrZero(pathParameters.get("issue"))); |
universe@146 | 474 | if (issue == null) { |
universe@146 | 475 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 476 | return; |
universe@146 | 477 | } |
universe@146 | 478 | |
universe@146 | 479 | issueDao.joinVersionInformation(issue); |
universe@146 | 480 | viewModel.setIssue(issue); |
universe@146 | 481 | viewModel.setComments(issueDao.listComments(issue)); |
universe@146 | 482 | |
universe@162 | 483 | viewModel.processMarkdown(); |
universe@162 | 484 | |
universe@157 | 485 | forwardView(req, resp, viewModel, "issue-view"); |
universe@146 | 486 | } |
universe@146 | 487 | |
universe@146 | 488 | // TODO: why should the issue editor be child of $project? |
universe@131 | 489 | @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET) |
mike@159 | 490 | public void editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@99 | 491 | final var viewModel = new IssueEditView(); |
universe@131 | 492 | populate(viewModel, pathParameters, dao); |
universe@99 | 493 | |
universe@131 | 494 | final var projectInfo = viewModel.getProjectInfo(); |
universe@131 | 495 | if (projectInfo == null) { |
universe@131 | 496 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 497 | return; |
universe@86 | 498 | } |
universe@64 | 499 | |
universe@131 | 500 | final var issueDao = dao.getIssueDao(); |
universe@158 | 501 | final var issue = issueDao.find(parseIntOrZero(pathParameters.get("issue"))); |
universe@131 | 502 | if (issue == null) { |
universe@131 | 503 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 504 | return; |
universe@131 | 505 | } |
universe@131 | 506 | |
universe@131 | 507 | issueDao.joinVersionInformation(issue); |
universe@133 | 508 | configureIssueEditor(viewModel, issue, dao); |
universe@131 | 509 | |
universe@157 | 510 | forwardView(req, resp, viewModel, "issue-form"); |
universe@64 | 511 | } |
universe@64 | 512 | |
universe@131 | 513 | @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET) |
mike@159 | 514 | public void createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DaoProvider dao) throws IOException, SQLException, ServletException { |
universe@131 | 515 | final var viewModel = new IssueEditView(); |
universe@131 | 516 | populate(viewModel, pathParameters, dao); |
universe@131 | 517 | |
universe@131 | 518 | final var projectInfo = viewModel.getProjectInfo(); |
universe@131 | 519 | if (projectInfo == null) { |
universe@131 | 520 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 521 | return; |
universe@131 | 522 | } |
universe@131 | 523 | |
universe@131 | 524 | final var issue = new Issue(-1); |
universe@131 | 525 | issue.setProject(projectInfo.getProject()); |
universe@133 | 526 | configureIssueEditor(viewModel, issue, dao); |
universe@131 | 527 | |
universe@157 | 528 | forwardView(req, resp, viewModel, "issue-form"); |
universe@131 | 529 | } |
universe@131 | 530 | |
universe@131 | 531 | @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST) |
mike@159 | 532 | public void commitIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@64 | 533 | try { |
universe@131 | 534 | final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@136 | 535 | final var componentId = getParameter(req, Integer.class, "component"); |
universe@136 | 536 | final Component component; |
universe@136 | 537 | if (componentId.isPresent()) { |
universe@136 | 538 | component = dao.getComponentDao().find(componentId.get()); |
universe@136 | 539 | } else { |
universe@136 | 540 | component = null; |
universe@136 | 541 | } |
universe@138 | 542 | final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow()); |
universe@138 | 543 | if (project == null) { |
universe@138 | 544 | // TODO: improve error handling, because not found is not correct for this POST request |
universe@138 | 545 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 546 | return; |
universe@138 | 547 | } |
universe@138 | 548 | issue.setProject(project); |
universe@75 | 549 | getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory); |
universe@75 | 550 | getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus); |
universe@75 | 551 | issue.setSubject(getParameter(req, String.class, "subject").orElseThrow()); |
universe@136 | 552 | issue.setComponent(component); |
universe@136 | 553 | getParameter(req, Integer.class, "assignee").map(userid -> { |
universe@136 | 554 | if (userid >= 0) { |
universe@136 | 555 | return new User(userid); |
universe@136 | 556 | } else if (userid == -2) { |
universe@136 | 557 | return Optional.ofNullable(component).map(Component::getLead).orElse(null); |
universe@136 | 558 | } else { |
universe@136 | 559 | return null; |
universe@136 | 560 | } |
universe@136 | 561 | } |
universe@75 | 562 | ).ifPresent(issue::setAssignee); |
universe@75 | 563 | getParameter(req, String.class, "description").ifPresent(issue::setDescription); |
universe@75 | 564 | getParameter(req, Date.class, "eta").ifPresent(issue::setEta); |
universe@83 | 565 | |
universe@83 | 566 | getParameter(req, Integer[].class, "affected") |
universe@83 | 567 | .map(Stream::of) |
universe@83 | 568 | .map(stream -> |
universe@96 | 569 | stream.map(Version::new).collect(Collectors.toList()) |
universe@83 | 570 | ).ifPresent(issue::setAffectedVersions); |
universe@83 | 571 | getParameter(req, Integer[].class, "resolved") |
universe@83 | 572 | .map(Stream::of) |
universe@83 | 573 | .map(stream -> |
universe@86 | 574 | stream.map(Version::new).collect(Collectors.toList()) |
universe@83 | 575 | ).ifPresent(issue::setResolvedVersions); |
universe@83 | 576 | |
mike@159 | 577 | final var issueDao = dao.getIssueDao(); |
mike@159 | 578 | if (issue.getId() > 0) { |
mike@159 | 579 | // TODO: use return value |
mike@159 | 580 | issueDao.update(issue); |
mike@159 | 581 | } else { |
mike@159 | 582 | issueDao.save(issue, project); |
mike@159 | 583 | } |
universe@64 | 584 | |
universe@146 | 585 | // TODO: fix redirect location |
universe@146 | 586 | setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view"); |
universe@74 | 587 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@124 | 588 | |
universe@157 | 589 | renderSite(req, resp); |
mike@159 | 590 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 591 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 592 | // TODO: implement - fix issue #21 |
universe@64 | 593 | } |
universe@124 | 594 | } |
universe@64 | 595 | |
universe@131 | 596 | @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST) |
mike@159 | 597 | public void commentIssue(HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException, ServletException { |
universe@124 | 598 | final var issueIdParam = getParameter(req, Integer.class, "issueid"); |
universe@124 | 599 | if (issueIdParam.isEmpty()) { |
universe@124 | 600 | resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form."); |
universe@157 | 601 | return; |
universe@124 | 602 | } |
universe@137 | 603 | final var issue = dao.getIssueDao().find(issueIdParam.get()); |
universe@137 | 604 | if (issue == null) { |
universe@137 | 605 | resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
universe@157 | 606 | return; |
universe@137 | 607 | } |
universe@124 | 608 | try { |
universe@150 | 609 | final var issueComment = new IssueComment(getParameter(req, Integer.class, "commentid").orElse(-1)); |
universe@124 | 610 | issueComment.setComment(getParameter(req, String.class, "comment").orElse("")); |
universe@124 | 611 | |
universe@124 | 612 | if (issueComment.getComment().isBlank()) { |
universe@124 | 613 | throw new IllegalArgumentException("comment.null"); |
universe@124 | 614 | } |
universe@124 | 615 | |
universe@124 | 616 | LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId()); |
universe@124 | 617 | if (req.getRemoteUser() != null) { |
mike@159 | 618 | Optional.ofNullable(dao.getUserDao().findByUsername(req.getRemoteUser())).ifPresent(issueComment::setAuthor); |
universe@124 | 619 | } |
universe@124 | 620 | |
universe@150 | 621 | dao.getIssueDao().saveComment(issue, issueComment); |
universe@124 | 622 | |
universe@146 | 623 | // TODO: fix redirect location |
universe@146 | 624 | setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view"); |
universe@124 | 625 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@124 | 626 | |
universe@157 | 627 | renderSite(req, resp); |
mike@159 | 628 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@131 | 629 | resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); |
universe@131 | 630 | // TODO: implement - fix issue #21 |
universe@124 | 631 | } |
universe@64 | 632 | } |
universe@41 | 633 | } |