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

Sun, 08 Apr 2018 14:40:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 08 Apr 2018 14:40:57 +0200
changeset 24
8137ec335416
parent 21
b213fef2539e
child 26
65d5a0ca49ae
permissions
-rw-r--r--

updates copyright header

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.AbstractMap;
universe@21 38 import java.util.ArrayList;
universe@21 39 import java.util.List;
universe@21 40 import java.util.Map;
universe@21 41 import java.util.Set;
universe@21 42
universe@21 43 public abstract class ModuleDao {
universe@21 44
universe@21 45 /**
universe@21 46 * Must return a prepared statement for a single object query with the specified properties.
universe@21 47 *
universe@21 48 * <ul>
universe@21 49 * <li>Parameter 1: classname</li>
universe@21 50 * <li>Result field 1: visible</li>
universe@21 51 * </ul>
universe@21 52 *
universe@21 53 * @param conn the connection to use
universe@21 54 * @return the prepared statement
universe@21 55 * @throws SQLException
universe@21 56 */
universe@21 57 protected PreparedStatement moduleCheckStatement(Connection conn) throws SQLException {
universe@21 58 return conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?");
universe@21 59 }
universe@21 60
universe@21 61 /**
universe@21 62 * Must return a prepared statement for insertion with the specified properties.
universe@21 63 *
universe@21 64 * <ul>
universe@21 65 * <li>Parameter 1: classname</li>
universe@21 66 * <li>Parameter 2: visible</li>
universe@21 67 * </ul>
universe@21 68 *
universe@21 69 * @param conn the connection to use
universe@21 70 * @return the prepared statement
universe@21 71 * @throws SQLException
universe@21 72 */
universe@21 73 protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
universe@21 74 return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
universe@21 75 }
universe@21 76
universe@21 77 /**
universe@21 78 * Synchronizes a set of registered module classes with the database and gives a list of visible modules in return.
universe@21 79 *
universe@21 80 * Inserts module classes which are not known to the database and sets them to be visible by default.
universe@21 81 * Module classes known to the database, which are not in the given set, are ignored.
universe@21 82 *
universe@21 83 * @param conn the connection to use
universe@21 84 * @param moduleSet the module set to synchronize
universe@21 85 * @return a list of elements from the given set, which should be visible in the menu
universe@21 86 * @throws SQLException
universe@21 87 */
universe@21 88 public final List<Map.Entry<String, LightPITModule>>
universe@21 89 syncRegisteredModuleClasses(Connection conn, Set<Map.Entry<String, LightPITModule>> moduleSet) throws SQLException {
universe@21 90
universe@21 91 PreparedStatement
universe@21 92 check = moduleCheckStatement(conn),
universe@21 93 insert = moduleInsertStatement(conn);
universe@21 94 insert.setBoolean(2, true);
universe@21 95 // update/delete not required, we do this in the module management UI
universe@21 96
universe@21 97 final List<Map.Entry<String, LightPITModule>> visibleModules = new ArrayList<>();
universe@21 98
universe@21 99 for (Map.Entry<String, LightPITModule> mod : moduleSet) {
universe@21 100 if (mod.getValue().systemModule()) continue;
universe@21 101
universe@21 102 check.setString(1, mod.getKey());
universe@21 103 try (ResultSet r = check.executeQuery()) {
universe@21 104 final boolean visible;
universe@21 105 if (r.next()) {
universe@21 106 visible = r.getBoolean(1);
universe@21 107 } else {
universe@21 108 insert.setString(1, mod.getKey());
universe@21 109 insert.executeUpdate();
universe@21 110 visible = !mod.getValue().menuKey().isEmpty();
universe@21 111 }
universe@21 112 if (visible) {
universe@21 113 visibleModules.add(new AbstractMap.SimpleEntry<>(
universe@21 114 mod.getKey(),
universe@21 115 mod.getValue()
universe@21 116 ));
universe@21 117 }
universe@21 118 }
universe@21 119 }
universe@21 120 return visibleModules;
universe@21 121 }
universe@21 122
universe@21 123 /**
universe@21 124 * Returns a list of all modules known by the database.
universe@21 125 *
universe@21 126 * Keep in mind, that system modules are never known to the database.
universe@21 127 *
universe@21 128 * @param conn the connection to use
universe@21 129 * @return a list of all modules known by the database
universe@21 130 * @throws SQLException
universe@21 131 */
universe@21 132 public List<Module> listAll(Connection conn) throws SQLException {
universe@21 133 List<Module> list = new ArrayList<>();
universe@21 134 try (
universe@21 135 Statement stmt = conn.createStatement();
universe@21 136 ResultSet result = stmt.executeQuery("SELECT * FROM lpitcore_module")) {
universe@21 137 while (result.next()) {
universe@21 138 final Module mod = new Module();
universe@21 139 mod.setModID(result.getInt("modid"));
universe@21 140 mod.setClassname(result.getString("classname"));
universe@21 141 mod.setVisible(result.getBoolean("visible"));
universe@21 142 list.add(mod);
universe@21 143 }
universe@21 144 }
universe@21 145 return list;
universe@21 146 }
universe@21 147 }

mercurial