src/main/java/de/uapcore/lightpit/modules/ProjectsModule.java

Sat, 16 May 2020 13:29:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 May 2020 13:29:44 +0200
changeset 51
dd0a45ae25d7
parent 47
57cfb94ab99f
child 52
67a02e79b7a1
permissions
-rw-r--r--

adds the possibility to add users / developers

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@41 36
universe@41 37 import javax.servlet.annotation.WebServlet;
universe@41 38 import javax.servlet.http.HttpServletRequest;
universe@47 39 import java.sql.SQLException;
universe@47 40 import java.util.Optional;
universe@41 41
universe@41 42 @LightPITModule(
universe@41 43 bundleBaseName = "localization.projects",
universe@41 44 modulePath = "projects",
universe@41 45 defaultPriority = 20
universe@41 46 )
universe@41 47 @WebServlet(
universe@41 48 name = "ProjectsModule",
universe@41 49 urlPatterns = "/projects/*"
universe@41 50 )
universe@41 51 public final class ProjectsModule extends AbstractLightPITServlet {
universe@41 52
universe@41 53 @RequestMapping(method = HttpMethod.GET)
universe@47 54 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@51 55 req.setAttribute("projects", dao.getProjectDao().list());
universe@47 56 setDynamicFragment(req, "projects");
universe@45 57
universe@45 58 return ResponseType.HTML;
universe@45 59 }
universe@45 60
universe@47 61 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
universe@51 62 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
universe@47 63 final var projectDao = dao.getProjectDao();
universe@47 64
universe@47 65 Optional<Integer> id = getParameter(req, Integer.class, "id");
universe@47 66 if (id.isPresent()) {
universe@47 67 req.setAttribute("project", Optional.ofNullable(projectDao.find(id.get())).orElse(new Project(-1)));
universe@47 68 } else {
universe@47 69 req.setAttribute("project", new Project(-1));
universe@47 70 }
universe@51 71 req.setAttribute("users", dao.getUserDao().list());
universe@47 72
universe@47 73 setDynamicFragment(req, "project-form");
universe@47 74
universe@47 75 return ResponseType.HTML;
universe@47 76 }
universe@47 77
universe@47 78 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
universe@47 79 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
universe@47 80
universe@47 81 Project project = new Project(-1);
universe@47 82 try {
universe@47 83 project = new Project(getParameter(req, Integer.class, "id").orElseThrow());
universe@47 84 project.setName(getParameter(req, String.class, "name").orElseThrow());
universe@47 85 getParameter(req, String.class, "description").ifPresent(project::setDescription);
universe@47 86 getParameter(req, String.class, "repoUrl").ifPresent(project::setRepoUrl);
universe@47 87 getParameter(req, Integer.class, "owner").map(
universe@47 88 ownerId -> ownerId >= 0 ? new User(ownerId) : null
universe@47 89 ).ifPresent(project::setOwner);
universe@47 90
universe@47 91 dao.getProjectDao().saveOrUpdate(project);
universe@47 92
universe@47 93 setRedirectLocation(req, "./projects/");
universe@47 94 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
universe@47 95 } catch (NullPointerException | NumberFormatException | SQLException ex) {
universe@47 96 // TODO: set request attribute with error text
universe@47 97 req.setAttribute("project", project);
universe@47 98 setDynamicFragment(req, "project-form");
universe@47 99 }
universe@47 100
universe@47 101 return ResponseType.HTML;
universe@47 102 }
universe@47 103
universe@47 104
universe@47 105 @RequestMapping(requestPath = "versions", method = HttpMethod.GET, menuKey = "menu.versions")
universe@45 106 public ResponseType versions(HttpServletRequest req, DataAccessObjects dao) {
universe@41 107
universe@43 108 return ResponseType.HTML;
universe@41 109 }
universe@41 110 }

mercurial