Search in sources :

Example 1 with CustomizableEnumType

use of de.symeda.sormas.api.customizableenum.CustomizableEnumType in project SORMAS-Project by hzi-braunschweig.

the class CustomizableEnumFacadeEjb method getEnumValues.

/**
 * @return Entries are currently not returned in any specific order
 */
@Lock(LockType.READ)
@Override
@SuppressWarnings("unchecked")
public <T extends CustomizableEnum> List<T> getEnumValues(CustomizableEnumType type, Disease disease) {
    Language language = I18nProperties.getUserLanguage();
    Class<T> enumClass = (Class<T>) type.getEnumClass();
    Optional<Disease> innerDisease = Optional.ofNullable(disease);
    if (!enumValuesByLanguage.get(enumClass).containsKey(language)) {
        fillLanguageCache(type, enumClass, language);
    }
    if (!enumValuesByDisease.get(enumClass).containsKey(innerDisease)) {
        fillDiseaseCache(type, enumClass, innerDisease);
    }
    Stream<String> diseaseValuesStream;
    if (innerDisease.isPresent()) {
        // combine specific and unspecific values
        diseaseValuesStream = Stream.concat(enumValuesByDisease.get(enumClass).get(innerDisease).stream(), enumValuesByDisease.get(enumClass).get(Optional.empty()).stream());
    } else {
        diseaseValuesStream = enumValuesByDisease.get(enumClass).get(Optional.empty()).stream();
    }
    return diseaseValuesStream.map(value -> buildCustomizableEnum(type, value, language, enumClass)).collect(Collectors.toList());
}
Also used : CustomEnumNotFoundException(de.symeda.sormas.api.customizableenum.CustomEnumNotFoundException) DtoHelper(de.symeda.sormas.backend.util.DtoHelper) Lock(javax.ejb.Lock) Date(java.util.Date) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) CustomizableEnumType(de.symeda.sormas.api.customizableenum.CustomizableEnumType) CustomizableEnumFacade(de.symeda.sormas.api.customizableenum.CustomizableEnumFacade) Map(java.util.Map) CustomizableEnumTranslation(de.symeda.sormas.api.customizableenum.CustomizableEnumTranslation) CustomizableEnumValueDto(de.symeda.sormas.api.customizableenum.CustomizableEnumValueDto) EJB(javax.ejb.EJB) ConfigFacadeEjb(de.symeda.sormas.backend.common.ConfigFacadeEjb) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) Language(de.symeda.sormas.api.Language) LockType(javax.ejb.LockType) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Stream(java.util.stream.Stream) Disease(de.symeda.sormas.api.Disease) Singleton(javax.ejb.Singleton) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) CustomizableEnum(de.symeda.sormas.api.customizableenum.CustomizableEnum) InvalidCustomizationException(de.symeda.sormas.api.utils.InvalidCustomizationException) Language(de.symeda.sormas.api.Language) Disease(de.symeda.sormas.api.Disease) Lock(javax.ejb.Lock)

Example 2 with CustomizableEnumType

use of de.symeda.sormas.api.customizableenum.CustomizableEnumType in project SORMAS-Project by hzi-braunschweig.

the class CustomizableEnumFacadeEjb method loadData.

@PostConstruct
@Override
public void loadData() {
    enumValueEntities.clear();
    enumValues.clear();
    enumValuesByLanguage.clear();
    enumValuesByDisease.clear();
    enumProperties.clear();
    for (CustomizableEnumType enumType : CustomizableEnumType.values()) {
        enumValueEntities.putIfAbsent(enumType, new ArrayList<>());
        enumValues.putIfAbsent(enumType, new ArrayList<>());
    }
    // Build list of customizable enums mapped by their enum type; other caches are built on-demand
    for (CustomizableEnumValue customizableEnumValue : service.getAll()) {
        CustomizableEnumType enumType = customizableEnumValue.getDataType();
        enumValueEntities.get(enumType).add(customizableEnumValue);
        String value = customizableEnumValue.getValue();
        if (enumValues.get(enumType).contains(value)) {
            throw new InvalidCustomizationException("Enum value " + value + " for customizable enum type " + enumType.toString() + " is not unique");
        }
        enumValues.get(enumType).add(value);
        enumProperties.putIfAbsent(enumType, new HashMap<>());
        enumProperties.get(enumType).putIfAbsent(customizableEnumValue.getValue(), customizableEnumValue.getProperties());
    }
    for (CustomizableEnumType enumType : CustomizableEnumType.values()) {
        Class<? extends CustomizableEnum> enumClass = enumType.getEnumClass();
        // access to contains has to be thread-safe
        enumValuesByLanguage.putIfAbsent(enumClass, new ConcurrentHashMap<>());
        enumValuesByDisease.putIfAbsent(enumClass, new ConcurrentHashMap<>());
        // Always add values for no disease because they are relevant in all cases
        fillDiseaseCache(enumType, enumClass, Optional.empty());
    }
}
Also used : CustomizableEnumType(de.symeda.sormas.api.customizableenum.CustomizableEnumType) InvalidCustomizationException(de.symeda.sormas.api.utils.InvalidCustomizationException) PostConstruct(javax.annotation.PostConstruct)

Example 3 with CustomizableEnumType

use of de.symeda.sormas.api.customizableenum.CustomizableEnumType in project SORMAS-Project by hzi-braunschweig.

the class CustomizableEnumValueDao method getEnumValues.

@SuppressWarnings("unchecked")
public <T extends CustomizableEnum> List<T> getEnumValues(CustomizableEnumType type, Disease disease) {
    if (customizableEnumsByType.isEmpty()) {
        loadData();
    }
    Language language = I18nProperties.getUserLanguage();
    Class<T> enumClass = (Class<T>) type.getEnumClass();
    initCaches(type, language);
    // Always add values for no disease because they are relevant in all cases
    if (!enumValuesByDisease.get(enumClass).containsKey(null)) {
        addValuesByDisease(type, enumClass, null);
    }
    if (!enumValuesByDisease.get(enumClass).containsKey(disease)) {
        addValuesByDisease(type, enumClass, disease);
    }
    List<T> enumValues = new ArrayList<>();
    enumValuesByLanguage.get(enumClass).get(language).forEach((value, caption) -> {
        T enumValue;
        try {
            enumValue = enumClass.newInstance();
            enumValue.setValue(value);
            enumValue.setCaption(caption);
            enumValue.setProperties(enumProperties.get(type).get(value));
            enumValues.add(enumValue);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    });
    return enumValues.stream().filter(e -> enumValuesByDisease.get(enumClass).get(disease).contains(e.getValue()) || enumValuesByDisease.get(enumClass).get(null).contains(e.getValue())).collect(Collectors.toList());
}
Also used : AbstractAdoDao(de.symeda.sormas.app.backend.common.AbstractAdoDao) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) HashMap(java.util.HashMap) Dao(com.j256.ormlite.dao.Dao) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Language(de.symeda.sormas.api.Language) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) CustomizableEnumType(de.symeda.sormas.api.customizableenum.CustomizableEnumType) List(java.util.List) Disease(de.symeda.sormas.api.Disease) Map(java.util.Map) ConfigProvider(de.symeda.sormas.app.backend.config.ConfigProvider) Optional(java.util.Optional) CustomizableEnum(de.symeda.sormas.api.customizableenum.CustomizableEnum) CustomizableEnumTranslation(de.symeda.sormas.api.customizableenum.CustomizableEnumTranslation) Language(de.symeda.sormas.api.Language) ArrayList(java.util.ArrayList)

Example 4 with CustomizableEnumType

use of de.symeda.sormas.api.customizableenum.CustomizableEnumType in project SORMAS-Project by hzi-braunschweig.

the class CustomizableEnumValueDao method loadData.

private void loadData() {
    customizableEnumsByType.clear();
    enumValuesByLanguage.clear();
    enumValuesByDisease.clear();
    enumProperties.clear();
    for (CustomizableEnumType enumType : CustomizableEnumType.values()) {
        customizableEnumsByType.putIfAbsent(enumType, new ArrayList<>());
    }
    for (CustomizableEnumValue customizableEnumValue : queryForAll()) {
        CustomizableEnumType enumType = customizableEnumValue.getDataType();
        customizableEnumsByType.get(enumType).add(customizableEnumValue);
        enumProperties.putIfAbsent(enumType, new HashMap<>());
        enumProperties.get(enumType).putIfAbsent(customizableEnumValue.getValue(), customizableEnumValue.getProperties());
    }
}
Also used : CustomizableEnumType(de.symeda.sormas.api.customizableenum.CustomizableEnumType)

Aggregations

CustomizableEnumType (de.symeda.sormas.api.customizableenum.CustomizableEnumType)4 Disease (de.symeda.sormas.api.Disease)2 Language (de.symeda.sormas.api.Language)2 CustomizableEnum (de.symeda.sormas.api.customizableenum.CustomizableEnum)2 CustomizableEnumTranslation (de.symeda.sormas.api.customizableenum.CustomizableEnumTranslation)2 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)2 InvalidCustomizationException (de.symeda.sormas.api.utils.InvalidCustomizationException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 PostConstruct (javax.annotation.PostConstruct)2 CollectionUtils (org.apache.commons.collections4.CollectionUtils)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Dao (com.j256.ormlite.dao.Dao)1 CustomEnumNotFoundException (de.symeda.sormas.api.customizableenum.CustomEnumNotFoundException)1 CustomizableEnumFacade (de.symeda.sormas.api.customizableenum.CustomizableEnumFacade)1 CustomizableEnumValueDto (de.symeda.sormas.api.customizableenum.CustomizableEnumValueDto)1