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

Sat, 30 May 2020 18:05:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 30 May 2020 18:05:06 +0200
changeset 83
24a3596b8f98
parent 81
1a2e7b5d48f7
child 86
0a658e53177c
permissions
-rw-r--r--

adds version selection in issue editor

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@59 35 import org.slf4j.Logger;
universe@59 36 import org.slf4j.LoggerFactory;
universe@41 37
universe@41 38 import javax.servlet.annotation.WebServlet;
universe@41 39 import javax.servlet.http.HttpServletRequest;
universe@59 40 import javax.servlet.http.HttpServletResponse;
universe@75 41 import javax.servlet.http.HttpSession;
universe@59 42 import java.io.IOException;
universe@75 43 import java.sql.Date;
universe@47 44 import java.sql.SQLException;
universe@83 45 import java.util.*;
universe@83 46 import java.util.stream.Collectors;
universe@83 47 import java.util.stream.Stream;
universe@41 48
universe@52 49 import static de.uapcore.lightpit.Functions.fqn;
universe@52 50
universe@41 51 @WebServlet(
universe@41 52 name = "ProjectsModule",
universe@41 53 urlPatterns = "/projects/*"
universe@41 54 )
universe@41 55 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 56
universe@59 57 private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class);
universe@59 58
universe@80 59 public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected_project");
universe@80 60 public static final String SESSION_ATTR_SELECTED_ISSUE = fqn(ProjectsModule.class, "selected_issue");
universe@80 61 public static final String SESSION_ATTR_SELECTED_VERSION = fqn(ProjectsModule.class, "selected_version");
universe@80 62 public static final String SESSION_ATTR_HIDE_ZEROS = fqn(ProjectsModule.class, "stats_hide_zeros");
universe@52 63
universe@75 64 private class SessionSelection {
universe@75 65 final HttpSession session;
universe@75 66 Project project;
universe@75 67 Version version;
universe@75 68 Issue issue;
universe@75 69
universe@75 70 SessionSelection(HttpServletRequest req, Project project) {
universe@75 71 this.session = req.getSession();
universe@75 72 this.project = project;
universe@75 73 version = null;
universe@75 74 issue = null;
universe@75 75 updateAttributes();
universe@64 76 }
universe@75 77
universe@75 78 SessionSelection(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@75 79 this.session = req.getSession();
universe@75 80 final var issueDao = dao.getIssueDao();
universe@75 81 final var projectDao = dao.getProjectDao();
universe@75 82 final var issueSelection = getParameter(req, Integer.class, "issue");
universe@75 83 if (issueSelection.isPresent()) {
universe@75 84 issue = issueDao.find(issueSelection.get());
universe@75 85 } else {
universe@75 86 final var issue = (Issue) session.getAttribute(SESSION_ATTR_SELECTED_ISSUE);
universe@75 87 this.issue = issue == null ? null : issueDao.find(issue.getId());
universe@75 88 }
universe@75 89 if (issue != null) {
universe@75 90 version = null; // show the issue globally
universe@75 91 project = projectDao.find(issue.getProject().getId());
universe@75 92 }
universe@75 93
universe@75 94 final var projectSelection = getParameter(req, Integer.class, "pid");
universe@75 95 if (projectSelection.isPresent()) {
universe@75 96 final var selectedProject = projectDao.find(projectSelection.get());
universe@75 97 if (!Objects.equals(selectedProject, project)) {
universe@75 98 // reset version and issue if project changed
universe@75 99 version = null;
universe@75 100 issue = null;
universe@75 101 }
universe@75 102 project = selectedProject;
universe@75 103 } else {
universe@75 104 final var sessionProject = (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
universe@75 105 project = sessionProject == null ? null : projectDao.find(sessionProject.getId());
universe@75 106 }
universe@75 107 updateAttributes();
universe@75 108 }
universe@75 109
universe@75 110 void selectVersion(Version version) {
universe@83 111 this.project = version.getProject();
universe@75 112 this.version = version;
universe@75 113 this.issue = null;
universe@75 114 updateAttributes();
universe@75 115 }
universe@75 116
universe@75 117 void selectIssue(Issue issue) {
universe@83 118 this.project = issue.getProject();
universe@75 119 this.issue = issue;
universe@75 120 this.version = null;
universe@75 121 updateAttributes();
universe@75 122 }
universe@75 123
universe@75 124 void updateAttributes() {
universe@75 125 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, project);
universe@75 126 session.setAttribute(SESSION_ATTR_SELECTED_VERSION, version);
universe@75 127 session.setAttribute(SESSION_ATTR_SELECTED_ISSUE, issue);
universe@75 128 }
universe@71 129 }
universe@71 130
universe@80 131 private void setAttributeHideZeros(HttpServletRequest req) {
universe@80 132 final Boolean value;
universe@80 133 final var param = getParameter(req, Boolean.class, "reduced");
universe@80 134 if (param.isPresent()) {
universe@80 135 value = param.get();
universe@80 136 req.getSession().setAttribute(SESSION_ATTR_HIDE_ZEROS, value);
universe@80 137 } else {
universe@80 138 final var sessionValue = req.getSession().getAttribute(SESSION_ATTR_HIDE_ZEROS);
universe@80 139 if (sessionValue != null) {
universe@80 140 value = (Boolean) sessionValue;
universe@80 141 } else {
universe@80 142 value = false;
universe@80 143 req.getSession().setAttribute(SESSION_ATTR_HIDE_ZEROS, value);
universe@80 144 }
universe@80 145 }
universe@80 146 req.setAttribute("statsHideZeros", value);
universe@80 147 }
universe@80 148
universe@78 149 @Override
universe@78 150 protected String getResourceBundleName() {
universe@78 151 return "localization.projects";
universe@78 152 }
universe@71 153
universe@80 154
universe@80 155 private static final int BREADCRUMB_LEVEL_ROOT = 0;
universe@80 156 private static final int BREADCRUMB_LEVEL_PROJECT = 1;
universe@80 157 private static final int BREADCRUMB_LEVEL_VERSION = 2;
universe@80 158 private static final int BREADCRUMB_LEVEL_ISSUE_LIST = 3;
universe@80 159 private static final int BREADCRUMB_LEVEL_ISSUE = 4;
universe@80 160
universe@71 161 /**
universe@71 162 * Creates the breadcrumb menu.
universe@71 163 *
universe@80 164 * @param level the current active level (0: root, 1: project, 2: version, 3: issue list, 4: issue)
universe@75 165 * @param sessionSelection the currently selected objects
universe@71 166 * @return a dynamic breadcrumb menu trying to display as many levels as possible
universe@71 167 */
universe@75 168 private List<MenuEntry> getBreadcrumbs(int level, SessionSelection sessionSelection) {
universe@71 169 MenuEntry entry;
universe@71 170
universe@71 171 final var breadcrumbs = new ArrayList<MenuEntry>();
universe@79 172 entry = new MenuEntry(new ResourceKey("localization.lightpit", "menu.projects"),
universe@79 173 "projects/");
universe@71 174 breadcrumbs.add(entry);
universe@80 175 if (level == BREADCRUMB_LEVEL_ROOT) entry.setActive(true);
universe@71 176
universe@75 177 if (sessionSelection.project != null) {
universe@75 178 if (sessionSelection.project.getId() < 0) {
universe@75 179 entry = new MenuEntry(new ResourceKey("localization.projects", "button.create"),
universe@79 180 "projects/edit");
universe@75 181 } else {
universe@75 182 entry = new MenuEntry(sessionSelection.project.getName(),
universe@79 183 "projects/view?pid=" + sessionSelection.project.getId());
universe@75 184 }
universe@80 185 if (level == BREADCRUMB_LEVEL_PROJECT) entry.setActive(true);
universe@75 186 breadcrumbs.add(entry);
universe@75 187 }
universe@71 188
universe@75 189 if (sessionSelection.version != null) {
universe@75 190 if (sessionSelection.version.getId() < 0) {
universe@75 191 entry = new MenuEntry(new ResourceKey("localization.projects", "button.version.create"),
universe@79 192 "projects/versions/edit");
universe@75 193 } else {
universe@75 194 entry = new MenuEntry(sessionSelection.version.getName(),
universe@75 195 // TODO: change link to issue overview for that version
universe@79 196 "projects/versions/edit?id=" + sessionSelection.version.getId());
universe@75 197 }
universe@80 198 if (level == BREADCRUMB_LEVEL_VERSION) entry.setActive(true);
universe@80 199 breadcrumbs.add(entry);
universe@80 200 }
universe@80 201
universe@80 202 if (sessionSelection.project != null) {
universe@80 203 entry = new MenuEntry(new ResourceKey("localization.projects", "menu.issues"),
universe@80 204 // TODO: maybe also add selected version
universe@80 205 "projects/issues/?pid=" + sessionSelection.project.getId());
universe@80 206 if (level == BREADCRUMB_LEVEL_ISSUE_LIST) entry.setActive(true);
universe@75 207 breadcrumbs.add(entry);
universe@75 208 }
universe@71 209
universe@75 210 if (sessionSelection.issue != null) {
universe@75 211 if (sessionSelection.issue.getId() < 0) {
universe@75 212 entry = new MenuEntry(new ResourceKey("localization.projects", "button.issue.create"),
universe@79 213 "projects/issues/edit");
universe@75 214 } else {
universe@75 215 entry = new MenuEntry("#" + sessionSelection.issue.getId(),
universe@75 216 // TODO: maybe change link to a view rather than directly opening the editor
universe@79 217 "projects/issues/edit?id=" + sessionSelection.issue.getId());
universe@75 218 }
universe@80 219 if (level == BREADCRUMB_LEVEL_ISSUE) entry.setActive(true);
universe@75 220 breadcrumbs.add(entry);
universe@75 221 }
universe@75 222
universe@71 223 return breadcrumbs;
universe@64 224 }
universe@64 225
universe@61 226 @RequestMapping(method = HttpMethod.GET)
universe@47 227 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@75 228 final var sessionSelection = new SessionSelection(req, dao);
universe@52 229 final var projectList = dao.getProjectDao().list();
universe@52 230 req.setAttribute("projects", projectList);
universe@74 231 setContentPage(req, "projects");
universe@52 232 setStylesheet(req, "projects");
universe@52 233
universe@80 234 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_ROOT, sessionSelection));
universe@45 235
universe@45 236 return ResponseType.HTML;
universe@45 237 }
universe@45 238
universe@75 239 private void configureEditForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
universe@75 240 req.setAttribute("project", selection.project);
universe@71 241 req.setAttribute("users", dao.getUserDao().list());
universe@74 242 setContentPage(req, "project-form");
universe@80 243 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_PROJECT, selection));
universe@71 244 }
universe@71 245
universe@47 246 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
universe@51 247 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@75 248 final var selection = new SessionSelection(req, findByParameter(req, Integer.class, "id",
universe@75 249 dao.getProjectDao()::find).orElse(new Project(-1)));
universe@47 250
universe@75 251 configureEditForm(req, dao, selection);
universe@47 252
universe@47 253 return ResponseType.HTML;
universe@47 254 }
universe@47 255
universe@47 256 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@68 257 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 258
universe@75 259 Project project = new Project(-1);
universe@47 260 try {
universe@47 261 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
universe@47 262 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@47 263 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 264 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 265 getParameter(req, Integer.class, "owner").map(
universe@47 266 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 267 ).ifPresent(project::setOwner);
universe@47 268
universe@47 269 dao.getProjectDao().saveOrUpdate(project);
universe@47 270
universe@70 271 setRedirectLocation(req, "./projects/");
universe@74 272 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@59 273 LOG.debug("Successfully updated project {}", project.getName());
universe@75 274 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@47 275 // TODO: set request attribute with error text
universe@59 276 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 277 LOG.debug("Details:", ex);
universe@75 278 configureEditForm(req, dao, new SessionSelection(req, project));
universe@47 279 }
universe@47 280
universe@47 281 return ResponseType.HTML;
universe@47 282 }
universe@47 283
universe@70 284 @RequestMapping(requestPath = "view", method = HttpMethod.GET)
universe@80 285 public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
universe@75 286 final var sessionSelection = new SessionSelection(req, dao);
universe@80 287 if (sessionSelection.project == null) {
universe@80 288 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
universe@80 289 return ResponseType.NONE;
universe@80 290 }
universe@47 291
universe@80 292 final var versionDao = dao.getVersionDao();
universe@80 293 final var versions = versionDao.list(sessionSelection.project);
universe@80 294 final var statsAffected = new ArrayList<VersionStatistics>();
universe@80 295 final var statsScheduled = new ArrayList<VersionStatistics>();
universe@80 296 final var statsResolved = new ArrayList<VersionStatistics>();
universe@80 297 for (Version version : versions) {
universe@80 298 statsAffected.add(versionDao.statsOpenedIssues(version));
universe@80 299 statsScheduled.add(versionDao.statsScheduledIssues(version));
universe@80 300 statsResolved.add(versionDao.statsResolvedIssues(version));
universe@80 301 }
universe@70 302
universe@80 303 setAttributeHideZeros(req);
universe@80 304
universe@81 305 req.setAttribute("project", sessionSelection.project);
universe@80 306 req.setAttribute("versions", versions);
universe@80 307 req.setAttribute("statsAffected", statsAffected);
universe@80 308 req.setAttribute("statsScheduled", statsScheduled);
universe@80 309 req.setAttribute("statsResolved", statsResolved);
universe@80 310
universe@80 311 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@80 312 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@80 313
universe@80 314 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_PROJECT, sessionSelection));
universe@74 315 setContentPage(req, "project-details");
universe@80 316 setStylesheet(req, "projects");
universe@59 317
universe@59 318 return ResponseType.HTML;
universe@59 319 }
universe@59 320
universe@76 321 private void configureEditVersionForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
universe@80 322 final var versionDao = dao.getVersionDao();
universe@76 323 req.setAttribute("projects", dao.getProjectDao().list());
universe@75 324 req.setAttribute("version", selection.version);
universe@71 325 req.setAttribute("versionStatusEnum", VersionStatus.values());
universe@71 326
universe@80 327 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@80 328 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@80 329 req.setAttribute("statsAffected", versionDao.statsOpenedIssues(selection.version));
universe@80 330 req.setAttribute("statsScheduled", versionDao.statsScheduledIssues(selection.version));
universe@80 331 req.setAttribute("statsResolved", versionDao.statsResolvedIssues(selection.version));
universe@80 332 setAttributeHideZeros(req);
universe@80 333
universe@74 334 setContentPage(req, "version-form");
universe@80 335 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_VERSION, selection));
universe@71 336 }
universe@71 337
universe@59 338 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
universe@80 339 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
universe@75 340 final var sessionSelection = new SessionSelection(req, dao);
universe@59 341
universe@75 342 sessionSelection.selectVersion(findByParameter(req, Integer.class, "id", dao.getVersionDao()::find)
universe@75 343 .orElse(new Version(-1, sessionSelection.project)));
universe@76 344 configureEditVersionForm(req, dao, sessionSelection);
universe@59 345
universe@59 346 return ResponseType.HTML;
universe@59 347 }
universe@59 348
universe@59 349 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
universe@80 350 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
universe@75 351 final var sessionSelection = new SessionSelection(req, dao);
universe@59 352
universe@75 353 var version = new Version(-1, sessionSelection.project);
universe@59 354 try {
universe@75 355 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project);
universe@59 356 version.setName(getParameter(req, String.class, "name").orElseThrow());
universe@59 357 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
universe@59 358 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
universe@59 359 dao.getVersionDao().saveOrUpdate(version);
universe@59 360
universe@75 361 // specifying the pid parameter will purposely reset the session selected version!
universe@75 362 setRedirectLocation(req, "./projects/view?pid="+sessionSelection.project.getId());
universe@74 363 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@75 364 LOG.debug("Successfully updated version {} for project {}", version.getName(), sessionSelection.project.getName());
universe@75 365 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@59 366 // TODO: set request attribute with error text
universe@59 367 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 368 LOG.debug("Details:", ex);
universe@75 369 sessionSelection.selectVersion(version);
universe@76 370 configureEditVersionForm(req, dao, sessionSelection);
universe@59 371 }
universe@41 372
universe@43 373 return ResponseType.HTML;
universe@41 374 }
universe@64 375
universe@75 376 private void configureEditIssueForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
universe@83 377
universe@83 378 if (selection.issue.getProject() == null || selection.issue.getProject().getId() < 0) {
universe@83 379 req.setAttribute("projects", dao.getProjectDao().list());
universe@83 380 req.setAttribute("versions", Collections.<Version>emptyList());
universe@83 381 } else {
universe@83 382 req.setAttribute("projects", Collections.<Project>emptyList());
universe@83 383 req.setAttribute("versions", dao.getVersionDao().list(selection.issue.getProject()));
universe@83 384 }
universe@83 385
universe@83 386 dao.getIssueDao().joinVersionInformation(selection.issue);
universe@75 387 req.setAttribute("issue", selection.issue);
universe@71 388 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@71 389 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@75 390 req.setAttribute("users", dao.getUserDao().list());
universe@71 391
universe@74 392 setContentPage(req, "issue-form");
universe@80 393 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_ISSUE, selection));
universe@80 394 }
universe@80 395
universe@80 396 @RequestMapping(requestPath = "issues/", method = HttpMethod.GET)
universe@80 397 public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
universe@80 398 final var sessionSelection = new SessionSelection(req, dao);
universe@80 399 if (sessionSelection.project == null) {
universe@80 400 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
universe@80 401 return ResponseType.NONE;
universe@80 402 }
universe@80 403
universe@80 404 req.setAttribute("issues", dao.getIssueDao().list(sessionSelection.project));
universe@80 405
universe@80 406 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_ISSUE_LIST, sessionSelection));
universe@80 407 setContentPage(req, "issues");
universe@80 408 setStylesheet(req, "projects");
universe@80 409
universe@80 410 return ResponseType.HTML;
universe@71 411 }
universe@71 412
universe@64 413 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
universe@80 414 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
universe@75 415 final var sessionSelection = new SessionSelection(req, dao);
universe@64 416
universe@75 417 sessionSelection.selectIssue(findByParameter(req, Integer.class, "id",
universe@75 418 dao.getIssueDao()::find).orElse(new Issue(-1, sessionSelection.project)));
universe@75 419 configureEditIssueForm(req, dao, sessionSelection);
universe@64 420
universe@64 421 return ResponseType.HTML;
universe@64 422 }
universe@64 423
universe@64 424 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
universe@80 425 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
universe@75 426 final var sessionSelection = new SessionSelection(req, dao);
universe@64 427
universe@75 428 Issue issue = new Issue(-1, sessionSelection.project);
universe@64 429 try {
universe@75 430 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project);
universe@75 431 getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
universe@75 432 getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
universe@75 433 issue.setSubject(getParameter(req, String.class, "subject").orElseThrow());
universe@75 434 getParameter(req, Integer.class, "assignee").map(
universe@75 435 userid -> userid >= 0 ? new User(userid) : null
universe@75 436 ).ifPresent(issue::setAssignee);
universe@75 437 getParameter(req, String.class, "description").ifPresent(issue::setDescription);
universe@75 438 getParameter(req, Date.class, "eta").ifPresent(issue::setEta);
universe@83 439
universe@83 440 getParameter(req, Integer[].class, "affected")
universe@83 441 .map(Stream::of)
universe@83 442 .map(stream ->
universe@83 443 stream.map(id -> new Version(id, sessionSelection.project)).collect(Collectors.toList())
universe@83 444 ).ifPresent(issue::setAffectedVersions);
universe@83 445 getParameter(req, Integer[].class, "scheduled")
universe@83 446 .map(Stream::of)
universe@83 447 .map(stream ->
universe@83 448 stream.map(id -> new Version(id, sessionSelection.project)).collect(Collectors.toList())
universe@83 449 ).ifPresent(issue::setScheduledVersions);
universe@83 450 getParameter(req, Integer[].class, "resolved")
universe@83 451 .map(Stream::of)
universe@83 452 .map(stream ->
universe@83 453 stream.map(id -> new Version(id, sessionSelection.project)).collect(Collectors.toList())
universe@83 454 ).ifPresent(issue::setResolvedVersions);
universe@83 455
universe@64 456 dao.getIssueDao().saveOrUpdate(issue);
universe@64 457
universe@75 458 // specifying the issue parameter keeps the edited issue as breadcrumb
universe@81 459 setRedirectLocation(req, "./projects/issues/?issue="+issue.getId());
universe@74 460 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@75 461 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), sessionSelection.project.getName());
universe@75 462 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@64 463 // TODO: set request attribute with error text
universe@64 464 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@64 465 LOG.debug("Details:", ex);
universe@75 466 sessionSelection.selectIssue(issue);
universe@75 467 configureEditIssueForm(req, dao, sessionSelection);
universe@64 468 }
universe@64 469
universe@64 470 return ResponseType.HTML;
universe@64 471 }
universe@41 472 }

mercurial