package net.curisit.securis.db.common; import java.util.Date; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import javax.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"; } }