use of org.hisp.dhis.setting.SettingKey in project dhis2-core by dhis2.
the class DefaultUserSettingService method getUserSettingsWithFallbackByUserAsMap.
@Override
@Transactional(readOnly = true)
public Map<String, Serializable> getUserSettingsWithFallbackByUserAsMap(User user, Set<UserSettingKey> userSettingKeys, boolean useFallback) {
Map<String, Serializable> result = Sets.newHashSet(getUserSettings(user)).stream().filter(userSetting -> userSetting != null && userSetting.getName() != null && userSetting.getValue() != null).collect(Collectors.toMap(UserSetting::getName, UserSetting::getValue));
userSettingKeys.forEach(userSettingKey -> {
if (!result.containsKey(userSettingKey.getName())) {
Optional<SettingKey> systemSettingKey = SettingKey.getByName(userSettingKey.getName());
if (useFallback && systemSettingKey.isPresent()) {
SettingKey setting = systemSettingKey.get();
result.put(userSettingKey.getName(), systemSettingManager.getSystemSetting(setting, setting.getClazz()));
} else {
result.put(userSettingKey.getName(), null);
}
}
});
return result;
}
use of org.hisp.dhis.setting.SettingKey in project dhis2-core by dhis2.
the class SystemSettingController method getSystemSettingOrTranslation.
private Serializable getSystemSettingOrTranslation(String key, String locale, User currentUser) {
Optional<SettingKey> settingKey = SettingKey.getByName(key);
if (!systemSettingManager.isConfidential(key) && settingKey.isPresent()) {
Optional<String> localeToFetch = getLocaleToFetch(locale, key, currentUser);
if (localeToFetch.isPresent()) {
Optional<String> translation = systemSettingManager.getSystemSettingTranslation(settingKey.get(), localeToFetch.get());
if (translation.isPresent()) {
return translation.get();
}
}
Serializable systemSetting = systemSettingManager.getSystemSetting(settingKey.get(), settingKey.get().getClazz());
if (systemSetting == null) {
return StringUtils.EMPTY;
}
return systemSetting;
}
return StringUtils.EMPTY;
}
use of org.hisp.dhis.setting.SettingKey in project dhis2-core by dhis2.
the class SystemSettingController method getSystemSettingAsText.
@RequestMapping(value = "/{key}", method = RequestMethod.GET, produces = ContextUtils.CONTENT_TYPE_TEXT)
@ResponseBody
public String getSystemSettingAsText(@PathVariable("key") String key) {
if (systemSettingManager.isConfidential(key)) {
return StringUtils.EMPTY;
} else {
Optional<SettingKey> settingKey = SettingKey.getByName(key);
Serializable setting;
if (settingKey.isPresent()) {
setting = systemSettingManager.getSystemSetting(settingKey.get());
} else {
setting = systemSettingManager.getSystemSetting(key);
}
return setting != null ? String.valueOf(setting) : null;
}
}
use of org.hisp.dhis.setting.SettingKey in project dhis2-core by dhis2.
the class DefaultUserSettingService method getUserSetting.
// -------------------------------------------------------------------------
// Private methods
// -------------------------------------------------------------------------
/**
* Returns a user setting optional. If the user settings does not have a
* value or default value, a corresponding system setting will be looked up.
*
* @param key the user setting key.
* @param user an optional {@link User}.
* @return an optional user setting value.
*/
private SerializableOptional getUserSetting(UserSettingKey key, Optional<User> user) {
if (key == null) {
return SerializableOptional.empty();
}
String username = user.isPresent() ? user.get().getUsername() : currentUserService.getCurrentUsername();
String cacheKey = getCacheKey(key.getName(), username);
SerializableOptional result = userSettingCache.get(cacheKey, c -> getUserSettingOptional(key, username));
if (!result.isPresent() && NAME_SETTING_KEY_MAP.containsKey(key.getName())) {
SettingKey settingKey = NAME_SETTING_KEY_MAP.get(key.getName());
return SerializableOptional.of(systemSettingManager.getSystemSetting(settingKey, settingKey.getClazz()));
} else {
return result;
}
}
use of org.hisp.dhis.setting.SettingKey in project dhis2-core by dhis2.
the class SystemSettingControllerTest method testGetSystemSettingsJson_AllKeys.
@Test
void testGetSystemSettingsJson_AllKeys() {
assertStatus(HttpStatus.OK, POST("/systemSettings/keyUiLocale?value=de"));
JsonObject setting = GET("/systemSettings").content(HttpStatus.OK);
assertTrue(setting.isObject());
stream(SettingKey.values()).filter(key -> !key.isConfidential() && key.getDefaultValue() != null).forEach(key -> assertTrue(setting.get(key.getName()).exists(), key.getName()));
stream(SettingKey.values()).filter(SettingKey::isConfidential).forEach(key -> assertFalse(setting.get(key.getName()).exists(), key.getName()));
}
Aggregations