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