src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java

Tue, 19 May 2020 16:50:05 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 19 May 2020 16:50:05 +0200
changeset 65
9861a68a4612
parent 64
0f1746c6abfb
child 67
d15779cf3982
permissions
-rw-r--r--

fixes missing orElse() calls

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@47 99 final var projectDao = dao.getProjectDao();
universe@47 100
universe@65 101 req.setAttribute("project", findByParameter(req, Integer.class, "id",
universe@65 102 projectDao::find).orElse(new Project(-1)));
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@47 109 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
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@47 129 setDynamicFragment(req, "project-form");
universe@59 130 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 131 LOG.debug("Details:", ex);
universe@47 132 }
universe@47 133
universe@47 134 return ResponseType.HTML;
universe@47 135 }
universe@47 136
universe@59 137 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
universe@59 138 public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 139 final var selectedProject = getSelectedProject(req, dao);
universe@59 140 if (selectedProject == null) {
universe@59 141 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@59 142 return ResponseType.NONE;
universe@59 143 }
universe@47 144
universe@59 145 req.setAttribute("versions", dao.getVersionDao().list(selectedProject));
universe@59 146 setDynamicFragment(req, "versions");
universe@59 147
universe@59 148 return ResponseType.HTML;
universe@59 149 }
universe@59 150
universe@59 151 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
universe@59 152 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 153 final var selectedProject = getSelectedProject(req, dao);
universe@59 154 if (selectedProject == null) {
universe@59 155 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@59 156 return ResponseType.NONE;
universe@59 157 }
universe@59 158
universe@65 159 req.setAttribute("version", findByParameter(req, Integer.class, "id",
universe@65 160 dao.getVersionDao()::find).orElse(new Version(-1, selectedProject)));
universe@59 161 req.setAttribute("versionStatusEnum", VersionStatus.values());
universe@59 162
universe@59 163 setDynamicFragment(req, "version-form");
universe@59 164
universe@59 165 return ResponseType.HTML;
universe@59 166 }
universe@59 167
universe@59 168 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
universe@64 169 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 170 final var selectedProject = getSelectedProject(req, dao);
universe@59 171 if (selectedProject == null) {
universe@59 172 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@59 173 return ResponseType.NONE;
universe@59 174 }
universe@59 175
universe@59 176 Version version = new Version(-1, selectedProject);
universe@59 177 try {
universe@59 178 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
universe@59 179 version.setName(getParameter(req, String.class, "name").orElseThrow());
universe@59 180 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
universe@59 181 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
universe@59 182 dao.getVersionDao().saveOrUpdate(version);
universe@59 183
universe@59 184 setRedirectLocation(req, "./projects/versions/");
universe@59 185 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@59 186 LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName());
universe@59 187 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
universe@59 188 // TODO: set request attribute with error text
universe@59 189 req.setAttribute("version", version);
universe@59 190 req.setAttribute("versionStatusEnum", VersionStatus.values());
universe@59 191 setDynamicFragment(req, "version-form");
universe@59 192 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 193 LOG.debug("Details:", ex);
universe@59 194 }
universe@41 195
universe@43 196 return ResponseType.HTML;
universe@41 197 }
universe@64 198
universe@64 199
universe@64 200 @RequestMapping(requestPath = "issues", method = HttpMethod.GET, menuKey = "menu.issues")
universe@64 201 public ResponseType issues(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 202 final var selectedProject = getSelectedProject(req, dao);
universe@64 203 if (selectedProject == null) {
universe@64 204 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@64 205 return ResponseType.NONE;
universe@64 206 }
universe@64 207
universe@64 208 req.setAttribute("issues", dao.getVersionDao().list(selectedProject));
universe@64 209 setDynamicFragment(req, "issues");
universe@64 210
universe@64 211 return ResponseType.HTML;
universe@64 212 }
universe@64 213
universe@64 214 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
universe@64 215 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 216 final var selectedProject = getSelectedProject(req, dao);
universe@64 217 if (selectedProject == null) {
universe@64 218 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@64 219 return ResponseType.NONE;
universe@64 220 }
universe@64 221
universe@65 222 req.setAttribute("issue", findByParameter(req, Integer.class, "id",
universe@65 223 dao.getIssueDao()::find).orElse(new Issue(-1, selectedProject)));
universe@64 224 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@64 225 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@64 226
universe@64 227 setDynamicFragment(req, "issue-form");
universe@64 228
universe@64 229 return ResponseType.HTML;
universe@64 230 }
universe@64 231
universe@64 232 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
universe@64 233 public ResponseType commitIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 234 final var selectedProject = getSelectedProject(req, dao);
universe@64 235 if (selectedProject == null) {
universe@64 236 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@64 237 return ResponseType.NONE;
universe@64 238 }
universe@64 239
universe@64 240 Issue issue = new Issue(-1, selectedProject);
universe@64 241 try {
universe@64 242 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
universe@64 243
universe@64 244 // TODO: implement
universe@64 245
universe@64 246 dao.getIssueDao().saveOrUpdate(issue);
universe@64 247
universe@64 248 setRedirectLocation(req, "./projects/issues/");
universe@64 249 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@64 250 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName());
universe@64 251 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
universe@64 252 // TODO: set request attribute with error text
universe@64 253 req.setAttribute("issue", issue);
universe@64 254 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@64 255 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@64 256 setDynamicFragment(req, "issue-form");
universe@64 257 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@64 258 LOG.debug("Details:", ex);
universe@64 259 }
universe@64 260
universe@64 261 return ResponseType.HTML;
universe@64 262 }
universe@41 263 }

mercurial