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

fr.ird.observe.navigation.model.DtoModelNavigationModelSupport Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.navigation.model;

/*-
 * #%L
 * ObServe Toolkit :: Common Dto
 * %%
 * Copyright (C) 2017 - 2020 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import fr.ird.observe.dto.IdDto;
import io.ultreia.java4all.bean.AbstractJavaBean;
import io.ultreia.java4all.lang.Objects2;
import io.ultreia.java4all.util.ServiceLoaders;

import java.lang.reflect.ParameterizedType;
import java.util.Objects;

/**
 * Created by tchemit on 26/05/2018.
 *
 * @author Tony Chemit - [email protected]
 */
@SuppressWarnings("WeakerAccess")
public abstract class DtoModelNavigationModelSupport> extends AbstractJavaBean implements DtoModelNavigationModel {

    private final ImmutableList nodes;
    private final N root;

    protected DtoModelNavigationModelSupport() {
        @SuppressWarnings("unchecked")
        Class nodeType = (Class) ((ParameterizedType) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]).getRawType();
        ImmutableMap.Builder>[]> nodesAnnotationBuilders = ImmutableMap.builder();
        ImmutableMap.Builder>, N> parentBuilders = ImmutableMap.builder();
        this.nodes = Objects.requireNonNull(loadNodes(nodeType, nodesAnnotationBuilders, parentBuilders));
        this.root = Objects.requireNonNull(loadRoot(nodeType, parentBuilders.build(), nodesAnnotationBuilders.build()));
        root.addPropertyChangeListener(DtoModelNavigationNode.PROPERTY_ENABLED, e -> {
            boolean oldValue = (boolean) e.getOldValue();
            boolean newValue = (boolean) e.getNewValue();
            firePropertyChange(PROPERTY_ENABLED, oldValue, newValue);
        });
    }

    @Override
    public boolean accept(IdDto dto) {
        return nodes.stream().anyMatch(n -> n.accept(dto));
    }


    @SuppressWarnings("unchecked")
    @Override
    public  DtoModelNavigationNode getDtoNode(Class type) {
        return (DtoModelNavigationNode) nodes.stream().filter(n -> type.equals(n.getType())).findFirst().orElse(null);
    }

    @SuppressWarnings("unchecked")
    @Override
    public  DtoModelNavigationNode getNavigationNode(DtoModelNavigationNode selectNode) {
        DtoModelNavigationNode editNode = null;
        while (selectNode != null && editNode == null) {
            Class type = selectNode.getType();
            DtoModelNavigationNode n = getDtoNode(type);
            if (n == null) {
                selectNode = selectNode.getParent();
            } else {
                editNode = n;
            }
        }
        return (DtoModelNavigationNode) editNode;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  D getNode(Class type) {
        return (D) nodes.stream().filter(n -> type.equals(n.getClass())).findFirst().orElse(null);
    }

    @Override
    public N getRoot() {
        return root;
    }

    @Override
    public ImmutableList getNodes() {
        return nodes;
    }

    @Override
    public ImmutableList getNodesWithIds() {
        ImmutableList.Builder builder = ImmutableList.builder();
        root.getIds(builder);
        return builder.build();
    }

    protected ImmutableList loadNodes(Class nodeType,
                                         ImmutableMap.Builder>[]> nodesAnnotationBuilders,
                                         ImmutableMap.Builder>, N> parentBuilders) {
        ImmutableList.Builder nodesBuilders = ImmutableList.builder();
        for (Class thisNodeType : ServiceLoaders.loadTypes(nodeType)) {
            N node = Objects2.newInstance(thisNodeType);
            nodesBuilders.add(node);
            DtoModelNavigationNodeDefinition annotation = Objects.requireNonNull(node.getClass().getAnnotation(DtoModelNavigationNodeDefinition.class));
            Class>[] children = annotation.value();
            if (children.length > 0) {
                nodesAnnotationBuilders.put(node, children);
                for (Class> child : children) {
                    parentBuilders.put(child, node);
                }
            }
        }
        return nodesBuilders.build();
    }

    protected N loadRoot(Class nodeType, ImmutableMap>, N> parentMap, ImmutableMap>[]> annotations) {
        N root = null;

        for (N node : nodes) {
            @SuppressWarnings("SuspiciousMethodCalls") N parentNode = parentMap.get(node.getClass());
            Class>[] children = annotations.get(node);
            ImmutableList.Builder> childrenNodes = ImmutableList.builder();
            if (children != null) {
                ImmutableMap, N> nodesByClass = Maps.uniqueIndex(nodes, DtoModelNavigationNode::getClass);
                for (Class> child : children) {
                    childrenNodes.add(Objects.requireNonNull(nodesByClass.get(child), "can't find node of type: " + child));
                }
            }
            node.init(parentNode, childrenNodes.build());
        }
        for (N node : nodes) {
            if (node.isRoot()) {
                if (root != null) {
                    throw new IllegalStateException(String.format("Found two root node: %s and %s", root, node));
                }
                root = node;
            }
        }
        return Objects.requireNonNull(root, String.format("Could not find a root node of type: %s", nodeType));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DtoModelNavigationModelSupport that = (DtoModelNavigationModelSupport) o;
        return Objects.equals(nodes, that.nodes) &&
                Objects.equals(root, that.root);
    }

    @Override
    public int hashCode() {
        return Objects.hash(nodes, root);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy