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

apoc.trigger.TriggerInfo Maven / Gradle / Ivy

There is a newer version: 5.24.0
Show newest version
package apoc.trigger;

import apoc.SystemPropertyKeys;
import apoc.util.Util;
import org.neo4j.graphdb.Node;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TriggerInfo {
    public String name;
    public String query;
    public Map selector;
    public Map params;
    public boolean installed = false;
    public boolean paused = false;

    public TriggerInfo(String name) {
        this.name = name;
    }

    public TriggerInfo(String name, String query) {
        this(name);
        this.query = query;
    }

    public TriggerInfo(String name, String query, Map selector, boolean installed, boolean paused) {
        this(name, query);
        this.selector = selector;
        this.installed = installed;
        this.paused = paused;
    }


    public TriggerInfo(String name, String query, Map selector, Map params, boolean installed, boolean paused) {
        this(name, query, selector, installed, paused);
        this.params = params;
    }

    public static TriggerInfo from(Map mapInfo, boolean installed, String name) {
        return new TriggerInfo(name,
                (String) mapInfo.get(SystemPropertyKeys.statement.name()),
                (Map) mapInfo.get(SystemPropertyKeys.selector.name()),
                (Map) mapInfo.get(SystemPropertyKeys.params.name()),
                installed,
                (boolean) mapInfo.getOrDefault(SystemPropertyKeys.paused.name(), true));
    }

    public static TriggerInfo from(Map mapInfo, boolean installed) {
        return from(mapInfo, installed, (String) mapInfo.get(SystemPropertyKeys.name.name()));
    }

    public static TriggerInfo fromNode(Node node, boolean installed) {
        // filter and transform node props to map
        final Map triggerMap = toTriggerMap(node);
        
        // transform map to TriggerInfo
        return from(triggerMap, installed);
    }
    
    private static Map toTriggerMap(Node node) {
        return node.getAllProperties()
                .entrySet().stream()
                .filter(e -> !SystemPropertyKeys.database.name().equals(e.getKey()))
                .collect(HashMap::new, // workaround for https://bugs.openjdk.java.net/browse/JDK-8148463
                        (mapAccumulator, e) -> {
                            Object value = List.of(SystemPropertyKeys.selector.name(), SystemPropertyKeys.params.name()).contains(e.getKey())
                                    ? Util.fromJson((String) e.getValue(), Map.class)
                                    : e.getValue();

                            mapAccumulator.put(e.getKey(), value);
                        }, HashMap::putAll);
    }

    // TODO: used only with deprecated procedures, it would be removed together with them
    public static TriggerInfo entryToTriggerInfo(Map.Entry e) {
        String name = e.getKey();
        if (e.getValue() instanceof Map) {
            try {
                Map value = (Map) e.getValue();
                return TriggerInfo.from(value, false, name);
            } catch (Exception ex) {
                return new TriggerInfo(name, ex.getMessage());
            }
        }
        return new TriggerInfo(name);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy