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

Sun, 01 Apr 2018 18:25:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Apr 2018 18:25:31 +0200
changeset 22
5a91fb7067af
parent 21
b213fef2539e
child 24
8137ec335416
permissions
-rw-r--r--

minor changes to logging output

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 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2017 Mike Becker. All rights reserved.
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.AbstractMap;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 import java.util.ArrayList;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 import java.util.List;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 import java.util.Map;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 import java.util.Set;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 public abstract class ModuleDao {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 /**
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 * 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
47 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 * <ul>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 * <li>Parameter 1: classname</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 * <li>Result field 1: visible</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 * </ul>
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 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 * @return the prepared statement
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 * @throws SQLException
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 */
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 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
58 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
59 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 /**
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 * 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
63 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 * <ul>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 * <li>Parameter 1: classname</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 * <li>Parameter 2: visible</li>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 * </ul>
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 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 * @return the prepared statement
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 * @throws SQLException
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 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
74 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
75 }
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 /**
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 * Synchronizes a set of registered module classes with the database and gives a list of visible modules in return.
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 * 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
81 * 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
82 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 * @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
85 * @return a list of elements from the given set, which should be visible in the menu
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 * @throws SQLException
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 public final List<Map.Entry<String, LightPITModule>>
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 PreparedStatement
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 check = moduleCheckStatement(conn),
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 insert = moduleInsertStatement(conn);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 insert.setBoolean(2, true);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 // 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
96
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 final List<Map.Entry<String, LightPITModule>> visibleModules = new ArrayList<>();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 for (Map.Entry<String, LightPITModule> mod : moduleSet) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 if (mod.getValue().systemModule()) continue;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 check.setString(1, mod.getKey());
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 try (ResultSet r = check.executeQuery()) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 final boolean visible;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 if (r.next()) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 visible = r.getBoolean(1);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 } else {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 insert.setString(1, mod.getKey());
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 insert.executeUpdate();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 visible = !mod.getValue().menuKey().isEmpty();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 if (visible) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 visibleModules.add(new AbstractMap.SimpleEntry<>(
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 mod.getKey(),
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 mod.getValue()
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 return visibleModules;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 }
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 /**
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 * 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
125 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 * 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
127 *
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 * @param conn the connection to use
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 * @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
130 * @throws SQLException
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 */
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 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
133 List<Module> list = new ArrayList<>();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 try (
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 Statement stmt = conn.createStatement();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 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
137 while (result.next()) {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 final Module mod = new Module();
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 mod.setModID(result.getInt("modid"));
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 mod.setClassname(result.getString("classname"));
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 mod.setVisible(result.getBoolean("visible"));
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 list.add(mod);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 return list;
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 }
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 }

mercurial