CCM Admin UI: Edit configuration settings
parent
9fce8f562f
commit
b514f8d32f
|
|
@ -22,18 +22,24 @@ import org.apache.logging.log4j.LogManager;
|
|||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.libreccm.core.CoreConstants;
|
||||
import org.libreccm.l10n.LocalizedString;
|
||||
import org.libreccm.security.AuthorizationRequired;
|
||||
import org.libreccm.security.RequiresPrivilege;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
@ -163,9 +169,33 @@ public class SettingManager {
|
|||
settingInfo.setValueType(field.getType().getName());
|
||||
|
||||
try {
|
||||
final Object conf = configuration.newInstance();
|
||||
settingInfo.setDefaultValue(Objects.toString(field.get(conf)));
|
||||
} catch (InstantiationException | IllegalAccessException ex) {
|
||||
final Constructor<?> constructor = configuration.getConstructor();
|
||||
final Object conf = constructor.newInstance();
|
||||
final Object defaultValueObj = field.get(conf);
|
||||
final String defaultValue;
|
||||
if (defaultValueObj instanceof LocalizedString) {
|
||||
final LocalizedString defaultValueLstr
|
||||
= (LocalizedString) defaultValueObj;
|
||||
defaultValue = defaultValueLstr
|
||||
.getValues()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.map(
|
||||
entry -> String.format(
|
||||
"%s: %s",
|
||||
entry.getKey().toString(), entry.getValue()
|
||||
)
|
||||
)
|
||||
.collect(Collectors.joining(", "));
|
||||
} else {
|
||||
defaultValue = Objects.toString(defaultValueObj);
|
||||
}
|
||||
settingInfo.setDefaultValue(defaultValue);
|
||||
|
||||
} catch (NoSuchMethodException
|
||||
| InstantiationException
|
||||
| InvocationTargetException
|
||||
| IllegalAccessException ex) {
|
||||
LOGGER.warn(String.format("Failed to create instance of \"%s\" to "
|
||||
+ "get default values.",
|
||||
configuration.getName()),
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ package org.libreccm.ui.admin.configuration;
|
|||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.libreccm.configuration.AbstractSetting;
|
||||
import org.libreccm.configuration.ConfigurationInfo;
|
||||
import org.libreccm.configuration.ConfigurationManager;
|
||||
import org.libreccm.configuration.SettingInfo;
|
||||
|
|
@ -35,6 +34,7 @@ import org.libreccm.security.RequiresPrivilege;
|
|||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
|
@ -163,9 +163,15 @@ public class SettingsController {
|
|||
.getClass()
|
||||
.getDeclaredField(settingInfo.getName());
|
||||
field.setAccessible(true);
|
||||
if (field.get(configuration) instanceof LocalizedString) {
|
||||
final LocalizedString localizedStr = (LocalizedString) field
|
||||
.get(configuration);
|
||||
final Object valueObj = field.get(configuration);
|
||||
if (valueObj instanceof List) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final List<String> list = (List<String>) valueObj;
|
||||
value = list
|
||||
.stream()
|
||||
.collect(Collectors.joining("\n"));
|
||||
} else if (valueObj instanceof LocalizedString) {
|
||||
final LocalizedString localizedStr = (LocalizedString) valueObj;
|
||||
value = localizedStr
|
||||
.getValues()
|
||||
.entrySet()
|
||||
|
|
@ -177,9 +183,15 @@ public class SettingsController {
|
|||
)
|
||||
)
|
||||
.sorted()
|
||||
.collect(Collectors.joining(", "));
|
||||
.collect(Collectors.joining("\n"));
|
||||
} else if (valueObj instanceof Set) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Set<String> set = (Set<String>) valueObj;
|
||||
value = set
|
||||
.stream()
|
||||
.collect(Collectors.joining("\n"));
|
||||
} else {
|
||||
value = Objects.toString(field.get(configuration));
|
||||
value = Objects.toString(valueObj);
|
||||
}
|
||||
} catch (NoSuchFieldException | IllegalAccessException | SecurityException ex) {
|
||||
LOGGER.error(
|
||||
|
|
@ -286,7 +298,7 @@ public class SettingsController {
|
|||
valueParam
|
||||
);
|
||||
} else if (valueType.equals(Set.class.getName())) {
|
||||
return updateStringListSetting(
|
||||
return updateStringSetSetting(
|
||||
configurationClassName,
|
||||
confClass,
|
||||
conf,
|
||||
|
|
@ -403,7 +415,7 @@ public class SettingsController {
|
|||
continue;
|
||||
}
|
||||
final Locale locale = new Locale(tokens[0]);
|
||||
final String localeValue = tokens[1];
|
||||
final String localeValue = tokens[1].trim();
|
||||
value.addValue(locale, localeValue);
|
||||
}
|
||||
return updateSetting(
|
||||
|
|
@ -452,9 +464,33 @@ public class SettingsController {
|
|||
final String settingName,
|
||||
final String valueType,
|
||||
final String valueParam
|
||||
) {
|
||||
final String[] tokens = valueParam.split("\n");
|
||||
final List<String> value = Arrays
|
||||
.asList(tokens)
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.collect(Collectors.toList());
|
||||
return updateSetting(
|
||||
configurationClassName,
|
||||
configurationClass,
|
||||
configuration,
|
||||
settingName,
|
||||
valueType,
|
||||
value
|
||||
);
|
||||
}
|
||||
|
||||
private String updateStringSetSetting(
|
||||
final String configurationClassName,
|
||||
final Class<?> configurationClass,
|
||||
final Object configuration,
|
||||
final String settingName,
|
||||
final String valueType,
|
||||
final String valueParam
|
||||
) {
|
||||
final String[] tokens = valueParam.split(",");
|
||||
final List<String> value = Arrays.asList(tokens);
|
||||
final Set<String> value = new HashSet<>(Arrays.asList(tokens));
|
||||
return updateSetting(
|
||||
configurationClassName,
|
||||
configurationClass,
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@
|
|||
</c:when>
|
||||
<c:otherwise>
|
||||
<strong>
|
||||
#{setting.value}
|
||||
<pre>#{setting.value}</pre>
|
||||
</strong>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
|
|
@ -148,7 +148,22 @@
|
|||
<div class="modal-body">
|
||||
<c:choose>
|
||||
<c:when test="#{setting.valueType.equals(BigDecimalClassName)}">
|
||||
<pre>BigDecimal</pre>
|
||||
<div class="form-group">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<input aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
id="#{setting.name}-setting-value"
|
||||
name="settingValue"
|
||||
step="0.01"
|
||||
type="number"
|
||||
value="#{setting.value}" />
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(BooleanClassName) or setting.valueType.equals('boolean')}">
|
||||
<div class="form-group form-check">
|
||||
|
|
@ -169,11 +184,28 @@
|
|||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(DoubleClassName) or setting.valueType.equals('double')}">
|
||||
<pre>double</pre>
|
||||
<div class="form-group">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<input aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
id="#{setting.name}-setting-value"
|
||||
max="#{DoubleMaxValue}"
|
||||
min="#{DoubleMinValue}"
|
||||
name="settingValue"
|
||||
step="0.01"
|
||||
type="number"
|
||||
value="#{setting.value}" />
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(IntegerClassName) or setting.valueType.equals('int')}">
|
||||
<div class="form-group">
|
||||
<label for="#{setting-name}-setting-value">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<input aria-describedby="#{setting.name}-setting-value-help"
|
||||
|
|
@ -193,41 +225,97 @@
|
|||
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(ListClassName)}">
|
||||
<pre>List</pre>
|
||||
<div class="form-group">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<textarea aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
cols="80"
|
||||
id="#{setting.name}-setting-value"
|
||||
name="settingValue"
|
||||
rows="10">#{setting.value}</textarea>
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(LocalizedStringClassName)}">
|
||||
<pre>LocalizedString</pre>
|
||||
<div class="form-group">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<textarea aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
cols="80"
|
||||
id="#{setting.name}-setting-value"
|
||||
name="settingValue"
|
||||
rows="10">#{setting.value}</textarea>
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(LongClassName) or setting.valueType.equals('long')}">
|
||||
<div class="form-group">
|
||||
<label for="#{setting-name}-setting-value">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<input aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
id="#{setting.name}-setting-value"
|
||||
max="#{LongMaxValue}"
|
||||
min="#{LongMinValue}"
|
||||
name="settingValue"
|
||||
step="1"
|
||||
type="number"
|
||||
value="#{setting.value}" />
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(SetClassName)}">
|
||||
<div class="form-group">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<textarea aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
cols="80"
|
||||
id="#{setting.name}-setting-value"
|
||||
name="settingValue"
|
||||
rows="10">#{setting.value}</textarea>
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(StringClassName)}">
|
||||
<div class="form-group">
|
||||
<label for="#{setting.name}-setting-value">
|
||||
#{AdminMessages['configuration.settings.setting.dialog.value.label']}
|
||||
</label>
|
||||
<input aria-describedby="#{setting.name}-setting-value-help"
|
||||
class="form-control"
|
||||
id="#{setting.name}-setting-value"
|
||||
name="settingValue"
|
||||
step="1"
|
||||
type="number"
|
||||
type="text"
|
||||
value="#{setting.value}" />
|
||||
|
||||
<small class="form-text text-muted"
|
||||
id="#{setting.name}-setting-value-help">
|
||||
#{setting.description}
|
||||
</small>
|
||||
</div>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(SetClassName)}">
|
||||
<pre>Set</pre>
|
||||
</c:when>
|
||||
<c:when test="#{setting.valueType.equals(StringClassName)}">
|
||||
<pre>String</pre>
|
||||
</c:when>
|
||||
<c:otherwise>
|
||||
<p>
|
||||
#{AdminMessages.getMessage('configuration.settings.setting.dialog.unsupported_type', [setting.valueType])}
|
||||
</p>
|
||||
<pre>
|
||||
setting.valueType: #{setting.valueType}
|
||||
Boolean: #{BooleanClassName}
|
||||
LocalizedString: #{LocalizedStringClassName}
|
||||
</pre>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Reference in New Issue