Sat, 09 May 2020 14:37:15 +0200
moves DAO classes to different package
1.1 --- a/src/main/java/de/uapcore/lightpit/ModuleManager.java Sat May 09 14:26:31 2020 +0200 1.2 +++ b/src/main/java/de/uapcore/lightpit/ModuleManager.java Sat May 09 14:37:15 2020 +0200 1.3 @@ -28,25 +28,22 @@ 1.4 */ 1.5 package de.uapcore.lightpit; 1.6 1.7 -import de.uapcore.lightpit.entities.CoreDAOFactory; 1.8 +import de.uapcore.lightpit.dao.CoreDAOFactory; 1.9 +import de.uapcore.lightpit.dao.ModuleDao; 1.10 import de.uapcore.lightpit.entities.Module; 1.11 -import de.uapcore.lightpit.entities.ModuleDao; 1.12 -import java.sql.Connection; 1.13 -import java.sql.SQLException; 1.14 -import java.util.Collections; 1.15 -import java.util.HashMap; 1.16 -import java.util.List; 1.17 -import java.util.Map; 1.18 -import java.util.Optional; 1.19 -import java.util.concurrent.atomic.AtomicBoolean; 1.20 -import java.util.stream.Collectors; 1.21 +import org.slf4j.Logger; 1.22 +import org.slf4j.LoggerFactory; 1.23 + 1.24 import javax.servlet.Registration; 1.25 import javax.servlet.ServletContext; 1.26 import javax.servlet.ServletContextEvent; 1.27 import javax.servlet.ServletContextListener; 1.28 import javax.servlet.annotation.WebListener; 1.29 -import org.slf4j.Logger; 1.30 -import org.slf4j.LoggerFactory; 1.31 +import java.sql.Connection; 1.32 +import java.sql.SQLException; 1.33 +import java.util.*; 1.34 +import java.util.concurrent.atomic.AtomicBoolean; 1.35 +import java.util.stream.Collectors; 1.36 1.37 /** 1.38 * Scans registered servlets for LightPIT modules. 1.39 @@ -144,17 +141,17 @@ 1.40 dirty.set(true); 1.41 LOG.info("Modules loaded."); 1.42 } 1.43 - 1.44 + 1.45 /** 1.46 * Synchronizes module information with the database. 1.47 - * 1.48 + * <p> 1.49 * This must be called from the {@link AbstractLightPITServlet}. 1.50 * Admittedly the call will perform the synchronization once after reload 1.51 * and be a no-op, afterwards. 1.52 - * However, we since the DatabaseFacade might be loaded after the module 1.53 + * However, since the DatabaseFacade might be loaded after the module 1.54 * manager, we must defer the synchronization to the first request 1.55 * handled by the Servlet. 1.56 - * 1.57 + * 1.58 * @param db interface to the database 1.59 */ 1.60 public void syncWithDatabase(DatabaseFacade db) {
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/main/java/de/uapcore/lightpit/dao/CoreDAOFactory.java Sat May 09 14:37:15 2020 +0200 2.3 @@ -0,0 +1,50 @@ 2.4 +/* 2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 + * 2.7 + * Copyright 2018 Mike Becker. All rights reserved. 2.8 + * 2.9 + * Redistribution and use in source and binary forms, with or without 2.10 + * modification, are permitted provided that the following conditions are met: 2.11 + * 2.12 + * 1. Redistributions of source code must retain the above copyright 2.13 + * notice, this list of conditions and the following disclaimer. 2.14 + * 2.15 + * 2. Redistributions in binary form must reproduce the above copyright 2.16 + * notice, this list of conditions and the following disclaimer in the 2.17 + * documentation and/or other materials provided with the distribution. 2.18 + * 2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.29 + * POSSIBILITY OF SUCH DAMAGE. 2.30 + * 2.31 + */ 2.32 +package de.uapcore.lightpit.dao; 2.33 + 2.34 +import de.uapcore.lightpit.DatabaseFacade; 2.35 + 2.36 +public final class CoreDAOFactory { 2.37 + 2.38 + private static final ModuleDao moduleDao = new ModuleDao(); 2.39 + 2.40 + private CoreDAOFactory() { 2.41 + } 2.42 + 2.43 + public static ModuleDao getModuleDao(DatabaseFacade.Dialect dialect) { 2.44 + // TODO: this is idiotic, we would not change the dialect while the app is running 2.45 + switch (dialect) { 2.46 + case Postgres: 2.47 + return moduleDao; 2.48 + default: 2.49 + assert (false); 2.50 + return null; 2.51 + } 2.52 + } 2.53 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java Sat May 09 14:37:15 2020 +0200 3.3 @@ -0,0 +1,140 @@ 3.4 +/* 3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 + * 3.7 + * Copyright 2018 Mike Becker. All rights reserved. 3.8 + * 3.9 + * Redistribution and use in source and binary forms, with or without 3.10 + * modification, are permitted provided that the following conditions are met: 3.11 + * 3.12 + * 1. Redistributions of source code must retain the above copyright 3.13 + * notice, this list of conditions and the following disclaimer. 3.14 + * 3.15 + * 2. Redistributions in binary form must reproduce the above copyright 3.16 + * notice, this list of conditions and the following disclaimer in the 3.17 + * documentation and/or other materials provided with the distribution. 3.18 + * 3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 + * POSSIBILITY OF SUCH DAMAGE. 3.30 + * 3.31 + */ 3.32 +package de.uapcore.lightpit.dao; 3.33 + 3.34 +import de.uapcore.lightpit.LightPITModule; 3.35 +import de.uapcore.lightpit.entities.Module; 3.36 + 3.37 +import java.sql.*; 3.38 +import java.util.ArrayList; 3.39 +import java.util.List; 3.40 +import java.util.Map; 3.41 +import java.util.Set; 3.42 + 3.43 +public class ModuleDao { 3.44 + 3.45 + /** 3.46 + * Maps database columns to POJO fields. 3.47 + * 3.48 + * @param result the database result set 3.49 + * @param mod the POJO 3.50 + * @throws SQLException 3.51 + */ 3.52 + protected void mapColumns(ResultSet result, Module mod) throws SQLException { 3.53 + mod.setModID(result.getInt("modid")); 3.54 + mod.setClassname(result.getString("classname")); 3.55 + mod.setVisible(result.getBoolean("visible")); 3.56 + } 3.57 + 3.58 + 3.59 + /** 3.60 + * Must return a prepared statement for a single object query with the specified properties. 3.61 + * 3.62 + * <ul> 3.63 + * <li>Parameter 1: classname</li> 3.64 + * <li>Result field 1: visible</li> 3.65 + * </ul> 3.66 + * 3.67 + * @param conn the connection to use 3.68 + * @return the prepared statement 3.69 + * @throws SQLException 3.70 + */ 3.71 + protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException { 3.72 + return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?"); 3.73 + } 3.74 + 3.75 + /** 3.76 + * Must return a prepared statement for insertion with the specified properties. 3.77 + * 3.78 + * <ul> 3.79 + * <li>Parameter 1: classname</li> 3.80 + * <li>Parameter 2: visible</li> 3.81 + * </ul> 3.82 + * 3.83 + * @param conn the connection to use 3.84 + * @return the prepared statement 3.85 + * @throws SQLException 3.86 + */ 3.87 + protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException { 3.88 + return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)"); 3.89 + } 3.90 + 3.91 + /** 3.92 + * Synchronizes a set of registered module classes with the database. 3.93 + * 3.94 + * Inserts module classes which are not known to the database and sets them to be visible by default. 3.95 + * Module classes known to the database, which are not in the given set, are ignored. 3.96 + * 3.97 + * @param conn the connection to use 3.98 + * @param moduleSet the module set to synchronize 3.99 + * @throws SQLException 3.100 + */ 3.101 + public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException { 3.102 + 3.103 + PreparedStatement 3.104 + check = moduleCheckStatement(conn), 3.105 + insert = moduleInsertStatement(conn); 3.106 + insert.setBoolean(2, true); 3.107 + // update/delete not required, we do this in the module management UI 3.108 + 3.109 + for (Map.Entry<String, LightPITModule> modEntry : moduleSet) { 3.110 + if (modEntry.getValue().systemModule()) continue; 3.111 + 3.112 + check.setString(1, modEntry.getKey()); 3.113 + try (ResultSet r = check.executeQuery()) { 3.114 + if (!r.next()) { 3.115 + insert.setString(1, modEntry.getKey()); 3.116 + insert.executeUpdate(); 3.117 + } 3.118 + } 3.119 + } 3.120 + } 3.121 + 3.122 + /** 3.123 + * Returns a list of all modules known by the database. 3.124 + * 3.125 + * Keep in mind, that system modules are never known to the database. 3.126 + * 3.127 + * @param conn the connection to use 3.128 + * @return a list of all modules known by the database 3.129 + * @throws SQLException 3.130 + */ 3.131 + public List<Module> listAll(Connection conn) throws SQLException { 3.132 + List<Module> list = new ArrayList<>(); 3.133 + try (Statement stmt = conn.createStatement(); 3.134 + ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) { 3.135 + while (result.next()) { 3.136 + final Module mod = new Module(); 3.137 + mapColumns(result, mod); 3.138 + list.add(mod); 3.139 + } 3.140 + } 3.141 + return list; 3.142 + } 3.143 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/main/java/de/uapcore/lightpit/dao/UserDao.java Sat May 09 14:37:15 2020 +0200 4.3 @@ -0,0 +1,80 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2018 Mike Becker. All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + * 4.31 + */ 4.32 +package de.uapcore.lightpit.dao; 4.33 + 4.34 +import de.uapcore.lightpit.entities.User; 4.35 + 4.36 +import java.sql.Connection; 4.37 +import java.sql.ResultSet; 4.38 +import java.sql.SQLException; 4.39 +import java.sql.Statement; 4.40 +import java.util.ArrayList; 4.41 +import java.util.List; 4.42 +import java.util.Optional; 4.43 + 4.44 +public class UserDao { 4.45 + 4.46 + /** 4.47 + * Maps SQL columns to POJO fields. 4.48 + * 4.49 + * @param result the database result set 4.50 + * @param user the POJO 4.51 + * @throws SQLException 4.52 + */ 4.53 + protected void mapColumns(ResultSet result, User user) throws SQLException { 4.54 + user.setUserID(result.getInt("userid")); 4.55 + user.setUsername(result.getString("username")); 4.56 + user.setGivenname(Optional.ofNullable(result.getString("givenname"))); 4.57 + user.setLastname(Optional.ofNullable(result.getString("lastname"))); 4.58 + } 4.59 + 4.60 + /** 4.61 + * Returns a list of all users ordered by their username. 4.62 + * 4.63 + * Does not return reserved system users with negative user IDs. 4.64 + * 4.65 + * @param conn the connection to use 4.66 + * @return a list of all users 4.67 + * @throws SQLException 4.68 + */ 4.69 + public List<User> listAll(Connection conn) throws SQLException { 4.70 + List<User> list = new ArrayList<>(); 4.71 + try ( 4.72 + Statement stmt = conn.createStatement(); 4.73 + ResultSet result = stmt.executeQuery( 4.74 + "SELECT * FROM lpitcore_user WHERE userid >= 0 ORDER BY username")) { 4.75 + while (result.next()) { 4.76 + final User user = new User(); 4.77 + mapColumns(result, user); 4.78 + list.add(user); 4.79 + } 4.80 + } 4.81 + return list; 4.82 + } 4.83 +}
5.1 --- a/src/main/java/de/uapcore/lightpit/entities/CoreDAOFactory.java Sat May 09 14:26:31 2020 +0200 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,47 +0,0 @@ 5.4 -/* 5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5.6 - * 5.7 - * Copyright 2018 Mike Becker. All rights reserved. 5.8 - * 5.9 - * Redistribution and use in source and binary forms, with or without 5.10 - * modification, are permitted provided that the following conditions are met: 5.11 - * 5.12 - * 1. Redistributions of source code must retain the above copyright 5.13 - * notice, this list of conditions and the following disclaimer. 5.14 - * 5.15 - * 2. Redistributions in binary form must reproduce the above copyright 5.16 - * notice, this list of conditions and the following disclaimer in the 5.17 - * documentation and/or other materials provided with the distribution. 5.18 - * 5.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 5.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 5.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5.29 - * POSSIBILITY OF SUCH DAMAGE. 5.30 - * 5.31 - */ 5.32 -package de.uapcore.lightpit.entities; 5.33 - 5.34 -import de.uapcore.lightpit.DatabaseFacade; 5.35 - 5.36 -public final class CoreDAOFactory { 5.37 - 5.38 - private CoreDAOFactory() {} 5.39 - 5.40 - private static class PostgresDaos { 5.41 - static final ModuleDao MODULE_DAO = new PostgresModuleDao(); 5.42 - } 5.43 - 5.44 - public static ModuleDao getModuleDao(DatabaseFacade.Dialect dialect) { 5.45 - switch (dialect) { 5.46 - case Postgres: return PostgresDaos.MODULE_DAO; 5.47 - default: assert(false); return null; 5.48 - } 5.49 - } 5.50 -}
6.1 --- a/src/main/java/de/uapcore/lightpit/entities/ModuleDao.java Sat May 09 14:26:31 2020 +0200 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,141 +0,0 @@ 6.4 -/* 6.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 6.6 - * 6.7 - * Copyright 2018 Mike Becker. All rights reserved. 6.8 - * 6.9 - * Redistribution and use in source and binary forms, with or without 6.10 - * modification, are permitted provided that the following conditions are met: 6.11 - * 6.12 - * 1. Redistributions of source code must retain the above copyright 6.13 - * notice, this list of conditions and the following disclaimer. 6.14 - * 6.15 - * 2. Redistributions in binary form must reproduce the above copyright 6.16 - * notice, this list of conditions and the following disclaimer in the 6.17 - * documentation and/or other materials provided with the distribution. 6.18 - * 6.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 6.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 6.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 6.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 6.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 6.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 6.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 6.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 6.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 6.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 6.29 - * POSSIBILITY OF SUCH DAMAGE. 6.30 - * 6.31 - */ 6.32 -package de.uapcore.lightpit.entities; 6.33 - 6.34 -import de.uapcore.lightpit.LightPITModule; 6.35 -import java.sql.Connection; 6.36 -import java.sql.PreparedStatement; 6.37 -import java.sql.ResultSet; 6.38 -import java.sql.SQLException; 6.39 -import java.sql.Statement; 6.40 -import java.util.ArrayList; 6.41 -import java.util.List; 6.42 -import java.util.Map; 6.43 -import java.util.Set; 6.44 - 6.45 -public abstract class ModuleDao { 6.46 - 6.47 - /** 6.48 - * Maps database columns to POJO fields. 6.49 - * @param result the database result set 6.50 - * @param mod the POJO 6.51 - * @throws SQLException 6.52 - */ 6.53 - protected void mapColumns(ResultSet result, Module mod) throws SQLException { 6.54 - mod.setModID(result.getInt("modid")); 6.55 - mod.setClassname(result.getString("classname")); 6.56 - mod.setVisible(result.getBoolean("visible")); 6.57 - } 6.58 - 6.59 - 6.60 - /** 6.61 - * Must return a prepared statement for a single object query with the specified properties. 6.62 - * 6.63 - * <ul> 6.64 - * <li>Parameter 1: classname</li> 6.65 - * <li>Result field 1: visible</li> 6.66 - * </ul> 6.67 - * 6.68 - * @param conn the connection to use 6.69 - * @return the prepared statement 6.70 - * @throws SQLException 6.71 - */ 6.72 - protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException { 6.73 - return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?"); 6.74 - } 6.75 - 6.76 - /** 6.77 - * Must return a prepared statement for insertion with the specified properties. 6.78 - * 6.79 - * <ul> 6.80 - * <li>Parameter 1: classname</li> 6.81 - * <li>Parameter 2: visible</li> 6.82 - * </ul> 6.83 - * 6.84 - * @param conn the connection to use 6.85 - * @return the prepared statement 6.86 - * @throws SQLException 6.87 - */ 6.88 - protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException { 6.89 - return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)"); 6.90 - } 6.91 - 6.92 - /** 6.93 - * Synchronizes a set of registered module classes with the database. 6.94 - * 6.95 - * Inserts module classes which are not known to the database and sets them to be visible by default. 6.96 - * Module classes known to the database, which are not in the given set, are ignored. 6.97 - * 6.98 - * @param conn the connection to use 6.99 - * @param moduleSet the module set to synchronize 6.100 - * @throws SQLException 6.101 - */ 6.102 - public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException { 6.103 - 6.104 - PreparedStatement 6.105 - check = moduleCheckStatement(conn), 6.106 - insert = moduleInsertStatement(conn); 6.107 - insert.setBoolean(2, true); 6.108 - // update/delete not required, we do this in the module management UI 6.109 - 6.110 - for (Map.Entry<String, LightPITModule> modEntry : moduleSet) { 6.111 - if (modEntry.getValue().systemModule()) continue; 6.112 - 6.113 - check.setString(1, modEntry.getKey()); 6.114 - try (ResultSet r = check.executeQuery()) { 6.115 - if (!r.next()) { 6.116 - insert.setString(1, modEntry.getKey()); 6.117 - insert.executeUpdate(); 6.118 - } 6.119 - } 6.120 - } 6.121 - } 6.122 - 6.123 - /** 6.124 - * Returns a list of all modules known by the database. 6.125 - * 6.126 - * Keep in mind, that system modules are never known to the database. 6.127 - * 6.128 - * @param conn the connection to use 6.129 - * @return a list of all modules known by the database 6.130 - * @throws SQLException 6.131 - */ 6.132 - public List<Module> listAll(Connection conn) throws SQLException { 6.133 - List<Module> list = new ArrayList<>(); 6.134 - try (Statement stmt = conn.createStatement(); 6.135 - ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) { 6.136 - while (result.next()) { 6.137 - final Module mod = new Module(); 6.138 - mapColumns(result, mod); 6.139 - list.add(mod); 6.140 - } 6.141 - } 6.142 - return list; 6.143 - } 6.144 -}
7.1 --- a/src/main/java/de/uapcore/lightpit/entities/PostgresModuleDao.java Sat May 09 14:26:31 2020 +0200 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,33 +0,0 @@ 7.4 -/* 7.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7.6 - * 7.7 - * Copyright 2018 Mike Becker. All rights reserved. 7.8 - * 7.9 - * Redistribution and use in source and binary forms, with or without 7.10 - * modification, are permitted provided that the following conditions are met: 7.11 - * 7.12 - * 1. Redistributions of source code must retain the above copyright 7.13 - * notice, this list of conditions and the following disclaimer. 7.14 - * 7.15 - * 2. Redistributions in binary form must reproduce the above copyright 7.16 - * notice, this list of conditions and the following disclaimer in the 7.17 - * documentation and/or other materials provided with the distribution. 7.18 - * 7.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 7.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 7.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 7.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 7.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 7.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 7.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 7.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 7.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 7.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 7.29 - * POSSIBILITY OF SUCH DAMAGE. 7.30 - * 7.31 - */ 7.32 -package de.uapcore.lightpit.entities; 7.33 - 7.34 -public class PostgresModuleDao extends ModuleDao { 7.35 - // No overrides needed. 7.36 -}
8.1 --- a/src/main/java/de/uapcore/lightpit/entities/UserDao.java Sat May 09 14:26:31 2020 +0200 8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 8.3 @@ -1,77 +0,0 @@ 8.4 -/* 8.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 8.6 - * 8.7 - * Copyright 2018 Mike Becker. All rights reserved. 8.8 - * 8.9 - * Redistribution and use in source and binary forms, with or without 8.10 - * modification, are permitted provided that the following conditions are met: 8.11 - * 8.12 - * 1. Redistributions of source code must retain the above copyright 8.13 - * notice, this list of conditions and the following disclaimer. 8.14 - * 8.15 - * 2. Redistributions in binary form must reproduce the above copyright 8.16 - * notice, this list of conditions and the following disclaimer in the 8.17 - * documentation and/or other materials provided with the distribution. 8.18 - * 8.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 8.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 8.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 8.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 8.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 8.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 8.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 8.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 8.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 8.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 8.29 - * POSSIBILITY OF SUCH DAMAGE. 8.30 - * 8.31 - */ 8.32 -package de.uapcore.lightpit.entities; 8.33 - 8.34 -import java.sql.Connection; 8.35 -import java.sql.ResultSet; 8.36 -import java.sql.SQLException; 8.37 -import java.sql.Statement; 8.38 -import java.util.ArrayList; 8.39 -import java.util.List; 8.40 -import java.util.Optional; 8.41 - 8.42 -public abstract class UserDao { 8.43 - 8.44 - /** 8.45 - * Maps SQL columns to POJO fields. 8.46 - * @param result the database result set 8.47 - * @param user the POJO 8.48 - * @throws SQLException 8.49 - */ 8.50 - protected void mapColumns(ResultSet result, User user) throws SQLException { 8.51 - user.setUserID(result.getInt("userid")); 8.52 - user.setUsername(result.getString("username")); 8.53 - user.setGivenname(Optional.ofNullable(result.getString("givenname"))); 8.54 - user.setLastname(Optional.ofNullable(result.getString("lastname"))); 8.55 - } 8.56 - 8.57 - /** 8.58 - * Returns a list of all users ordered by their username. 8.59 - * 8.60 - * Does not return reserved system users with negative user IDs. 8.61 - * 8.62 - * @param conn the connection to use 8.63 - * @return a list of all users 8.64 - * @throws SQLException 8.65 - */ 8.66 - public List<User> listAll(Connection conn) throws SQLException { 8.67 - List<User> list = new ArrayList<>(); 8.68 - try ( 8.69 - Statement stmt = conn.createStatement(); 8.70 - ResultSet result = stmt.executeQuery( 8.71 - "SELECT * FROM lpitcore_user WHERE userid >= 0 ORDER BY username")) { 8.72 - while (result.next()) { 8.73 - final User user = new User(); 8.74 - mapColumns(result, user); 8.75 - list.add(user); 8.76 - } 8.77 - } 8.78 - return list; 8.79 - } 8.80 -}
9.1 --- a/src/main/java/de/uapcore/lightpit/modules/ModuleManagerModule.java Sat May 09 14:26:31 2020 +0200 9.2 +++ b/src/main/java/de/uapcore/lightpit/modules/ModuleManagerModule.java Sat May 09 14:37:15 2020 +0200 9.3 @@ -28,26 +28,23 @@ 9.4 */ 9.5 package de.uapcore.lightpit.modules; 9.6 9.7 -import de.uapcore.lightpit.LightPITModule; 9.8 -import de.uapcore.lightpit.AbstractLightPITServlet; 9.9 -import de.uapcore.lightpit.HttpMethod; 9.10 +import de.uapcore.lightpit.*; 9.11 import de.uapcore.lightpit.LightPITModule.ELProxy; 9.12 -import de.uapcore.lightpit.RequestMapping; 9.13 -import de.uapcore.lightpit.ResponseType; 9.14 -import de.uapcore.lightpit.entities.CoreDAOFactory; 9.15 +import de.uapcore.lightpit.dao.CoreDAOFactory; 9.16 import de.uapcore.lightpit.entities.Module; 9.17 +import org.slf4j.Logger; 9.18 +import org.slf4j.LoggerFactory; 9.19 + 9.20 +import javax.servlet.annotation.WebServlet; 9.21 +import javax.servlet.http.HttpServletRequest; 9.22 +import javax.servlet.http.HttpServletResponse; 9.23 +import javax.sql.DataSource; 9.24 import java.io.IOException; 9.25 import java.sql.Connection; 9.26 import java.sql.SQLException; 9.27 import java.util.List; 9.28 import java.util.Map; 9.29 import java.util.Optional; 9.30 -import javax.servlet.annotation.WebServlet; 9.31 -import javax.servlet.http.HttpServletRequest; 9.32 -import javax.servlet.http.HttpServletResponse; 9.33 -import javax.sql.DataSource; 9.34 -import org.slf4j.Logger; 9.35 -import org.slf4j.LoggerFactory; 9.36 9.37 /** 9.38 * Entry point for the application.