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