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

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

mercurial