All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.factoryfx.dom.rest.DynamicDataDictionary Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
package io.github.factoryfx.dom.rest;

import com.google.common.base.Strings;
import io.github.factoryfx.factory.FactoryBase;
import io.github.factoryfx.factory.attribute.dependency.FactoryAttribute;
import io.github.factoryfx.factory.attribute.dependency.FactoryBaseAttribute;
import io.github.factoryfx.factory.attribute.dependency.FactoryListAttribute;
import io.github.factoryfx.factory.attribute.dependency.FactoryListBaseAttribute;
import io.github.factoryfx.factory.attribute.types.ObjectValueAttribute;
import io.github.factoryfx.factory.metadata.AttributeMetadata;
import io.github.factoryfx.factory.metadata.FactoryMetadata;
import io.github.factoryfx.factory.metadata.FactoryMetadataManager;

import java.util.*;
import java.util.stream.Collectors;

public class DynamicDataDictionary {
    public HashMap classNameToItem = new HashMap<>();


    public DynamicDataDictionary(FactoryBase root){
        HashSet> factoryClasses = new HashSet<>();
        for (FactoryBase factoryBase : root.internal().collectChildrenDeep()) {
            factoryClasses.add(factoryBase.getClass());
            factoryBase.internal().visitAttributesMetadata((attributeMetadata) -> {
                if (attributeMetadata.referenceClass!=null){
                    factoryClasses.add(attributeMetadata.referenceClass);
                }
            });
        }
        fillClassaNameToItem(factoryClasses);

    }

    public DynamicDataDictionary(List> roots){
        HashSet> factoryClasses = new HashSet<>();
        for (FactoryBase root : roots) {
            root.internal().finalise();
            for (FactoryBase factoryBase : root.internal().collectChildrenDeep()) {
                factoryClasses.add(factoryBase.getClass());
                factoryBase.internal().visitAttributesMetadata((attributeMetadata) -> {
                    if (attributeMetadata.referenceClass!=null){
                        factoryClasses.add(attributeMetadata.referenceClass);
                    }
                });
            }
        }
        fillClassaNameToItem(factoryClasses);
    }

    private void fillClassaNameToItem(HashSet> factoryClasses) {

        for (Class factoryClass : factoryClasses) {
            DynamicDataDictionaryItem item = new DynamicDataDictionaryItem();
            classNameToItem.put(factoryClass.getName(), item);

            FactoryMetadata metadata = FactoryMetadataManager.getMetadataUnsafe(factoryClass);
            metadata.visitAttributeMetadata((attributeMetadata) -> {
                DynamicDataDictionaryAttributeItem attributeItem = new DynamicDataDictionaryAttributeItem(getAttributeType(attributeMetadata),
                        !attributeMetadata.required, getLabel(Locale.ENGLISH,attributeMetadata), getLabel(Locale.GERMAN,attributeMetadata),
                        getPossibleEnumValues(attributeMetadata)
                );
                if (!(ObjectValueAttribute.class.isAssignableFrom(attributeMetadata.attributeClass))) {
                    item.attributeNameToItem.put(attributeMetadata.attributeVariableName, attributeItem);
                }
            });
        }
    }

    private String getLabel(Locale locale, AttributeMetadata attributeMetadata) {
        String label = attributeMetadata.labelText.internal_getPreferred(locale);
        if (Strings.isNullOrEmpty(label)) {
            label=attributeMetadata.attributeVariableName;
        }
        return label;
    }

    private String getAttributeType(AttributeMetadata attributeMetadata) {
        if (FactoryBaseAttribute.class.isAssignableFrom(attributeMetadata.attributeClass)){
            return FactoryAttribute.class.getSimpleName();
        }
        if (FactoryListBaseAttribute.class.isAssignableFrom(attributeMetadata.attributeClass)){
            return FactoryListAttribute.class.getSimpleName();
        }
        return attributeMetadata.attributeClass.getSimpleName();
    }

    private List getPossibleEnumValues(AttributeMetadata attributeMetadata) {
        if (attributeMetadata.enumClass!=null){
            return Arrays.stream(attributeMetadata.enumClass.getEnumConstants()).map(Object::toString).collect(Collectors.toList());
        }
        return List.of();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy