Search in sources :

Example 41 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class CreateRelationshipFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 3)) {
            return "";
        }
        final Object source = sources[0];
        final Object target = sources[1];
        final String relType = (String) sources[2];
        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.";
        }
        final Class relClass = StructrApp.getConfiguration().getRelationClassForCombinedType(sourceNode.getType(), relType, targetNode.getType());
        if (relClass != null) {
            return StructrApp.getInstance(sourceNode.getSecurityContext()).create(sourceNode, targetNode, relClass);
        } else {
            logger.warn("Error: Unknown relationship type. Parameters: {}", getParametersAsString(sources));
            return "Error: Unknown relationship type";
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode)

Example 42 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class GetOutgoingRelationshipsFunction 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.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)) {
                    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.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)) {
                    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 43 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class HasRelationshipFunction 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.getRelationships()) {
                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)) || (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.getRelationships()) {
                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)) || (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 44 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class IsAllowedFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 3)) {
            return false;
        }
        if (sources[0] instanceof Principal) {
            final Principal principal = (Principal) sources[0];
            if (sources[1] instanceof AbstractNode) {
                final AbstractNode node = (AbstractNode) sources[1];
                if (sources[2] instanceof String) {
                    final String[] parts = ((String) sources[2]).split("[,]+");
                    boolean allowed = true;
                    for (final String part : parts) {
                        final String trimmedPart = part.trim();
                        if (trimmedPart.length() > 0) {
                            final Permission permission = Permissions.valueOf(trimmedPart);
                            if (permission != null) {
                                allowed &= node.isGranted(permission, SecurityContext.getInstance(principal, AccessMode.Backend));
                            } else {
                                logger.warn("Error: unknown permission \"{}\". Parameters: {}", new Object[] { trimmedPart, getParametersAsString(sources) });
                                return "Error: unknown permission " + trimmedPart;
                            }
                        }
                    }
                    return allowed;
                } else {
                    logger.warn("Error: third argument is not a string. Parameters: {}", getParametersAsString(sources));
                    return "Error: third argument is not a string.";
                }
            } else {
                logger.warn("Error: second argument is not a node. Parameters: {}", getParametersAsString(sources));
                return "Error: second argument is not a node.";
            }
        } else {
            logger.warn("Error: first argument is not of type Principal. Parameters: {}", getParametersAsString(sources));
            return "Error: first argument is not of type Principal.";
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Permission(org.structr.common.Permission) Principal(org.structr.core.entity.Principal)

Example 45 with AbstractNode

use of org.structr.core.entity.AbstractNode in project structr by structr.

the class SearchAndSortingTest method test01SearchSingleNodeByName.

@Test
public void test01SearchSingleNodeByName() {
    try {
        PropertyMap props = new PropertyMap();
        final PropertyKey key = AbstractNode.name;
        final String name = "89w3hkl sdfghsdkljth";
        props.put(key, name);
        final AbstractNode node = createTestNode(TestOne.class, props);
        Result result = null;
        try (final Tx tx = app.tx()) {
            result = app.nodeQuery(TestOne.class).andName(name).includeDeletedAndHidden().getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(node));
        }
        // Change name attribute and search again
        final String name2 = "klppptzoehi gösoiu tzüw0e9hg";
        try (final Tx tx = app.tx()) {
            node.setProperty(key, name2);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            result = app.nodeQuery(TestOne.class).andName(name2).includeDeletedAndHidden().getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(node));
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Aggregations

AbstractNode (org.structr.core.entity.AbstractNode)62 FrameworkException (org.structr.common.error.FrameworkException)31 Tx (org.structr.core.graph.Tx)20 App (org.structr.core.app.App)18 StructrApp (org.structr.core.app.StructrApp)18 GraphObject (org.structr.core.GraphObject)17 SecurityContext (org.structr.common.SecurityContext)16 PropertyMap (org.structr.core.property.PropertyMap)12 Result (org.structr.core.Result)10 Test (org.junit.Test)9 AbstractRelationship (org.structr.core.entity.AbstractRelationship)9 LinkedList (java.util.LinkedList)8 TestOne (org.structr.core.entity.TestOne)8 DatabaseService (org.structr.api.DatabaseService)7 NodeInterface (org.structr.core.graph.NodeInterface)7 PropertyKey (org.structr.core.property.PropertyKey)7 Principal (org.structr.core.entity.Principal)6 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)5 DOMNode (org.structr.web.entity.dom.DOMNode)5 LinkedHashSet (java.util.LinkedHashSet)4