32 import java.sql.Connection; |
32 import java.sql.Connection; |
33 import java.sql.PreparedStatement; |
33 import java.sql.PreparedStatement; |
34 import java.sql.ResultSet; |
34 import java.sql.ResultSet; |
35 import java.sql.SQLException; |
35 import java.sql.SQLException; |
36 import java.sql.Statement; |
36 import java.sql.Statement; |
37 import java.util.AbstractMap; |
|
38 import java.util.ArrayList; |
37 import java.util.ArrayList; |
39 import java.util.List; |
38 import java.util.List; |
40 import java.util.Map; |
39 import java.util.Map; |
41 import java.util.Set; |
40 import java.util.Set; |
42 |
41 |
86 protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException { |
85 protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException { |
87 return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)"); |
86 return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)"); |
88 } |
87 } |
89 |
88 |
90 /** |
89 /** |
91 * Synchronizes a set of registered module classes with the database and gives a list of visible modules in return. |
90 * Synchronizes a set of registered module classes with the database. |
92 * |
91 * |
93 * Inserts module classes which are not known to the database and sets them to be visible by default. |
92 * Inserts module classes which are not known to the database and sets them to be visible by default. |
94 * Module classes known to the database, which are not in the given set, are ignored. |
93 * Module classes known to the database, which are not in the given set, are ignored. |
95 * |
94 * |
96 * @param conn the connection to use |
95 * @param conn the connection to use |
97 * @param moduleSet the module set to synchronize |
96 * @param moduleSet the module set to synchronize |
98 * @return a list of elements from the given set, which should be visible in the menu |
|
99 * @throws SQLException |
97 * @throws SQLException |
100 */ |
98 */ |
101 public final List<Map.Entry<String, LightPITModule>> |
99 public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException { |
102 syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException { |
|
103 |
100 |
104 PreparedStatement |
101 PreparedStatement |
105 check = moduleCheckStatement(conn), |
102 check = moduleCheckStatement(conn), |
106 insert = moduleInsertStatement(conn); |
103 insert = moduleInsertStatement(conn); |
107 insert.setBoolean(2, true); |
104 insert.setBoolean(2, true); |
108 // update/delete not required, we do this in the module management UI |
105 // update/delete not required, we do this in the module management UI |
109 |
106 |
110 final List<Map.Entry<String, LightPITModule>> visibleModules = new ArrayList<>(); |
107 for (Map.Entry<String, LightPITModule> modEntry : moduleSet) { |
|
108 if (modEntry.getValue().systemModule()) continue; |
111 |
109 |
112 for (Map.Entry<String, LightPITModule> mod : moduleSet) { |
110 check.setString(1, modEntry.getKey()); |
113 if (mod.getValue().systemModule()) continue; |
|
114 |
|
115 check.setString(1, mod.getKey()); |
|
116 try (ResultSet r = check.executeQuery()) { |
111 try (ResultSet r = check.executeQuery()) { |
117 final boolean visible; |
112 if (!r.next()) { |
118 if (r.next()) { |
113 insert.setString(1, modEntry.getKey()); |
119 visible = r.getBoolean(1); |
|
120 } else { |
|
121 insert.setString(1, mod.getKey()); |
|
122 insert.executeUpdate(); |
114 insert.executeUpdate(); |
123 visible = !mod.getValue().menuKey().isEmpty(); |
|
124 } |
|
125 if (visible) { |
|
126 visibleModules.add(new AbstractMap.SimpleEntry<>( |
|
127 mod.getKey(), |
|
128 mod.getValue() |
|
129 )); |
|
130 } |
115 } |
131 } |
116 } |
132 } |
117 } |
133 return visibleModules; |
|
134 } |
118 } |
135 |
119 |
136 /** |
120 /** |
137 * Returns a list of all modules known by the database. |
121 * Returns a list of all modules known by the database. |
138 * |
122 * |
142 * @return a list of all modules known by the database |
126 * @return a list of all modules known by the database |
143 * @throws SQLException |
127 * @throws SQLException |
144 */ |
128 */ |
145 public List<Module> listAll(Connection conn) throws SQLException { |
129 public List<Module> listAll(Connection conn) throws SQLException { |
146 List<Module> list = new ArrayList<>(); |
130 List<Module> list = new ArrayList<>(); |
147 try ( |
131 try (Statement stmt = conn.createStatement(); |
148 Statement stmt = conn.createStatement(); |
|
149 ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) { |
132 ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) { |
150 while (result.next()) { |
133 while (result.next()) { |
151 final Module mod = new Module(); |
134 final Module mod = new Module(); |
152 mapColumns(result, mod); |
135 mapColumns(result, mod); |
153 list.add(mod); |
136 list.add(mod); |