Search in sources :

Example 26 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class UiSyncCommand method doExport.

// ----- private methods -----
private void doExport(final String fileName) throws FrameworkException {
    // collect all nodes etc that belong to the frontend (including files)
    // and export them to the given output file
    final Set<RelationshipInterface> rels = new LinkedHashSet<>();
    final Set<NodeInterface> nodes = new LinkedHashSet<>();
    final Set<String> filePaths = new LinkedHashSet<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        // collect folders that are marked for export
        for (final Folder folder : app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "includeInFrontendExport"), true).getAsList()) {
            collectDataRecursively(app, folder, nodes, rels, filePaths);
        }
        // collect pages (including files, shared components etc.)
        for (final Page page : app.nodeQuery(Page.class).getAsList()) {
            collectDataRecursively(app, page, nodes, rels, filePaths);
        }
        SyncCommand.exportToFile(fileName, nodes, rels, filePaths, true);
        tx.success();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Page(org.structr.web.entity.dom.Page) Folder(org.structr.web.entity.Folder) NodeInterface(org.structr.core.graph.NodeInterface)

Example 27 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class UserConsoleCommand method handleDelete.

private void handleDelete(final SecurityContext securityContext, final Writable writable, final String name, final String confirm) throws FrameworkException, IOException {
    if (StringUtils.isEmpty(name)) {
        throw new FrameworkException(422, "Missing user name for delete command.");
    }
    final Class<? extends NodeInterface> type = StructrApp.getConfiguration().getNodeEntityClass("User");
    final App app = StructrApp.getInstance(securityContext);
    if (type != null) {
        try (final Tx tx = app.tx()) {
            NodeInterface user = app.nodeQuery(type).andName(name).getFirst();
            if (user == null) {
                user = app.get(type, name);
            }
            if (user != null) {
                if (user.getProperty(Principal.ownedNodes).isEmpty()) {
                    app.delete(user);
                    writable.println("User deleted.");
                } else {
                    final String hash = user.getUuid().substring(7, 11);
                    if (confirm == null || !confirm.equals(hash)) {
                        writable.print("User '");
                        writable.print(name);
                        writable.print("' has owned nodes, please confirm deletion with 'user delete ");
                        writable.print(name);
                        writable.print(" ");
                        writable.print(hash);
                        writable.println("'.");
                    } else {
                        app.delete(user);
                        writable.println("User deleted.");
                    }
                }
            } else {
                throw new FrameworkException(422, "User " + name + " not found.");
            }
            tx.success();
        }
    } else {
        throw new FrameworkException(422, "Cannot delete user, no User class found.");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) NodeInterface(org.structr.core.graph.NodeInterface)

Example 28 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class GetIncomingRelationshipsFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final List<AbstractRelationship> list = new ArrayList<>();
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && s.equals(targetNode) && t.equals(sourceNode)) {
                    list.add(rel);
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && rel.getRelType().name().equals(relType) && s.equals(targetNode) && t.equals(sourceNode)) {
                    list.add(rel);
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return list;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) ArrayList(java.util.ArrayList) NodeInterface(org.structr.core.graph.NodeInterface)

Example 29 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class HasIncomingRelationshipFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && s.equals(targetNode) && t.equals(sourceNode)) {
                    return true;
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getIncomingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && rel.getRelType().name().equals(relType) && s.equals(targetNode) && t.equals(sourceNode)) {
                    return true;
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return false;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface)

Example 30 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class HasOutgoingRelationshipFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && s.equals(sourceNode) && t.equals(targetNode)) {
                    return true;
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && rel.getRelType().name().equals(relType) && s.equals(sourceNode) && t.equals(targetNode)) {
                    return true;
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return false;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12