Search in sources :

Example 1 with SettingKey

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;
}
Also used : Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) CacheProvider(org.hisp.dhis.cache.CacheProvider) SerializableOptional(org.hisp.dhis.system.util.SerializableOptional) List(java.util.List) Stream(java.util.stream.Stream) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Service(org.springframework.stereotype.Service) Map(java.util.Map) Cache(org.hisp.dhis.cache.Cache) Optional(java.util.Optional) SettingKey(org.hisp.dhis.setting.SettingKey) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) Transactional(org.springframework.transaction.annotation.Transactional) Serializable(java.io.Serializable) SettingKey(org.hisp.dhis.setting.SettingKey) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SettingKey

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;
}
Also used : Serializable(java.io.Serializable) UserSettingKey(org.hisp.dhis.user.UserSettingKey) SettingKey(org.hisp.dhis.setting.SettingKey)

Example 3 with SettingKey

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;
    }
}
Also used : Serializable(java.io.Serializable) SettingKey(org.hisp.dhis.setting.SettingKey) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with SettingKey

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;
    }
}
Also used : SerializableOptional(org.hisp.dhis.system.util.SerializableOptional) SettingKey(org.hisp.dhis.setting.SettingKey)

Example 5 with SettingKey

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()));
}
Also used : ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) ContentType(org.hisp.dhis.webapi.WebClient.ContentType) Body(org.hisp.dhis.webapi.WebClient.Body) WebClientUtils.assertStatus(org.hisp.dhis.webapi.utils.WebClientUtils.assertStatus) Autowired(org.springframework.beans.factory.annotation.Autowired) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) JsonObject(org.hisp.dhis.jsontree.JsonObject) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SettingKey(org.hisp.dhis.setting.SettingKey) Arrays.stream(java.util.Arrays.stream) SystemSettingManager(org.hisp.dhis.setting.SystemSettingManager) JsonObject(org.hisp.dhis.jsontree.JsonObject) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Aggregations

SettingKey (org.hisp.dhis.setting.SettingKey)5 Serializable (java.io.Serializable)3 SystemSettingManager (org.hisp.dhis.setting.SystemSettingManager)2 SerializableOptional (org.hisp.dhis.system.util.SerializableOptional)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Sets (com.google.common.collect.Sets)1 ArrayList (java.util.ArrayList)1 Arrays.stream (java.util.Arrays.stream)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Cache (org.hisp.dhis.cache.Cache)1 CacheProvider (org.hisp.dhis.cache.CacheProvider)1 DimensionalObject (org.hisp.dhis.common.DimensionalObject)1 JsonObject (org.hisp.dhis.jsontree.JsonObject)1 UserSettingKey (org.hisp.dhis.user.UserSettingKey)1 DhisControllerConvenienceTest (org.hisp.dhis.webapi.DhisControllerConvenienceTest)1