universe@21: /* universe@21: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@21: * universe@21: * Copyright 2017 Mike Becker. All rights reserved. universe@21: * universe@21: * Redistribution and use in source and binary forms, with or without universe@21: * modification, are permitted provided that the following conditions are met: universe@21: * universe@21: * 1. Redistributions of source code must retain the above copyright universe@21: * notice, this list of conditions and the following disclaimer. universe@21: * universe@21: * 2. Redistributions in binary form must reproduce the above copyright universe@21: * notice, this list of conditions and the following disclaimer in the universe@21: * documentation and/or other materials provided with the distribution. universe@21: * universe@21: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@21: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@21: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@21: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@21: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@21: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@21: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@21: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@21: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@21: * POSSIBILITY OF SUCH DAMAGE. universe@21: * universe@21: */ universe@21: package de.uapcore.lightpit.entities; universe@21: universe@21: import de.uapcore.lightpit.LightPITModule; universe@21: import java.sql.Connection; universe@21: import java.sql.PreparedStatement; universe@21: import java.sql.ResultSet; universe@21: import java.sql.SQLException; universe@21: import java.sql.Statement; universe@21: import java.util.AbstractMap; universe@21: import java.util.ArrayList; universe@21: import java.util.List; universe@21: import java.util.Map; universe@21: import java.util.Set; universe@21: universe@21: public abstract class ModuleDao { universe@21: universe@21: /** universe@21: * Must return a prepared statement for a single object query with the specified properties. universe@21: * universe@21: * universe@21: * universe@21: * @param conn the connection to use universe@21: * @return the prepared statement universe@21: * @throws SQLException universe@21: */ universe@21: protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException { universe@21: return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?"); universe@21: } universe@21: universe@21: /** universe@21: * Must return a prepared statement for insertion with the specified properties. universe@21: * universe@21: * universe@21: * universe@21: * @param conn the connection to use universe@21: * @return the prepared statement universe@21: * @throws SQLException universe@21: */ universe@21: protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException { universe@21: return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)"); universe@21: } universe@21: universe@21: /** universe@21: * Synchronizes a set of registered module classes with the database and gives a list of visible modules in return. universe@21: * universe@21: * Inserts module classes which are not known to the database and sets them to be visible by default. universe@21: * Module classes known to the database, which are not in the given set, are ignored. universe@21: * universe@21: * @param conn the connection to use universe@21: * @param moduleSet the module set to synchronize universe@21: * @return a list of elements from the given set, which should be visible in the menu universe@21: * @throws SQLException universe@21: */ universe@21: public final List> universe@21: syncRegisteredModuleClasses(Connection conn, Set> moduleSet) throws SQLException { universe@21: universe@21: PreparedStatement universe@21: check = moduleCheckStatement(conn), universe@21: insert = moduleInsertStatement(conn); universe@21: insert.setBoolean(2, true); universe@21: // update/delete not required, we do this in the module management UI universe@21: universe@21: final List> visibleModules = new ArrayList<>(); universe@21: universe@21: for (Map.Entry mod : moduleSet) { universe@21: if (mod.getValue().systemModule()) continue; universe@21: universe@21: check.setString(1, mod.getKey()); universe@21: try (ResultSet r = check.executeQuery()) { universe@21: final boolean visible; universe@21: if (r.next()) { universe@21: visible = r.getBoolean(1); universe@21: } else { universe@21: insert.setString(1, mod.getKey()); universe@21: insert.executeUpdate(); universe@21: visible = !mod.getValue().menuKey().isEmpty(); universe@21: } universe@21: if (visible) { universe@21: visibleModules.add(new AbstractMap.SimpleEntry<>( universe@21: mod.getKey(), universe@21: mod.getValue() universe@21: )); universe@21: } universe@21: } universe@21: } universe@21: return visibleModules; universe@21: } universe@21: universe@21: /** universe@21: * Returns a list of all modules known by the database. universe@21: * universe@21: * Keep in mind, that system modules are never known to the database. universe@21: * universe@21: * @param conn the connection to use universe@21: * @return a list of all modules known by the database universe@21: * @throws SQLException universe@21: */ universe@21: public List listAll(Connection conn) throws SQLException { universe@21: List list = new ArrayList<>(); universe@21: try ( universe@21: Statement stmt = conn.createStatement(); universe@21: ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) { universe@21: while (result.next()) { universe@21: final Module mod = new Module(); universe@21: mod.setModID(result.getInt("modid")); universe@21: mod.setClassname(result.getString("classname")); universe@21: mod.setVisible(result.getBoolean("visible")); universe@21: list.add(mod); universe@21: } universe@21: } universe@21: return list; universe@21: } universe@21: }