Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
securis/src/main/java/net/curisit/securis/db/common/SystemParams.java
....@@ -1,3 +1,6 @@
1
+/*
2
+* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
3
+*/
14 package net.curisit.securis.db.common;
25
36 import java.util.Date;
....@@ -13,120 +16,148 @@
1316 import org.apache.logging.log4j.LogManager;
1417 import org.apache.logging.log4j.Logger;
1518
19
+/**
20
+* SystemParams
21
+* <p>
22
+* Simple façade to read/write application-wide parameters stored in the
23
+* {@code settings} table (key/value + timestamps).
24
+*
25
+* Features:
26
+* - Typed getters: {@code String}, {@code Integer}, {@code Boolean}, {@code Double}, {@code Date}.
27
+* - Default value overloads.
28
+* - Upsert semantics in {@link #setParam(String, String)} and typed variants.
29
+* - Removal with {@link #removeParam(String)}.
30
+*
31
+* Transaction note:
32
+* - Each write method starts and commits its own transaction. Rollback is invoked
33
+* only on exceptions.
34
+*
35
+* @author JRA
36
+* Last reviewed by JRA on Oct 7, 2025.
37
+*/
1638 @ApplicationScoped
1739 public class SystemParams {
1840
1941 @SuppressWarnings("unused")
2042 private static final Logger LOG = LogManager.getLogger(SystemParams.class);
2143
22
- @Inject
23
- private EntityManagerProvider emProvider;
44
+ @Inject private EntityManagerProvider emProvider;
45
+
46
+ // -------------------- Read API --------------------
2447
2548 /**
26
- * Returns the system parameter value for given key
27
- *
28
- * @param key
29
- * @return the value of the param or null if it doesn't exist
30
- */
49
+ * getParam<p>
50
+ * Get raw string value or {@code null} when absent.
51
+ *
52
+ * @param key setting key
53
+ * @return string value or null
54
+ */
3155 public String getParam(String key) {
3256 return getParam(key, null);
3357 }
3458
3559 /**
36
- * Returns the system parameter as int value for given key
37
- *
38
- * @param key
39
- * @return the value of the param or null if it doesn't exist
40
- */
60
+ * getParamAsInt<p>
61
+ * Get value as Integer or null when absent.
62
+ *
63
+ * @param key setting key
64
+ * @return integer value or null
65
+ */
4166 public Integer getParamAsInt(String key) {
4267 String value = getParam(key, null);
4368 return value == null ? null : Integer.parseInt(value);
4469 }
4570
4671 /**
47
- *
48
- * @param key
49
- * @param defaulValue
50
- * returned if key doesn't exist in params table
51
- * @return
52
- */
72
+ * getParamAsInt<p>
73
+ * Get value as Integer with default.
74
+ *
75
+ * @param key setting key
76
+ * @param defaulValue returned if key is missing
77
+ * @return integer value or default
78
+ */
5379 public Integer getParamAsInt(String key, Integer defaulValue) {
5480 String value = getParam(key, null);
5581 return value == null ? defaulValue : Integer.parseInt(value);
5682 }
5783
5884 /**
59
- * Returns the system parameter as Date value for given key
60
- *
61
- * @param key
62
- * @return the value of the param or null if it doesn't exist
63
- */
85
+ * getParamAsDate<p>
86
+ * Get value parsed from ISO-8601.
87
+ *
88
+ * @param key setting key
89
+ * @return date value or null
90
+ */
6491 public Date getParamAsDate(String key) {
6592 String value = getParam(key, null);
6693 return value == null ? null : Utils.toDateFromIso(value);
6794 }
6895
6996 /**
70
- * Returns the system parameter as boolean value for given key
71
- *
72
- * @param key
73
- * @return the value of the param or null if it doesn't exist
74
- */
97
+ * getParamAsBool<p>
98
+ * Get value parsed as boolean.
99
+ *
100
+ * @param key setting key
101
+ * @return boolean value or null
102
+ */
75103 public Boolean getParamAsBool(String key) {
76104 String value = getParam(key, null);
77105 return value == null ? null : Boolean.parseBoolean(value);
78106 }
79107
80108 /**
81
- *
82
- * @param key
83
- * @param defaulValue
84
- * returned if key doesn't exist in params table
85
- * @return
86
- */
109
+ * getParamAsBool<p>
110
+ * Get value parsed as boolean with default.
111
+ *
112
+ * @param key setting key
113
+ * @param defaulValue default when missing
114
+ * @return boolean value or default
115
+ */
87116 public Boolean getParamAsBool(String key, boolean defaulValue) {
88117 String value = getParam(key, null);
89118 return value == null ? defaulValue : Boolean.parseBoolean(value);
90119 }
91120
92121 /**
93
- * Returns the system parameter as boolean value for given key
94
- *
95
- * @param key
96
- * @return the value of the param or null if it doesn't exist
97
- */
122
+ * getParamAsDouble<p>
123
+ * Get value parsed as double.
124
+ *
125
+ * @param key setting key
126
+ * @return double value or null
127
+ */
98128 public Double getParamAsDouble(String key) {
99129 String value = getParam(key, null);
100130 return value == null ? null : Double.parseDouble(value);
101131 }
102132
103133 /**
104
- * Returns the system parameter value for given key
105
- *
106
- * @param key
107
- * @param defaultValue
108
- * returned if key doesn't exist in params table
109
- * @return
110
- */
134
+ * getParam<p>
135
+ * Get raw string value or a default when missing.
136
+ *
137
+ * @param key setting key
138
+ * @param defaultValue default when missing
139
+ * @return value or default
140
+ */
111141 public String getParam(String key, String defaultValue) {
112142 EntityManager em = emProvider.getEntityManager();
113143 Settings p = em.find(Settings.class, key);
114144 return p == null ? defaultValue : p.getValue();
115145 }
116146
147
+ // -------------------- Write API --------------------
148
+
117149 /**
118
- * Returns the system parameter value passed as parameter to method
119
- *
120
- * @param key
121
- * @param defaultValue
122
- * @return
123
- */
150
+ * setParam<p>
151
+ * Upsert a parameter as string.
152
+ *
153
+ * @param key setting key
154
+ * @param value string value
155
+ */
124156 public void setParam(String key, String value) {
125157 EntityManager em = emProvider.getEntityManager();
126158 em.getTransaction().begin();
127159 try {
128160 Settings p = em.find(Settings.class, key);
129
-
130161 if (p == null) {
131162 p = new Settings();
132163 p.setKey(key);
....@@ -136,15 +167,16 @@
136167 p.setValue(value);
137168 em.merge(p);
138169 }
139
- em.flush();
140170 em.getTransaction().commit();
141
- } finally {
171
+ } catch (Exception ex) {
142172 em.getTransaction().rollback();
173
+ throw ex;
143174 }
144175 }
145176
146
- /**
147
- * Save a parameter as a Date
177
+ /**
178
+ * setParam<p>
179
+ * Save parameter as ISO date string.
148180 *
149181 * @param key
150182 * @param value
....@@ -153,8 +185,9 @@
153185 setParam(key, Utils.toIsoFormat(value));
154186 }
155187
156
- /**
157
- * Save a parameter as a integer
188
+ /**
189
+ * setParam<p>
190
+ * Save parameter as integer.
158191 *
159192 * @param key
160193 * @param value
....@@ -163,8 +196,9 @@
163196 setParam(key, String.valueOf(value));
164197 }
165198
166
- /**
167
- * Save a parameter as a boolean
199
+ /**
200
+ * setParam<p>
201
+ * Save parameter as boolean.
168202 *
169203 * @param key
170204 * @param value
....@@ -173,8 +207,9 @@
173207 setParam(key, String.valueOf(value));
174208 }
175209
176
- /**
177
- * Save a parameter as a double
210
+ /**
211
+ * setParam<p>
212
+ * Save parameter as double.
178213 *
179214 * @param key
180215 * @param value
....@@ -184,11 +219,11 @@
184219 }
185220
186221 /**
187
- * Remove a parameter from params table
188
- *
189
- * @param key
190
- * @return
191
- */
222
+ * removeParam<p>
223
+ * Delete a parameter by key (no-op if missing).
224
+ *
225
+ * @param key setting key
226
+ */
192227 public void removeParam(String key) {
193228 EntityManager em = emProvider.getEntityManager();
194229 em.getTransaction().begin();
....@@ -198,13 +233,19 @@
198233 em.remove(p);
199234 }
200235 em.getTransaction().commit();
201
- } finally {
236
+ } catch (Exception ex) {
202237 em.getTransaction().rollback();
238
+ throw ex;
203239 }
204240 }
205241
242
+ /**
243
+ * Keys
244
+ * <p>
245
+ * Centralized constants for parameter keys (client/common/server).
246
+ */
206247 public static class Keys {
207
- // Keys used in basic app
248
+ // Client app keys
208249 public static final String CONFIG_CLIENT_HOST = "config.client.host";
209250 public static final String CONFIG_CLIENT_PORT = "config.client.port";
210251 public static final String CONFIG_CLIENT_LAST_UPDATE = "config.client.last_update";
....@@ -217,9 +258,9 @@
217258 public static final String CONFIG_CLIENT_GS_HOST = "config.client.gs_host";
218259 public static final String CONFIG_CLIENT_GS_PORT = "config.client.gs_port";
219260
220
- // Keys used in both app
221
- public static final String CONFIG_COMMON_CUSTOMER_CODE = "config.common.customer_code"; // BP
222
- public static final String CONFIG_COMMON_CS_CODE = "config.common.cs_code"; // 0000
261
+ // Shared keys
262
+ public static final String CONFIG_COMMON_CUSTOMER_CODE = "config.common.customer_code";
263
+ public static final String CONFIG_COMMON_CS_CODE = "config.common.cs_code";
223264 public static final String CONFIG_COMMON_USERS_VERSION = "config.common.user_version";
224265 public static final String CONFIG_COMMON_SETTINGS_VERSION = "config.common.settings_version";
225266 public static final String CONFIG_COMMON_DATASET_VERSION = "config.common.dataset_version";
....@@ -227,7 +268,7 @@
227268 public static final String CONFIG_COMMON_TIMEOUT_SESSION_BA = "config.common.timeout_session_ba";
228269 public static final String CONFIG_COMMON_TIMEOUT_SESSION_CS = "config.common.timeout_session_cs";
229270
230
- // Keys used in server app
271
+ // Server app keys
231272 public static final String CONFIG_SERVER_LICENSE_EXPIRATION = "config.server.license.expiation";
232273 public static final String CONFIG_SERVER_MAX_INSTANCES = "config.server.max_instances";
233274 public static final String CONFIG_SERVER_MAX_USERS = "config.server.max_users";
....@@ -240,5 +281,4 @@
240281 public static final String CONFIG_SERVER_CREATE_DATASET = "config.server.create_dataset_in_next_startup";
241282 public static final String CONFIG_SERVER_PORT = "config.server.port";
242283 }
243
-
244284 }