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

Sat, 23 May 2020 14:13:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 23 May 2020 14:13:09 +0200
changeset 79
f64255a88d66
parent 78
bb4c52bf3439
child 80
27a25f32048e
permissions
-rw-r--r--

bloat removal 3/3 - LightPITModule annotation and ModuleManager

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

mercurial