src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java

Fri, 23 Oct 2020 11:14:37 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 Oct 2020 11:14:37 +0200
changeset 141
8160dfc4dbc3
parent 138
e2aa673dd473
child 146
b0e83cab0bde
permissions
-rw-r--r--

renames sort criteria PHASE to DONE, because phase 0 and 1 are combined

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.*;
universe@41 33 import de.uapcore.lightpit.dao.DataAccessObjects;
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@41 41 import javax.servlet.annotation.WebServlet;
universe@41 42 import javax.servlet.http.HttpServletRequest;
universe@59 43 import javax.servlet.http.HttpServletResponse;
universe@59 44 import java.io.IOException;
universe@75 45 import java.sql.Date;
universe@47 46 import java.sql.SQLException;
universe@134 47 import java.util.List;
universe@86 48 import java.util.NoSuchElementException;
universe@136 49 import java.util.Optional;
universe@83 50 import java.util.stream.Collectors;
universe@83 51 import java.util.stream.Stream;
universe@41 52
universe@41 53 @WebServlet(
universe@41 54 name = "ProjectsModule",
universe@41 55 urlPatterns = "/projects/*"
universe@41 56 )
universe@41 57 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 58
universe@59 59 private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class);
universe@59 60
universe@78 61 @Override
universe@78 62 protected String getResourceBundleName() {
universe@78 63 return "localization.projects";
universe@78 64 }
universe@71 65
universe@131 66 private void populate(ProjectView viewModel, PathParameters pathParameters, DataAccessObjects dao) throws SQLException {
universe@99 67 final var projectDao = dao.getProjectDao();
universe@99 68 final var versionDao = dao.getVersionDao();
universe@129 69 final var componentDao = dao.getComponentDao();
universe@99 70
universe@99 71 projectDao.list().stream().map(ProjectInfo::new).forEach(viewModel.getProjectList()::add);
universe@99 72
universe@131 73 if (pathParameters == null)
universe@131 74 return;
universe@131 75
universe@99 76 // Select Project
universe@138 77 final var project = projectDao.findByNode(pathParameters.get("project"));
universe@138 78 if (project == null)
universe@138 79 return;
universe@138 80
universe@138 81 final var info = new ProjectInfo(project);
universe@138 82 info.setVersions(versionDao.list(project));
universe@138 83 info.setComponents(componentDao.list(project));
universe@138 84 info.setIssueSummary(projectDao.getIssueSummary(project));
universe@138 85 viewModel.setProjectInfo(info);
universe@99 86
universe@99 87 // Select Version
universe@138 88 final var versionNode = pathParameters.get("version");
universe@138 89 if ("no-version".equals(versionNode)) {
universe@134 90 viewModel.setVersionFilter(ProjectView.NO_VERSION);
universe@138 91 } else if ("all-versions".equals(versionNode)) {
universe@134 92 viewModel.setVersionFilter(ProjectView.ALL_VERSIONS);
universe@134 93 } else {
universe@138 94 viewModel.setVersionFilter(versionDao.findByNode(project, versionNode));
universe@131 95 }
universe@131 96
universe@131 97 // Select Component
universe@138 98 final var componentNode = pathParameters.get("component");
universe@138 99 if ("no-component".equals(componentNode)) {
universe@134 100 viewModel.setComponentFilter(ProjectView.NO_COMPONENT);
universe@138 101 } else if ("all-components".equals(componentNode)) {
universe@134 102 viewModel.setComponentFilter(ProjectView.ALL_COMPONENTS);
universe@134 103 } else {
universe@138 104 viewModel.setComponentFilter(componentDao.findByNode(project, componentNode));
universe@138 105 }
universe@138 106 }
universe@138 107
universe@138 108 private static String sanitizeNode(String node, String defaultValue) {
universe@138 109 String result = node == null || node.isBlank() ? defaultValue : node;
universe@138 110 result = result.replace('/', '-');
universe@138 111 if (result.equals(".") || result.equals("..")) {
universe@138 112 return "_"+result;
universe@138 113 } else {
universe@138 114 return result;
universe@129 115 }
universe@99 116 }
universe@99 117
universe@99 118 private ResponseType forwardView(HttpServletRequest req, ProjectView viewModel, String name) {
universe@99 119 setViewModel(req, viewModel);
universe@99 120 setContentPage(req, name);
universe@99 121 setStylesheet(req, "projects");
universe@109 122 setNavigationMenu(req, "project-navmenu");
universe@99 123 return ResponseType.HTML;
universe@64 124 }
universe@64 125
universe@61 126 @RequestMapping(method = HttpMethod.GET)
universe@47 127 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@99 128 final var viewModel = new ProjectView();
universe@131 129 populate(viewModel, null, dao);
universe@86 130
universe@86 131 final var projectDao = dao.getProjectDao();
universe@86 132 final var versionDao = dao.getVersionDao();
universe@86 133
universe@99 134 for (var info : viewModel.getProjectList()) {
universe@99 135 info.setVersions(versionDao.list(info.getProject()));
universe@99 136 info.setIssueSummary(projectDao.getIssueSummary(info.getProject()));
universe@86 137 }
universe@86 138
universe@99 139 return forwardView(req, viewModel, "projects");
universe@45 140 }
universe@45 141
universe@131 142 private void configureProjectEditor(ProjectEditView viewModel, Project project, DataAccessObjects dao) throws SQLException {
universe@99 143 viewModel.setProject(project);
universe@86 144 viewModel.setUsers(dao.getUserDao().list());
universe@71 145 }
universe@71 146
universe@131 147 @RequestMapping(requestPath = "$project/edit", method = HttpMethod.GET)
universe@131 148 public ResponseType edit(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws IOException, SQLException {
universe@99 149 final var viewModel = new ProjectEditView();
universe@131 150 populate(viewModel, pathParams, dao);
universe@47 151
universe@134 152 if (!viewModel.isProjectInfoPresent()) {
universe@131 153 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 154 return ResponseType.NONE;
universe@131 155 }
universe@47 156
universe@131 157 configureProjectEditor(viewModel, viewModel.getProjectInfo().getProject(), dao);
universe@131 158 return forwardView(req, viewModel, "project-form");
universe@131 159 }
universe@131 160
universe@131 161 @RequestMapping(requestPath = "create", method = HttpMethod.GET)
universe@131 162 public ResponseType create(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@131 163 final var viewModel = new ProjectEditView();
universe@131 164 populate(viewModel, null, dao);
universe@131 165 configureProjectEditor(viewModel, new Project(-1), dao);
universe@99 166 return forwardView(req, viewModel, "project-form");
universe@47 167 }
universe@47 168
universe@47 169 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@131 170 public ResponseType commit(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
universe@47 171
universe@47 172 try {
universe@131 173 final var project = new Project(getParameter(req, Integer.class, "pid").orElseThrow());
universe@47 174 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@138 175
universe@138 176 final var node = getParameter(req, String.class, "node").orElse(null);
universe@138 177 project.setNode(sanitizeNode(node, project.getName()));
universe@138 178
universe@47 179 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 180 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 181 getParameter(req, Integer.class, "owner").map(
universe@47 182 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 183 ).ifPresent(project::setOwner);
universe@47 184
universe@47 185 dao.getProjectDao().saveOrUpdate(project);
universe@47 186
universe@118 187 setRedirectLocation(req, "./projects/");
universe@74 188 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@59 189 LOG.debug("Successfully updated project {}", project.getName());
universe@99 190
universe@99 191 return ResponseType.HTML;
universe@75 192 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 193 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 194 // TODO: implement - fix issue #21
universe@131 195 return ResponseType.NONE;
universe@47 196 }
universe@47 197 }
universe@47 198
universe@134 199 @RequestMapping(requestPath = "$project/$component/$version/issues/", method = HttpMethod.GET)
universe@134 200 public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParams, DataAccessObjects dao) throws SQLException, IOException {
universe@99 201 final var viewModel = new ProjectDetailsView();
universe@131 202 populate(viewModel, pathParams, dao);
universe@86 203
universe@134 204 if (!viewModel.isEveryFilterValid()) {
universe@131 205 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@80 206 return ResponseType.NONE;
universe@80 207 }
universe@47 208
universe@134 209 final var project = viewModel.getProjectInfo().getProject();
universe@134 210 final var version = viewModel.getVersionFilter();
universe@134 211 final var component = viewModel.getComponentFilter();
universe@134 212
universe@86 213 final var issueDao = dao.getIssueDao();
universe@70 214
universe@134 215 final List<Issue> issues;
universe@134 216 if (version.equals(ProjectView.NO_VERSION)) {
universe@134 217 if (component.equals(ProjectView.ALL_COMPONENTS)) {
universe@134 218 issues = issueDao.list(project, (Version) null);
universe@134 219 } else if (component.equals(ProjectView.NO_COMPONENT)) {
universe@134 220 issues = issueDao.list(project, null, null);
universe@134 221 } else {
universe@134 222 issues = issueDao.list(project, component, null);
universe@134 223 }
universe@134 224 } else if (version.equals(ProjectView.ALL_VERSIONS)) {
universe@134 225 if (component.equals(ProjectView.ALL_COMPONENTS)) {
universe@134 226 issues = issueDao.list(project);
universe@134 227 } else if (component.equals(ProjectView.NO_COMPONENT)) {
universe@134 228 issues = issueDao.list(project, (Component)null);
universe@134 229 } else {
universe@134 230 issues = issueDao.list(project, component);
universe@134 231 }
universe@134 232 } else {
universe@134 233 if (component.equals(ProjectView.ALL_COMPONENTS)) {
universe@134 234 issues = issueDao.list(project, version);
universe@134 235 } else if (component.equals(ProjectView.NO_COMPONENT)) {
universe@134 236 issues = issueDao.list(project, null, version);
universe@134 237 } else {
universe@134 238 issues = issueDao.list(project, component, version);
universe@134 239 }
universe@134 240 }
universe@134 241
universe@100 242 for (var issue : issues) issueDao.joinVersionInformation(issue);
universe@121 243 issues.sort(new IssueSorter(
universe@141 244 new IssueSorter.Criteria(IssueSorter.Field.DONE, true),
universe@121 245 new IssueSorter.Criteria(IssueSorter.Field.ETA, true),
universe@121 246 new IssueSorter.Criteria(IssueSorter.Field.UPDATED, false)
universe@121 247 ));
universe@134 248
universe@134 249
universe@134 250 viewModel.getProjectDetails().updateDetails(issues);
universe@134 251 if (version.getId() > 0)
universe@134 252 viewModel.getProjectDetails().updateVersionInfo(version);
universe@80 253
universe@99 254 return forwardView(req, viewModel, "project-details");
universe@71 255 }
universe@71 256
universe@131 257 @RequestMapping(requestPath = "$project/versions/", method = HttpMethod.GET)
universe@131 258 public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@109 259 final var viewModel = new VersionsView();
universe@131 260 populate(viewModel, pathParameters, dao);
universe@109 261
universe@109 262 final var projectInfo = viewModel.getProjectInfo();
universe@109 263 if (projectInfo == null) {
universe@131 264 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@109 265 return ResponseType.NONE;
universe@109 266 }
universe@109 267
universe@109 268 final var issueDao = dao.getIssueDao();
universe@109 269 final var issues = issueDao.list(projectInfo.getProject());
universe@109 270 for (var issue : issues) issueDao.joinVersionInformation(issue);
universe@109 271 viewModel.update(projectInfo.getVersions(), issues);
universe@109 272
universe@109 273 return forwardView(req, viewModel, "versions");
universe@109 274 }
universe@109 275
universe@131 276 @RequestMapping(requestPath = "$project/versions/$version/edit", method = HttpMethod.GET)
universe@131 277 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@99 278 final var viewModel = new VersionEditView();
universe@131 279 populate(viewModel, pathParameters, dao);
universe@99 280
universe@131 281 if (viewModel.getProjectInfo() == null || viewModel.getVersionFilter() == null) {
universe@131 282 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@110 283 return ResponseType.NONE;
universe@110 284 }
universe@110 285
universe@131 286 viewModel.setVersion(viewModel.getVersionFilter());
universe@59 287
universe@99 288 return forwardView(req, viewModel, "version-form");
universe@59 289 }
universe@59 290
universe@131 291 @RequestMapping(requestPath = "$project/create-version", method = HttpMethod.GET)
universe@131 292 public ResponseType createVersion(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@131 293 final var viewModel = new VersionEditView();
universe@131 294 populate(viewModel, pathParameters, dao);
universe@59 295
universe@131 296 if (viewModel.getProjectInfo() == null) {
universe@131 297 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 298 return ResponseType.NONE;
universe@131 299 }
universe@131 300
universe@131 301 viewModel.setVersion(viewModel.getVersionFilter());
universe@131 302
universe@131 303 return forwardView(req, viewModel, "version-form");
universe@131 304 }
universe@131 305
universe@131 306 @RequestMapping(requestPath = "commit-version", method = HttpMethod.POST)
universe@131 307 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
universe@131 308
universe@59 309 try {
universe@138 310 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 311 if (project == null) {
universe@138 312 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 313 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@138 314 return ResponseType.NONE;
universe@138 315 }
universe@131 316 final var version = new Version(getParameter(req, Integer.class, "id").orElseThrow());
universe@59 317 version.setName(getParameter(req, String.class, "name").orElseThrow());
universe@138 318
universe@138 319 final var node = getParameter(req, String.class, "node").orElse(null);
universe@138 320 version.setNode(sanitizeNode(node, version.getName()));
universe@138 321
universe@59 322 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
universe@59 323 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
universe@128 324 dao.getVersionDao().saveOrUpdate(version, project);
universe@59 325
universe@138 326 setRedirectLocation(req, "./projects/" + project.getNode() + "/versions/");
universe@74 327 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@75 328 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 329 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 330 // TODO: implement - fix issue #21
universe@131 331 return ResponseType.NONE;
universe@59 332 }
universe@41 333
universe@43 334 return ResponseType.HTML;
universe@41 335 }
universe@64 336
universe@134 337 @RequestMapping(requestPath = "$project/components/", method = HttpMethod.GET)
universe@134 338 public ResponseType components(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@134 339 final var viewModel = new ComponentsView();
universe@134 340 populate(viewModel, pathParameters, dao);
universe@134 341
universe@134 342 final var projectInfo = viewModel.getProjectInfo();
universe@134 343 if (projectInfo == null) {
universe@134 344 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@134 345 return ResponseType.NONE;
universe@134 346 }
universe@134 347
universe@134 348 final var issueDao = dao.getIssueDao();
universe@134 349 final var issues = issueDao.list(projectInfo.getProject());
universe@134 350 viewModel.update(projectInfo.getComponents(), issues);
universe@134 351
universe@134 352 return forwardView(req, viewModel, "components");
universe@134 353 }
universe@134 354
universe@134 355 @RequestMapping(requestPath = "$project/components/$component/edit", method = HttpMethod.GET)
universe@134 356 public ResponseType editComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@134 357 final var viewModel = new ComponentEditView();
universe@134 358 populate(viewModel, pathParameters, dao);
universe@134 359
universe@134 360 if (viewModel.getProjectInfo() == null || viewModel.getComponentFilter() == null) {
universe@134 361 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@134 362 return ResponseType.NONE;
universe@134 363 }
universe@134 364
universe@134 365 viewModel.setComponent(viewModel.getComponentFilter());
universe@134 366 viewModel.setUsers(dao.getUserDao().list());
universe@134 367
universe@134 368 return forwardView(req, viewModel, "component-form");
universe@134 369 }
universe@134 370
universe@134 371 @RequestMapping(requestPath = "$project/create-component", method = HttpMethod.GET)
universe@134 372 public ResponseType createComponent(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@134 373 final var viewModel = new ComponentEditView();
universe@134 374 populate(viewModel, pathParameters, dao);
universe@134 375
universe@134 376 if (viewModel.getProjectInfo() == null) {
universe@134 377 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@134 378 return ResponseType.NONE;
universe@134 379 }
universe@134 380
universe@134 381 viewModel.setComponent(new Component(-1));
universe@134 382 viewModel.setUsers(dao.getUserDao().list());
universe@134 383
universe@134 384 return forwardView(req, viewModel, "component-form");
universe@134 385 }
universe@134 386
universe@134 387 @RequestMapping(requestPath = "commit-component", method = HttpMethod.POST)
universe@134 388 public ResponseType commitComponent(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
universe@134 389
universe@134 390 try {
universe@138 391 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 392 if (project == null) {
universe@138 393 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 394 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@138 395 return ResponseType.NONE;
universe@138 396 }
universe@134 397 final var component = new Component(getParameter(req, Integer.class, "id").orElseThrow());
universe@134 398 component.setName(getParameter(req, String.class, "name").orElseThrow());
universe@138 399
universe@138 400 final var node = getParameter(req, String.class, "node").orElse(null);
universe@138 401 component.setNode(sanitizeNode(node, component.getName()));
universe@138 402
universe@134 403 component.setColor(getParameter(req, WebColor.class, "color").orElseThrow());
universe@134 404 getParameter(req, Integer.class, "ordinal").ifPresent(component::setOrdinal);
universe@134 405 getParameter(req, Integer.class, "lead").map(
universe@134 406 userid -> userid >= 0 ? new User(userid) : null
universe@134 407 ).ifPresent(component::setLead);
universe@134 408 getParameter(req, String.class, "description").ifPresent(component::setDescription);
universe@134 409
universe@134 410 dao.getComponentDao().saveOrUpdate(component, project);
universe@134 411
universe@138 412 setRedirectLocation(req, "./projects/" + project.getNode() + "/components/");
universe@134 413 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@134 414 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@134 415 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@134 416 // TODO: implement - fix issue #21
universe@134 417 return ResponseType.NONE;
universe@134 418 }
universe@134 419
universe@134 420 return ResponseType.HTML;
universe@134 421 }
universe@134 422
universe@133 423 private void configureIssueEditor(IssueEditView viewModel, Issue issue, DataAccessObjects dao) throws SQLException {
universe@134 424 final var project = viewModel.getProjectInfo().getProject();
universe@134 425 issue.setProject(project);
universe@99 426 viewModel.setIssue(issue);
universe@99 427 viewModel.configureVersionSelectors(viewModel.getProjectInfo().getVersions());
universe@86 428 viewModel.setUsers(dao.getUserDao().list());
universe@134 429 viewModel.setComponents(dao.getComponentDao().list(project));
universe@124 430 if (issue.getId() >= 0) {
universe@124 431 viewModel.setComments(dao.getIssueDao().listComments(issue));
universe@124 432 }
universe@71 433 }
universe@71 434
universe@131 435 @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET)
universe@131 436 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@99 437 final var viewModel = new IssueEditView();
universe@131 438 populate(viewModel, pathParameters, dao);
universe@99 439
universe@131 440 final var projectInfo = viewModel.getProjectInfo();
universe@131 441 if (projectInfo == null) {
universe@131 442 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 443 return ResponseType.NONE;
universe@86 444 }
universe@64 445
universe@131 446 final var issueDao = dao.getIssueDao();
universe@131 447 final var issue = issueDao.find(Functions.parseIntOrZero(pathParameters.get("issue")));
universe@131 448 if (issue == null) {
universe@131 449 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 450 return ResponseType.NONE;
universe@131 451 }
universe@131 452
universe@131 453 issueDao.joinVersionInformation(issue);
universe@133 454 configureIssueEditor(viewModel, issue, dao);
universe@131 455
universe@99 456 return forwardView(req, viewModel, "issue-form");
universe@64 457 }
universe@64 458
universe@131 459 @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET)
universe@131 460 public ResponseType createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@131 461 final var viewModel = new IssueEditView();
universe@131 462 populate(viewModel, pathParameters, dao);
universe@131 463
universe@131 464 final var projectInfo = viewModel.getProjectInfo();
universe@131 465 if (projectInfo == null) {
universe@131 466 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 467 return ResponseType.NONE;
universe@131 468 }
universe@131 469
universe@131 470 final var issue = new Issue(-1);
universe@131 471 issue.setProject(projectInfo.getProject());
universe@133 472 configureIssueEditor(viewModel, issue, dao);
universe@131 473
universe@131 474 return forwardView(req, viewModel, "issue-form");
universe@131 475 }
universe@131 476
universe@131 477 @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST)
universe@131 478 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
universe@64 479 try {
universe@131 480 final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
universe@136 481 final var componentId = getParameter(req, Integer.class, "component");
universe@136 482 final Component component;
universe@136 483 if (componentId.isPresent()) {
universe@136 484 component = dao.getComponentDao().find(componentId.get());
universe@136 485 } else {
universe@136 486 component = null;
universe@136 487 }
universe@138 488 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 489 if (project == null) {
universe@138 490 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 491 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@138 492 return ResponseType.NONE;
universe@138 493 }
universe@138 494 issue.setProject(project);
universe@75 495 getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
universe@75 496 getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
universe@75 497 issue.setSubject(getParameter(req, String.class, "subject").orElseThrow());
universe@136 498 issue.setComponent(component);
universe@136 499 getParameter(req, Integer.class, "assignee").map(userid -> {
universe@136 500 if (userid >= 0) {
universe@136 501 return new User(userid);
universe@136 502 } else if (userid == -2) {
universe@136 503 return Optional.ofNullable(component).map(Component::getLead).orElse(null);
universe@136 504 } else {
universe@136 505 return null;
universe@136 506 }
universe@136 507 }
universe@75 508 ).ifPresent(issue::setAssignee);
universe@75 509 getParameter(req, String.class, "description").ifPresent(issue::setDescription);
universe@75 510 getParameter(req, Date.class, "eta").ifPresent(issue::setEta);
universe@83 511
universe@83 512 getParameter(req, Integer[].class, "affected")
universe@83 513 .map(Stream::of)
universe@83 514 .map(stream ->
universe@96 515 stream.map(Version::new).collect(Collectors.toList())
universe@83 516 ).ifPresent(issue::setAffectedVersions);
universe@83 517 getParameter(req, Integer[].class, "resolved")
universe@83 518 .map(Stream::of)
universe@83 519 .map(stream ->
universe@86 520 stream.map(Version::new).collect(Collectors.toList())
universe@83 521 ).ifPresent(issue::setResolvedVersions);
universe@83 522
universe@128 523 dao.getIssueDao().saveOrUpdate(issue, issue.getProject());
universe@64 524
universe@131 525 // TODO: fix issue #14
universe@138 526 setRedirectLocation(req, "./projects/" + issue.getProject().getNode() + "/all-components/all-versions/issues/");
universe@74 527 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@124 528
universe@124 529 return ResponseType.HTML;
universe@75 530 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 531 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 532 // TODO: implement - fix issue #21
universe@131 533 return ResponseType.NONE;
universe@64 534 }
universe@124 535 }
universe@64 536
universe@131 537 @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST)
universe@124 538 public ResponseType commentIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
universe@124 539 final var issueIdParam = getParameter(req, Integer.class, "issueid");
universe@124 540 if (issueIdParam.isEmpty()) {
universe@124 541 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form.");
universe@124 542 return ResponseType.NONE;
universe@124 543 }
universe@137 544 final var issue = dao.getIssueDao().find(issueIdParam.get());
universe@137 545 if (issue == null) {
universe@137 546 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@137 547 return ResponseType.NONE;
universe@137 548 }
universe@124 549 try {
universe@124 550 final var issueComment = new IssueComment(getParameter(req, Integer.class, "commentid").orElse(-1), issue);
universe@124 551 issueComment.setComment(getParameter(req, String.class, "comment").orElse(""));
universe@124 552
universe@124 553 if (issueComment.getComment().isBlank()) {
universe@124 554 throw new IllegalArgumentException("comment.null");
universe@124 555 }
universe@124 556
universe@124 557 LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId());
universe@124 558 if (req.getRemoteUser() != null) {
universe@124 559 dao.getUserDao().findByUsername(req.getRemoteUser()).ifPresent(issueComment::setAuthor);
universe@124 560 }
universe@124 561
universe@124 562 dao.getIssueDao().saveComment(issueComment);
universe@124 563
universe@131 564 // TODO: fix redirect location (e.g. after fixing #24)
universe@138 565 setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/edit");
universe@124 566 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@124 567
universe@124 568 return ResponseType.HTML;
universe@124 569 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 570 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 571 // TODO: implement - fix issue #21
universe@131 572 return ResponseType.NONE;
universe@124 573 }
universe@64 574 }
universe@41 575 }

mercurial