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

Sat, 23 May 2020 13:24:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 23 May 2020 13:24:49 +0200
changeset 76
82f71fb1758a
parent 75
33b6843fdf8a
child 78
bb4c52bf3439
permissions
-rw-r--r--

issue and version form now also work if no project is selected in the session

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@71 45 import java.util.ArrayList;
universe@71 46 import java.util.List;
universe@59 47 import java.util.NoSuchElementException;
universe@75 48 import java.util.Objects;
universe@41 49
universe@52 50 import static de.uapcore.lightpit.Functions.fqn;
universe@52 51
universe@41 52 @LightPITModule(
universe@41 53 bundleBaseName = "localization.projects",
universe@41 54 modulePath = "projects",
universe@41 55 defaultPriority = 20
universe@41 56 )
universe@41 57 @WebServlet(
universe@41 58 name = "ProjectsModule",
universe@41 59 urlPatterns = "/projects/*"
universe@41 60 )
universe@41 61 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 62
universe@59 63 private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class);
universe@59 64
universe@52 65 public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project");
universe@75 66 public static final String SESSION_ATTR_SELECTED_ISSUE = fqn(ProjectsModule.class, "selected-issue");
universe@75 67 public static final String SESSION_ATTR_SELECTED_VERSION = fqn(ProjectsModule.class, "selected-version");
universe@52 68
universe@75 69 private class SessionSelection {
universe@75 70 final HttpSession session;
universe@75 71 Project project;
universe@75 72 Version version;
universe@75 73 Issue issue;
universe@75 74
universe@75 75 SessionSelection(HttpServletRequest req, Project project) {
universe@75 76 this.session = req.getSession();
universe@75 77 this.project = project;
universe@75 78 version = null;
universe@75 79 issue = null;
universe@75 80 updateAttributes();
universe@64 81 }
universe@75 82
universe@75 83 SessionSelection(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@75 84 this.session = req.getSession();
universe@75 85 final var issueDao = dao.getIssueDao();
universe@75 86 final var projectDao = dao.getProjectDao();
universe@75 87 final var issueSelection = getParameter(req, Integer.class, "issue");
universe@75 88 if (issueSelection.isPresent()) {
universe@75 89 issue = issueDao.find(issueSelection.get());
universe@75 90 } else {
universe@75 91 final var issue = (Issue) session.getAttribute(SESSION_ATTR_SELECTED_ISSUE);
universe@75 92 this.issue = issue == null ? null : issueDao.find(issue.getId());
universe@75 93 }
universe@75 94 if (issue != null) {
universe@75 95 version = null; // show the issue globally
universe@75 96 project = projectDao.find(issue.getProject().getId());
universe@75 97 }
universe@75 98
universe@75 99 final var projectSelection = getParameter(req, Integer.class, "pid");
universe@75 100 if (projectSelection.isPresent()) {
universe@75 101 final var selectedProject = projectDao.find(projectSelection.get());
universe@75 102 if (!Objects.equals(selectedProject, project)) {
universe@75 103 // reset version and issue if project changed
universe@75 104 version = null;
universe@75 105 issue = null;
universe@75 106 }
universe@75 107 project = selectedProject;
universe@75 108 } else {
universe@75 109 final var sessionProject = (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
universe@75 110 project = sessionProject == null ? null : projectDao.find(sessionProject.getId());
universe@75 111 }
universe@75 112 updateAttributes();
universe@75 113 }
universe@75 114
universe@75 115 void selectVersion(Version version) {
universe@76 116 if (!Objects.equals(project, version.getProject())) throw new AssertionError("Nice, you implemented a bug!");
universe@75 117 this.version = version;
universe@75 118 this.issue = null;
universe@75 119 updateAttributes();
universe@75 120 }
universe@75 121
universe@75 122 void selectIssue(Issue issue) {
universe@76 123 if (!Objects.equals(issue.getProject(), project)) throw new AssertionError("Nice, you implemented a bug!");
universe@75 124 this.issue = issue;
universe@75 125 this.version = null;
universe@75 126 updateAttributes();
universe@75 127 }
universe@75 128
universe@75 129 void updateAttributes() {
universe@75 130 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, project);
universe@75 131 session.setAttribute(SESSION_ATTR_SELECTED_VERSION, version);
universe@75 132 session.setAttribute(SESSION_ATTR_SELECTED_ISSUE, issue);
universe@75 133 }
universe@71 134 }
universe@71 135
universe@71 136
universe@71 137 /**
universe@71 138 * Creates the breadcrumb menu.
universe@71 139 *
universe@75 140 * @param level the current active level (0: root, 1: project, 2: version, 3: issue)
universe@75 141 * @param sessionSelection the currently selected objects
universe@71 142 * @return a dynamic breadcrumb menu trying to display as many levels as possible
universe@71 143 */
universe@75 144 private List<MenuEntry> getBreadcrumbs(int level, SessionSelection sessionSelection) {
universe@71 145 MenuEntry entry;
universe@71 146
universe@71 147 final var breadcrumbs = new ArrayList<MenuEntry>();
universe@71 148 entry = new MenuEntry(new ResourceKey("localization.projects", "menuLabel"),
universe@71 149 "projects/", 0);
universe@71 150 breadcrumbs.add(entry);
universe@71 151 if (level == 0) entry.setActive(true);
universe@71 152
universe@75 153 if (sessionSelection.project != null) {
universe@75 154 if (sessionSelection.project.getId() < 0) {
universe@75 155 entry = new MenuEntry(new ResourceKey("localization.projects", "button.create"),
universe@75 156 "projects/edit", 1);
universe@75 157 } else {
universe@75 158 entry = new MenuEntry(sessionSelection.project.getName(),
universe@75 159 "projects/view?pid=" + sessionSelection.project.getId(), 1);
universe@75 160 }
universe@75 161 if (level == 1) entry.setActive(true);
universe@75 162 breadcrumbs.add(entry);
universe@75 163 }
universe@71 164
universe@75 165 if (sessionSelection.version != null) {
universe@75 166 if (sessionSelection.version.getId() < 0) {
universe@75 167 entry = new MenuEntry(new ResourceKey("localization.projects", "button.version.create"),
universe@75 168 "projects/versions/edit", 2);
universe@75 169 } else {
universe@75 170 entry = new MenuEntry(sessionSelection.version.getName(),
universe@75 171 // TODO: change link to issue overview for that version
universe@75 172 "projects/versions/edit?id=" + sessionSelection.version.getId(), 2);
universe@75 173 }
universe@75 174 if (level == 2) entry.setActive(true);
universe@75 175 breadcrumbs.add(entry);
universe@75 176 }
universe@71 177
universe@75 178 if (sessionSelection.issue != null) {
universe@75 179 entry = new MenuEntry(new ResourceKey("localization.projects", "menu.issues"),
universe@75 180 // TODO: change link to a separate issue view (maybe depending on the selected version)
universe@75 181 "projects/view?pid=" + sessionSelection.issue.getProject().getId(), 3);
universe@75 182 breadcrumbs.add(entry);
universe@75 183 if (sessionSelection.issue.getId() < 0) {
universe@75 184 entry = new MenuEntry(new ResourceKey("localization.projects", "button.issue.create"),
universe@75 185 "projects/issues/edit", 2);
universe@75 186 } else {
universe@75 187 entry = new MenuEntry("#" + sessionSelection.issue.getId(),
universe@75 188 // TODO: maybe change link to a view rather than directly opening the editor
universe@75 189 "projects/issues/edit?id=" + sessionSelection.issue.getId(), 4);
universe@75 190 }
universe@75 191 if (level == 3) entry.setActive(true);
universe@75 192 breadcrumbs.add(entry);
universe@75 193 }
universe@75 194
universe@71 195 return breadcrumbs;
universe@64 196 }
universe@64 197
universe@61 198 @RequestMapping(method = HttpMethod.GET)
universe@47 199 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@75 200 final var sessionSelection = new SessionSelection(req, dao);
universe@52 201 final var projectList = dao.getProjectDao().list();
universe@52 202 req.setAttribute("projects", projectList);
universe@74 203 setContentPage(req, "projects");
universe@52 204 setStylesheet(req, "projects");
universe@52 205
universe@75 206 setBreadcrumbs(req, getBreadcrumbs(0, sessionSelection));
universe@45 207
universe@45 208 return ResponseType.HTML;
universe@45 209 }
universe@45 210
universe@75 211 private void configureEditForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
universe@75 212 req.setAttribute("project", selection.project);
universe@71 213 req.setAttribute("users", dao.getUserDao().list());
universe@74 214 setContentPage(req, "project-form");
universe@75 215 setBreadcrumbs(req, getBreadcrumbs(1, selection));
universe@71 216 }
universe@71 217
universe@47 218 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
universe@51 219 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@75 220 final var selection = new SessionSelection(req, findByParameter(req, Integer.class, "id",
universe@75 221 dao.getProjectDao()::find).orElse(new Project(-1)));
universe@47 222
universe@75 223 configureEditForm(req, dao, selection);
universe@47 224
universe@47 225 return ResponseType.HTML;
universe@47 226 }
universe@47 227
universe@47 228 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@68 229 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 230
universe@75 231 Project project = new Project(-1);
universe@47 232 try {
universe@47 233 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
universe@47 234 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@47 235 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 236 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 237 getParameter(req, Integer.class, "owner").map(
universe@47 238 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 239 ).ifPresent(project::setOwner);
universe@47 240
universe@47 241 dao.getProjectDao().saveOrUpdate(project);
universe@47 242
universe@70 243 setRedirectLocation(req, "./projects/");
universe@74 244 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@59 245 LOG.debug("Successfully updated project {}", project.getName());
universe@75 246 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@47 247 // TODO: set request attribute with error text
universe@59 248 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 249 LOG.debug("Details:", ex);
universe@75 250 configureEditForm(req, dao, new SessionSelection(req, project));
universe@47 251 }
universe@47 252
universe@47 253 return ResponseType.HTML;
universe@47 254 }
universe@47 255
universe@70 256 @RequestMapping(requestPath = "view", method = HttpMethod.GET)
universe@70 257 public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@75 258 final var sessionSelection = new SessionSelection(req, dao);
universe@47 259
universe@75 260 req.setAttribute("versions", dao.getVersionDao().list(sessionSelection.project));
universe@75 261 req.setAttribute("issues", dao.getIssueDao().list(sessionSelection.project));
universe@70 262
universe@75 263 setBreadcrumbs(req, getBreadcrumbs(1, sessionSelection));
universe@74 264 setContentPage(req, "project-details");
universe@59 265
universe@59 266 return ResponseType.HTML;
universe@59 267 }
universe@59 268
universe@76 269 private void configureEditVersionForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
universe@76 270 req.setAttribute("projects", dao.getProjectDao().list());
universe@75 271 req.setAttribute("version", selection.version);
universe@71 272 req.setAttribute("versionStatusEnum", VersionStatus.values());
universe@71 273
universe@74 274 setContentPage(req, "version-form");
universe@75 275 setBreadcrumbs(req, getBreadcrumbs(2, selection));
universe@71 276 }
universe@71 277
universe@59 278 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
universe@59 279 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@75 280 final var sessionSelection = new SessionSelection(req, dao);
universe@59 281
universe@75 282 sessionSelection.selectVersion(findByParameter(req, Integer.class, "id", dao.getVersionDao()::find)
universe@75 283 .orElse(new Version(-1, sessionSelection.project)));
universe@76 284 configureEditVersionForm(req, dao, sessionSelection);
universe@59 285
universe@59 286 return ResponseType.HTML;
universe@59 287 }
universe@59 288
universe@59 289 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
universe@64 290 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@75 291 final var sessionSelection = new SessionSelection(req, dao);
universe@59 292
universe@75 293 var version = new Version(-1, sessionSelection.project);
universe@59 294 try {
universe@75 295 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project);
universe@59 296 version.setName(getParameter(req, String.class, "name").orElseThrow());
universe@59 297 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
universe@59 298 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
universe@59 299 dao.getVersionDao().saveOrUpdate(version);
universe@59 300
universe@75 301 // specifying the pid parameter will purposely reset the session selected version!
universe@75 302 setRedirectLocation(req, "./projects/view?pid="+sessionSelection.project.getId());
universe@74 303 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@75 304 LOG.debug("Successfully updated version {} for project {}", version.getName(), sessionSelection.project.getName());
universe@75 305 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@59 306 // TODO: set request attribute with error text
universe@59 307 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 308 LOG.debug("Details:", ex);
universe@75 309 sessionSelection.selectVersion(version);
universe@76 310 configureEditVersionForm(req, dao, sessionSelection);
universe@59 311 }
universe@41 312
universe@43 313 return ResponseType.HTML;
universe@41 314 }
universe@64 315
universe@75 316 private void configureEditIssueForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
universe@76 317 req.setAttribute("projects", dao.getProjectDao().list());
universe@75 318 req.setAttribute("issue", selection.issue);
universe@71 319 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@71 320 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@75 321 req.setAttribute("users", dao.getUserDao().list());
universe@71 322
universe@74 323 setContentPage(req, "issue-form");
universe@75 324 setBreadcrumbs(req, getBreadcrumbs(3, selection));
universe@71 325 }
universe@71 326
universe@64 327 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
universe@64 328 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@75 329 final var sessionSelection = new SessionSelection(req, dao);
universe@64 330
universe@75 331 sessionSelection.selectIssue(findByParameter(req, Integer.class, "id",
universe@75 332 dao.getIssueDao()::find).orElse(new Issue(-1, sessionSelection.project)));
universe@75 333 configureEditIssueForm(req, dao, sessionSelection);
universe@64 334
universe@64 335 return ResponseType.HTML;
universe@64 336 }
universe@64 337
universe@64 338 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
universe@64 339 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@75 340 final var sessionSelection = new SessionSelection(req, dao);
universe@64 341
universe@75 342 Issue issue = new Issue(-1, sessionSelection.project);
universe@64 343 try {
universe@75 344 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project);
universe@75 345 getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
universe@75 346 getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
universe@75 347 issue.setSubject(getParameter(req, String.class, "subject").orElseThrow());
universe@75 348 getParameter(req, Integer.class, "assignee").map(
universe@75 349 userid -> userid >= 0 ? new User(userid) : null
universe@75 350 ).ifPresent(issue::setAssignee);
universe@75 351 getParameter(req, String.class, "description").ifPresent(issue::setDescription);
universe@75 352 getParameter(req, Date.class, "eta").ifPresent(issue::setEta);
universe@64 353 dao.getIssueDao().saveOrUpdate(issue);
universe@64 354
universe@75 355 // TODO: redirect to issue overview
universe@75 356 // specifying the issue parameter keeps the edited issue as breadcrumb
universe@75 357 setRedirectLocation(req, "./projects/view?issue="+issue.getId());
universe@74 358 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
universe@75 359 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), sessionSelection.project.getName());
universe@75 360 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
universe@64 361 // TODO: set request attribute with error text
universe@64 362 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@64 363 LOG.debug("Details:", ex);
universe@75 364 sessionSelection.selectIssue(issue);
universe@75 365 configureEditIssueForm(req, dao, sessionSelection);
universe@64 366 }
universe@64 367
universe@64 368 return ResponseType.HTML;
universe@64 369 }
universe@41 370 }

mercurial