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

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

mercurial