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

Tue, 19 May 2020 19:34:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 19 May 2020 19:34:57 +0200
changeset 70
821c4950b619
parent 68
ded1b5639bd3
child 71
dca186d3911f
permissions
-rw-r--r--

removes the sub menu and removes the home module
fixes the queries in the PGIssueDao
adds placeholder for a breadcrumb menu

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@47 77 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@64 78
universe@52 79 final var projectList = dao.getProjectDao().list();
universe@52 80 req.setAttribute("projects", projectList);
universe@47 81 setDynamicFragment(req, "projects");
universe@52 82 setStylesheet(req, "projects");
universe@52 83
universe@64 84 if (getSelectedProject(req, dao) == null) {
universe@64 85 projectList.stream().findFirst().ifPresent(proj -> req.getSession().setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj));
universe@52 86 }
universe@45 87
universe@45 88 return ResponseType.HTML;
universe@45 89 }
universe@45 90
universe@47 91 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
universe@51 92 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@67 93 req.setAttribute("project", findByParameter(req, Integer.class, "id",
universe@67 94 dao.getProjectDao()::find).orElse(new Project(-1)));
universe@67 95 req.setAttribute("users", dao.getUserDao().list());
universe@47 96
universe@47 97 setDynamicFragment(req, "project-form");
universe@47 98
universe@47 99 return ResponseType.HTML;
universe@47 100 }
universe@47 101
universe@47 102 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@68 103 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 104
universe@47 105 Project project = new Project(-1);
universe@47 106 try {
universe@47 107 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
universe@47 108 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@47 109 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 110 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 111 getParameter(req, Integer.class, "owner").map(
universe@47 112 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 113 ).ifPresent(project::setOwner);
universe@47 114
universe@47 115 dao.getProjectDao().saveOrUpdate(project);
universe@47 116
universe@70 117 setRedirectLocation(req, "./projects/");
universe@47 118 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@59 119 LOG.debug("Successfully updated project {}", project.getName());
universe@59 120 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
universe@47 121 // TODO: set request attribute with error text
universe@47 122 req.setAttribute("project", project);
universe@68 123 req.setAttribute("users", dao.getUserDao().list());
universe@47 124 setDynamicFragment(req, "project-form");
universe@59 125 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 126 LOG.debug("Details:", ex);
universe@47 127 }
universe@47 128
universe@47 129 return ResponseType.HTML;
universe@47 130 }
universe@47 131
universe@70 132 @RequestMapping(requestPath = "view", method = HttpMethod.GET)
universe@70 133 public ResponseType view(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 134 final var selectedProject = getSelectedProject(req, dao);
universe@59 135 if (selectedProject == null) {
universe@59 136 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@59 137 return ResponseType.NONE;
universe@59 138 }
universe@47 139
universe@59 140 req.setAttribute("versions", dao.getVersionDao().list(selectedProject));
universe@70 141 req.setAttribute("issues", dao.getIssueDao().list(selectedProject));
universe@70 142
universe@70 143 setDynamicFragment(req, "project-details");
universe@59 144
universe@59 145 return ResponseType.HTML;
universe@59 146 }
universe@59 147
universe@59 148 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
universe@59 149 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 150 final var selectedProject = getSelectedProject(req, dao);
universe@59 151 if (selectedProject == null) {
universe@59 152 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@59 153 return ResponseType.NONE;
universe@59 154 }
universe@59 155
universe@65 156 req.setAttribute("version", findByParameter(req, Integer.class, "id",
universe@65 157 dao.getVersionDao()::find).orElse(new Version(-1, selectedProject)));
universe@59 158 req.setAttribute("versionStatusEnum", VersionStatus.values());
universe@59 159
universe@59 160 setDynamicFragment(req, "version-form");
universe@59 161
universe@59 162 return ResponseType.HTML;
universe@59 163 }
universe@59 164
universe@59 165 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
universe@64 166 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 167 final var selectedProject = getSelectedProject(req, dao);
universe@59 168 if (selectedProject == null) {
universe@59 169 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@59 170 return ResponseType.NONE;
universe@59 171 }
universe@59 172
universe@59 173 Version version = new Version(-1, selectedProject);
universe@59 174 try {
universe@59 175 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
universe@59 176 version.setName(getParameter(req, String.class, "name").orElseThrow());
universe@59 177 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
universe@59 178 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
universe@59 179 dao.getVersionDao().saveOrUpdate(version);
universe@59 180
universe@59 181 setRedirectLocation(req, "./projects/versions/");
universe@59 182 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@59 183 LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName());
universe@59 184 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
universe@59 185 // TODO: set request attribute with error text
universe@59 186 req.setAttribute("version", version);
universe@59 187 req.setAttribute("versionStatusEnum", VersionStatus.values());
universe@59 188 setDynamicFragment(req, "version-form");
universe@59 189 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@59 190 LOG.debug("Details:", ex);
universe@59 191 }
universe@41 192
universe@43 193 return ResponseType.HTML;
universe@41 194 }
universe@64 195
universe@64 196 @RequestMapping(requestPath = "issues/edit", method = HttpMethod.GET)
universe@64 197 public ResponseType editIssue(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
universe@64 198 final var selectedProject = getSelectedProject(req, dao);
universe@64 199 if (selectedProject == null) {
universe@64 200 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
universe@64 201 return ResponseType.NONE;
universe@64 202 }
universe@64 203
universe@65 204 req.setAttribute("issue", findByParameter(req, Integer.class, "id",
universe@65 205 dao.getIssueDao()::find).orElse(new Issue(-1, selectedProject)));
universe@64 206 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@64 207 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@64 208
universe@64 209 setDynamicFragment(req, "issue-form");
universe@64 210
universe@64 211 return ResponseType.HTML;
universe@64 212 }
universe@64 213
universe@64 214 @RequestMapping(requestPath = "issues/commit", method = HttpMethod.POST)
universe@64 215 public ResponseType commitIssue(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@64 222 Issue issue = new Issue(-1, selectedProject);
universe@64 223 try {
universe@64 224 issue = new Issue(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
universe@64 225
universe@64 226 // TODO: implement
universe@64 227
universe@64 228 dao.getIssueDao().saveOrUpdate(issue);
universe@64 229
universe@64 230 setRedirectLocation(req, "./projects/issues/");
universe@64 231 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@64 232 LOG.debug("Successfully updated issue {} for project {}", issue.getId(), selectedProject.getName());
universe@64 233 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
universe@64 234 // TODO: set request attribute with error text
universe@64 235 req.setAttribute("issue", issue);
universe@64 236 req.setAttribute("issueStatusEnum", IssueStatus.values());
universe@64 237 req.setAttribute("issueCategoryEnum", IssueCategory.values());
universe@64 238 setDynamicFragment(req, "issue-form");
universe@64 239 LOG.warn("Form validation failure: {}", ex.getMessage());
universe@64 240 LOG.debug("Details:", ex);
universe@64 241 }
universe@64 242
universe@64 243 return ResponseType.HTML;
universe@64 244 }
universe@41 245 }

mercurial