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

changeset 34
824d4042c857
parent 33
fd8c40ff78c3
--- a/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java	Sat May 09 15:19:21 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java	Sat May 09 17:01:29 2020 +0200
@@ -1,8 +1,8 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- * 
+ *
  * Copyright 2018 Mike Becker. All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
  *
@@ -24,120 +24,29 @@
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
- * 
+ *
  */
 package de.uapcore.lightpit.dao;
 
 import de.uapcore.lightpit.LightPITModule;
 import de.uapcore.lightpit.entities.Module;
 
-import java.sql.*;
-import java.util.ArrayList;
-import java.util.List;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.Map;
 import java.util.Set;
 
-public class ModuleDao {
-
-    /**
-     * Maps database columns to POJO fields.
-     *
-     * @param result the database result set
-     * @param mod    the POJO
-     * @throws SQLException on any kind of SQL errors
-     */
-    protected void mapColumns(ResultSet result, Module mod) throws SQLException {
-        mod.setModID(result.getInt("modid"));
-        mod.setClassname(result.getString("classname"));
-        mod.setVisible(result.getBoolean("visible"));
-        mod.setPriority(result.getInt("priority"));
-    }
-
+public interface ModuleDao extends GenericDao<Module> {
 
     /**
-     * Must return a prepared statement for a single object query with the specified properties.
-     *
-     * <ul>
-     * <li>Parameter 1: classname</li>
-     * <li>Result field 1: visible</li>
-     * </ul>
-     *
-     * @param conn the connection to use
-     * @return the prepared statement
-     * @throws SQLException on any kind of SQL errors
-     */
-    protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException {
-        return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?");
-    }
-
-    /**
-     * Must return a prepared statement for insertion with the specified properties.
-     *
-     * <ul>
-     * <li>Parameter 1: classname</li>
-     * <li>Parameter 2: visible</li>
-     * <li>Parameter 3: priority</li>
-     * </ul>
-     *
-     * @param conn the connection to use
-     * @return the prepared statement
-     * @throws SQLException on any kind of SQL errors
-     */
-    protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
-        return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible, priority) VALUES (?, ?, ?)");
-    }
-    
-    /**
      * Synchronizes a set of registered module classes with the database.
-     *
+     * <p>
      * Inserts module classes which are not known to the database and sets them to be visible by default.
      * Module classes known to the database, which are not in the given set, are ignored.
      *
-     * @param conn the connection to use
+     * @param conn      the connection to use
      * @param moduleSet the module set to synchronize
      * @throws SQLException on any kind of SQL errors
      */
-    public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
-                
-        PreparedStatement
-                check = moduleCheckStatement(conn),
-                insert = moduleInsertStatement(conn);
-        insert.setBoolean(2, true);
-        // update/delete not required, we do this in the module management UI
-
-        for (Map.Entry<String, LightPITModule> modEntry : moduleSet) {
-            if (modEntry.getValue().systemModule()) continue;
-
-            check.setString(1, modEntry.getKey());
-            try (ResultSet r = check.executeQuery()) {
-                if (!r.next()) {
-                    insert.setString(1, modEntry.getKey());
-                    insert.setInt(3, modEntry.getValue().defaultPriority());
-                    insert.executeUpdate();
-                }
-            }
-        }
-    }
-
-    /**
-     * Returns a list of all modules known by the database.
-     *
-     * Keep in mind, that system modules are never known to the database.
-     *
-     * @param conn the connection to use
-     * @return a list of all modules known by the database
-     * @throws SQLException on any kind of SQL errors
-     */
-    public List<Module> listAll(Connection conn) throws SQLException {
-        List<Module> list = new ArrayList<>();
-        try (Statement stmt = conn.createStatement();
-                ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) {
-            while (result.next()) {
-                final Module mod = new Module();
-                mapColumns(result, mod);
-                list.add(mod);
-            }
-        }
-        return list;
-    }
+    void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException;
 }

mercurial