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

Sat, 31 Oct 2020 09:15:57 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 31 Oct 2020 09:15:57 +0100
changeset 152
7761c37c5e61
parent 150
822b7e3d064d
child 157
1e6f16fad3a5
permissions
-rw-r--r--

fixes default values for version and component form

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@152 301 viewModel.setVersion(new Version(-1));
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@146 425 issue.setProject(project); // automatically set current project for new issues
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@71 430 }
universe@71 431
universe@146 432 @RequestMapping(requestPath = "$project/issues/$issue/view", method = HttpMethod.GET)
universe@146 433 public ResponseType viewIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@146 434 final var viewModel = new IssueDetailView();
universe@146 435 populate(viewModel, pathParameters, dao);
universe@146 436
universe@146 437 final var projectInfo = viewModel.getProjectInfo();
universe@146 438 if (projectInfo == null) {
universe@146 439 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@146 440 return ResponseType.NONE;
universe@146 441 }
universe@146 442
universe@146 443 final var issueDao = dao.getIssueDao();
universe@146 444 final var issue = issueDao.find(Functions.parseIntOrZero(pathParameters.get("issue")));
universe@146 445 if (issue == null) {
universe@146 446 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@146 447 return ResponseType.NONE;
universe@146 448 }
universe@146 449
universe@146 450 issueDao.joinVersionInformation(issue);
universe@146 451 viewModel.setIssue(issue);
universe@146 452 viewModel.setComments(issueDao.listComments(issue));
universe@146 453
universe@146 454 return forwardView(req, viewModel, "issue-view");
universe@146 455 }
universe@146 456
universe@146 457 // TODO: why should the issue editor be child of $project?
universe@131 458 @RequestMapping(requestPath = "$project/issues/$issue/edit", method = HttpMethod.GET)
universe@131 459 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@99 460 final var viewModel = new IssueEditView();
universe@131 461 populate(viewModel, pathParameters, dao);
universe@99 462
universe@131 463 final var projectInfo = viewModel.getProjectInfo();
universe@131 464 if (projectInfo == null) {
universe@131 465 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 466 return ResponseType.NONE;
universe@86 467 }
universe@64 468
universe@131 469 final var issueDao = dao.getIssueDao();
universe@131 470 final var issue = issueDao.find(Functions.parseIntOrZero(pathParameters.get("issue")));
universe@131 471 if (issue == null) {
universe@131 472 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 473 return ResponseType.NONE;
universe@131 474 }
universe@131 475
universe@131 476 issueDao.joinVersionInformation(issue);
universe@133 477 configureIssueEditor(viewModel, issue, dao);
universe@131 478
universe@99 479 return forwardView(req, viewModel, "issue-form");
universe@64 480 }
universe@64 481
universe@131 482 @RequestMapping(requestPath = "$project/create-issue", method = HttpMethod.GET)
universe@131 483 public ResponseType createIssue(HttpServletRequest req, HttpServletResponse resp, PathParameters pathParameters, DataAccessObjects dao) throws IOException, SQLException {
universe@131 484 final var viewModel = new IssueEditView();
universe@131 485 populate(viewModel, pathParameters, dao);
universe@131 486
universe@131 487 final var projectInfo = viewModel.getProjectInfo();
universe@131 488 if (projectInfo == null) {
universe@131 489 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@131 490 return ResponseType.NONE;
universe@131 491 }
universe@131 492
universe@131 493 final var issue = new Issue(-1);
universe@131 494 issue.setProject(projectInfo.getProject());
universe@133 495 configureIssueEditor(viewModel, issue, dao);
universe@131 496
universe@131 497 return forwardView(req, viewModel, "issue-form");
universe@131 498 }
universe@131 499
universe@131 500 @RequestMapping(requestPath = "commit-issue", method = HttpMethod.POST)
universe@131 501 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
universe@64 502 try {
universe@131 503 final var issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow());
universe@136 504 final var componentId = getParameter(req, Integer.class, "component");
universe@136 505 final Component component;
universe@136 506 if (componentId.isPresent()) {
universe@136 507 component = dao.getComponentDao().find(componentId.get());
universe@136 508 } else {
universe@136 509 component = null;
universe@136 510 }
universe@138 511 final var project = dao.getProjectDao().find(getParameter(req, Integer.class, "pid").orElseThrow());
universe@138 512 if (project == null) {
universe@138 513 // TODO: improve error handling, because not found is not correct for this POST request
universe@138 514 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@138 515 return ResponseType.NONE;
universe@138 516 }
universe@138 517 issue.setProject(project);
universe@75 518 getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
universe@75 519 getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
universe@75 520 issue.setSubject(getParameter(req, String.class, "subject").orElseThrow());
universe@136 521 issue.setComponent(component);
universe@136 522 getParameter(req, Integer.class, "assignee").map(userid -> {
universe@136 523 if (userid >= 0) {
universe@136 524 return new User(userid);
universe@136 525 } else if (userid == -2) {
universe@136 526 return Optional.ofNullable(component).map(Component::getLead).orElse(null);
universe@136 527 } else {
universe@136 528 return null;
universe@136 529 }
universe@136 530 }
universe@75 531 ).ifPresent(issue::setAssignee);
universe@75 532 getParameter(req, String.class, "description").ifPresent(issue::setDescription);
universe@75 533 getParameter(req, Date.class, "eta").ifPresent(issue::setEta);
universe@83 534
universe@83 535 getParameter(req, Integer[].class, "affected")
universe@83 536 .map(Stream::of)
universe@83 537 .map(stream ->
universe@96 538 stream.map(Version::new).collect(Collectors.toList())
universe@83 539 ).ifPresent(issue::setAffectedVersions);
universe@83 540 getParameter(req, Integer[].class, "resolved")
universe@83 541 .map(Stream::of)
universe@83 542 .map(stream ->
universe@86 543 stream.map(Version::new).collect(Collectors.toList())
universe@83 544 ).ifPresent(issue::setResolvedVersions);
universe@83 545
universe@128 546 dao.getIssueDao().saveOrUpdate(issue, issue.getProject());
universe@64 547
universe@146 548 // TODO: fix redirect location
universe@146 549 setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view");
universe@74 550 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@124 551
universe@124 552 return ResponseType.HTML;
universe@75 553 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 554 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 555 // TODO: implement - fix issue #21
universe@131 556 return ResponseType.NONE;
universe@64 557 }
universe@124 558 }
universe@64 559
universe@131 560 @RequestMapping(requestPath = "commit-issue-comment", method = HttpMethod.POST)
universe@124 561 public ResponseType commentIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
universe@124 562 final var issueIdParam = getParameter(req, Integer.class, "issueid");
universe@124 563 if (issueIdParam.isEmpty()) {
universe@124 564 resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Detected manipulated form.");
universe@124 565 return ResponseType.NONE;
universe@124 566 }
universe@137 567 final var issue = dao.getIssueDao().find(issueIdParam.get());
universe@137 568 if (issue == null) {
universe@137 569 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
universe@137 570 return ResponseType.NONE;
universe@137 571 }
universe@124 572 try {
universe@150 573 final var issueComment = new IssueComment(getParameter(req, Integer.class, "commentid").orElse(-1));
universe@124 574 issueComment.setComment(getParameter(req, String.class, "comment").orElse(""));
universe@124 575
universe@124 576 if (issueComment.getComment().isBlank()) {
universe@124 577 throw new IllegalArgumentException("comment.null");
universe@124 578 }
universe@124 579
universe@124 580 LOG.debug("User {} is commenting on issue #{}", req.getRemoteUser(), issue.getId());
universe@124 581 if (req.getRemoteUser() != null) {
universe@124 582 dao.getUserDao().findByUsername(req.getRemoteUser()).ifPresent(issueComment::setAuthor);
universe@124 583 }
universe@124 584
universe@150 585 dao.getIssueDao().saveComment(issue, issueComment);
universe@124 586
universe@146 587 // TODO: fix redirect location
universe@146 588 setRedirectLocation(req, "./projects/" + issue.getProject().getNode()+"/issues/"+issue.getId()+"/view");
universe@124 589 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@124 590
universe@124 591 return ResponseType.HTML;
universe@124 592 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@131 593 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
universe@131 594 // TODO: implement - fix issue #21
universe@131 595 return ResponseType.NONE;
universe@124 596 }
universe@64 597 }
universe@41 598 }

mercurial