Tue, 19 May 2020 16:50:05 +0200
fixes missing orElse() calls
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",
102 projectDao::find).orElse(new Project(-1)));
103 setDynamicFragment(req, "project-form");
105 return ResponseType.HTML;
106 }
108 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
109 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
111 Project project = new Project(-1);
112 try {
113 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
114 project.setName(getParameter(req, String.class, "name").orElseThrow());
115 getParameter(req, String.class, "description").ifPresent(project::setDescription);
116 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
117 getParameter(req, Integer.class, "owner").map(
118 ownerId -> ownerId >= 0 ? new User(ownerId) : null
119 ).ifPresent(project::setOwner);
121 dao.getProjectDao().saveOrUpdate(project);
123 setRedirectLocation(req, "./projects/index/");
124 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
125 LOG.debug("Successfully updated project {}", project.getName());
126 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
127 // TODO: set request attribute with error text
128 req.setAttribute("project", project);
129 setDynamicFragment(req, "project-form");
130 LOG.warn("Form validation failure: {}", ex.getMessage());
131 LOG.debug("Details:", ex);
132 }
134 return ResponseType.HTML;
135 }
137 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
138 public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
139 final var selectedProject = getSelectedProject(req, dao);
140 if (selectedProject == null) {
141 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
142 return ResponseType.NONE;
143 }
145 req.setAttribute("versions", dao.getVersionDao().list(selectedProject));
146 setDynamicFragment(req, "versions");
148 return ResponseType.HTML;
149 }
151 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
152 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
153 final var selectedProject = getSelectedProject(req, dao);
154 if (selectedProject == null) {
155 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
156 return ResponseType.NONE;
157 }
159 req.setAttribute("version", findByParameter(req, Integer.class, "id",
160 dao.getVersionDao()::find).orElse(new Version(-1, selectedProject)));
161 req.setAttribute("versionStatusEnum", VersionStatus.values());
163 setDynamicFragment(req, "version-form");
165 return ResponseType.HTML;
166 }
168 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
169 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
170 final var selectedProject = getSelectedProject(req, dao);
171 if (selectedProject == null) {
172 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
173 return ResponseType.NONE;
174 }
176 Version version = new Version(-1, selectedProject);
177 try {
178 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
179 version.setName(getParameter(req, String.class, "name").orElseThrow());
180 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
181 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
182 dao.getVersionDao().saveOrUpdate(version);
184 setRedirectLocation(req, "./projects/versions/");
185 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
186 LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName());
187 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
188 // TODO: set request attribute with error text
189 req.setAttribute("version", version);
190 req.setAttribute("versionStatusEnum", VersionStatus.values());
191 setDynamicFragment(req, "version-form");
192 LOG.warn("Form validation failure: {}", ex.getMessage());
193 LOG.debug("Details:", ex);
194 }
196 return ResponseType.HTML;
197 }
200 @RequestMapping(requestPath = "issues", method = HttpMethod.GET, menuKey = "menu.issues")
201 public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
202 final var selectedProject = getSelectedProject(req, dao);
203 if (selectedProject == null) {
204 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
205 return ResponseType.NONE;
206 }
208 req.setAttribute("issues", dao.getVersionDao().list(selectedProject));
209 setDynamicFragment(req, "issues");
211 return ResponseType.HTML;
212 }
214 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
215 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
216 final var selectedProject = getSelectedProject(req, dao);
217 if (selectedProject == null) {
218 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
219 return ResponseType.NONE;
220 }
222 req.setAttribute("issue", findByParameter(req, Integer.class, "id",
223 dao.getIssueDao()::find).orElse(new Issue(-1, selectedProject)));
224 req.setAttribute("issueStatusEnum", IssueStatus.values());
225 req.setAttribute("issueCategoryEnum", IssueCategory.values());
227 setDynamicFragment(req, "issue-form");
229 return ResponseType.HTML;
230 }
232 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
233 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
234 final var selectedProject = getSelectedProject(req, dao);
235 if (selectedProject == null) {
236 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
237 return ResponseType.NONE;
238 }
240 Issue issue = new Issue(-1, selectedProject);
241 try {
242 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
244 // TODO: implement
246 dao.getIssueDao().saveOrUpdate(issue);
248 setRedirectLocation(req, "./projects/issues/");
249 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
250 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName());
251 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
252 // TODO: set request attribute with error text
253 req.setAttribute("issue", issue);
254 req.setAttribute("issueStatusEnum", IssueStatus.values());
255 req.setAttribute("issueCategoryEnum", IssueCategory.values());
256 setDynamicFragment(req, "issue-form");
257 LOG.warn("Form validation failure: {}", ex.getMessage());
258 LOG.debug("Details:", ex);
259 }
261 return ResponseType.HTML;
262 }
263 }