Sun, 17 May 2020 16:38:04 +0200
moves project index to separate sub path
unfortunately the current menu highlighting strategy sucks and this is the easiest way so that everything looks correct
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@47 | 34 | import de.uapcore.lightpit.entities.Project; |
universe@47 | 35 | import de.uapcore.lightpit.entities.User; |
universe@59 | 36 | import de.uapcore.lightpit.entities.Version; |
universe@59 | 37 | import de.uapcore.lightpit.entities.VersionStatus; |
universe@59 | 38 | import org.slf4j.Logger; |
universe@59 | 39 | import org.slf4j.LoggerFactory; |
universe@41 | 40 | |
universe@41 | 41 | import javax.servlet.annotation.WebServlet; |
universe@41 | 42 | import javax.servlet.http.HttpServletRequest; |
universe@59 | 43 | import javax.servlet.http.HttpServletResponse; |
universe@59 | 44 | import java.io.IOException; |
universe@47 | 45 | import java.sql.SQLException; |
universe@59 | 46 | import java.util.NoSuchElementException; |
universe@47 | 47 | import java.util.Optional; |
universe@41 | 48 | |
universe@52 | 49 | import static de.uapcore.lightpit.Functions.fqn; |
universe@52 | 50 | |
universe@41 | 51 | @LightPITModule( |
universe@41 | 52 | bundleBaseName = "localization.projects", |
universe@41 | 53 | modulePath = "projects", |
universe@41 | 54 | defaultPriority = 20 |
universe@41 | 55 | ) |
universe@41 | 56 | @WebServlet( |
universe@41 | 57 | name = "ProjectsModule", |
universe@41 | 58 | urlPatterns = "/projects/*" |
universe@41 | 59 | ) |
universe@41 | 60 | public final class ProjectsModule extends AbstractLightPITServlet { |
universe@41 | 61 | |
universe@59 | 62 | private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); |
universe@59 | 63 | |
universe@52 | 64 | public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project"); |
universe@52 | 65 | |
universe@61 | 66 | @RequestMapping(method = HttpMethod.GET) |
universe@61 | 67 | public ResponseType indexRedirect(HttpServletResponse resp) throws IOException { |
universe@61 | 68 | resp.sendRedirect("index/"); |
universe@61 | 69 | return ResponseType.NONE; |
universe@61 | 70 | } |
universe@61 | 71 | |
universe@61 | 72 | @RequestMapping(requestPath = "index", method = HttpMethod.GET, menuKey = "menu.index") |
universe@47 | 73 | public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@52 | 74 | final var projectList = dao.getProjectDao().list(); |
universe@52 | 75 | req.setAttribute("projects", projectList); |
universe@47 | 76 | setDynamicFragment(req, "projects"); |
universe@52 | 77 | setStylesheet(req, "projects"); |
universe@52 | 78 | |
universe@52 | 79 | final var session = req.getSession(); |
universe@52 | 80 | final var projectSelection = getParameter(req, Integer.class, "select"); |
universe@52 | 81 | if (projectSelection.isPresent()) { |
universe@52 | 82 | final var selectedId = projectSelection.get(); |
universe@52 | 83 | for (var proj : projectList) { |
universe@52 | 84 | if (proj.getId() == selectedId) { |
universe@52 | 85 | session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj); |
universe@52 | 86 | break; |
universe@52 | 87 | } |
universe@52 | 88 | } |
universe@52 | 89 | } else { |
universe@52 | 90 | final var selectedProject = session.getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
universe@52 | 91 | if (selectedProject == null) { |
universe@52 | 92 | projectList.stream().findFirst().ifPresent(proj -> session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj)); |
universe@52 | 93 | } |
universe@52 | 94 | } |
universe@45 | 95 | |
universe@45 | 96 | return ResponseType.HTML; |
universe@45 | 97 | } |
universe@45 | 98 | |
universe@47 | 99 | @RequestMapping(requestPath = "edit", method = HttpMethod.GET) |
universe@51 | 100 | public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { |
universe@47 | 101 | final var projectDao = dao.getProjectDao(); |
universe@47 | 102 | |
universe@47 | 103 | Optional<Integer> id = getParameter(req, Integer.class, "id"); |
universe@47 | 104 | if (id.isPresent()) { |
universe@47 | 105 | req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1))); |
universe@47 | 106 | } else { |
universe@47 | 107 | req.setAttribute("project", new Project(-1)); |
universe@47 | 108 | } |
universe@51 | 109 | req.setAttribute("users", dao.getUserDao().list()); |
universe@47 | 110 | |
universe@47 | 111 | setDynamicFragment(req, "project-form"); |
universe@47 | 112 | |
universe@47 | 113 | return ResponseType.HTML; |
universe@47 | 114 | } |
universe@47 | 115 | |
universe@47 | 116 | @RequestMapping(requestPath = "commit", method = HttpMethod.POST) |
universe@47 | 117 | public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) { |
universe@47 | 118 | |
universe@47 | 119 | Project project = new Project(-1); |
universe@47 | 120 | try { |
universe@47 | 121 | project = new Project(getParameter(req, Integer.class, "id").orElseThrow()); |
universe@47 | 122 | project.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@47 | 123 | getParameter(req, String.class, "description").ifPresent(project::setDescription); |
universe@47 | 124 | getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); |
universe@47 | 125 | getParameter(req, Integer.class, "owner").map( |
universe@47 | 126 | ownerId -> ownerId >= 0 ? new User(ownerId) : null |
universe@47 | 127 | ).ifPresent(project::setOwner); |
universe@47 | 128 | |
universe@47 | 129 | dao.getProjectDao().saveOrUpdate(project); |
universe@47 | 130 | |
universe@61 | 131 | setRedirectLocation(req, "./projects/index/"); |
universe@47 | 132 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@59 | 133 | LOG.debug("Successfully updated project {}", project.getName()); |
universe@59 | 134 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@47 | 135 | // TODO: set request attribute with error text |
universe@47 | 136 | req.setAttribute("project", project); |
universe@47 | 137 | setDynamicFragment(req, "project-form"); |
universe@59 | 138 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 139 | LOG.debug("Details:", ex); |
universe@47 | 140 | } |
universe@47 | 141 | |
universe@47 | 142 | return ResponseType.HTML; |
universe@47 | 143 | } |
universe@47 | 144 | |
universe@59 | 145 | @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions") |
universe@59 | 146 | public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@59 | 147 | final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
universe@59 | 148 | if (selectedProject == null) { |
universe@59 | 149 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 150 | return ResponseType.NONE; |
universe@59 | 151 | } |
universe@47 | 152 | |
universe@59 | 153 | req.setAttribute("versions", dao.getVersionDao().list(selectedProject)); |
universe@59 | 154 | setDynamicFragment(req, "versions"); |
universe@59 | 155 | |
universe@59 | 156 | return ResponseType.HTML; |
universe@59 | 157 | } |
universe@59 | 158 | |
universe@59 | 159 | @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET) |
universe@59 | 160 | public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { |
universe@59 | 161 | final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
universe@59 | 162 | if (selectedProject == null) { |
universe@59 | 163 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 164 | return ResponseType.NONE; |
universe@59 | 165 | } |
universe@59 | 166 | |
universe@59 | 167 | Optional<Integer> id = getParameter(req, Integer.class, "id"); |
universe@59 | 168 | if (id.isPresent()) { |
universe@59 | 169 | req.setAttribute("version", Optional.ofNullable(dao.getVersionDao().find(id.get())).orElse(new Version(-1, selectedProject))); |
universe@59 | 170 | } else { |
universe@59 | 171 | req.setAttribute("version", new Version(-1, selectedProject)); |
universe@59 | 172 | } |
universe@59 | 173 | req.setAttribute("versionStatusEnum", VersionStatus.values()); |
universe@59 | 174 | |
universe@59 | 175 | setDynamicFragment(req, "version-form"); |
universe@59 | 176 | |
universe@59 | 177 | return ResponseType.HTML; |
universe@59 | 178 | } |
universe@59 | 179 | |
universe@59 | 180 | @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST) |
universe@59 | 181 | public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException { |
universe@59 | 182 | final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT); |
universe@59 | 183 | if (selectedProject == null) { |
universe@59 | 184 | resp.sendError(HttpServletResponse.SC_FORBIDDEN); |
universe@59 | 185 | return ResponseType.NONE; |
universe@59 | 186 | } |
universe@59 | 187 | |
universe@59 | 188 | Version version = new Version(-1, selectedProject); |
universe@59 | 189 | try { |
universe@59 | 190 | version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject); |
universe@59 | 191 | version.setName(getParameter(req, String.class, "name").orElseThrow()); |
universe@59 | 192 | getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); |
universe@59 | 193 | version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); |
universe@59 | 194 | dao.getVersionDao().saveOrUpdate(version); |
universe@59 | 195 | |
universe@59 | 196 | setRedirectLocation(req, "./projects/versions/"); |
universe@59 | 197 | setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); |
universe@59 | 198 | LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName()); |
universe@59 | 199 | } catch (NoSuchElementException | NumberFormatException | SQLException ex) { |
universe@59 | 200 | // TODO: set request attribute with error text |
universe@59 | 201 | req.setAttribute("version", version); |
universe@59 | 202 | req.setAttribute("versionStatusEnum", VersionStatus.values()); |
universe@59 | 203 | setDynamicFragment(req, "version-form"); |
universe@59 | 204 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 205 | LOG.debug("Details:", ex); |
universe@59 | 206 | } |
universe@41 | 207 | |
universe@43 | 208 | return ResponseType.HTML; |
universe@41 | 209 | } |
universe@41 | 210 | } |