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

changeset 29
27a0fdd7bca7
parent 28
cfc3d11884ad
child 30
fb30f7b78f9b
equal deleted inserted replaced
28:cfc3d11884ad 29:27a0fdd7bca7
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2018 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 package de.uapcore.lightpit.entities;
30
31 import de.uapcore.lightpit.LightPITModule;
32 import java.sql.Connection;
33 import java.sql.PreparedStatement;
34 import java.sql.ResultSet;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42 public abstract class ModuleDao {
43
44 /**
45 * Maps database columns to POJO fields.
46 * @param result the database result set
47 * @param mod the POJO
48 * @throws SQLException
49 */
50 protected void mapColumns(ResultSet result, Module mod) throws SQLException {
51 mod.setModID(result.getInt("modid"));
52 mod.setClassname(result.getString("classname"));
53 mod.setVisible(result.getBoolean("visible"));
54 }
55
56
57 /**
58 * Must return a prepared statement for a single object query with the specified properties.
59 *
60 * <ul>
61 * <li>Parameter 1: classname</li>
62 * <li>Result field 1: visible</li>
63 * </ul>
64 *
65 * @param conn the connection to use
66 * @return the prepared statement
67 * @throws SQLException
68 */
69 protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException {
70 return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?");
71 }
72
73 /**
74 * Must return a prepared statement for insertion with the specified properties.
75 *
76 * <ul>
77 * <li>Parameter 1: classname</li>
78 * <li>Parameter 2: visible</li>
79 * </ul>
80 *
81 * @param conn the connection to use
82 * @return the prepared statement
83 * @throws SQLException
84 */
85 protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
86 return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
87 }
88
89 /**
90 * Synchronizes a set of registered module classes with the database.
91 *
92 * Inserts module classes which are not known to the database and sets them to be visible by default.
93 * Module classes known to the database, which are not in the given set, are ignored.
94 *
95 * @param conn the connection to use
96 * @param moduleSet the module set to synchronize
97 * @throws SQLException
98 */
99 public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
100
101 PreparedStatement
102 check = moduleCheckStatement(conn),
103 insert = moduleInsertStatement(conn);
104 insert.setBoolean(2, true);
105 // update/delete not required, we do this in the module management UI
106
107 for (Map.Entry<String, LightPITModule> modEntry : moduleSet) {
108 if (modEntry.getValue().systemModule()) continue;
109
110 check.setString(1, modEntry.getKey());
111 try (ResultSet r = check.executeQuery()) {
112 if (!r.next()) {
113 insert.setString(1, modEntry.getKey());
114 insert.executeUpdate();
115 }
116 }
117 }
118 }
119
120 /**
121 * Returns a list of all modules known by the database.
122 *
123 * Keep in mind, that system modules are never known to the database.
124 *
125 * @param conn the connection to use
126 * @return a list of all modules known by the database
127 * @throws SQLException
128 */
129 public List<Module> listAll(Connection conn) throws SQLException {
130 List<Module> list = new ArrayList<>();
131 try (Statement stmt = conn.createStatement();
132 ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) {
133 while (result.next()) {
134 final Module mod = new Module();
135 mapColumns(result, mod);
136 list.add(mod);
137 }
138 }
139 return list;
140 }
141 }

mercurial