adds module priorities

Sat, 09 May 2020 14:58:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 May 2020 14:58:20 +0200
changeset 31
58f78f0142e8
parent 30
fb30f7b78f9b
child 32
63a31871189e

adds module priorities

setup/postgres/psql_create_tables.sql file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/LightPITModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/ModuleManager.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/dao/ModuleDao.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/entities/Module.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/HomeModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/LanguageModule.java file | annotate | diff | comparison | revisions
src/main/java/de/uapcore/lightpit/modules/ModuleManagerModule.java file | annotate | diff | comparison | revisions
--- a/setup/postgres/psql_create_tables.sql	Sat May 09 14:37:15 2020 +0200
+++ b/setup/postgres/psql_create_tables.sql	Sat May 09 14:58:20 2020 +0200
@@ -4,7 +4,8 @@
 create table lpitcore_module (
     modid       serial          primary key,
     classname   varchar(100)    not null unique,
-    visible     boolean         not null default(true)
+    visible     boolean         not null default(true),
+    priority    integer         not null default(1000)
 );
 
 create table lpitcore_user (
--- a/src/main/java/de/uapcore/lightpit/LightPITModule.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/LightPITModule.java	Sat May 09 14:58:20 2020 +0200
@@ -28,12 +28,8 @@
  */
 package de.uapcore.lightpit;
 
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
 import javax.servlet.annotation.WebServlet;
+import java.lang.annotation.*;
 
 
 /**
@@ -102,26 +98,34 @@
      * @return the properties key
      */
     String titleKey() default "menuLabel";
-    
+
     /**
      * If set to <code>true</code>, this module is always loaded, but never
      * visible in the menu or the Web UI module manager.
-     * 
+     *
      * @return true, if this is a system module
      */
     boolean systemModule() default false;
-    
+
+    /**
+     * Optionally specifies a default priority for this module.
+     * The priority is used to order the menu entries.
+     *
+     * @return an integer priority
+     */
+    int defaultPriority() default 1000;
+
     /**
      * Class representing the annotation.
      * This is necessary, because the EL resolver cannot deal with
      * annotation objects.
-     * 
+     * <p>
      * Note, that only the properties which are interesting for the JSP pages
      * are proxied by this object.
      */
-    public static class ELProxy {
+    class ELProxy {
         private final String bundleBaseName, modulePath, menuKey, titleKey, nameKey, descKey;
-        
+
         public static ELProxy convert(LightPITModule annotation) {
             return new ELProxy(
                     annotation.bundleBaseName(),
--- a/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 09 14:58:20 2020 +0200
@@ -192,20 +192,18 @@
             try (Connection conn = db.getDataSource().get().getConnection()) {
                 final ModuleDao dao = CoreDAOFactory.getModuleDao(db.getSQLDialect());
                 final List<Module> modules = dao.listAll(conn);
-                
-                final List<Menu> menu = modules
-                    .stream()
-                    .filter((mod) -> mod.isVisible())
-                    .collect(Collectors.mapping(
-                            (mod) -> new Menu(
-                                    mod.getClassname(),
-                                    new ResourceKey(
-                                            registeredModules.get(mod.getClassname()).bundleBaseName(),
-                                            registeredModules.get(mod.getClassname()).menuKey()),
-                                    registeredModules.get(mod.getClassname()).modulePath()),
-                            Collectors.toList())
-                    );
-                return menu;
+
+                return modules
+                        .stream()
+                        .filter(Module::isVisible)
+                        .sorted(new Module.PriorityComparator())
+                        .map(mod -> new Menu(
+                                mod.getClassname(),
+                                new ResourceKey(
+                                        registeredModules.get(mod.getClassname()).bundleBaseName(),
+                                        registeredModules.get(mod.getClassname()).menuKey()),
+                                registeredModules.get(mod.getClassname()).modulePath()))
+                        .collect(Collectors.toList());
             } catch (SQLException ex) {
                 LOG.error("Unexpected SQLException when loading the main menu", ex);
                 return Collections.emptyList();
--- a/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/dao/ModuleDao.java	Sat May 09 14:58:20 2020 +0200
@@ -50,6 +50,7 @@
         mod.setModID(result.getInt("modid"));
         mod.setClassname(result.getString("classname"));
         mod.setVisible(result.getBoolean("visible"));
+        mod.setPriority(result.getInt("priority"));
     }
             
     
@@ -68,21 +69,22 @@
     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 
+     * @throws SQLException
      */
     protected PreparedStatement moduleInsertStatement(Connection conn) throws SQLException {
-        return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
+        return conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible, priority) VALUES (?, ?, ?)");
     }
     
     /**
@@ -110,6 +112,7 @@
             try (ResultSet r = check.executeQuery()) {
                 if (!r.next()) {
                     insert.setString(1, modEntry.getKey());
+                    insert.setInt(3, modEntry.getValue().defaultPriority());
                     insert.executeUpdate();
                 }
             }
--- a/src/main/java/de/uapcore/lightpit/entities/Module.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/entities/Module.java	Sat May 09 14:58:20 2020 +0200
@@ -24,17 +24,20 @@
  * 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.entities;
 
 import de.uapcore.lightpit.LightPITModule;
 
+import java.util.Comparator;
+
 public final class Module {
     private int modID;
     private String classname;
     private boolean visible;
-    
+    private int priority;
+
     private LightPITModule.ELProxy annotatedInfos;
 
     public int getModID() {
@@ -61,6 +64,14 @@
         this.visible = visible;
     }
 
+    public int getPriority() {
+        return priority;
+    }
+
+    public void setPriority(int priority) {
+        this.priority = priority;
+    }
+
     public LightPITModule.ELProxy getAnnotatedInfos() {
         return annotatedInfos;
     }
@@ -86,5 +97,18 @@
         } else {
             return this.modID == ((Module) obj).modID;
         }
-    }    
+    }
+
+    public static final class PriorityComparator implements Comparator<Module> {
+
+        @Override
+        public int compare(Module left, Module right) {
+            return Integer.compare(left.priority, right.priority);
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            return o instanceof PriorityComparator;
+        }
+    }
 }
--- a/src/main/java/de/uapcore/lightpit/modules/HomeModule.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/HomeModule.java	Sat May 09 14:58:20 2020 +0200
@@ -28,11 +28,8 @@
  */
 package de.uapcore.lightpit.modules;
 
-import de.uapcore.lightpit.LightPITModule;
-import de.uapcore.lightpit.AbstractLightPITServlet;
-import de.uapcore.lightpit.HttpMethod;
-import de.uapcore.lightpit.RequestMapping;
-import de.uapcore.lightpit.ResponseType;
+import de.uapcore.lightpit.*;
+
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -42,7 +39,8 @@
  */
 @LightPITModule(
         bundleBaseName = "localization.home",
-        modulePath = "home"
+        modulePath = "home",
+        defaultPriority = -10000
 )
 @WebServlet(
         name = "HomeModule",
--- a/src/main/java/de/uapcore/lightpit/modules/LanguageModule.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/LanguageModule.java	Sat May 09 14:58:20 2020 +0200
@@ -28,29 +28,21 @@
  */
 package de.uapcore.lightpit.modules;
 
-import de.uapcore.lightpit.LightPITModule;
-import de.uapcore.lightpit.AbstractLightPITServlet;
-import de.uapcore.lightpit.Constants;
-import de.uapcore.lightpit.Functions;
-import de.uapcore.lightpit.HttpMethod;
+import de.uapcore.lightpit.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import de.uapcore.lightpit.RequestMapping;
-import de.uapcore.lightpit.ResponseType;
-import java.util.ArrayList;
-import java.util.IllformedLocaleException;
-import java.util.List;
-import java.util.Locale;
-import java.util.Optional;
-import javax.servlet.ServletException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.util.*;
 
 
 @LightPITModule(
         bundleBaseName = "localization.language",
-        modulePath = "language"
+        modulePath = "language",
+        defaultPriority = 20000
 )
 @WebServlet(
         name = "LanguageModule",
--- a/src/main/java/de/uapcore/lightpit/modules/ModuleManagerModule.java	Sat May 09 14:37:15 2020 +0200
+++ b/src/main/java/de/uapcore/lightpit/modules/ModuleManagerModule.java	Sat May 09 14:58:20 2020 +0200
@@ -51,7 +51,8 @@
  */
 @LightPITModule(
         bundleBaseName = "localization.modmgmt",
-        modulePath = "modmgmt"
+        modulePath = "modmgmt",
+        defaultPriority = 21000
 )
 @WebServlet(
         name = "ModuleManagerModule",

mercurial