universe@41: /* universe@41: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@41: * universe@41: * Copyright 2018 Mike Becker. All rights reserved. universe@41: * universe@41: * Redistribution and use in source and binary forms, with or without universe@41: * modification, are permitted provided that the following conditions are met: universe@41: * universe@41: * 1. Redistributions of source code must retain the above copyright universe@41: * notice, this list of conditions and the following disclaimer. universe@41: * universe@41: * 2. Redistributions in binary form must reproduce the above copyright universe@41: * notice, this list of conditions and the following disclaimer in the universe@41: * documentation and/or other materials provided with the distribution. universe@41: * universe@41: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@41: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@41: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@41: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@41: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@41: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@41: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@41: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@41: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@41: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@41: * POSSIBILITY OF SUCH DAMAGE. universe@41: * universe@41: */ universe@41: package de.uapcore.lightpit.modules; universe@41: universe@41: universe@41: import de.uapcore.lightpit.*; universe@41: import de.uapcore.lightpit.dao.DataAccessObjects; universe@47: import de.uapcore.lightpit.entities.Project; universe@47: import de.uapcore.lightpit.entities.User; universe@59: import de.uapcore.lightpit.entities.Version; universe@59: import de.uapcore.lightpit.entities.VersionStatus; universe@59: import org.slf4j.Logger; universe@59: import org.slf4j.LoggerFactory; universe@41: universe@41: import javax.servlet.annotation.WebServlet; universe@41: import javax.servlet.http.HttpServletRequest; universe@59: import javax.servlet.http.HttpServletResponse; universe@59: import java.io.IOException; universe@47: import java.sql.SQLException; universe@59: import java.util.NoSuchElementException; universe@47: import java.util.Optional; universe@41: universe@52: import static de.uapcore.lightpit.Functions.fqn; universe@52: universe@41: @LightPITModule( universe@41: bundleBaseName = "localization.projects", universe@41: modulePath = "projects", universe@41: defaultPriority = 20 universe@41: ) universe@41: @WebServlet( universe@41: name = "ProjectsModule", universe@41: urlPatterns = "/projects/*" universe@41: ) universe@41: public final class ProjectsModule extends AbstractLightPITServlet { universe@41: universe@59: private static final Logger LOG = LoggerFactory.getLogger(ProjectsModule.class); universe@59: universe@52: public static final String SESSION_ATTR_SELECTED_PROJECT = fqn(ProjectsModule.class, "selected-project"); universe@52: universe@61: @RequestMapping(method = HttpMethod.GET) universe@61: public ResponseType indexRedirect(HttpServletResponse resp) throws IOException { universe@61: resp.sendRedirect("index/"); universe@61: return ResponseType.NONE; universe@61: } universe@61: universe@61: @RequestMapping(requestPath = "index", method = HttpMethod.GET, menuKey = "menu.index") universe@47: public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { universe@52: final var projectList = dao.getProjectDao().list(); universe@52: req.setAttribute("projects", projectList); universe@47: setDynamicFragment(req, "projects"); universe@52: setStylesheet(req, "projects"); universe@52: universe@52: final var session = req.getSession(); universe@52: final var projectSelection = getParameter(req, Integer.class, "select"); universe@52: if (projectSelection.isPresent()) { universe@52: final var selectedId = projectSelection.get(); universe@52: for (var proj : projectList) { universe@52: if (proj.getId() == selectedId) { universe@52: session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj); universe@52: break; universe@52: } universe@52: } universe@52: } else { universe@52: final var selectedProject = session.getAttribute(SESSION_ATTR_SELECTED_PROJECT); universe@52: if (selectedProject == null) { universe@52: projectList.stream().findFirst().ifPresent(proj -> session.setAttribute(SESSION_ATTR_SELECTED_PROJECT, proj)); universe@52: } universe@52: } universe@45: universe@45: return ResponseType.HTML; universe@45: } universe@45: universe@47: @RequestMapping(requestPath = "edit", method = HttpMethod.GET) universe@51: public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { universe@47: final var projectDao = dao.getProjectDao(); universe@47: universe@47: Optional id = getParameter(req, Integer.class, "id"); universe@47: if (id.isPresent()) { universe@47: req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1))); universe@47: } else { universe@47: req.setAttribute("project", new Project(-1)); universe@47: } universe@51: req.setAttribute("users", dao.getUserDao().list()); universe@47: universe@47: setDynamicFragment(req, "project-form"); universe@47: universe@47: return ResponseType.HTML; universe@47: } universe@47: universe@47: @RequestMapping(requestPath = "commit", method = HttpMethod.POST) universe@47: public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) { universe@47: universe@47: Project project = new Project(-1); universe@47: try { universe@47: project = new Project(getParameter(req, Integer.class, "id").orElseThrow()); universe@47: project.setName(getParameter(req, String.class, "name").orElseThrow()); universe@47: getParameter(req, String.class, "description").ifPresent(project::setDescription); universe@47: getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl); universe@47: getParameter(req, Integer.class, "owner").map( universe@47: ownerId -> ownerId >= 0 ? new User(ownerId) : null universe@47: ).ifPresent(project::setOwner); universe@47: universe@47: dao.getProjectDao().saveOrUpdate(project); universe@47: universe@61: setRedirectLocation(req, "./projects/index/"); universe@47: setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); universe@59: LOG.debug("Successfully updated project {}", project.getName()); universe@59: } catch (NoSuchElementException | NumberFormatException | SQLException ex) { universe@47: // TODO: set request attribute with error text universe@47: req.setAttribute("project", project); universe@47: setDynamicFragment(req, "project-form"); universe@59: LOG.warn("Form validation failure: {}", ex.getMessage()); universe@59: LOG.debug("Details:", ex); universe@47: } universe@47: universe@47: return ResponseType.HTML; universe@47: } universe@47: universe@59: @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions") universe@59: public ResponseType versions(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { universe@59: final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT); universe@59: if (selectedProject == null) { universe@59: resp.sendError(HttpServletResponse.SC_FORBIDDEN); universe@59: return ResponseType.NONE; universe@59: } universe@47: universe@59: req.setAttribute("versions", dao.getVersionDao().list(selectedProject)); universe@59: setDynamicFragment(req, "versions"); universe@59: universe@59: return ResponseType.HTML; universe@59: } universe@59: universe@59: @RequestMapping(requestPath = "versions/edit", method = HttpMethod.GET) universe@59: public ResponseType editVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException, SQLException { universe@59: final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT); universe@59: if (selectedProject == null) { universe@59: resp.sendError(HttpServletResponse.SC_FORBIDDEN); universe@59: return ResponseType.NONE; universe@59: } universe@59: universe@59: Optional id = getParameter(req, Integer.class, "id"); universe@59: if (id.isPresent()) { universe@59: req.setAttribute("version", Optional.ofNullable(dao.getVersionDao().find(id.get())).orElse(new Version(-1, selectedProject))); universe@59: } else { universe@59: req.setAttribute("version", new Version(-1, selectedProject)); universe@59: } universe@59: req.setAttribute("versionStatusEnum", VersionStatus.values()); universe@59: universe@59: setDynamicFragment(req, "version-form"); universe@59: universe@59: return ResponseType.HTML; universe@59: } universe@59: universe@59: @RequestMapping(requestPath = "versions/commit", method = HttpMethod.POST) universe@59: public ResponseType commitVersion(HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException { universe@59: final var selectedProject = (Project)req.getSession().getAttribute(SESSION_ATTR_SELECTED_PROJECT); universe@59: if (selectedProject == null) { universe@59: resp.sendError(HttpServletResponse.SC_FORBIDDEN); universe@59: return ResponseType.NONE; universe@59: } universe@59: universe@59: Version version = new Version(-1, selectedProject); universe@59: try { universe@59: version = new Version(getParameter(req, Integer.class, "id").orElseThrow(), selectedProject); universe@59: version.setName(getParameter(req, String.class, "name").orElseThrow()); universe@59: getParameter(req, Integer.class, "ordinal").ifPresent(version::setOrdinal); universe@59: version.setStatus(VersionStatus.valueOf(getParameter(req, String.class, "status").orElseThrow())); universe@59: dao.getVersionDao().saveOrUpdate(version); universe@59: universe@59: setRedirectLocation(req, "./projects/versions/"); universe@59: setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); universe@59: LOG.debug("Successfully updated version {} for project {}", version.getName(), selectedProject.getName()); universe@59: } catch (NoSuchElementException | NumberFormatException | SQLException ex) { universe@59: // TODO: set request attribute with error text universe@59: req.setAttribute("version", version); universe@59: req.setAttribute("versionStatusEnum", VersionStatus.values()); universe@59: setDynamicFragment(req, "version-form"); universe@59: LOG.warn("Form validation failure: {}", ex.getMessage()); universe@59: LOG.debug("Details:", ex); universe@59: } universe@41: universe@43: return ResponseType.HTML; universe@41: } universe@41: }