src/main/java/de/uapcore/lightpit/dao/UserDao.java

Sat, 09 May 2020 14:37:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 May 2020 14:37:15 +0200
changeset 30
fb30f7b78f9b
parent 29
src/main/java/de/uapcore/lightpit/entities/UserDao.java@27a0fdd7bca7
child 33
fd8c40ff78c3
permissions
-rw-r--r--

moves DAO classes to different package

universe@21 1 /*
universe@21 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@21 3 *
universe@24 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@21 5 *
universe@21 6 * Redistribution and use in source and binary forms, with or without
universe@21 7 * modification, are permitted provided that the following conditions are met:
universe@21 8 *
universe@21 9 * 1. Redistributions of source code must retain the above copyright
universe@21 10 * notice, this list of conditions and the following disclaimer.
universe@21 11 *
universe@21 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@21 13 * notice, this list of conditions and the following disclaimer in the
universe@21 14 * documentation and/or other materials provided with the distribution.
universe@21 15 *
universe@21 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@21 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@21 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@21 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@21 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@21 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@21 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@21 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@21 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@21 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@21 26 * POSSIBILITY OF SUCH DAMAGE.
universe@30 27 *
universe@21 28 */
universe@30 29 package de.uapcore.lightpit.dao;
universe@30 30
universe@30 31 import de.uapcore.lightpit.entities.User;
universe@21 32
universe@21 33 import java.sql.Connection;
universe@21 34 import java.sql.ResultSet;
universe@21 35 import java.sql.SQLException;
universe@21 36 import java.sql.Statement;
universe@21 37 import java.util.ArrayList;
universe@21 38 import java.util.List;
universe@26 39 import java.util.Optional;
universe@21 40
universe@30 41 public class UserDao {
universe@30 42
universe@21 43 /**
universe@26 44 * Maps SQL columns to POJO fields.
universe@30 45 *
universe@26 46 * @param result the database result set
universe@30 47 * @param user the POJO
universe@30 48 * @throws SQLException
universe@21 49 */
universe@26 50 protected void mapColumns(ResultSet result, User user) throws SQLException {
universe@26 51 user.setUserID(result.getInt("userid"));
universe@26 52 user.setUsername(result.getString("username"));
universe@26 53 user.setGivenname(Optional.ofNullable(result.getString("givenname")));
universe@26 54 user.setLastname(Optional.ofNullable(result.getString("lastname")));
universe@21 55 }
universe@21 56
universe@21 57 /**
universe@26 58 * Returns a list of all users ordered by their username.
universe@21 59 *
universe@26 60 * Does not return reserved system users with negative user IDs.
universe@21 61 *
universe@21 62 * @param conn the connection to use
universe@26 63 * @return a list of all users
universe@21 64 * @throws SQLException
universe@21 65 */
universe@26 66 public List<User> listAll(Connection conn) throws SQLException {
universe@26 67 List<User> list = new ArrayList<>();
universe@21 68 try (
universe@21 69 Statement stmt = conn.createStatement();
universe@26 70 ResultSet result = stmt.executeQuery(
universe@26 71 "SELECT * FROM lpitcore_user WHERE userid >= 0 ORDER BY username")) {
universe@21 72 while (result.next()) {
universe@26 73 final User user = new User();
universe@26 74 mapColumns(result, user);
universe@26 75 list.add(user);
universe@21 76 }
universe@21 77 }
universe@21 78 return list;
universe@21 79 }
universe@21 80 }

mercurial