From d6ba57414ceb54082903ed19b8c9bca954acd31c Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 23 Oct 2020 21:38:18 +0200 Subject: [PATCH] UI for managing settings --- .../configuration/ConfigurationPage.java | 1 + .../configuration/SettingsController.java | 102 +++++++++++++++++ .../configuration/SettingsTableEntry.java | 106 ++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsController.java create mode 100644 ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsTableEntry.java diff --git a/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/ConfigurationPage.java b/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/ConfigurationPage.java index d9ebcb039..1a0f38922 100644 --- a/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/ConfigurationPage.java +++ b/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/ConfigurationPage.java @@ -36,6 +36,7 @@ public class ConfigurationPage implements AdminPage { public Set> getControllerClasses() { final Set> classes = new HashSet<>(); classes.add(ConfigurationController.class); + classes.add(SettingsController.class); return classes; } diff --git a/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsController.java b/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsController.java new file mode 100644 index 000000000..e59ee180f --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsController.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2020 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.ui.admin.configuration; + +import org.libreccm.configuration.ConfigurationInfo; +import org.libreccm.configuration.ConfigurationManager; +import org.libreccm.configuration.SettingInfo; +import org.libreccm.l10n.GlobalizationHelper; +import org.libreccm.l10n.LocalizedTextsUtil; + +import java.util.Objects; +import java.util.stream.Collectors; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.mvc.Controller; +import javax.mvc.Models; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +/** + * + * @author Jens Pelzetter + */ +@Controller +@RequestScoped +@Path("/configuration/{configurationClass}") +public class SettingsController { + + @Inject + private ConfigurationManager confManager; + + @Inject + private GlobalizationHelper globalizationHelper; + + @Inject + private Models models; + + @Path("/") + public String showSettings( + @PathParam("configurationClass") final String configurationClass + ) { + final Class confClass; + try { + confClass = Class.forName(configurationClass); + } catch(ClassNotFoundException ex) { + models.put("configurationClass", configurationClass); + return "org/libreccm/ui/admin/configuration/configuration-class-not-found.xhtml"; + } + + final Object conf = confManager.findConfiguration(confClass); + final ConfigurationInfo confInfo = confManager.getConfigurationInfo( + confClass + ); + + confInfo + .getSettings() + .entrySet() + .stream() + .map(info -> buildSettingsTableEntry(info.getValue(), conf)) + .sorted() + .collect(Collectors.toList()); + + + return "org/libreccm/ui/admin/configuration/settings.xhtml"; + } + + private SettingsTableEntry buildSettingsTableEntry( + final SettingInfo settingInfo, final Object conf + ) { + Objects.requireNonNull(settingInfo); + + final LocalizedTextsUtil textsUtil = globalizationHelper + .getLocalizedTextsUtil(settingInfo.getDescBundle()); + + final SettingsTableEntry entry = new SettingsTableEntry(); + entry.setName(settingInfo.getName()); + entry.setValueType(settingInfo.getValueType()); + entry.setDefaultValue(settingInfo.getDefaultValue()); + entry.setLabel(textsUtil.getText(settingInfo.getLabelKey())); + entry.setDescription(textsUtil.getText(settingInfo.getDescKey())); + + return entry; + } + +} diff --git a/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsTableEntry.java b/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsTableEntry.java new file mode 100644 index 000000000..bf83bb223 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/ui/admin/configuration/SettingsTableEntry.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2020 LibreCCM Foundation. + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.ui.admin.configuration; + +import java.util.Objects; + +/** + * + * @author Jens Pelzetter + */ +public class SettingsTableEntry implements Comparable { + + private String name; + + private String valueType; + + private String defaultValue; + + private String value; + + private String label; + + private String description; + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getValueType() { + return valueType; + } + + public void setValueType(final String valueType) { + this.valueType = valueType; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(final String defaultValue) { + this.defaultValue = defaultValue; + } + + public String getLabel() { + return label; + } + + public void setLabel(final String label) { + this.label = label; + } + + public String getDescription() { + return description; + } + + public void setDescription(final String description) { + this.description = description; + } + + public String getValue() { + return value; + } + + public void setValue(final String value) { + this.value = value; + } + + @Override + public int compareTo(final SettingsTableEntry other) { + int result = Objects.compare( + label, + Objects.requireNonNull(other).getLabel(), + String::compareTo + ); + if (result == 0) { + result = Objects.compare( + name, + other.getName(), + String::compareTo + ); + } + return result; + } + +}