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());
}
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());
}
}
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());
}
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());
}
}
Aggregations