Joaquín Reñé
2025-05-27 89b1c533d1b48b8b339b9c74a59c2ce73e6431af
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
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;
@ApplicationScoped
public class SystemParams {
    @SuppressWarnings("unused")
    private static final Logger LOG = LogManager.getLogger(SystemParams.class);
    @Inject
    private EntityManagerProvider emProvider;
    /**
     * Returns the system parameter value for given key
     * 
     * @param key
     * @return the value of the param or null if it doesn't exist
     */
    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
     */
    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
     */
    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
     */
    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
     */
    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
     */
    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
     */
    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
     */
    public String getParam(String key, String defaultValue) {
        EntityManager em = emProvider.getEntityManager();
        Settings p = em.find(Settings.class, key);
        return p == null ? defaultValue : p.getValue();
    }
    /**
     * Returns the system parameter value passed as parameter to method
     * 
     * @param key
     * @param defaultValue
     * @return
     */
    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.flush();
            em.getTransaction().commit();
        } finally {
            em.getTransaction().rollback();
        }
    }
    /**
     * Save a parameter as a Date
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, Date value) {
        setParam(key, Utils.toIsoFormat(value));
    }
    /**
     * Save a parameter as a integer
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, int value) {
        setParam(key, String.valueOf(value));
    }
    /**
     * Save a parameter as a boolean
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, boolean value) {
        setParam(key, String.valueOf(value));
    }
    /**
     * Save a parameter as a double
     * 
     * @param key
     * @param value
     */
    public void setParam(String key, double value) {
        setParam(key, String.valueOf(value));
    }
    /**
     * Remove a parameter from params table
     * 
     * @param key
     * @return
     */
    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();
        } finally {
            em.getTransaction().rollback();
        }
    }
    public static class Keys {
        // Keys used in basic app
        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";
        // 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
        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";
        // Keys used in server app
        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";
    }
}