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

Sun, 17 May 2020 16:38:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 May 2020 16:38:04 +0200
changeset 61
3e287f361c7a
parent 59
c759c60507a2
child 66
635ae67e73b5
permissions
-rw-r--r--

moves project index to separate sub path

unfortunately the current menu highlighting strategy sucks and this is the easiest way so that everything looks correct

51
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2018 Mike Becker. All rights reserved.
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 package de.uapcore.lightpit.modules;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 import de.uapcore.lightpit.*;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 import de.uapcore.lightpit.dao.DataAccessObjects;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 import de.uapcore.lightpit.entities.User;
59
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
35 import org.slf4j.Logger;
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
36 import org.slf4j.LoggerFactory;
51
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 import javax.servlet.annotation.WebServlet;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 import javax.servlet.http.HttpServletRequest;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 import java.sql.SQLException;
59
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
41 import java.util.NoSuchElementException;
51
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 import java.util.Optional;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 @LightPITModule(
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 bundleBaseName = "localization.users",
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 modulePath = "teams",
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 defaultPriority = 100
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 )
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 @WebServlet(
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 name = "UsersModule",
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 urlPatterns = "/teams/*"
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 )
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 public final class UsersModule extends AbstractLightPITServlet {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54
59
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
55 private static final Logger LOG = LoggerFactory.getLogger(UsersModule.class);
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
56
51
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 @RequestMapping(method = HttpMethod.GET)
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 final var userDao = dao.getUserDao();
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 req.setAttribute("users", userDao.list());
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 setDynamicFragment(req, "users");
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 return ResponseType.HTML;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 @RequestMapping(requestPath = "edit", method = HttpMethod.GET)
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 final var userDao = dao.getUserDao();
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 Optional<Integer> id = getParameter(req, Integer.class, "id");
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 if (id.isPresent()) {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 req.setAttribute("user", Optional.ofNullable(userDao.find(id.get())).orElse(new User(-1)));
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 } else {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 req.setAttribute("user", new User(-1));
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 setDynamicFragment(req, "user-form");
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 return ResponseType.HTML;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 @RequestMapping(requestPath = "commit", method = HttpMethod.POST)
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 User user = new User(-1);
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 try {
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 user = new User(getParameter(req, Integer.class, "userid").orElseThrow());
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 user.setUsername(getParameter(req, String.class, "username").orElseThrow());
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 getParameter(req, String.class, "givenname").ifPresent(user::setGivenname);
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 getParameter(req, String.class, "lastname").ifPresent(user::setLastname);
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 getParameter(req, String.class, "mail").ifPresent(user::setMail);
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 dao.getUserDao().saveOrUpdate(user);
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 setRedirectLocation(req, "./teams/");
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 setDynamicFragment(req, Constants.DYN_FRAGMENT_COMMIT_SUCCESSFUL);
59
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
98
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
99 LOG.debug("Successfully updated user {}", user.getUsername());
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
100 } catch (NoSuchElementException | NumberFormatException | SQLException ex) {
51
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 // TODO: set request attribute with error text
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 req.setAttribute("user", user);
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 setDynamicFragment(req, "user-form");
59
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
104 LOG.warn("Form validation failure: {}", ex.getMessage());
c759c60507a2 adds version management
Mike Becker <universe@uap-core.de>
parents: 51
diff changeset
105 LOG.debug("Details:", ex);
51
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 return ResponseType.HTML;
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 }
dd0a45ae25d7 adds the possibility to add users / developers
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 }

mercurial