use of io.datarouter.storage.setting.cached.CachedSetting in project datarouter by hotpads.
the class ClusterSettingsHandler method browseSettings.
@Handler
public Mav browseSettings(OptionalString name) {
Mav mav = new Mav(files.jsp.admin.datarouter.setting.browseSettingsJsp);
String path = servletContext.getContextPath() + paths.datarouter.settings.customSettings.toSlashedString();
mav.put("customSettingsPath", path);
String requestedNodeName = name.orElse(settingRootFinder.getName());
mav.put("nodeName", name.orElse(""));
Map<SimpleSettingCategory, Set<SettingNodeJspDto>> categoryMap = new LinkedHashMap<>();
for (Entry<SimpleSettingCategory, Set<SettingRoot>> entry : settingRootFinder.getRootNodesByCategory().entrySet()) {
var nodes = entry.getValue().stream().map(SettingNodeJspDto::new).collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SettingNodeJspDto::getShortName))));
categoryMap.put(entry.getKey(), nodes);
}
Optional<String> currentCategory = settingRootFinder.getRootNodesByCategory().entrySet().stream().findFirst().map(Entry::getKey).map(SimpleSettingCategory::getDisplay);
mav.put("currentCategory", currentCategory.orElse(""));
mav.put("categoryMap", categoryMap);
mav.put("serverTypeOptions", serverTypes.getHtmlSelectOptionsVarNames());
Optional<SettingNode> node = getSettingNode(requestedNodeName);
// if the node was not found, get the most recent ancestor to search its settings list
boolean trySearchingForSpecificSetting = node.isEmpty();
if (trySearchingForSpecificSetting) {
node = getMostRecentAncestorSettingNode(requestedNodeName);
}
if (node.isEmpty()) {
return mav;
}
node.ifPresent(settingNode -> {
String rootName = StringTool.getStringBeforeFirstOccurrence('.', settingNode.getName());
settingRootFinder.getRootNodesSortedByShortName().stream().filter(rootNode -> rootNode.getShortName().equals(rootName)).findFirst().map(SettingRoot.class::cast).map(SettingRoot::getSettingCategory).map(SettingCategory::getDisplay).ifPresent(category -> mav.put("currentCategory", category));
mav.put("currentRootName", rootName);
});
mav.put("ancestors", Scanner.of(settingRootFinder.getDescendants(node.get().getName())).map(SettingNodeJspDto::new).list());
mav.put("currentRootName", node.get().getName().substring(0, node.get().getName().indexOf('.')));
mav.put("children", Scanner.of(node.get().getListChildren()).map(SettingNodeJspDto::new).list());
List<CachedSetting<?>> settingsList = node.get().getListSettings();
if (trySearchingForSpecificSetting) {
settingsList = settingsList.stream().filter(setting -> setting.getName().equals(requestedNodeName)).collect(Collectors.toList());
}
Map<String, List<ClusterSettingJspDto>> customSettingsByName = new HashMap<>();
for (CachedSetting<?> setting : settingsList) {
ClusterSettingKey settingKey = new ClusterSettingKey(setting.getName(), null, null, null);
List<ClusterSetting> settingsInDb = clusterSettingDao.scanWithPrefix(settingKey).list();
Optional<ClusterSetting> mostSpecificSetting = clusterSettingService.getMostSpecificClusterSetting(settingsInDb);
boolean isActive = setting.getMostSpecificDatabeanValue().isPresent();
Scanner.of(settingsInDb).map(settingFromDb -> {
boolean isWinner = mostSpecificSetting.isPresent() && settingFromDb.equals(mostSpecificSetting.get());
ClusterSettingJspDto jspDto = new ClusterSettingJspDto(settingFromDb, isActive, isWinner);
return jspDto;
}).flush(customSettings -> customSettingsByName.put(setting.getName(), customSettings));
}
mav.put("listSettings", Scanner.of(settingsList).map(SettingJspDto::new).list());
mav.put("mightBeDevelopment", datarouterServerTypeDetector.mightBeDevelopment());
mav.put("mapListsCustomSettings", customSettingsByName);
return mav;
}
use of io.datarouter.storage.setting.cached.CachedSetting in project datarouter by hotpads.
the class ClusterSettingsHandler method putSettingFromParams.
/*-------------------------------- private -------------------------------*/
private ClusterSettingActionResultJson putSettingFromParams() {
ClusterSettingKey clusterSettingKey = parseClusterSettingKeyFromParams();
Optional<String> comment = parseCommentFromParams();
String value = params.optional("value").orElse(null);
var newClusterSetting = new ClusterSetting(clusterSettingKey, value);
Optional<CachedSetting<?>> setting = settingRootFinder.getSettingByName(newClusterSetting.getName());
ClusterSettingLogAction action;
Optional<ClusterSetting> dbSetting = clusterSettingDao.find(clusterSettingKey);
if (dbSetting.isPresent()) {
action = ClusterSettingLogAction.UPDATED;
} else {
action = ClusterSettingLogAction.INSERTED;
}
var result = new ClusterSettingActionResultJson(action);
if (setting.isPresent() && !setting.get().isValid(newClusterSetting.getValue())) {
String badNewValue = newClusterSetting.getValue();
String error = "Invalid value detected, setting did not accept new value: \"" + badNewValue + "\"";
return result.markError(error);
}
String oldValue = dbSetting.map(ClusterSetting::getValue).orElse("?");
String changedBy = getRequestorsUsername();
var log = new ClusterSettingLog(newClusterSetting, action, changedBy, comment.orElse(null));
clusterSettingDao.put(newClusterSetting);
clusterSettingLogDao.put(log);
sendEmail(log, oldValue);
recordChangelog(log.getKey().getName(), log.getAction().getPersistentString(), log.getChangedBy(), Optional.ofNullable(log.getComment()));
return result.markSuccess();
}
Aggregations