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