| .. | .. |
|---|
| 1 | +/* |
|---|
| 2 | +* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. |
|---|
| 3 | +*/ |
|---|
| 1 | 4 | package net.curisit.securis.db.common; |
|---|
| 2 | 5 | |
|---|
| 3 | 6 | import java.util.Date; |
|---|
| .. | .. |
|---|
| 13 | 16 | import org.apache.logging.log4j.LogManager; |
|---|
| 14 | 17 | import org.apache.logging.log4j.Logger; |
|---|
| 15 | 18 | |
|---|
| 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 | +*/ |
|---|
| 16 | 38 | @ApplicationScoped |
|---|
| 17 | 39 | public class SystemParams { |
|---|
| 18 | 40 | |
|---|
| 19 | 41 | @SuppressWarnings("unused") |
|---|
| 20 | 42 | private static final Logger LOG = LogManager.getLogger(SystemParams.class); |
|---|
| 21 | 43 | |
|---|
| 22 | | - @Inject |
|---|
| 23 | | - private EntityManagerProvider emProvider; |
|---|
| 44 | + @Inject private EntityManagerProvider emProvider; |
|---|
| 45 | + |
|---|
| 46 | + // -------------------- Read API -------------------- |
|---|
| 24 | 47 | |
|---|
| 25 | 48 | /** |
|---|
| 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 | + */ |
|---|
| 31 | 55 | public String getParam(String key) { |
|---|
| 32 | 56 | return getParam(key, null); |
|---|
| 33 | 57 | } |
|---|
| 34 | 58 | |
|---|
| 35 | 59 | /** |
|---|
| 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 | + */ |
|---|
| 41 | 66 | public Integer getParamAsInt(String key) { |
|---|
| 42 | 67 | String value = getParam(key, null); |
|---|
| 43 | 68 | return value == null ? null : Integer.parseInt(value); |
|---|
| 44 | 69 | } |
|---|
| 45 | 70 | |
|---|
| 46 | 71 | /** |
|---|
| 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 | + */ |
|---|
| 53 | 79 | public Integer getParamAsInt(String key, Integer defaulValue) { |
|---|
| 54 | 80 | String value = getParam(key, null); |
|---|
| 55 | 81 | return value == null ? defaulValue : Integer.parseInt(value); |
|---|
| 56 | 82 | } |
|---|
| 57 | 83 | |
|---|
| 58 | 84 | /** |
|---|
| 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 | + */ |
|---|
| 64 | 91 | public Date getParamAsDate(String key) { |
|---|
| 65 | 92 | String value = getParam(key, null); |
|---|
| 66 | 93 | return value == null ? null : Utils.toDateFromIso(value); |
|---|
| 67 | 94 | } |
|---|
| 68 | 95 | |
|---|
| 69 | 96 | /** |
|---|
| 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 | + */ |
|---|
| 75 | 103 | public Boolean getParamAsBool(String key) { |
|---|
| 76 | 104 | String value = getParam(key, null); |
|---|
| 77 | 105 | return value == null ? null : Boolean.parseBoolean(value); |
|---|
| 78 | 106 | } |
|---|
| 79 | 107 | |
|---|
| 80 | 108 | /** |
|---|
| 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 | + */ |
|---|
| 87 | 116 | public Boolean getParamAsBool(String key, boolean defaulValue) { |
|---|
| 88 | 117 | String value = getParam(key, null); |
|---|
| 89 | 118 | return value == null ? defaulValue : Boolean.parseBoolean(value); |
|---|
| 90 | 119 | } |
|---|
| 91 | 120 | |
|---|
| 92 | 121 | /** |
|---|
| 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 | + */ |
|---|
| 98 | 128 | public Double getParamAsDouble(String key) { |
|---|
| 99 | 129 | String value = getParam(key, null); |
|---|
| 100 | 130 | return value == null ? null : Double.parseDouble(value); |
|---|
| 101 | 131 | } |
|---|
| 102 | 132 | |
|---|
| 103 | 133 | /** |
|---|
| 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 | + */ |
|---|
| 111 | 141 | public String getParam(String key, String defaultValue) { |
|---|
| 112 | 142 | EntityManager em = emProvider.getEntityManager(); |
|---|
| 113 | 143 | Settings p = em.find(Settings.class, key); |
|---|
| 114 | 144 | return p == null ? defaultValue : p.getValue(); |
|---|
| 115 | 145 | } |
|---|
| 116 | 146 | |
|---|
| 147 | + // -------------------- Write API -------------------- |
|---|
| 148 | + |
|---|
| 117 | 149 | /** |
|---|
| 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 | + */ |
|---|
| 124 | 156 | public void setParam(String key, String value) { |
|---|
| 125 | 157 | EntityManager em = emProvider.getEntityManager(); |
|---|
| 126 | 158 | em.getTransaction().begin(); |
|---|
| 127 | 159 | try { |
|---|
| 128 | 160 | Settings p = em.find(Settings.class, key); |
|---|
| 129 | | - |
|---|
| 130 | 161 | if (p == null) { |
|---|
| 131 | 162 | p = new Settings(); |
|---|
| 132 | 163 | p.setKey(key); |
|---|
| .. | .. |
|---|
| 136 | 167 | p.setValue(value); |
|---|
| 137 | 168 | em.merge(p); |
|---|
| 138 | 169 | } |
|---|
| 139 | | - em.flush(); |
|---|
| 140 | 170 | em.getTransaction().commit(); |
|---|
| 141 | | - } finally { |
|---|
| 171 | + } catch (Exception ex) { |
|---|
| 142 | 172 | em.getTransaction().rollback(); |
|---|
| 173 | + throw ex; |
|---|
| 143 | 174 | } |
|---|
| 144 | 175 | } |
|---|
| 145 | 176 | |
|---|
| 146 | | - /** |
|---|
| 147 | | - * Save a parameter as a Date |
|---|
| 177 | + /** |
|---|
| 178 | + * setParam<p> |
|---|
| 179 | + * Save parameter as ISO date string. |
|---|
| 148 | 180 | * |
|---|
| 149 | 181 | * @param key |
|---|
| 150 | 182 | * @param value |
|---|
| .. | .. |
|---|
| 153 | 185 | setParam(key, Utils.toIsoFormat(value)); |
|---|
| 154 | 186 | } |
|---|
| 155 | 187 | |
|---|
| 156 | | - /** |
|---|
| 157 | | - * Save a parameter as a integer |
|---|
| 188 | + /** |
|---|
| 189 | + * setParam<p> |
|---|
| 190 | + * Save parameter as integer. |
|---|
| 158 | 191 | * |
|---|
| 159 | 192 | * @param key |
|---|
| 160 | 193 | * @param value |
|---|
| .. | .. |
|---|
| 163 | 196 | setParam(key, String.valueOf(value)); |
|---|
| 164 | 197 | } |
|---|
| 165 | 198 | |
|---|
| 166 | | - /** |
|---|
| 167 | | - * Save a parameter as a boolean |
|---|
| 199 | + /** |
|---|
| 200 | + * setParam<p> |
|---|
| 201 | + * Save parameter as boolean. |
|---|
| 168 | 202 | * |
|---|
| 169 | 203 | * @param key |
|---|
| 170 | 204 | * @param value |
|---|
| .. | .. |
|---|
| 173 | 207 | setParam(key, String.valueOf(value)); |
|---|
| 174 | 208 | } |
|---|
| 175 | 209 | |
|---|
| 176 | | - /** |
|---|
| 177 | | - * Save a parameter as a double |
|---|
| 210 | + /** |
|---|
| 211 | + * setParam<p> |
|---|
| 212 | + * Save parameter as double. |
|---|
| 178 | 213 | * |
|---|
| 179 | 214 | * @param key |
|---|
| 180 | 215 | * @param value |
|---|
| .. | .. |
|---|
| 184 | 219 | } |
|---|
| 185 | 220 | |
|---|
| 186 | 221 | /** |
|---|
| 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 | + */ |
|---|
| 192 | 227 | public void removeParam(String key) { |
|---|
| 193 | 228 | EntityManager em = emProvider.getEntityManager(); |
|---|
| 194 | 229 | em.getTransaction().begin(); |
|---|
| .. | .. |
|---|
| 198 | 233 | em.remove(p); |
|---|
| 199 | 234 | } |
|---|
| 200 | 235 | em.getTransaction().commit(); |
|---|
| 201 | | - } finally { |
|---|
| 236 | + } catch (Exception ex) { |
|---|
| 202 | 237 | em.getTransaction().rollback(); |
|---|
| 238 | + throw ex; |
|---|
| 203 | 239 | } |
|---|
| 204 | 240 | } |
|---|
| 205 | 241 | |
|---|
| 242 | + /** |
|---|
| 243 | + * Keys |
|---|
| 244 | + * <p> |
|---|
| 245 | + * Centralized constants for parameter keys (client/common/server). |
|---|
| 246 | + */ |
|---|
| 206 | 247 | public static class Keys { |
|---|
| 207 | | - // Keys used in basic app |
|---|
| 248 | + // Client app keys |
|---|
| 208 | 249 | public static final String CONFIG_CLIENT_HOST = "config.client.host"; |
|---|
| 209 | 250 | public static final String CONFIG_CLIENT_PORT = "config.client.port"; |
|---|
| 210 | 251 | public static final String CONFIG_CLIENT_LAST_UPDATE = "config.client.last_update"; |
|---|
| .. | .. |
|---|
| 217 | 258 | public static final String CONFIG_CLIENT_GS_HOST = "config.client.gs_host"; |
|---|
| 218 | 259 | public static final String CONFIG_CLIENT_GS_PORT = "config.client.gs_port"; |
|---|
| 219 | 260 | |
|---|
| 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"; |
|---|
| 223 | 264 | public static final String CONFIG_COMMON_USERS_VERSION = "config.common.user_version"; |
|---|
| 224 | 265 | public static final String CONFIG_COMMON_SETTINGS_VERSION = "config.common.settings_version"; |
|---|
| 225 | 266 | public static final String CONFIG_COMMON_DATASET_VERSION = "config.common.dataset_version"; |
|---|
| .. | .. |
|---|
| 227 | 268 | public static final String CONFIG_COMMON_TIMEOUT_SESSION_BA = "config.common.timeout_session_ba"; |
|---|
| 228 | 269 | public static final String CONFIG_COMMON_TIMEOUT_SESSION_CS = "config.common.timeout_session_cs"; |
|---|
| 229 | 270 | |
|---|
| 230 | | - // Keys used in server app |
|---|
| 271 | + // Server app keys |
|---|
| 231 | 272 | public static final String CONFIG_SERVER_LICENSE_EXPIRATION = "config.server.license.expiation"; |
|---|
| 232 | 273 | public static final String CONFIG_SERVER_MAX_INSTANCES = "config.server.max_instances"; |
|---|
| 233 | 274 | public static final String CONFIG_SERVER_MAX_USERS = "config.server.max_users"; |
|---|
| .. | .. |
|---|
| 240 | 281 | public static final String CONFIG_SERVER_CREATE_DATASET = "config.server.create_dataset_in_next_startup"; |
|---|
| 241 | 282 | public static final String CONFIG_SERVER_PORT = "config.server.port"; |
|---|
| 242 | 283 | } |
|---|
| 243 | | - |
|---|
| 244 | 284 | } |
|---|