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