src/java/de/uapcore/lightpit/entities/ModuleDao.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 27
1f2a96efa69f
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 de.uapcore.lightpit.LightPITModule;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 import java.sql.Connection;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 import java.sql.PreparedStatement;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 import java.sql.ResultSet;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 import java.sql.SQLException;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 import java.sql.Statement;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 import java.util.ArrayList;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 import java.util.List;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 import java.util.Map;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 import java.util.Set;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 public abstract class ModuleDao {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 /**
26
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
45 * Maps database columns to POJO fields.
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
46 * @param result the database result set
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
47 * @param mod the POJO
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
48 * @throws SQLException
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
49 */
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
50 protected void mapColumns(ResultSet result, Module mod) throws SQLException {
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
51 mod.setModID(result.getInt("modid"));
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
52 mod.setClassname(result.getString("classname"));
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
53 mod.setVisible(result.getBoolean("visible"));
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
54 }
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
55
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
56
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
57 /**
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 * Must return a prepared statement for a single object query with the specified properties.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 * <ul>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 * <li>Parameter 1: classname</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 * <li>Result field 1: visible</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 * </ul>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 * @return the prepared statement
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 * @throws SQLException
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 */
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?");
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72
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 * Must return a prepared statement for insertion with the specified properties.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 * <ul>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 * <li>Parameter 1: classname</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 * <li>Parameter 2: visible</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 * </ul>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 * @return the prepared statement
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 * @throws SQLException
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 */
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 /**
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
90 * Synchronizes a set of registered module classes with the database.
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 * Inserts module classes which are not known to the database and sets them to be visible by default.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 * Module classes known to the database, which are not in the given set, are ignored.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 * @param moduleSet the module set to synchronize
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 * @throws SQLException
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 */
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
99 public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 PreparedStatement
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 check = moduleCheckStatement(conn),
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 insert = moduleInsertStatement(conn);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 insert.setBoolean(2, true);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 // update/delete not required, we do this in the module management UI
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
107 for (Map.Entry<String, LightPITModule> modEntry : moduleSet) {
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
108 if (modEntry.getValue().systemModule()) continue;
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
110 check.setString(1, modEntry.getKey());
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 try (ResultSet r = check.executeQuery()) {
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
112 if (!r.next()) {
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
113 insert.setString(1, modEntry.getKey());
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 insert.executeUpdate();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 /**
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 * Returns a list of all modules known by the database.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 * Keep in mind, that system modules are never known to the database.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 * @return a list of all modules known by the database
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 * @throws SQLException
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 */
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 public List<Module> listAll(Connection conn) throws SQLException {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 List<Module> list = new ArrayList<>();
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
131 try (Statement stmt = conn.createStatement();
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 while (result.next()) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 final Module mod = new Module();
26
65d5a0ca49ae Module names and descriptions + some more styling
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
135 mapColumns(result, mod);
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 list.add(mod);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 return list;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 }

mercurial