Tue, 19 May 2020 16:57:29 +0200
adds request attribute users in commit() error path
universe@41 | 1 | /* |
universe@41 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@41 | 3 | * |
universe@41 | 4 | * Copyright 2018 Mike Becker. All rights reserved. |
universe@41 | 5 | * |
universe@41 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@41 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@41 | 8 | * |
universe@41 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@41 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@41 | 11 | * |
universe@41 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@41 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@41 | 14 | * documentation and/or other materials provided with the distribution. |
universe@41 | 15 | * |
universe@41 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@41 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@41 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@41 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@41 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@41 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@41 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@41 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@41 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@41 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@41 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@41 | 27 | * |
universe@41 | 28 | */ |
universe@41 | 29 | package de.uapcore.lightpit.modules; |
universe@41 | 30 | |
universe@41 | 31 | |
universe@41 | 32 | import de.uapcore.lightpit.*; |
universe@41 | 33 | import de.uapcore.lightpit.dao.DataAccessObjects; |
universe@64 | 34 | import de.uapcore.lightpit.entities.*; |
universe@59 | 35 | import org.slf4j.Logger; |
universe@59 | 36 | import org.slf4j.LoggerFactory; |
universe@41 | 37 | |
universe@41 | 38 | import javax.servlet.annotation.WebServlet; |
universe@41 | 39 | import javax.servlet.http.HttpServletRequest; |
universe@59 | 40 | import javax.servlet.http.HttpServletResponse; |
universe@59 | 41 | import java.io.IOException; |
universe@47 | 42 | import java.sql.SQLException; |
universe@59 | 43 | import java.util.NoSuchElementException; |
universe@41 | 44 | |
universe@52 | 45 | import static de.uapcore.lightpit.Functions.fqn; |
universe@52 | 46 | |
universe@41 | 47 | @LightPITModule( |
universe@41 | 48 | bundleBaseName = "localization.projects", |
universe@41 | 49 | modulePath = "projects", |
universe@41 | 50 | defaultPriority = 20 |
universe@41 | 51 | ) |
universe@41 | 52 | @WebServlet( |
universe@41 | 53 | name = "ProjectsModule", |
universe@41 | 54 | urlPatterns = "/projects/*" |
universe@41 | 55 | ) |
universe@41 | 56 | public final class ProjectsModule extends AbstractLightPITServlet { |
universe@41 | 57 | |
universe@59 | 58 | private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); |
universe@59 | 59 | |
universe@52 | 60 | public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project"); |
universe@52 | 61 | |
universe@64 | 62 | private Project getSelectedProject(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@64 | 63 | final var projectDao = dao.getProjectDao(); |
universe@64 | 64 | final var session = req.getSession(); |
universe@64 | 65 | final var projectSelection = getParameter(req, Integer.class, "pid"); |
universe@64 | 66 | if (projectSelection.isPresent()) { |
universe@64 | 67 | final var selectedId = projectSelection.get(); |
universe@64 | 68 | final var selectedProject = projectDao.find(selectedId); |
universe@64 | 69 | session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, selectedProject); |
universe@64 | 70 | return selectedProject; |
universe@64 | 71 | } else { |
universe@64 | 72 | return (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
universe@64 | 73 | } |
universe@64 | 74 | } |
universe@64 | 75 | |
universe@61 | 76 | @RequestMapping(method = HttpMethod.GET) |
universe@61 | 77 | public ResponseType indexRedirect(HttpServletResponse resp) throws IOException { |
universe@61 | 78 | resp.sendRedirect("index/"); |
universe@61 | 79 | return ResponseType.NONE; |
universe@61 | 80 | } |
universe@61 | 81 | |
universe@61 | 82 | @RequestMapping(requestPath = "index", method = HttpMethod.GET, menuKey = "menu.index") |
universe@47 | 83 | public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@64 | 84 | |
universe@52 | 85 | final var projectList = dao.getProjectDao().list(); |
universe@52 | 86 | req.setAttribute("projects", projectList); |
universe@47 | 87 | setDynamicFragment(req, "projects"); |
universe@52 | 88 | setStylesheet(req, "projects"); |
universe@52 | 89 | |
universe@64 | 90 | if (getSelectedProject(req, dao) == null) { |
universe@64 | 91 | projectList.stream().findFirst().ifPresent(proj -> req.getSession().setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj)); |
universe@52 | 92 | } |
universe@45 | 93 | |
universe@45 | 94 | return ResponseType.HTML; |
universe@45 | 95 | } |
universe@45 | 96 | |
universe@47 | 97 | @RequestMapping(requestPath = "edit", method = HttpMethod.GET) |
universe@51 | 98 | public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@67 | 99 | req.setAttribute("project", findByParameter(req, Integer.class, "id", |
universe@67 | 100 | dao.getProjectDao()::find).orElse(new Project(-1))); |
universe@67 | 101 | req.setAttribute("users", dao.getUserDao().list()); |
universe@47 | 102 | |
universe@47 | 103 | setDynamicFragment(req, "project-form"); |
universe@47 | 104 | |
universe@47 | 105 | return ResponseType.HTML; |
universe@47 | 106 | } |
universe@47 | 107 | |
universe@47 | 108 | @RequestMapping(requestPath = "commit", method = HttpMethod.POST) |
universe@68 | 109 | public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@47 | 110 | |
universe@47 | 111 | Project project = new Project(-1); |
universe@47 | 112 | try { |
universe@47 | 113 | project = new Project(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@47 | 114 | project.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@47 | 115 | getParameter(req, String.class, "description").ifPresent(project::setDescription); |
universe@47 | 116 | getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); |
universe@47 | 117 | getParameter(req, Integer.class, "owner").map( |
universe@47 | 118 | ownerId -> ownerId >= 0 ? new User(ownerId) : null |
universe@47 | 119 | ).ifPresent(project::setOwner); |
universe@47 | 120 | |
universe@47 | 121 | dao.getProjectDao().saveOrUpdate(project); |
universe@47 | 122 | |
universe@61 | 123 | setRedirectLocation(req, "./projects/index/"); |
universe@47 | 124 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@59 | 125 | LOG.debug("Successfully updated project {}", project.getName()); |
universe@59 | 126 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@47 | 127 | // TODO: set request attribute with error text |
universe@47 | 128 | req.setAttribute("project", project); |
universe@68 | 129 | req.setAttribute("users", dao.getUserDao().list()); |
universe@47 | 130 | setDynamicFragment(req, "project-form"); |
universe@59 | 131 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 132 | LOG.debug("Details:", ex); |
universe@47 | 133 | } |
universe@47 | 134 | |
universe@47 | 135 | return ResponseType.HTML; |
universe@47 | 136 | } |
universe@47 | 137 | |
universe@59 | 138 | @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions") |
universe@59 | 139 | public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 140 | final var selectedProject = getSelectedProject(req, dao); |
universe@59 | 141 | if (selectedProject == null) { |
universe@59 | 142 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 143 | return ResponseType.NONE; |
universe@59 | 144 | } |
universe@47 | 145 | |
universe@59 | 146 | req.setAttribute("versions", dao.getVersionDao().list(selectedProject)); |
universe@59 | 147 | setDynamicFragment(req, "versions"); |
universe@59 | 148 | |
universe@59 | 149 | return ResponseType.HTML; |
universe@59 | 150 | } |
universe@59 | 151 | |
universe@59 | 152 | @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET) |
universe@59 | 153 | public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 154 | final var selectedProject = getSelectedProject(req, dao); |
universe@59 | 155 | if (selectedProject == null) { |
universe@59 | 156 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 157 | return ResponseType.NONE; |
universe@59 | 158 | } |
universe@59 | 159 | |
universe@65 | 160 | req.setAttribute("version", findByParameter(req, Integer.class, "id", |
universe@65 | 161 | dao.getVersionDao()::find).orElse(new Version(-1, selectedProject))); |
universe@59 | 162 | req.setAttribute("versionStatusEnum", VersionStatus.values()); |
universe@59 | 163 | |
universe@59 | 164 | setDynamicFragment(req, "version-form"); |
universe@59 | 165 | |
universe@59 | 166 | return ResponseType.HTML; |
universe@59 | 167 | } |
universe@59 | 168 | |
universe@59 | 169 | @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST) |
universe@64 | 170 | public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 171 | final var selectedProject = getSelectedProject(req, dao); |
universe@59 | 172 | if (selectedProject == null) { |
universe@59 | 173 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 174 | return ResponseType.NONE; |
universe@59 | 175 | } |
universe@59 | 176 | |
universe@59 | 177 | Version version = new Version(-1, selectedProject); |
universe@59 | 178 | try { |
universe@59 | 179 | version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject); |
universe@59 | 180 | version.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@59 | 181 | getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); |
universe@59 | 182 | version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); |
universe@59 | 183 | dao.getVersionDao().saveOrUpdate(version); |
universe@59 | 184 | |
universe@59 | 185 | setRedirectLocation(req, "./projects/versions/"); |
universe@59 | 186 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@59 | 187 | LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName()); |
universe@59 | 188 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@59 | 189 | // TODO: set request attribute with error text |
universe@59 | 190 | req.setAttribute("version", version); |
universe@59 | 191 | req.setAttribute("versionStatusEnum", VersionStatus.values()); |
universe@59 | 192 | setDynamicFragment(req, "version-form"); |
universe@59 | 193 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 194 | LOG.debug("Details:", ex); |
universe@59 | 195 | } |
universe@41 | 196 | |
universe@43 | 197 | return ResponseType.HTML; |
universe@41 | 198 | } |
universe@64 | 199 | |
universe@64 | 200 | |
universe@64 | 201 | @RequestMapping(requestPath = "issues", method = HttpMethod.GET, menuKey = "menu.issues") |
universe@64 | 202 | public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 203 | final var selectedProject = getSelectedProject(req, dao); |
universe@64 | 204 | if (selectedProject == null) { |
universe@64 | 205 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@64 | 206 | return ResponseType.NONE; |
universe@64 | 207 | } |
universe@64 | 208 | |
universe@64 | 209 | req.setAttribute("issues", dao.getVersionDao().list(selectedProject)); |
universe@64 | 210 | setDynamicFragment(req, "issues"); |
universe@64 | 211 | |
universe@64 | 212 | return ResponseType.HTML; |
universe@64 | 213 | } |
universe@64 | 214 | |
universe@64 | 215 | @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET) |
universe@64 | 216 | public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 217 | final var selectedProject = getSelectedProject(req, dao); |
universe@64 | 218 | if (selectedProject == null) { |
universe@64 | 219 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@64 | 220 | return ResponseType.NONE; |
universe@64 | 221 | } |
universe@64 | 222 | |
universe@65 | 223 | req.setAttribute("issue", findByParameter(req, Integer.class, "id", |
universe@65 | 224 | dao.getIssueDao()::find).orElse(new Issue(-1, selectedProject))); |
universe@64 | 225 | req.setAttribute("issueStatusEnum", IssueStatus.values()); |
universe@64 | 226 | req.setAttribute("issueCategoryEnum", IssueCategory.values()); |
universe@64 | 227 | |
universe@64 | 228 | setDynamicFragment(req, "issue-form"); |
universe@64 | 229 | |
universe@64 | 230 | return ResponseType.HTML; |
universe@64 | 231 | } |
universe@64 | 232 | |
universe@64 | 233 | @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST) |
universe@64 | 234 | public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 235 | final var selectedProject = getSelectedProject(req, dao); |
universe@64 | 236 | if (selectedProject == null) { |
universe@64 | 237 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@64 | 238 | return ResponseType.NONE; |
universe@64 | 239 | } |
universe@64 | 240 | |
universe@64 | 241 | Issue issue = new Issue(-1, selectedProject); |
universe@64 | 242 | try { |
universe@64 | 243 | issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject); |
universe@64 | 244 | |
universe@64 | 245 | // TODO: implement |
universe@64 | 246 | |
universe@64 | 247 | dao.getIssueDao().saveOrUpdate(issue); |
universe@64 | 248 | |
universe@64 | 249 | setRedirectLocation(req, "./projects/issues/"); |
universe@64 | 250 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@64 | 251 | LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName()); |
universe@64 | 252 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@64 | 253 | // TODO: set request attribute with error text |
universe@64 | 254 | req.setAttribute("issue", issue); |
universe@64 | 255 | req.setAttribute("issueStatusEnum", IssueStatus.values()); |
universe@64 | 256 | req.setAttribute("issueCategoryEnum", IssueCategory.values()); |
universe@64 | 257 | setDynamicFragment(req, "issue-form"); |
universe@64 | 258 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@64 | 259 | LOG.debug("Details:", ex); |
universe@64 | 260 | } |
universe@64 | 261 | |
universe@64 | 262 | return ResponseType.HTML; |
universe@64 | 263 | } |
universe@41 | 264 | } |