Search in sources :

Example 1 with CountryFieldVisibilityChecker

use of de.symeda.sormas.api.utils.fieldvisibility.checkers.CountryFieldVisibilityChecker in project SORMAS-Project by hzi-braunschweig.

the class ImportFacadeEjb method appendListOfFields.

/**
 * Builds a list of all fields in the case and its relevant sub entities. IMPORTANT: The order
 * is not guaranteed; at the time of writing, clazz.getDeclaredFields() seems to return the
 * fields in the order of declaration (which is what we need here), but that could change
 * in the future.
 */
private void appendListOfFields(List<ImportColumn> importColumns, Class<?> clazz, String prefix, char separator) {
    for (Field field : clazz.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        }
        String currentCountry = configFacade.getCountryCode();
        CountryFieldVisibilityChecker visibilityChecker = new CountryFieldVisibilityChecker(currentCountry);
        if (!visibilityChecker.isVisible(field)) {
            continue;
        }
        Method readMethod;
        try {
            readMethod = clazz.getDeclaredMethod("get" + WordUtils.capitalize(field.getName()));
        } catch (NoSuchMethodException e) {
            try {
                readMethod = clazz.getDeclaredMethod("is" + WordUtils.capitalize(field.getName()));
            } catch (NoSuchMethodException f) {
                continue;
            }
        }
        // Fields without a getter or whose getters are declared in a superclass are ignored
        if (readMethod.getDeclaringClass() != clazz) {
            continue;
        }
        // Fields with the @ImportIgnore annotation are ignored
        if (readMethod.isAnnotationPresent(ImportIgnore.class)) {
            continue;
        }
        // Fields that are depending on a certain feature type to be active may be ignored
        if (readMethod.isAnnotationPresent(DependingOnFeatureType.class)) {
            List<FeatureType> activeServerFeatures = featureConfigurationFacade.getActiveServerFeatureTypes();
            if (!activeServerFeatures.isEmpty() && !activeServerFeatures.contains(readMethod.getAnnotation(DependingOnFeatureType.class).featureType())) {
                continue;
            }
        }
        // List types are ignored
        if (Collection.class.isAssignableFrom(field.getType())) {
            continue;
        }
        // Certain field types are ignored
        if (field.getType() == UserReferenceDto.class) {
            continue;
        }
        // Other non-infrastructure EntityDto/ReferenceDto classes, recursively call this method to include fields of the sub-entity
        if (EntityDto.class.isAssignableFrom(field.getType()) && !isInfrastructureClass(field.getType())) {
            appendListOfFields(importColumns, field.getType(), StringUtils.isEmpty(prefix) ? field.getName() + "." : prefix + field.getName() + ".", separator);
        } else if (PersonReferenceDto.class.isAssignableFrom(field.getType()) && !isInfrastructureClass(field.getType())) {
            appendListOfFields(importColumns, PersonDto.class, StringUtils.isEmpty(prefix) ? field.getName() + "." : prefix + field.getName() + ".", separator);
            addPrimaryPhoneAndEmail(separator, importColumns);
        } else {
            importColumns.add(ImportColumn.from(clazz, prefix + field.getName(), field.getType(), separator));
        }
    }
}
Also used : Field(java.lang.reflect.Field) FeatureType(de.symeda.sormas.api.feature.FeatureType) DependingOnFeatureType(de.symeda.sormas.api.utils.DependingOnFeatureType) EntityDto(de.symeda.sormas.api.EntityDto) CountryFieldVisibilityChecker(de.symeda.sormas.api.utils.fieldvisibility.checkers.CountryFieldVisibilityChecker) PersonDto(de.symeda.sormas.api.person.PersonDto) Method(java.lang.reflect.Method)

Example 2 with CountryFieldVisibilityChecker

use of de.symeda.sormas.api.utils.fieldvisibility.checkers.CountryFieldVisibilityChecker in project SORMAS-Project by hzi-braunschweig.

the class ImportExportUtils method getExportProperties.

private static List<ExportPropertyMetaInfo> getExportProperties(Class<?> exportDtoClass, PropertyTypeFilter filterExportGroup, PropertyCaptionProvider propertyCaptionProvider, String countryLocale) {
    List<Method> readMethods = new ArrayList<>();
    for (Method method : exportDtoClass.getDeclaredMethods()) {
        if ((!method.getName().startsWith("get") && !method.getName().startsWith("is")) || !method.isAnnotationPresent(ExportGroup.class)) {
            continue;
        }
        readMethods.add(method);
    }
    Collections.sort(readMethods, new Comparator<Method>() {

        @Override
        public int compare(Method m1, Method m2) {
            return Integer.compare(getOrderValue(m1), getOrderValue(m2));
        }
    });
    CountryFieldVisibilityChecker countryFieldVisibilityChecker = new CountryFieldVisibilityChecker(countryLocale);
    Set<String> combinedProperties = new HashSet<>();
    List<ExportPropertyMetaInfo> properties = new ArrayList<>();
    for (Method method : readMethods) {
        if (!countryFieldVisibilityChecker.isVisible(method)) {
            continue;
        }
        ExportGroupType groupType = method.getAnnotation(ExportGroup.class).value();
        if (!filterExportGroup.accept(groupType)) {
            continue;
        }
        String[] propertyPath = method.getAnnotation(ExportProperty.class).value();
        String property = StringUtils.join(propertyPath, ".");
        if (method.getAnnotation(ExportProperty.class).combined()) {
            if (!combinedProperties.add(property)) {
                continue;
            }
        }
        // prepare ExportPropertyMetaInfo
        // In order to get the correct caption, we try to fetch the i18n-prefix of the methods declaring class
        String i18n_prefix = null;
        ExportEntity MethodClassEntity = method.getAnnotation(ExportEntity.class);
        if (MethodClassEntity != null) {
            try {
                i18n_prefix = (String) MethodClassEntity.value().getDeclaredField("I18N_PREFIX").get(null);
            } catch (NoSuchFieldException | IllegalAccessException ex) {
            // Field doesn't exist or is private
            }
        }
        properties.add(new ExportPropertyMetaInfo(property, propertyCaptionProvider.get(propertyPath[propertyPath.length - 1], i18n_prefix), groupType));
    }
    return properties;
}
Also used : CountryFieldVisibilityChecker(de.symeda.sormas.api.utils.fieldvisibility.checkers.CountryFieldVisibilityChecker) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HashSet(java.util.HashSet)

Aggregations

CountryFieldVisibilityChecker (de.symeda.sormas.api.utils.fieldvisibility.checkers.CountryFieldVisibilityChecker)2 Method (java.lang.reflect.Method)2 EntityDto (de.symeda.sormas.api.EntityDto)1 FeatureType (de.symeda.sormas.api.feature.FeatureType)1 PersonDto (de.symeda.sormas.api.person.PersonDto)1 DependingOnFeatureType (de.symeda.sormas.api.utils.DependingOnFeatureType)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1