Sun, 17 May 2020 16:23:39 +0200
adds version management
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, menuKey = "menu.index")
67 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
68 final var projectList = dao.getProjectDao().list();
69 req.setAttribute("projects", projectList);
70 setDynamicFragment(req, "projects");
71 setStylesheet(req, "projects");
73 final var session = req.getSession();
74 final var projectSelection = getParameter(req, Integer.class, "select");
75 if (projectSelection.isPresent()) {
76 final var selectedId = projectSelection.get();
77 for (var proj : projectList) {
78 if (proj.getId() == selectedId) {
79 session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj);
80 break;
81 }
82 }
83 } else {
84 final var selectedProject = session.getAttribute(SESSION_ATTR_SELECTED_PROJECT);
85 if (selectedProject == null) {
86 projectList.stream().findFirst().ifPresent(proj -> session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj));
87 }
88 }
90 return ResponseType.HTML;
91 }
93 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
94 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
95 final var projectDao = dao.getProjectDao();
97 Optional<Integer> id = getParameter(req, Integer.class, "id");
98 if (id.isPresent()) {
99 req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1)));
100 } else {
101 req.setAttribute("project", new Project(-1));
102 }
103 req.setAttribute("users", dao.getUserDao().list());
105 setDynamicFragment(req, "project-form");
107 return ResponseType.HTML;
108 }
110 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
111 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
113 Project project = new Project(-1);
114 try {
115 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
116 project.setName(getParameter(req, String.class, "name").orElseThrow());
117 getParameter(req, String.class, "description").ifPresent(project::setDescription);
118 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
119 getParameter(req, Integer.class, "owner").map(
120 ownerId -> ownerId >= 0 ? new User(ownerId) : null
121 ).ifPresent(project::setOwner);
123 dao.getProjectDao().saveOrUpdate(project);
125 setRedirectLocation(req, "./projects/");
126 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
127 LOG.debug("Successfully updated project {}", project.getName());
128 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
129 // TODO: set request attribute with error text
130 req.setAttribute("project", project);
131 setDynamicFragment(req, "project-form");
132 LOG.warn("Form validation failure: {}", ex.getMessage());
133 LOG.debug("Details:", ex);
134 }
136 return ResponseType.HTML;
137 }
139 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
140 public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
141 final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT);
142 if (selectedProject == null) {
143 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
144 return ResponseType.NONE;
145 }
147 req.setAttribute("versions", dao.getVersionDao().list(selectedProject));
148 setDynamicFragment(req, "versions");
150 return ResponseType.HTML;
151 }
153 @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET)
154 public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException {
155 final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT);
156 if (selectedProject == null) {
157 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
158 return ResponseType.NONE;
159 }
161 Optional<Integer> id = getParameter(req, Integer.class, "id");
162 if (id.isPresent()) {
163 req.setAttribute("version", Optional.ofNullable(dao.getVersionDao().find(id.get())).orElse(new Version(-1, selectedProject)));
164 } else {
165 req.setAttribute("version", new Version(-1, selectedProject));
166 }
167 req.setAttribute("versionStatusEnum", VersionStatus.values());
169 setDynamicFragment(req, "version-form");
171 return ResponseType.HTML;
172 }
174 @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST)
175 public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException {
176 final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT);
177 if (selectedProject == null) {
178 resp.sendError(HttpServletResponse.SC_FORBIDDEN);
179 return ResponseType.NONE;
180 }
182 Version version = new Version(-1, selectedProject);
183 try {
184 version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject);
185 version.setName(getParameter(req, String.class, "name").orElseThrow());
186 getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal);
187 version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow()));
188 dao.getVersionDao().saveOrUpdate(version);
190 setRedirectLocation(req, "./projects/versions/");
191 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
192 LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName());
193 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
194 // TODO: set request attribute with error text
195 req.setAttribute("version", version);
196 req.setAttribute("versionStatusEnum", VersionStatus.values());
197 setDynamicFragment(req, "version-form");
198 LOG.warn("Form validation failure: {}", ex.getMessage());
199 LOG.debug("Details:", ex);
200 }
202 return ResponseType.HTML;
203 }
204 }