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

Sat, 09 May 2020 14:26:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 May 2020 14:26:31 +0200
changeset 29
27a0fdd7bca7
parent 27
src/java/de/uapcore/lightpit/entities/ModuleDao.java@1f2a96efa69f
permissions
-rw-r--r--

converts to maven project

universe@21 1 /*
universe@21 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@21 3 *
universe@24 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@21 5 *
universe@21 6 * Redistribution and use in source and binary forms, with or without
universe@21 7 * modification, are permitted provided that the following conditions are met:
universe@21 8 *
universe@21 9 * 1. Redistributions of source code must retain the above copyright
universe@21 10 * notice, this list of conditions and the following disclaimer.
universe@21 11 *
universe@21 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@21 13 * notice, this list of conditions and the following disclaimer in the
universe@21 14 * documentation and/or other materials provided with the distribution.
universe@21 15 *
universe@21 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@21 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@21 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@21 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@21 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@21 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@21 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@21 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@21 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@21 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@21 26 * POSSIBILITY OF SUCH DAMAGE.
universe@21 27 *
universe@21 28 */
universe@21 29 package de.uapcore.lightpit.entities;
universe@21 30
universe@21 31 import de.uapcore.lightpit.LightPITModule;
universe@21 32 import java.sql.Connection;
universe@21 33 import java.sql.PreparedStatement;
universe@21 34 import java.sql.ResultSet;
universe@21 35 import java.sql.SQLException;
universe@21 36 import java.sql.Statement;
universe@21 37 import java.util.ArrayList;
universe@21 38 import java.util.List;
universe@21 39 import java.util.Map;
universe@21 40 import java.util.Set;
universe@21 41
universe@21 42 public abstract class ModuleDao {
universe@21 43
universe@21 44 /**
universe@26 45 * Maps database columns to POJO fields.
universe@26 46 * @param result the database result set
universe@26 47 * @param mod the POJO
universe@26 48 * @throws SQLException
universe@26 49 */
universe@26 50 protected void mapColumns(ResultSet result, Module mod) throws SQLException {
universe@26 51 mod.setModID(result.getInt("modid"));
universe@26 52 mod.setClassname(result.getString("classname"));
universe@26 53 mod.setVisible(result.getBoolean("visible"));
universe@26 54 }
universe@26 55
universe@26 56
universe@26 57 /**
universe@21 58 * Must return a prepared statement for a single object query with the specified properties.
universe@21 59 *
universe@21 60 * <ul>
universe@21 61 * <li>Parameter 1: classname</li>
universe@21 62 * <li>Result field 1: visible</li>
universe@21 63 * </ul>
universe@21 64 *
universe@21 65 * @param conn the connection to use
universe@21 66 * @return the prepared statement
universe@21 67 * @throws SQLException
universe@21 68 */
universe@21 69 protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException {
universe@21 70 return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?");
universe@21 71 }
universe@21 72
universe@21 73 /**
universe@21 74 * Must return a prepared statement for insertion with the specified properties.
universe@21 75 *
universe@21 76 * <ul>
universe@21 77 * <li>Parameter 1: classname</li>
universe@21 78 * <li>Parameter 2: visible</li>
universe@21 79 * </ul>
universe@21 80 *
universe@21 81 * @param conn the connection to use
universe@21 82 * @return the prepared statement
universe@21 83 * @throws SQLException
universe@21 84 */
universe@21 85 protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
universe@21 86 return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
universe@21 87 }
universe@21 88
universe@21 89 /**
universe@27 90 * Synchronizes a set of registered module classes with the database.
universe@21 91 *
universe@21 92 * Inserts module classes which are not known to the database and sets them to be visible by default.
universe@21 93 * Module classes known to the database, which are not in the given set, are ignored.
universe@21 94 *
universe@21 95 * @param conn the connection to use
universe@21 96 * @param moduleSet the module set to synchronize
universe@21 97 * @throws SQLException
universe@21 98 */
universe@27 99 public final void syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
universe@21 100
universe@21 101 PreparedStatement
universe@21 102 check = moduleCheckStatement(conn),
universe@21 103 insert = moduleInsertStatement(conn);
universe@21 104 insert.setBoolean(2, true);
universe@21 105 // update/delete not required, we do this in the module management UI
universe@21 106
universe@27 107 for (Map.Entry<String, LightPITModule> modEntry : moduleSet) {
universe@27 108 if (modEntry.getValue().systemModule()) continue;
universe@21 109
universe@27 110 check.setString(1, modEntry.getKey());
universe@21 111 try (ResultSet r = check.executeQuery()) {
universe@27 112 if (!r.next()) {
universe@27 113 insert.setString(1, modEntry.getKey());
universe@21 114 insert.executeUpdate();
universe@21 115 }
universe@21 116 }
universe@21 117 }
universe@21 118 }
universe@21 119
universe@21 120 /**
universe@21 121 * Returns a list of all modules known by the database.
universe@21 122 *
universe@21 123 * Keep in mind, that system modules are never known to the database.
universe@21 124 *
universe@21 125 * @param conn the connection to use
universe@21 126 * @return a list of all modules known by the database
universe@21 127 * @throws SQLException
universe@21 128 */
universe@21 129 public List<Module> listAll(Connection conn) throws SQLException {
universe@21 130 List<Module> list = new ArrayList<>();
universe@27 131 try (Statement stmt = conn.createStatement();
universe@21 132 ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) {
universe@21 133 while (result.next()) {
universe@21 134 final Module mod = new Module();
universe@26 135 mapColumns(result, mod);
universe@21 136 list.add(mod);
universe@21 137 }
universe@21 138 }
universe@21 139 return list;
universe@21 140 }
universe@21 141 }

mercurial