/* * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. */ package net.curisit.securis.db.common; import java.util.Date; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.persistence.EntityManager; import net.curisit.integrity.commons.Utils; import net.curisit.securis.db.Settings; import net.curisit.securis.ioc.EntityManagerProvider; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * SystemParams *

* 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; // -------------------- Read API -------------------- /** * getParam

* 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); } /** * getParamAsInt

* 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); } /** * getParamAsInt

* 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); } /** * getParamAsDate

* 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); } /** * getParamAsBool

* 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); } /** * getParamAsBool

* 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); } /** * getParamAsDouble

* 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); } /** * getParam

* 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 -------------------- /** * setParam

* 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); p.setValue(value); em.persist(p); } else { p.setValue(value); em.merge(p); } em.getTransaction().commit(); } catch (Exception ex) { em.getTransaction().rollback(); throw ex; } } /** * setParam

* Save parameter as ISO date string. * * @param key * @param value */ public void setParam(String key, Date value) { setParam(key, Utils.toIsoFormat(value)); } /** * setParam

* Save parameter as integer. * * @param key * @param value */ public void setParam(String key, int value) { setParam(key, String.valueOf(value)); } /** * setParam

* Save parameter as boolean. * * @param key * @param value */ public void setParam(String key, boolean value) { setParam(key, String.valueOf(value)); } /** * setParam

* Save parameter as double. * * @param key * @param value */ public void setParam(String key, double value) { setParam(key, String.valueOf(value)); } /** * removeParam

* 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(); try { Settings p = em.find(Settings.class, key); if (p != null) { em.remove(p); } em.getTransaction().commit(); } catch (Exception ex) { em.getTransaction().rollback(); throw ex; } } /** * Keys *

* Centralized constants for parameter keys (client/common/server). */ public static class Keys { // 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"; public static final String CONFIG_CLIENT_LICENSE = "config.client.license"; public static final String CONFIG_CLIENT_MAC = "config.client.mac"; public static final String CONFIG_CLIENT_MACS = "config.client.macs"; public static final String CONFIG_CLIENT_CPU_NAME = "config.client.cpu_name"; public static final String CONFIG_CLIENT_HOURES_AUTO_SYNC = "config.client.houres_auto_sync"; public static final String CONFIG_CLIENT_HOURES_RETRY_AUTO_SYNC = "config.client.houres_retry_auto_sync"; public static final String CONFIG_CLIENT_GS_HOST = "config.client.gs_host"; public static final String CONFIG_CLIENT_GS_PORT = "config.client.gs_port"; // 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"; public static final String CONFIG_COMMON_SYNC_TIME_THRESHOLD = "config.common.sync.time_threshols"; 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"; // 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"; public static final String CONFIG_SERVER_LICENSE_MAX_INSTANCES = "config.server.license.max_instances"; public static final String CONFIG_SERVER_LICENSE_MAX_USERS = "config.server.license.max_users"; public static final String CONFIG_SERVER_LICENSE_MAX_TIME_THRESHOLD = "config.server.license.max_time_threshols"; public static final String CONFIG_SERVER_LICENSE_EXTENDED_MODE = "config.server.license.extended_mode"; public static final String CONFIG_SERVER_LICENSE_MAX_EVENTS = "config.server.license.max_events"; public static final String CONFIG_SERVER_LICENSE_MAX_WELL_LIFE_LINES = "config.server.license.max_well_life_lines"; 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"; } }