Sat, 30 May 2020 18:05:06 +0200
adds version selection in issue editor
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2018 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 package de.uapcore.lightpit.modules;
32 import de.uapcore.lightpit.*;
33 import de.uapcore.lightpit.dao.DataAccessObjects;
34 import de.uapcore.lightpit.entities.*;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import javax.servlet.annotation.WebServlet;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.http.HttpSession;
42 import java.io.IOException;
43 import java.sql.Date;
44 import java.sql.SQLException;
45 import java.util.*;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
49 import static de.uapcore.lightpit.Functions.fqn;
51 @WebServlet(
52 name = "ProjectsModule",
53 urlPatterns = "/projects/*"
54 )
55 public final class ProjectsModule extends AbstractLightPITServlet {
57 private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class);
59 public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected_project");
60 public static final String SESSION_ATTR_SELECTED_ISSUE = fqn(ProjectsModule.class, "selected_issue");
61 public static final String SESSION_ATTR_SELECTED_VERSION = fqn(ProjectsModule.class, "selected_version");
62 public static final String SESSION_ATTR_HIDE_ZEROS = fqn(ProjectsModule.class, "stats_hide_zeros");
64 private class SessionSelection {
65 final HttpSession session;
66 Project project;
67 Version version;
68 Issue issue;
70 SessionSelection(HttpServletRequest req, Project project) {
71 this.session = req.getSession();
72 this.project = project;
73 version = null;
74 issue = null;
75 updateAttributes();
76 }
78 SessionSelection(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
79 this.session = req.getSession();
80 final var issueDao = dao.getIssueDao();
81 final var projectDao = dao.getProjectDao();
82 final var issueSelection = getParameter(req, Integer.class, "issue");
83 if (issueSelection.isPresent()) {
84 issue = issueDao.find(issueSelection.get());
85 } else {
86 final var issue = (Issue) session.getAttribute(SESSION_ATTR_SELECTED_ISSUE);
87 this.issue = issue == null ? null : issueDao.find(issue.getId());
88 }
89 if (issue != null) {
90 version = null; // show the issue globally
91 project = projectDao.find(issue.getProject().getId());
92 }
94 final var projectSelection = getParameter(req, Integer.class, "pid");
95 if (projectSelection.isPresent()) {
96 final var selectedProject = projectDao.find(projectSelection.get());
97 if (!Objects.equals(selectedProject, project)) {
98 // reset version and issue if project changed
99 version = null;
100 issue = null;
101 }
102 project = selectedProject;
103 } else {
104 final var sessionProject = (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
105 project = sessionProject == null ? null : projectDao.find(sessionProject.getId());
106 }
107 updateAttributes();
108 }
110 void selectVersion(Version version) {
111 this.project = version.getProject();
112 this.version = version;
113 this.issue = null;
114 updateAttributes();
115 }
117 void selectIssue(Issue issue) {
118 this.project = issue.getProject();
119 this.issue = issue;
120 this.version = null;
121 updateAttributes();
122 }
124 void updateAttributes() {
125 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, project);
126 session.setAttribute(SESSION_ATTR_SELECTED_VERSION, version);
127 session.setAttribute(SESSION_ATTR_SELECTED_ISSUE, issue);
128 }
129 }
131 private void setAttributeHideZeros(HttpServletRequest req) {
132 final Boolean value;
133 final var param = getParameter(req, Boolean.class, "reduced");
134 if (param.isPresent()) {
135 value = param.get();
136 req.getSession().setAttribute(SESSION_ATTR_HIDE_ZEROS, value);
137 } else {
138 final var sessionValue = req.getSession().getAttribute(SESSION_ATTR_HIDE_ZEROS);
139 if (sessionValue != null) {
140 value = (Boolean) sessionValue;
141 } else {
142 value = false;
143 req.getSession().setAttribute(SESSION_ATTR_HIDE_ZEROS, value);
144 }
145 }
146 req.setAttribute("statsHideZeros", value);
147 }
149 @Override
150 protected String getResourceBundleName() {
151 return "localization.projects";
152 }
155 private static final int BREADCRUMB_LEVEL_ROOT = 0;
156 private static final int BREADCRUMB_LEVEL_PROJECT = 1;
157 private static final int BREADCRUMB_LEVEL_VERSION = 2;
158 private static final int BREADCRUMB_LEVEL_ISSUE_LIST = 3;
159 private static final int BREADCRUMB_LEVEL_ISSUE = 4;
161 /**
162 * Creates the breadcrumb menu.
163 *
164 * @param level the current active level (0: root, 1: project, 2: version, 3: issue list, 4: issue)
165 * @param sessionSelection the currently selected objects
166 * @return a dynamic breadcrumb menu trying to display as many levels as possible
167 */
168 private List<MenuEntry> getBreadcrumbs(int level, SessionSelection sessionSelection) {
169 MenuEntry entry;
171 final var breadcrumbs = new ArrayList<MenuEntry>();
172 entry = new MenuEntry(new ResourceKey("localization.lightpit", "menu.projects"),
173 "projects/");
174 breadcrumbs.add(entry);
175 if (level == BREADCRUMB_LEVEL_ROOT) entry.setActive(true);
177 if (sessionSelection.project != null) {
178 if (sessionSelection.project.getId() < 0) {
179 entry = new MenuEntry(new ResourceKey("localization.projects", "button.create"),
180 "projects/edit");
181 } else {
182 entry = new MenuEntry(sessionSelection.project.getName(),
183 "projects/view?pid=" + sessionSelection.project.getId());
184 }
185 if (level == BREADCRUMB_LEVEL_PROJECT) entry.setActive(true);
186 breadcrumbs.add(entry);
187 }
189 if (sessionSelection.version != null) {
190 if (sessionSelection.version.getId() < 0) {
191 entry = new MenuEntry(new ResourceKey("localization.projects", "button.version.create"),
192 "projects/versions/edit");
193 } else {
194 entry = new MenuEntry(sessionSelection.version.getName(),
195 // TODO: change link to issue overview for that version
196 "projects/versions/edit?id=" + sessionSelection.version.getId());
197 }
198 if (level == BREADCRUMB_LEVEL_VERSION) entry.setActive(true);
199 breadcrumbs.add(entry);
200 }
202 if (sessionSelection.project != null) {
203 entry = new MenuEntry(new ResourceKey("localization.projects", "menu.issues"),
204 // TODO: maybe also add selected version
205 "projects/issues/?pid=" + sessionSelection.project.getId());
206 if (level == BREADCRUMB_LEVEL_ISSUE_LIST) entry.setActive(true);
207 breadcrumbs.add(entry);
208 }
210 if (sessionSelection.issue != null) {
211 if (sessionSelection.issue.getId() < 0) {
212 entry = new MenuEntry(new ResourceKey("localization.projects", "button.issue.create"),
213 "projects/issues/edit");
214 } else {
215 entry = new MenuEntry("#" + sessionSelection.issue.getId(),
216 // TODO: maybe change link to a view rather than directly opening the editor
217 "projects/issues/edit?id=" + sessionSelection.issue.getId());
218 }
219 if (level == BREADCRUMB_LEVEL_ISSUE) entry.setActive(true);
220 breadcrumbs.add(entry);
221 }
223 return breadcrumbs;
224 }
226 @RequestMapping(method = HttpMethod.GET)
227 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
228 final var sessionSelection = new SessionSelection(req, dao);
229 final var projectList = dao.getProjectDao().list();
230 req.setAttribute("projects", projectList);
231 setContentPage(req, "projects");
232 setStylesheet(req, "projects");
234 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_ROOT, sessionSelection));
236 return ResponseType.HTML;
237 }
239 private void configureEditForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
240 req.setAttribute("project", selection.project);
241 req.setAttribute("users", dao.getUserDao().list());
242 setContentPage(req, "project-form");
243 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_PROJECT, selection));
244 }
246 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
247 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
248 final var selection = new SessionSelection(req, findByParameter(req, Integer.class, "id",
249 dao.getProjectDao()::find).orElse(new Project(-1)));
251 configureEditForm(req, dao, selection);
253 return ResponseType.HTML;
254 }
256 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
257 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
259 Project project = new Project(-1);
260 try {
261 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
262 project.setName(getParameter(req, String.class, "name").orElseThrow());
263 getParameter(req, String.class, "description").ifPresent(project::setDescription);
264 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
265 getParameter(req, Integer.class, "owner").map(
266 ownerId -> ownerId >= 0 ? new User(ownerId) : null
267 ).ifPresent(project::setOwner);
269 dao.getProjectDao().saveOrUpdate(project);
271 setRedirectLocation(req, "./projects/");
272 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
273 LOG.debug("Successfully updated project {}", project.getName());
274 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
275 // TODO: set request attribute with error text
276 LOG.warn("Form validation failure: {}", ex.getMessage());
277 LOG.debug("Details:", ex);
278 configureEditForm(req, dao, new SessionSelection(req, project));
279 }
281 return ResponseType.HTML;
282 }
284 @RequestMapping(requestPath = "view", method = HttpMethod.GET)
285 public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
286 final var sessionSelection = new SessionSelection(req, dao);
287 if (sessionSelection.project == null) {
288 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
289 return ResponseType.NONE;
290 }
292 final var versionDao = dao.getVersionDao();
293 final var versions = versionDao.list(sessionSelection.project);
294 final var statsAffected = new ArrayList<VersionStatistics>();
295 final var statsScheduled = new ArrayList<VersionStatistics>();
296 final var statsResolved = new ArrayList<VersionStatistics>();
297 for (Version version : versions) {
298 statsAffected.add(versionDao.statsOpenedIssues(version));
299 statsScheduled.add(versionDao.statsScheduledIssues(version));
300 statsResolved.add(versionDao.statsResolvedIssues(version));
301 }
303 setAttributeHideZeros(req);
305 req.setAttribute("project", sessionSelection.project);
306 req.setAttribute("versions", versions);
307 req.setAttribute("statsAffected", statsAffected);
308 req.setAttribute("statsScheduled", statsScheduled);
309 req.setAttribute("statsResolved", statsResolved);
311 req.setAttribute("issueStatusEnum", IssueStatus.values());
312 req.setAttribute("issueCategoryEnum", IssueCategory.values());
314 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_PROJECT, sessionSelection));
315 setContentPage(req, "project-details");
316 setStylesheet(req, "projects");
318 return ResponseType.HTML;
319 }
321 private void configureEditVersionForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
322 final var versionDao = dao.getVersionDao();
323 req.setAttribute("projects", dao.getProjectDao().list());
324 req.setAttribute("version", selection.version);
325 req.setAttribute("versionStatusEnum", VersionStatus.values());
327 req.setAttribute("issueStatusEnum", IssueStatus.values());
328 req.setAttribute("issueCategoryEnum", IssueCategory.values());
329 req.setAttribute("statsAffected", versionDao.statsOpenedIssues(selection.version));
330 req.setAttribute("statsScheduled", versionDao.statsScheduledIssues(selection.version));
331 req.setAttribute("statsResolved", versionDao.statsResolvedIssues(selection.version));
332 setAttributeHideZeros(req);
334 setContentPage(req, "version-form");
335 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_VERSION, selection));
336 }
338 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
339 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
340 final var sessionSelection = new SessionSelection(req, dao);
342 sessionSelection.selectVersion(findByParameter(req, Integer.class, "id", dao.getVersionDao()::find)
343 .orElse(new Version(-1, sessionSelection.project)));
344 configureEditVersionForm(req, dao, sessionSelection);
346 return ResponseType.HTML;
347 }
349 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
350 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
351 final var sessionSelection = new SessionSelection(req, dao);
353 var version = new Version(-1, sessionSelection.project);
354 try {
355 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project);
356 version.setName(getParameter(req, String.class, "name").orElseThrow());
357 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
358 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
359 dao.getVersionDao().saveOrUpdate(version);
361 // specifying the pid parameter will purposely reset the session selected version!
362 setRedirectLocation(req, "./projects/view?pid="+sessionSelection.project.getId());
363 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
364 LOG.debug("Successfully updated version {} for project {}", version.getName(), sessionSelection.project.getName());
365 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
366 // TODO: set request attribute with error text
367 LOG.warn("Form validation failure: {}", ex.getMessage());
368 LOG.debug("Details:", ex);
369 sessionSelection.selectVersion(version);
370 configureEditVersionForm(req, dao, sessionSelection);
371 }
373 return ResponseType.HTML;
374 }
376 private void configureEditIssueForm(HttpServletRequest req, DataAccessObjects dao, SessionSelection selection) throws SQLException {
378 if (selection.issue.getProject() == null || selection.issue.getProject().getId() < 0) {
379 req.setAttribute("projects", dao.getProjectDao().list());
380 req.setAttribute("versions", Collections.<Version>emptyList());
381 } else {
382 req.setAttribute("projects", Collections.<Project>emptyList());
383 req.setAttribute("versions", dao.getVersionDao().list(selection.issue.getProject()));
384 }
386 dao.getIssueDao().joinVersionInformation(selection.issue);
387 req.setAttribute("issue", selection.issue);
388 req.setAttribute("issueStatusEnum", IssueStatus.values());
389 req.setAttribute("issueCategoryEnum", IssueCategory.values());
390 req.setAttribute("users", dao.getUserDao().list());
392 setContentPage(req, "issue-form");
393 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_ISSUE, selection));
394 }
396 @RequestMapping(requestPath = "issues/", method = HttpMethod.GET)
397 public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException, IOException {
398 final var sessionSelection = new SessionSelection(req, dao);
399 if (sessionSelection.project == null) {
400 resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No project selected.");
401 return ResponseType.NONE;
402 }
404 req.setAttribute("issues", dao.getIssueDao().list(sessionSelection.project));
406 setBreadcrumbs(req, getBreadcrumbs(BREADCRUMB_LEVEL_ISSUE_LIST, sessionSelection));
407 setContentPage(req, "issues");
408 setStylesheet(req, "projects");
410 return ResponseType.HTML;
411 }
413 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
414 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
415 final var sessionSelection = new SessionSelection(req, dao);
417 sessionSelection.selectIssue(findByParameter(req, Integer.class, "id",
418 dao.getIssueDao()::find).orElse(new Issue(-1, sessionSelection.project)));
419 configureEditIssueForm(req, dao, sessionSelection);
421 return ResponseType.HTML;
422 }
424 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
425 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws SQLException {
426 final var sessionSelection = new SessionSelection(req, dao);
428 Issue issue = new Issue(-1, sessionSelection.project);
429 try {
430 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), sessionSelection.project);
431 getParameter(req, String.class, "category").map(IssueCategory::valueOf).ifPresent(issue::setCategory);
432 getParameter(req, String.class, "status").map(IssueStatus::valueOf).ifPresent(issue::setStatus);
433 issue.setSubject(getParameter(req, String.class, "subject").orElseThrow());
434 getParameter(req, Integer.class, "assignee").map(
435 userid -> userid >= 0 ? new User(userid) : null
436 ).ifPresent(issue::setAssignee);
437 getParameter(req, String.class, "description").ifPresent(issue::setDescription);
438 getParameter(req, Date.class, "eta").ifPresent(issue::setEta);
440 getParameter(req, Integer[].class, "affected")
441 .map(Stream::of)
442 .map(stream ->
443 stream.map(id -> new Version(id, sessionSelection.project)).collect(Collectors.toList())
444 ).ifPresent(issue::setAffectedVersions);
445 getParameter(req, Integer[].class, "scheduled")
446 .map(Stream::of)
447 .map(stream ->
448 stream.map(id -> new Version(id, sessionSelection.project)).collect(Collectors.toList())
449 ).ifPresent(issue::setScheduledVersions);
450 getParameter(req, Integer[].class, "resolved")
451 .map(Stream::of)
452 .map(stream ->
453 stream.map(id -> new Version(id, sessionSelection.project)).collect(Collectors.toList())
454 ).ifPresent(issue::setResolvedVersions);
456 dao.getIssueDao().saveOrUpdate(issue);
458 // specifying the issue parameter keeps the edited issue as breadcrumb
459 setRedirectLocation(req, "./projects/issues/?issue="+issue.getId());
460 setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL);
461 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), sessionSelection.project.getName());
462 } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) {
463 // TODO: set request attribute with error text
464 LOG.warn("Form validation failure: {}", ex.getMessage());
465 LOG.debug("Details:", ex);
466 sessionSelection.selectIssue(issue);
467 configureEditIssueForm(req, dao, sessionSelection);
468 }
470 return ResponseType.HTML;
471 }
472 }