/**
* This file is part of Faktotum.
*
*
* Faktotum is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* Faktotum is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Faktotum.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
package de.romankreisel.faktotum.beans;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import de.romankreisel.faktotum.beans.CryptBean.AUTHENTICATION_SCHEME;
import de.romankreisel.faktotum.dao.SettingDao;
import de.romankreisel.faktotum.datamodel.SettingEntity;
/**
* This Bean manages the Settings for Faktotum.
*
* @author Roman Kreisel <mail@romankreisel.de>
*/
@Singleton
public class SettingBean extends FaktotumBean {
/**
* Represents a setting available in Faktotum.
*
* It contains the description, the default value and - lazy loaded - the
* SettingEntity
*
* @author Roman Kreisel <mail@romankreisel.de>
*
* @param <T>
* The type of the Setting
*/
private class SettingBeanItem<T> {
private SettingEntity<?> settingEntity = null;
private final T defaultValue;
private final String description;
public SettingBeanItem(T defaultValue, String description) {
super();
this.defaultValue = defaultValue;
this.description = description;
}
/**
* @return the defaultValue
*/
public T getDefaultValue() {
return this.defaultValue;
}
/**
* @return the description
*/
public String getDescription() {
return this.description;
}
/**
* @return the settingEntity
*/
public SettingEntity<?> getSettingEntity() {
return this.settingEntity;
}
/**
* @param settingEntity
* the settingEntity to set
*/
public void setSettingEntity(SettingEntity<?> settingEntity) {
this.settingEntity = settingEntity;
}
}
private final Map<String, SettingBeanItem<?>> settings = new TreeMap<>();
public final static String SETTING_STRING_PRODUCT_NAME = "product.name";
public final static String SETTING_STRING_AUTH_SCHEME = "auth.scheme";
public final static String SETTING_LONG_AUTH_SESSION_MAXAGE = "auth.session.maxage";
public static final String SETTING_DATE_FOUNDATION = "fraternity.foundation";
public static final String SETTING_STRING_FRATERNITY_NAME = "fraternity.name";
public static final String SETTING_BOOLEAN_FRATERNITY_USES_ALIASES = "fraternity.aliases";
public static final String SETTING_STRING_FRATERNITY_ALIASES_PREFIX = "fraternity.aliases.prefix";
public static final String SETTING_STRING_ADMINISTRATOR_NAME = "administrator.name";
public static final String SETTING_STRING_ADMINISTRATOR_EMAIL = "administrator.email";
@EJB
private SettingDao settingDao;
public SettingBean() {
super();
this.initializeSettings();
}
/**
* Adds a setting for Faktotum
*
* @param identifier
* used to identify the setting
* @param description
* The description for the setting
* @param defaultValue
* The default value for this setting
*/
private <T> void addSetting(String identifier, String description, T defaultValue) {
this.settings.put(identifier, new SettingBeanItem<T>(defaultValue, description));
}
/**
* Returns the settings value - used for Boolean settings
*
* @param identifier
* used to identify the setting
* @return the settings value, retrieved from the database or from the
* default value
*/
public Boolean getBooleanSettingValue(String identifier) {
Object retValObject = this.getSettingValue(identifier);
if (retValObject instanceof Boolean) {
return (Boolean) retValObject;
} else {
throw new IllegalArgumentException("Setting " + identifier + " is not of type String");
}
}
/**
* Returns the settings value - used for date settings
*
* @param identifier
* used to identify the setting
* @return the settings value, retrieved from the database or from the
* default value
*/
public Date getDateSettingValue(String identifier) {
Object retValObject = this.getSettingValue(identifier);
if (retValObject instanceof Date) {
return (Date) retValObject;
} else {
throw new IllegalArgumentException("Setting " + identifier + " is not of type Date");
}
}
/**
* Returns the settings value - used for enum settings
*
* @param identifier
* used to identify the setting
* @return the settings value, retrieved from the database or from the
* default value
*/
public Enum<?> getEnumSettingValue(String identifier) {
Object retValObject = this.getSettingValue(identifier);
if (retValObject instanceof Enum<?>) {
return (Enum<?>) retValObject;
} else {
throw new IllegalArgumentException("Setting " + identifier + " is not of type enum");
}
}
/**
* Returns the settings value - used for Integer settings
*
* @param identifier
* used to identify the setting
* @return the settings value, retrieved from the database or from the
* default value
*/
public Integer getIntegerSettingValue(String identifier) {
Object retValObject = this.getSettingValue(identifier);
if (retValObject instanceof Integer) {
return (Integer) retValObject;
} else {
throw new IllegalArgumentException("Setting " + identifier + " is not of type Integer");
}
}
/**
* Returns the settings value - used for Long settings
*
* @param identifier
* used to identify the setting
* @return the settings value, retrieved from the database or from the
* default value
*/
public Long getLongSettingValue(String identifier) {
Object retValObject = this.getSettingValue(identifier);
if (retValObject instanceof Long) {
return (Long) retValObject;
} else {
throw new IllegalArgumentException("Setting " + identifier + " is not of type Long");
}
}
/**
* Returns the default value for a setting, independent from its type
*
* @param identifier
* used to identify the setting
* @return the default value for this setting
*/
public Object getSettingDefaultValue(String identifier) {
SettingBeanItem<?> settingItem = this.getSettingItem(identifier);
if (settingItem != null) {
return settingItem.getDefaultValue();
} else {
throw new IllegalArgumentException("No setting with identifier " + identifier + " available");
}
}
/**
* Returns the default value for a setting, independent from its type
*
* @param identifier
* used to identify the setting
* @return the description for this setting
*/
public String getSettingDescription(String identifier) {
SettingBeanItem<?> settingItem = this.getSettingItem(identifier);
if (settingItem != null) {
return settingItem.getDescription();
} else {
throw new IllegalArgumentException("No setting with identifier " + identifier + " available");
}
}
/**
* Returns the setting bean item for a setting, independent from its type
*
* @param identifier
* used to identify the setting
* @return the setting bean item for this setting
*/
private SettingBeanItem<?> getSettingItem(String identifier) {
return this.settings.get(identifier);
}
/**
* Returns the value for a setting, independent from its type
*
* @param identifier
* used to identify the setting
* @return the value for this setting
*/
public Object getSettingValue(String identifier) {
SettingBeanItem<?> settingItem = this.getSettingItem(identifier);
if (settingItem != null) {
if (settingItem.getSettingEntity() == null) {
SettingEntity<?> settingEntity = this.settingDao.getSetting(identifier);
settingItem.setSettingEntity(settingEntity);
}
if (settingItem.getSettingEntity() != null && settingItem.getSettingEntity().getValue() != null) {
return settingItem.getSettingEntity().getValue();
} else {
return settingItem.defaultValue;
}
} else {
throw new IllegalArgumentException("No setting with identifier " + identifier + " available");
}
}
/**
* Returns the settings value - used for String settings
*
* @param identifier
* used to identify the setting
* @return the settings value, retrieved from the database or from the
* default value
*/
public String getStringSettingValue(String identifier) {
Object retValObject = this.getSettingValue(identifier);
if (retValObject instanceof String) {
return (String) retValObject;
} else {
throw new IllegalArgumentException("Setting " + identifier + " is not of type String");
}
}
/**
* Initializes the setting bean by adding all available settings
*/
private void initializeSettings() {
this.addSetting(SettingBean.SETTING_STRING_AUTH_SCHEME, "Technik für Passwort-Verschlüsselung", AUTHENTICATION_SCHEME.MD5_CRYPT);
this.addSetting(SettingBean.SETTING_LONG_AUTH_SESSION_MAXAGE, "Maximales Dauer der Gültigkeit von Sessions (s)", 365 * 24 * 60 * 60);
this.addSetting(SettingBean.SETTING_STRING_PRODUCT_NAME, "Produktname", "Faktotum");
this.addSetting(SettingBean.SETTING_DATE_FOUNDATION, "Gründungsdatum", new GregorianCalendar(1914, Calendar.FEBRUARY, 5).getTime());
this.addSetting(SettingBean.SETTING_STRING_FRATERNITY_NAME, "Verbindungsname", "Verbindung");
this.addSetting(SettingBean.SETTING_BOOLEAN_FRATERNITY_USES_ALIASES, "Kneipnamen", true);
this.addSetting(SettingBean.SETTING_STRING_FRATERNITY_ALIASES_PREFIX, "Kneipnamen-Prefix", "v.");
this.addSetting(SettingBean.SETTING_STRING_ADMINISTRATOR_NAME, "Name des Administrators", "Administrator");
this.addSetting(SettingBean.SETTING_STRING_ADMINISTRATOR_EMAIL, "E-Mailadrese des Administrators", "admin@example.org");
}
}