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@41: universe@41: import javax.servlet.annotation.WebServlet; universe@41: import javax.servlet.http.HttpServletRequest; universe@47: import java.sql.SQLException; universe@47: import java.util.Optional; universe@41: 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@41: @RequestMapping(method = HttpMethod.GET) universe@47: public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { universe@51: req.setAttribute("projects", dao.getProjectDao().list()); universe@47: setDynamicFragment(req, "projects"); 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@47: setRedirectLocation(req, "./projects/"); universe@47: setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL); universe@47: } catch (NullPointerException | 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@47: } universe@47: universe@47: return ResponseType.HTML; universe@47: } universe@47: universe@47: universe@47: @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions") universe@45: public ResponseType versions(HttpServletRequest req, DataAccessObjects dao) { universe@41: universe@43: return ResponseType.HTML; universe@41: } universe@41: }