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

Sat, 23 May 2020 13:52:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 23 May 2020 13:52:04 +0200
changeset 78
bb4c52bf3439
parent 76
82f71fb1758a
child 79
f64255a88d66
permissions
-rw-r--r--

bloat removal 2/3 - moduleInfo

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

mercurial