Mon, 21 Dec 2020 18:29:34 +0100
major refactoring of DAO architecture - also fixes #114
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@157 | 31 | import de.uapcore.lightpit.AbstractLightPITServlet; |
universe@157 | 32 | import de.uapcore.lightpit.Constants; |
universe@157 | 33 | import de.uapcore.lightpit.HttpMethod; |
universe@157 | 34 | import de.uapcore.lightpit.RequestMapping; |
universe@167 | 35 | import de.uapcore.lightpit.dao.DataAccessObject; |
universe@51 | 36 | import de.uapcore.lightpit.entities.User; |
universe@86 | 37 | import de.uapcore.lightpit.viewmodel.UsersEditView; |
universe@86 | 38 | import de.uapcore.lightpit.viewmodel.UsersView; |
universe@59 | 39 | import org.slf4j.Logger; |
universe@59 | 40 | import org.slf4j.LoggerFactory; |
universe@51 | 41 | |
universe@157 | 42 | import javax.servlet.ServletException; |
universe@51 | 43 | import javax.servlet.annotation.WebServlet; |
universe@51 | 44 | import javax.servlet.http.HttpServletRequest; |
universe@157 | 45 | import javax.servlet.http.HttpServletResponse; |
universe@157 | 46 | import java.io.IOException; |
universe@51 | 47 | import java.sql.SQLException; |
universe@59 | 48 | import java.util.NoSuchElementException; |
universe@51 | 49 | |
universe@51 | 50 | @WebServlet( |
universe@51 | 51 | name = "UsersModule", |
universe@51 | 52 | urlPatterns = "/teams/*" |
universe@51 | 53 | ) |
universe@51 | 54 | public final class UsersModule extends AbstractLightPITServlet { |
universe@51 | 55 | |
universe@59 | 56 | private static final Logger LOG = LoggerFactory.getLogger(UsersModule.class); |
universe@59 | 57 | |
universe@78 | 58 | @Override |
universe@78 | 59 | protected String getResourceBundleName() { |
universe@78 | 60 | return "localization.users"; |
universe@78 | 61 | } |
universe@78 | 62 | |
universe@51 | 63 | @RequestMapping(method = HttpMethod.GET) |
universe@167 | 64 | public void index(HttpServletRequest req, HttpServletResponse resp, DataAccessObject dao) throws SQLException, ServletException, IOException { |
universe@86 | 65 | final var viewModel = new UsersView(); |
universe@167 | 66 | viewModel.setUsers(dao.listUsers()); |
universe@86 | 67 | setViewModel(req, viewModel); |
universe@74 | 68 | setContentPage(req, "users"); |
universe@51 | 69 | |
universe@157 | 70 | renderSite(req, resp); |
universe@51 | 71 | } |
universe@51 | 72 | |
universe@51 | 73 | @RequestMapping(requestPath = "edit", method = HttpMethod.GET) |
universe@167 | 74 | public void edit(HttpServletRequest req, HttpServletResponse resp, DataAccessObject dao) throws SQLException, ServletException, IOException { |
universe@51 | 75 | |
universe@86 | 76 | final var viewModel = new UsersEditView(); |
universe@167 | 77 | viewModel.setUser(findByParameter(req, Integer.class, "id", dao::findUser).orElse(new User(-1))); |
universe@51 | 78 | |
universe@86 | 79 | setViewModel(req, viewModel); |
universe@74 | 80 | setContentPage(req, "user-form"); |
universe@51 | 81 | |
universe@157 | 82 | renderSite(req, resp); |
universe@51 | 83 | } |
universe@51 | 84 | |
universe@51 | 85 | @RequestMapping(requestPath = "commit", method = HttpMethod.POST) |
universe@167 | 86 | public void commit(HttpServletRequest req, HttpServletResponse resp, DataAccessObject dao) throws ServletException, IOException { |
universe@51 | 87 | |
universe@51 | 88 | User user = new User(-1); |
universe@51 | 89 | try { |
universe@51 | 90 | user = new User(getParameter(req, Integer.class, "userid").orElseThrow()); |
universe@51 | 91 | user.setUsername(getParameter(req, String.class, "username").orElseThrow()); |
universe@51 | 92 | getParameter(req, String.class, "givenname").ifPresent(user::setGivenname); |
universe@51 | 93 | getParameter(req, String.class, "lastname").ifPresent(user::setLastname); |
universe@51 | 94 | getParameter(req, String.class, "mail").ifPresent(user::setMail); |
universe@51 | 95 | |
mike@159 | 96 | if (user.getId() > 0) { |
universe@167 | 97 | dao.updateUser(user); |
mike@159 | 98 | } else { |
universe@167 | 99 | dao.insertUser(user); |
mike@159 | 100 | } |
universe@51 | 101 | |
universe@51 | 102 | setRedirectLocation(req, "./teams/"); |
universe@74 | 103 | setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); |
universe@59 | 104 | |
universe@59 | 105 | LOG.debug("Successfully updated user {}", user.getUsername()); |
mike@159 | 106 | } catch (NoSuchElementException | IllegalArgumentException ex) { |
universe@86 | 107 | final var viewModel = new UsersEditView(); |
universe@86 | 108 | viewModel.setUser(user); |
universe@86 | 109 | // TODO: viewModel.setErrorText() |
universe@86 | 110 | setViewModel(req, viewModel); |
universe@74 | 111 | setContentPage(req, "user-form"); |
universe@59 | 112 | LOG.warn("Form validation failure: {}", ex.getMessage()); |
universe@59 | 113 | LOG.debug("Details:", ex); |
universe@51 | 114 | } |
universe@51 | 115 | |
universe@157 | 116 | renderSite(req, resp); |
universe@51 | 117 | } |
universe@51 | 118 | } |