Fri, 22 May 2020 16:21:59 +0200
some reformatting
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@71 | 43 | import java.util.ArrayList; |
universe@71 | 44 | import java.util.List; |
universe@59 | 45 | import java.util.NoSuchElementException; |
universe@71 | 46 | import java.util.Optional; |
universe@41 | 47 | |
universe@52 | 48 | import static de.uapcore.lightpit.Functions.fqn; |
universe@52 | 49 | |
universe@41 | 50 | @LightPITModule( |
universe@41 | 51 | bundleBaseName = "localization.projects", |
universe@41 | 52 | modulePath = "projects", |
universe@41 | 53 | defaultPriority = 20 |
universe@41 | 54 | ) |
universe@41 | 55 | @WebServlet( |
universe@41 | 56 | name = "ProjectsModule", |
universe@41 | 57 | urlPatterns = "/projects/*" |
universe@41 | 58 | ) |
universe@41 | 59 | public final class ProjectsModule extends AbstractLightPITServlet { |
universe@41 | 60 | |
universe@59 | 61 | private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); |
universe@59 | 62 | |
universe@52 | 63 | public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project"); |
universe@52 | 64 | |
universe@64 | 65 | private Project getSelectedProject(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@64 | 66 | final var projectDao = dao.getProjectDao(); |
universe@64 | 67 | final var session = req.getSession(); |
universe@64 | 68 | final var projectSelection = getParameter(req, Integer.class, "pid"); |
universe@71 | 69 | final Project selectedProject; |
universe@64 | 70 | if (projectSelection.isPresent()) { |
universe@71 | 71 | selectedProject = projectDao.find(projectSelection.get()); |
universe@64 | 72 | } else { |
universe@71 | 73 | final var sessionProject = (Project) session.getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
universe@71 | 74 | selectedProject = sessionProject == null ? null : projectDao.find(sessionProject.getId()); |
universe@64 | 75 | } |
universe@71 | 76 | session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, selectedProject); |
universe@71 | 77 | return selectedProject; |
universe@71 | 78 | } |
universe@71 | 79 | |
universe@71 | 80 | |
universe@71 | 81 | /** |
universe@71 | 82 | * Creates the breadcrumb menu. |
universe@71 | 83 | * |
universe@71 | 84 | * @param level the current active level |
universe@71 | 85 | * @param selectedProject the selected project, if any, or null |
universe@71 | 86 | * @return a dynamic breadcrumb menu trying to display as many levels as possible |
universe@71 | 87 | */ |
universe@71 | 88 | private List<MenuEntry> getBreadcrumbs(int level, |
universe@71 | 89 | Project selectedProject) { |
universe@71 | 90 | MenuEntry entry; |
universe@71 | 91 | |
universe@71 | 92 | final var breadcrumbs = new ArrayList<MenuEntry>(); |
universe@71 | 93 | entry = new MenuEntry(new ResourceKey("localization.projects", "menuLabel"), |
universe@71 | 94 | "projects/", 0); |
universe@71 | 95 | breadcrumbs.add(entry); |
universe@71 | 96 | if (level == 0) entry.setActive(true); |
universe@71 | 97 | |
universe@71 | 98 | if (selectedProject == null) |
universe@71 | 99 | return breadcrumbs; |
universe@71 | 100 | |
universe@71 | 101 | entry = new MenuEntry(selectedProject.getName(), |
universe@71 | 102 | "projects/view?pid=" + selectedProject.getId(), 1); |
universe@71 | 103 | if (level == 1) entry.setActive(true); |
universe@71 | 104 | |
universe@71 | 105 | breadcrumbs.add(entry); |
universe@71 | 106 | return breadcrumbs; |
universe@64 | 107 | } |
universe@64 | 108 | |
universe@61 | 109 | @RequestMapping(method = HttpMethod.GET) |
universe@47 | 110 | public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@64 | 111 | |
universe@52 | 112 | final var projectList = dao.getProjectDao().list(); |
universe@52 | 113 | req.setAttribute("projects", projectList); |
universe@47 | 114 | setDynamicFragment(req, "projects"); |
universe@52 | 115 | setStylesheet(req, "projects"); |
universe@52 | 116 | |
universe@71 | 117 | final var selectedProject = getSelectedProject(req, dao); |
universe@71 | 118 | setBreadcrumbs(req, getBreadcrumbs(0, selectedProject)); |
universe@45 | 119 | |
universe@45 | 120 | return ResponseType.HTML; |
universe@45 | 121 | } |
universe@45 | 122 | |
universe@71 | 123 | private void configureEditForm(HttpServletRequest req, DataAccessObjects dao, Optional<Project> project) throws SQLException { |
universe@71 | 124 | if (project.isPresent()) { |
universe@71 | 125 | req.setAttribute("project", project.get()); |
universe@71 | 126 | setBreadcrumbs(req, getBreadcrumbs(1, project.get())); |
universe@71 | 127 | } else { |
universe@71 | 128 | req.setAttribute("project", new Project(-1)); |
universe@71 | 129 | setBreadcrumbs(req, getBreadcrumbs(0, null)); |
universe@71 | 130 | } |
universe@71 | 131 | |
universe@71 | 132 | req.setAttribute("users", dao.getUserDao().list()); |
universe@71 | 133 | setDynamicFragment(req, "project-form"); |
universe@71 | 134 | } |
universe@71 | 135 | |
universe@47 | 136 | @RequestMapping(requestPath = "edit", method = HttpMethod.GET) |
universe@51 | 137 | public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@47 | 138 | |
universe@71 | 139 | Optional<Project> project = findByParameter(req, Integer.class, "id", dao.getProjectDao()::find); |
universe@71 | 140 | configureEditForm(req, dao, project); |
universe@71 | 141 | if (project.isPresent()) { |
universe@71 | 142 | req.getSession().setAttribute(SESSION_ATTR_SELECTED_PROJECT, project.get()); |
universe@71 | 143 | } |
universe@47 | 144 | |
universe@47 | 145 | return ResponseType.HTML; |
universe@47 | 146 | } |
universe@47 | 147 | |
universe@47 | 148 | @RequestMapping(requestPath = "commit", method = HttpMethod.POST) |
universe@68 | 149 | public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@47 | 150 | |
universe@71 | 151 | Project project = null; |
universe@47 | 152 | try { |
universe@47 | 153 | project = new Project(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@47 | 154 | project.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@47 | 155 | getParameter(req, String.class, "description").ifPresent(project::setDescription); |
universe@47 | 156 | getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); |
universe@47 | 157 | getParameter(req, Integer.class, "owner").map( |
universe@47 | 158 | ownerId -> ownerId >= 0 ? new User(ownerId) : null |
universe@47 | 159 | ).ifPresent(project::setOwner); |
universe@47 | 160 | |
universe@47 | 161 | dao.getProjectDao().saveOrUpdate(project); |
universe@47 | 162 | |
universe@70 | 163 | setRedirectLocation(req, "./projects/"); |
universe@47 | 164 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@59 | 165 | LOG.debug("Successfully updated project {}", project.getName()); |
universe@59 | 166 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@47 | 167 | // TODO: set request attribute with error text |
universe@59 | 168 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 169 | LOG.debug("Details:", ex); |
universe@71 | 170 | configureEditForm(req, dao, Optional.ofNullable(project)); |
universe@47 | 171 | } |
universe@47 | 172 | |
universe@47 | 173 | return ResponseType.HTML; |
universe@47 | 174 | } |
universe@47 | 175 | |
universe@70 | 176 | @RequestMapping(requestPath = "view", method = HttpMethod.GET) |
universe@70 | 177 | public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 178 | final var selectedProject = getSelectedProject(req, dao); |
universe@59 | 179 | if (selectedProject == null) { |
universe@59 | 180 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 181 | return ResponseType.NONE; |
universe@59 | 182 | } |
universe@47 | 183 | |
universe@59 | 184 | req.setAttribute("versions", dao.getVersionDao().list(selectedProject)); |
universe@70 | 185 | req.setAttribute("issues", dao.getIssueDao().list(selectedProject)); |
universe@70 | 186 | |
universe@71 | 187 | // TODO: add more levels depending on last visited location |
universe@71 | 188 | setBreadcrumbs(req, getBreadcrumbs(1, selectedProject)); |
universe@71 | 189 | |
universe@70 | 190 | setDynamicFragment(req, "project-details"); |
universe@59 | 191 | |
universe@59 | 192 | return ResponseType.HTML; |
universe@59 | 193 | } |
universe@59 | 194 | |
universe@71 | 195 | private void configureEditVersionForm(HttpServletRequest req, Optional<Version> version, Project selectedProject) { |
universe@71 | 196 | req.setAttribute("version", version.orElse(new Version(-1, selectedProject))); |
universe@71 | 197 | req.setAttribute("versionStatusEnum", VersionStatus.values()); |
universe@71 | 198 | |
universe@71 | 199 | setDynamicFragment(req, "version-form"); |
universe@71 | 200 | } |
universe@71 | 201 | |
universe@59 | 202 | @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET) |
universe@59 | 203 | public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 204 | final var selectedProject = getSelectedProject(req, dao); |
universe@59 | 205 | if (selectedProject == null) { |
universe@59 | 206 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 207 | return ResponseType.NONE; |
universe@59 | 208 | } |
universe@59 | 209 | |
universe@71 | 210 | configureEditVersionForm(req, |
universe@71 | 211 | findByParameter(req, Integer.class, "id", dao.getVersionDao()::find), |
universe@71 | 212 | selectedProject); |
universe@59 | 213 | |
universe@59 | 214 | return ResponseType.HTML; |
universe@59 | 215 | } |
universe@59 | 216 | |
universe@59 | 217 | @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST) |
universe@64 | 218 | public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 219 | final var selectedProject = getSelectedProject(req, dao); |
universe@59 | 220 | if (selectedProject == null) { |
universe@59 | 221 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 222 | return ResponseType.NONE; |
universe@59 | 223 | } |
universe@59 | 224 | |
universe@71 | 225 | Version version = null; |
universe@59 | 226 | try { |
universe@59 | 227 | version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject); |
universe@59 | 228 | version.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@59 | 229 | getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); |
universe@59 | 230 | version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); |
universe@59 | 231 | dao.getVersionDao().saveOrUpdate(version); |
universe@59 | 232 | |
universe@59 | 233 | setRedirectLocation(req, "./projects/versions/"); |
universe@59 | 234 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@59 | 235 | LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName()); |
universe@59 | 236 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@59 | 237 | // TODO: set request attribute with error text |
universe@59 | 238 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 239 | LOG.debug("Details:", ex); |
universe@71 | 240 | configureEditVersionForm(req, Optional.ofNullable(version), selectedProject); |
universe@59 | 241 | } |
universe@41 | 242 | |
universe@43 | 243 | return ResponseType.HTML; |
universe@41 | 244 | } |
universe@64 | 245 | |
universe@71 | 246 | private void configureEditIssueForm(HttpServletRequest req, DataAccessObjects dao, Optional<Issue> issue, Project selectedProject) throws SQLException { |
universe@71 | 247 | |
universe@71 | 248 | req.setAttribute("issue", issue.orElse(new Issue(-1, selectedProject))); |
universe@71 | 249 | req.setAttribute("issueStatusEnum", IssueStatus.values()); |
universe@71 | 250 | req.setAttribute("issueCategoryEnum", IssueCategory.values()); |
universe@71 | 251 | req.setAttribute("versions", dao.getVersionDao().list(selectedProject)); |
universe@71 | 252 | |
universe@71 | 253 | setDynamicFragment(req, "issue-form"); |
universe@71 | 254 | } |
universe@71 | 255 | |
universe@64 | 256 | @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET) |
universe@64 | 257 | public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 258 | final var selectedProject = getSelectedProject(req, dao); |
universe@64 | 259 | if (selectedProject == null) { |
universe@64 | 260 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@64 | 261 | return ResponseType.NONE; |
universe@64 | 262 | } |
universe@64 | 263 | |
universe@71 | 264 | configureEditIssueForm(req, dao, |
universe@71 | 265 | findByParameter(req, Integer.class, "id", dao.getIssueDao()::find), |
universe@71 | 266 | selectedProject); |
universe@64 | 267 | |
universe@64 | 268 | return ResponseType.HTML; |
universe@64 | 269 | } |
universe@64 | 270 | |
universe@64 | 271 | @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST) |
universe@64 | 272 | public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@64 | 273 | final var selectedProject = getSelectedProject(req, dao); |
universe@64 | 274 | if (selectedProject == null) { |
universe@64 | 275 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@64 | 276 | return ResponseType.NONE; |
universe@64 | 277 | } |
universe@64 | 278 | |
universe@71 | 279 | Issue issue = null; |
universe@64 | 280 | try { |
universe@64 | 281 | issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject); |
universe@64 | 282 | |
universe@64 | 283 | // TODO: implement |
universe@64 | 284 | |
universe@64 | 285 | dao.getIssueDao().saveOrUpdate(issue); |
universe@64 | 286 | |
universe@64 | 287 | setRedirectLocation(req, "./projects/issues/"); |
universe@64 | 288 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@64 | 289 | LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName()); |
universe@64 | 290 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@64 | 291 | // TODO: set request attribute with error text |
universe@64 | 292 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@64 | 293 | LOG.debug("Details:", ex); |
universe@71 | 294 | configureEditIssueForm(req, dao, Optional.ofNullable(issue), selectedProject); |
universe@64 | 295 | } |
universe@64 | 296 | |
universe@64 | 297 | return ResponseType.HTML; |
universe@64 | 298 | } |
universe@41 | 299 | } |