universe@51: /* universe@51: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@51: * universe@51: * Copyright 2018 Mike Becker. All rights reserved. universe@51: * universe@51: * Redistribution and use in source and binary forms, with or without universe@51: * modification, are permitted provided that the following conditions are met: universe@51: * universe@51: * 1. Redistributions of source code must retain the above copyright universe@51: * notice, this list of conditions and the following disclaimer. universe@51: * universe@51: * 2. Redistributions in binary form must reproduce the above copyright universe@51: * notice, this list of conditions and the following disclaimer in the universe@51: * documentation and/or other materials provided with the distribution. universe@51: * universe@51: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@51: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@51: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@51: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@51: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@51: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@51: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@51: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@51: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@51: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@51: * POSSIBILITY OF SUCH DAMAGE. universe@51: * universe@51: */ universe@51: package de.uapcore.lightpit.modules; universe@51: universe@51: universe@51: import de.uapcore.lightpit.*; universe@51: import de.uapcore.lightpit.dao.DataAccessObjects; universe@51: import de.uapcore.lightpit.entities.User; universe@86: import de.uapcore.lightpit.viewmodel.UsersEditView; universe@86: import de.uapcore.lightpit.viewmodel.UsersView; universe@59: import org.slf4j.Logger; universe@59: import org.slf4j.LoggerFactory; universe@51: universe@51: import javax.servlet.annotation.WebServlet; universe@51: import javax.servlet.http.HttpServletRequest; universe@51: import java.sql.SQLException; universe@59: import java.util.NoSuchElementException; universe@51: universe@51: @WebServlet( universe@51: name = "UsersModule", universe@51: urlPatterns = "/teams/*" universe@51: ) universe@51: public final class UsersModule extends AbstractLightPITServlet { universe@51: universe@59: private static final Logger LOG = LoggerFactory.getLogger(UsersModule.class); universe@59: universe@78: @Override universe@78: protected String getResourceBundleName() { universe@78: return "localization.users"; universe@78: } universe@78: universe@51: @RequestMapping(method = HttpMethod.GET) universe@51: public ResponseType index(HttpServletRequest req, DataAccessObjects dao) throws SQLException { universe@51: final var userDao = dao.getUserDao(); universe@51: universe@86: final var viewModel = new UsersView(); universe@86: viewModel.setUsers(userDao.list()); universe@86: setViewModel(req, viewModel); universe@74: setContentPage(req, "users"); universe@51: universe@51: return ResponseType.HTML; universe@51: } universe@51: universe@51: @RequestMapping(requestPath = "edit", method = HttpMethod.GET) universe@51: public ResponseType edit(HttpServletRequest req, DataAccessObjects dao) throws SQLException { universe@51: universe@86: final var viewModel = new UsersEditView(); universe@86: viewModel.setUser(findByParameter(req, Integer.class, "id", universe@66: dao.getUserDao()::find).orElse(new User(-1))); universe@51: universe@86: setViewModel(req, viewModel); universe@74: setContentPage(req, "user-form"); universe@51: universe@51: return ResponseType.HTML; universe@51: } universe@51: universe@51: @RequestMapping(requestPath = "commit", method = HttpMethod.POST) universe@51: public ResponseType commit(HttpServletRequest req, DataAccessObjects dao) { universe@51: universe@51: User user = new User(-1); universe@51: try { universe@51: user = new User(getParameter(req, Integer.class, "userid").orElseThrow()); universe@51: user.setUsername(getParameter(req, String.class, "username").orElseThrow()); universe@51: getParameter(req, String.class, "givenname").ifPresent(user::setGivenname); universe@51: getParameter(req, String.class, "lastname").ifPresent(user::setLastname); universe@51: getParameter(req, String.class, "mail").ifPresent(user::setMail); universe@51: universe@51: dao.getUserDao().saveOrUpdate(user); universe@51: universe@51: setRedirectLocation(req, "./teams/"); universe@74: setContentPage(req, Constants.JSP_COMMIT_SUCCESSFUL); universe@59: universe@59: LOG.debug("Successfully updated user {}", user.getUsername()); universe@75: } catch (NoSuchElementException | IllegalArgumentException | SQLException ex) { universe@86: final var viewModel = new UsersEditView(); universe@86: viewModel.setUser(user); universe@86: // TODO: viewModel.setErrorText() universe@86: setViewModel(req, viewModel); universe@74: setContentPage(req, "user-form"); universe@59: LOG.warn("Form validation failure: {}", ex.getMessage()); universe@59: LOG.debug("Details:", ex); universe@51: } universe@51: universe@51: return ResponseType.HTML; universe@51: } universe@51: }