From 146a0fb8b0e90f9196e569152f649baf60d6cc8f Mon Sep 17 00:00:00 2001
From: Joaquín Reñé <jrene@curisit.net>
Date: Tue, 07 Oct 2025 14:52:57 +0000
Subject: [PATCH] #4410 - Comments on classes

---
 securis/src/main/java/net/curisit/securis/db/common/SystemParams.java |  190 ++++++++++++++++++++++++++++------------------
 1 files changed, 115 insertions(+), 75 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/db/common/SystemParams.java b/securis/src/main/java/net/curisit/securis/db/common/SystemParams.java
index 0e745f2..a21a5ca 100644
--- a/securis/src/main/java/net/curisit/securis/db/common/SystemParams.java
+++ b/securis/src/main/java/net/curisit/securis/db/common/SystemParams.java
@@ -1,3 +1,6 @@
+/*
+* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
+*/
 package net.curisit.securis.db.common;
 
 import java.util.Date;
@@ -13,120 +16,148 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+/**
+* SystemParams
+* <p>
+* Simple façade to read/write application-wide parameters stored in the
+* {@code settings} table (key/value + timestamps).
+*
+* Features:
+* - Typed getters: {@code String}, {@code Integer}, {@code Boolean}, {@code Double}, {@code Date}.
+* - Default value overloads.
+* - Upsert semantics in {@link #setParam(String, String)} and typed variants.
+* - Removal with {@link #removeParam(String)}.
+*
+* Transaction note:
+* - Each write method starts and commits its own transaction. Rollback is invoked
+*   only on exceptions.
+*
+* @author JRA
+* Last reviewed by JRA on Oct 7, 2025.
+*/
 @ApplicationScoped
 public class SystemParams {
 
     @SuppressWarnings("unused")
     private static final Logger LOG = LogManager.getLogger(SystemParams.class);
 
-    @Inject
-    private EntityManagerProvider emProvider;
+    @Inject private EntityManagerProvider emProvider;
+
+    // -------------------- Read API --------------------
 
     /**
-     * Returns the system parameter value for given key
-     * 
-     * @param key
-     * @return the value of the param or null if it doesn't exist
-     */
+    * getParam<p>
+    * Get raw string value or {@code null} when absent.
+    *
+    * @param key setting key
+    * @return string value or null
+    */
     public String getParam(String key) {
         return getParam(key, null);
     }
 
     /**
-     * Returns the system parameter as int value for given key
-     * 
-     * @param key
-     * @return the value of the param or null if it doesn't exist
-     */
+    * getParamAsInt<p>
+    * Get value as Integer or null when absent.
+    *
+    * @param key setting key
+    * @return integer value or null
+    */
     public Integer getParamAsInt(String key) {
         String value = getParam(key, null);
         return value == null ? null : Integer.parseInt(value);
     }
 
     /**
-     * 
-     * @param key
-     * @param defaulValue
-     *            returned if key doesn't exist in params table
-     * @return
-     */
+    * getParamAsInt<p>
+    * Get value as Integer with default.
+    *
+    * @param key setting key
+    * @param defaulValue returned if key is missing
+    * @return integer value or default
+    */
     public Integer getParamAsInt(String key, Integer defaulValue) {
         String value = getParam(key, null);
         return value == null ? defaulValue : Integer.parseInt(value);
     }
 
     /**
-     * Returns the system parameter as Date value for given key
-     * 
-     * @param key
-     * @return the value of the param or null if it doesn't exist
-     */
+    * getParamAsDate<p>
+    * Get value parsed from ISO-8601.
+    *
+    * @param key setting key
+    * @return date value or null
+    */
     public Date getParamAsDate(String key) {
         String value = getParam(key, null);
         return value == null ? null : Utils.toDateFromIso(value);
     }
 
     /**
-     * Returns the system parameter as boolean value for given key
-     * 
-     * @param key
-     * @return the value of the param or null if it doesn't exist
-     */
+    * getParamAsBool<p>
+    * Get value parsed as boolean.
+    *
+    * @param key setting key
+    * @return boolean value or null
+    */
     public Boolean getParamAsBool(String key) {
         String value = getParam(key, null);
         return value == null ? null : Boolean.parseBoolean(value);
     }
 
     /**
-     * 
-     * @param key
-     * @param defaulValue
-     *            returned if key doesn't exist in params table
-     * @return
-     */
+    * getParamAsBool<p>
+    * Get value parsed as boolean with default.
+    *
+    * @param key setting key
+    * @param defaulValue default when missing
+    * @return boolean value or default
+    */
     public Boolean getParamAsBool(String key, boolean defaulValue) {
         String value = getParam(key, null);
         return value == null ? defaulValue : Boolean.parseBoolean(value);
     }
 
     /**
-     * Returns the system parameter as boolean value for given key
-     * 
-     * @param key
-     * @return the value of the param or null if it doesn't exist
-     */
+    * getParamAsDouble<p>
+    * Get value parsed as double.
+    *
+    * @param key setting key
+    * @return double value or null
+    */
     public Double getParamAsDouble(String key) {
         String value = getParam(key, null);
         return value == null ? null : Double.parseDouble(value);
     }
 
     /**
-     * Returns the system parameter value for given key
-     * 
-     * @param key
-     * @param defaultValue
-     *            returned if key doesn't exist in params table
-     * @return
-     */
+    * getParam<p>
+    * Get raw string value or a default when missing.
+    *
+    * @param key setting key
+    * @param defaultValue default when missing
+    * @return value or default
+    */
     public String getParam(String key, String defaultValue) {
         EntityManager em = emProvider.getEntityManager();
         Settings p = em.find(Settings.class, key);
         return p == null ? defaultValue : p.getValue();
     }
 
+    // -------------------- Write API --------------------
+
     /**
-     * Returns the system parameter value passed as parameter to method
-     * 
-     * @param key
-     * @param defaultValue
-     * @return
-     */
+    * setParam<p>
+    * Upsert a parameter as string.
+    *
+    * @param key setting key
+    * @param value string value
+    */
     public void setParam(String key, String value) {
         EntityManager em = emProvider.getEntityManager();
         em.getTransaction().begin();
         try {
             Settings p = em.find(Settings.class, key);
-
             if (p == null) {
                 p = new Settings();
                 p.setKey(key);
@@ -136,15 +167,16 @@
                 p.setValue(value);
                 em.merge(p);
             }
-            em.flush();
             em.getTransaction().commit();
-        } finally {
+        } catch (Exception ex) {
             em.getTransaction().rollback();
+            throw ex;
         }
     }
 
-    /**
-     * Save a parameter as a Date
+    /** 
+     * setParam<p>
+     * Save parameter as ISO date string. 
      * 
      * @param key
      * @param value
@@ -153,8 +185,9 @@
         setParam(key, Utils.toIsoFormat(value));
     }
 
-    /**
-     * Save a parameter as a integer
+    /** 
+     * setParam<p>
+     * Save parameter as integer. 
      * 
      * @param key
      * @param value
@@ -163,8 +196,9 @@
         setParam(key, String.valueOf(value));
     }
 
-    /**
-     * Save a parameter as a boolean
+    /** 
+     * setParam<p>
+     * Save parameter as boolean. 
      * 
      * @param key
      * @param value
@@ -173,8 +207,9 @@
         setParam(key, String.valueOf(value));
     }
 
-    /**
-     * Save a parameter as a double
+    /** 
+     * setParam<p>
+     * Save parameter as double. 
      * 
      * @param key
      * @param value
@@ -184,11 +219,11 @@
     }
 
     /**
-     * Remove a parameter from params table
-     * 
-     * @param key
-     * @return
-     */
+    * removeParam<p>
+    * Delete a parameter by key (no-op if missing).
+    *
+    * @param key setting key
+    */
     public void removeParam(String key) {
         EntityManager em = emProvider.getEntityManager();
         em.getTransaction().begin();
@@ -198,13 +233,19 @@
                 em.remove(p);
             }
             em.getTransaction().commit();
-        } finally {
+        } catch (Exception ex) {
             em.getTransaction().rollback();
+            throw ex;
         }
     }
 
+    /**
+    * Keys
+    * <p>
+    * Centralized constants for parameter keys (client/common/server).
+    */
     public static class Keys {
-        // Keys used in basic app
+        // Client app keys
         public static final String CONFIG_CLIENT_HOST = "config.client.host";
         public static final String CONFIG_CLIENT_PORT = "config.client.port";
         public static final String CONFIG_CLIENT_LAST_UPDATE = "config.client.last_update";
@@ -217,9 +258,9 @@
         public static final String CONFIG_CLIENT_GS_HOST = "config.client.gs_host";
         public static final String CONFIG_CLIENT_GS_PORT = "config.client.gs_port";
 
-        // Keys used in both app
-        public static final String CONFIG_COMMON_CUSTOMER_CODE = "config.common.customer_code"; // BP
-        public static final String CONFIG_COMMON_CS_CODE = "config.common.cs_code"; // 0000
+        // Shared keys
+        public static final String CONFIG_COMMON_CUSTOMER_CODE = "config.common.customer_code";
+        public static final String CONFIG_COMMON_CS_CODE = "config.common.cs_code";
         public static final String CONFIG_COMMON_USERS_VERSION = "config.common.user_version";
         public static final String CONFIG_COMMON_SETTINGS_VERSION = "config.common.settings_version";
         public static final String CONFIG_COMMON_DATASET_VERSION = "config.common.dataset_version";
@@ -227,7 +268,7 @@
         public static final String CONFIG_COMMON_TIMEOUT_SESSION_BA = "config.common.timeout_session_ba";
         public static final String CONFIG_COMMON_TIMEOUT_SESSION_CS = "config.common.timeout_session_cs";
 
-        // Keys used in server app
+        // Server app keys
         public static final String CONFIG_SERVER_LICENSE_EXPIRATION = "config.server.license.expiation";
         public static final String CONFIG_SERVER_MAX_INSTANCES = "config.server.max_instances";
         public static final String CONFIG_SERVER_MAX_USERS = "config.server.max_users";
@@ -240,5 +281,4 @@
         public static final String CONFIG_SERVER_CREATE_DATASET = "config.server.create_dataset_in_next_startup";
         public static final String CONFIG_SERVER_PORT = "config.server.port";
     }
-
 }

--
Gitblit v1.3.2