Joaquín Reñé
2026-03-27 4ee50e257b32f6ec0f72907305d1f2b1212808a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* 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
* <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;
    // -------------------- Read API --------------------
    /**
    * 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);
    }
    /**
    * 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);
    }
    /**
    * 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);
    }
    /**
    * 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);
    }
    /**
    * 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);
    }
    /**
    * 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);
    }
    /**
    * 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);
    }
    /**
    * 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 --------------------
    /**
    * 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);
                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<p>
     * Save parameter as ISO date string. 
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, Date value) {
        setParam(key, Utils.toIsoFormat(value));
    }
    /** 
     * setParam<p>
     * Save parameter as integer. 
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, int value) {
        setParam(key, String.valueOf(value));
    }
    /** 
     * setParam<p>
     * Save parameter as boolean. 
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, boolean value) {
        setParam(key, String.valueOf(value));
    }
    /** 
     * setParam<p>
     * Save parameter as double. 
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, double value) {
        setParam(key, String.valueOf(value));
    }
    /**
    * 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();
        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
    * <p>
    * 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";
    }
}