src/java/de/uapcore/lightpit/entities/UserDao.java

Sun, 08 Apr 2018 16:51:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Apr 2018 16:51:15 +0200
changeset 28
cfc3d11884ad
parent 26
65d5a0ca49ae
permissions
-rw-r--r--

smaller main menu with better main menu buttons

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

mercurial