Mon, 18 May 2020 21:08:14 +0200
adds backend methods for issues (TODO: implement commitIssue())
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 java.io.IOException;
42 import java.sql.SQLException;
43 import java.util.NoSuchElementException;
45 import static de.uapcore.lightpit.Functions.fqn;
47 @LightPITModule(
48 bundleBaseName = "localization.projects",
49 modulePath = "projects",
50 defaultPriority = 20
51 )
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");
62 private Project getSelectedProject(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
63 final var projectDao = dao.getProjectDao();
64 final var session = req.getSession();
65 final var projectSelection = getParameter(req, Integer.class, "pid");
66 if (projectSelection.isPresent()) {
67 final var selectedId = projectSelection.get();
68 final var selectedProject = projectDao.find(selectedId);
69 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, selectedProject);
70 return selectedProject;
71 } else {
72 return (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
73 }
74 }
76 @RequestMapping(method = HttpMethod.GET)
77 public ResponseType indexRedirect(HttpServletResponse resp) throws IOException {
78 resp.sendRedirect("index/");
79 return ResponseType.NONE;
80 }
82 @RequestMapping(requestPath = "index", method = HttpMethod.GET, menuKey = "menu.index")
83 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
85 final var projectList = dao.getProjectDao().list();
86 req.setAttribute("projects", projectList);
87 setDynamicFragment(req, "projects");
88 setStylesheet(req, "projects");
90 if (getSelectedProject(req, dao) == null) {
91 projectList.stream().findFirst().ifPresent(proj -> req.getSession().setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj));
92 }
94 return ResponseType.HTML;
95 }
97 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
98 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
99 final var projectDao = dao.getProjectDao();
101 req.setAttribute("project", findByParameter(req, Integer.class, "id", projectDao::find));
102 setDynamicFragment(req, "project-form");
104 return ResponseType.HTML;
105 }
107 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
108 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
110 Project project = new Project(-1);
111 try {
112 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
113 project.setName(getParameter(req, String.class, "name").orElseThrow());
114 getParameter(req, String.class, "description").ifPresent(project::setDescription);
115 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
116 getParameter(req, Integer.class, "owner").map(
117 ownerId -> ownerId >= 0 ? new User(ownerId) : null
118 ).ifPresent(project::setOwner);
120 dao.getProjectDao().saveOrUpdate(project);
122 setRedirectLocation(req, "./projects/index/");
123 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
124 LOG.debug("Successfully updated project {}", project.getName());
125 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
126 // TODO: set request attribute with error text
127 req.setAttribute("project", project);
128 setDynamicFragment(req, "project-form");
129 LOG.warn("Form validation failure: {}", ex.getMessage());
130 LOG.debug("Details:", ex);
131 }
133 return ResponseType.HTML;
134 }
136 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
137 public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
138 final var selectedProject = getSelectedProject(req, dao);
139 if (selectedProject == null) {
140 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
141 return ResponseType.NONE;
142 }
144 req.setAttribute("versions", dao.getVersionDao().list(selectedProject));
145 setDynamicFragment(req, "versions");
147 return ResponseType.HTML;
148 }
150 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
151 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
152 final var selectedProject = getSelectedProject(req, dao);
153 if (selectedProject == null) {
154 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
155 return ResponseType.NONE;
156 }
158 req.setAttribute("version", findByParameter(req, Integer.class, "id", dao.getVersionDao()::find));
159 req.setAttribute("versionStatusEnum", VersionStatus.values());
161 setDynamicFragment(req, "version-form");
163 return ResponseType.HTML;
164 }
166 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
167 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
168 final var selectedProject = getSelectedProject(req, dao);
169 if (selectedProject == null) {
170 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
171 return ResponseType.NONE;
172 }
174 Version version = new Version(-1, selectedProject);
175 try {
176 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
177 version.setName(getParameter(req, String.class, "name").orElseThrow());
178 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
179 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
180 dao.getVersionDao().saveOrUpdate(version);
182 setRedirectLocation(req, "./projects/versions/");
183 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
184 LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName());
185 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
186 // TODO: set request attribute with error text
187 req.setAttribute("version", version);
188 req.setAttribute("versionStatusEnum", VersionStatus.values());
189 setDynamicFragment(req, "version-form");
190 LOG.warn("Form validation failure: {}", ex.getMessage());
191 LOG.debug("Details:", ex);
192 }
194 return ResponseType.HTML;
195 }
198 @RequestMapping(requestPath = "issues", method = HttpMethod.GET, menuKey = "menu.issues")
199 public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
200 final var selectedProject = getSelectedProject(req, dao);
201 if (selectedProject == null) {
202 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
203 return ResponseType.NONE;
204 }
206 req.setAttribute("issues", dao.getVersionDao().list(selectedProject));
207 setDynamicFragment(req, "issues");
209 return ResponseType.HTML;
210 }
212 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
213 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
214 final var selectedProject = getSelectedProject(req, dao);
215 if (selectedProject == null) {
216 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
217 return ResponseType.NONE;
218 }
220 req.setAttribute("issue", findByParameter(req, Integer.class, "id", dao.getIssueDao()::find));
221 req.setAttribute("issueStatusEnum", IssueStatus.values());
222 req.setAttribute("issueCategoryEnum", IssueCategory.values());
224 setDynamicFragment(req, "issue-form");
226 return ResponseType.HTML;
227 }
229 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
230 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
231 final var selectedProject = getSelectedProject(req, dao);
232 if (selectedProject == null) {
233 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
234 return ResponseType.NONE;
235 }
237 Issue issue = new Issue(-1, selectedProject);
238 try {
239 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
241 // TODO: implement
243 dao.getIssueDao().saveOrUpdate(issue);
245 setRedirectLocation(req, "./projects/issues/");
246 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
247 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName());
248 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
249 // TODO: set request attribute with error text
250 req.setAttribute("issue", issue);
251 req.setAttribute("issueStatusEnum", IssueStatus.values());
252 req.setAttribute("issueCategoryEnum", IssueCategory.values());
253 setDynamicFragment(req, "issue-form");
254 LOG.warn("Form validation failure: {}", ex.getMessage());
255 LOG.debug("Details:", ex);
256 }
258 return ResponseType.HTML;
259 }
260 }