Search in sources :

Example 1 with CustomizableEnum

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

the class BaseCriteria method parseUrlParam.

protected Object parseUrlParam(Class<?> type, List<String> fieldParams) throws InstantiationException, IllegalAccessException {
    Object value = null;
    if (ReferenceDto.class.isAssignableFrom(type)) {
        value = type.newInstance();
        ((ReferenceDto) value).setUuid(fieldParams.get(0));
    } else if (Date.class.isAssignableFrom(type)) {
        try {
            value = new Date(Long.parseLong(fieldParams.get(0)));
        } catch (NumberFormatException e) {
        // ignore
        }
    } else if (Boolean.class.isAssignableFrom(type)) {
        value = Boolean.valueOf(fieldParams.get(0));
    } else if (Enum.class.isAssignableFrom(type)) {
        try {
            // noinspection unchecked
            value = Enum.valueOf((Class<Enum>) type, fieldParams.get(0));
        } catch (IllegalArgumentException e) {
        // ignore
        }
    } else if (String.class.isAssignableFrom(type)) {
        value = fieldParams.get(0);
    } else if (Integer.class.isAssignableFrom(type)) {
        value = Integer.valueOf(fieldParams.get(0));
    } else if (EpiWeek.class.isAssignableFrom(type)) {
        value = EpiWeek.fromUrlString(fieldParams.get(0));
    } else if (CustomizableEnum.class.isAssignableFrom(type)) {
        value = type.newInstance();
        ((CustomizableEnum) value).setValue(fieldParams.get(0));
    } else {
        throw new NotImplementedException(type.toString());
    }
    return value;
}
Also used : CustomizableEnum(de.symeda.sormas.api.customizableenum.CustomizableEnum) ReferenceDto(de.symeda.sormas.api.ReferenceDto) CustomizableEnum(de.symeda.sormas.api.customizableenum.CustomizableEnum) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Date(java.util.Date)

Example 2 with CustomizableEnum

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

the class BaseCriteria method toUrlParams.

public String toUrlParams() {
    StringBuilder urlFilter = new StringBuilder();
    String encoding = "UTF-8";
    try {
        for (Method getter : getClass().getDeclaredMethods()) {
            if (Modifier.isStatic(getter.getModifiers()) || Modifier.isPrivate(getter.getModifiers()) || !(getter.getName().startsWith("get") || getter.getName().startsWith("is")) || getter.isAnnotationPresent(IgnoreForUrl.class))
                continue;
            String propertyName = getter.getName();
            if (propertyName.startsWith("get")) {
                propertyName = propertyName.substring(3, 4).toLowerCase() + propertyName.substring(4);
            } else if (propertyName.startsWith("is")) {
                propertyName = propertyName.substring(2, 3).toLowerCase() + propertyName.substring(3);
            }
            Object value = getter.invoke(this);
            Class<?> type = getter.getReturnType();
            if (value != null) {
                if (urlFilter.length() > 0)
                    urlFilter.append('&');
                urlFilter.append(URLEncoder.encode(propertyName, encoding));
                urlFilter.append('=');
                String stringValue;
                if (ReferenceDto.class.isAssignableFrom(type)) {
                    stringValue = ((ReferenceDto) value).getUuid();
                } else if (Date.class.isAssignableFrom(type)) {
                    stringValue = String.valueOf(((Date) value).getTime());
                } else if (Boolean.class.isAssignableFrom(type)) {
                    stringValue = String.valueOf(((Boolean) value).booleanValue());
                } else if (Enum.class.isAssignableFrom(type)) {
                    stringValue = ((Enum<?>) value).name();
                } else if (String.class.isAssignableFrom(type)) {
                    stringValue = (String) value;
                } else if (Integer.class.isAssignableFrom(type)) {
                    stringValue = String.valueOf(value);
                } else if (EpiWeek.class.isAssignableFrom(type)) {
                    stringValue = ((EpiWeek) value).toUrlString();
                } else if (CriteriaDateType.class.isAssignableFrom(type)) {
                    stringValue = CriteriaDateTypeHelper.toUrlString((CriteriaDateType) value);
                } else if (CustomizableEnum.class.isAssignableFrom(type)) {
                    stringValue = ((CustomizableEnum) value).getValue();
                } else {
                    throw new NotImplementedException(type.toString());
                }
                urlFilter.append(URLEncoder.encode(stringValue, encoding));
            }
        }
    } catch (IllegalArgumentException | UnsupportedEncodingException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
    return urlFilter.toString();
}
Also used : CustomizableEnum(de.symeda.sormas.api.customizableenum.CustomizableEnum) NotImplementedException(org.apache.commons.lang3.NotImplementedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Method(java.lang.reflect.Method) Date(java.util.Date) InvocationTargetException(java.lang.reflect.InvocationTargetException) IgnoreForUrl(de.symeda.sormas.api.utils.IgnoreForUrl)

Example 3 with CustomizableEnum

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

the class ImportParserService method parseCustomizableEnum.

private CustomizableEnum parseCustomizableEnum(String v, Class<CustomizableEnum> clazz, String path) throws ImportErrorException {
    try {
        CustomizableEnum customizableEnum = clazz.newInstance();
        customizableEnum.setValue(v);
        return customizableEnum;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ImportErrorException(I18nProperties.getValidationError(Validations.importErrorCustomizableEnumValue, v, path));
    }
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) CustomizableEnum(de.symeda.sormas.api.customizableenum.CustomizableEnum)

Example 4 with CustomizableEnum

use of de.symeda.sormas.api.customizableenum.CustomizableEnum 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 5 with CustomizableEnum

use of de.symeda.sormas.api.customizableenum.CustomizableEnum 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)

Aggregations

CustomizableEnum (de.symeda.sormas.api.customizableenum.CustomizableEnum)5 Date (java.util.Date)3 Disease (de.symeda.sormas.api.Disease)2 Language (de.symeda.sormas.api.Language)2 CustomizableEnumTranslation (de.symeda.sormas.api.customizableenum.CustomizableEnumTranslation)2 CustomizableEnumType (de.symeda.sormas.api.customizableenum.CustomizableEnumType)2 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)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 CollectionUtils (org.apache.commons.collections4.CollectionUtils)2 NotImplementedException (org.apache.commons.lang3.NotImplementedException)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Dao (com.j256.ormlite.dao.Dao)1 ReferenceDto (de.symeda.sormas.api.ReferenceDto)1 CustomEnumNotFoundException (de.symeda.sormas.api.customizableenum.CustomEnumNotFoundException)1