src/main/java/de/uapcore/lightpit/modules/UsersModule.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
child 59
c759c60507a2
permissions
-rw-r--r--

adds the possibility to add users / developers

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

mercurial