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