Search in sources :

Example 6 with AbstractRelationship

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

the class StartNodeGroup method getGroupedProperties.

@Override
public PropertyMap getGroupedProperties(SecurityContext securityContext, GraphObject source) {
    if (source instanceof AbstractRelationship) {
        RelationshipInterface rel = (RelationshipInterface) source;
        NodeInterface startNode = rel.getSourceNode();
        return super.getGroupedProperties(securityContext, startNode);
    }
    return null;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) RelationshipInterface(org.structr.core.graph.RelationshipInterface) NodeInterface(org.structr.core.graph.NodeInterface)

Example 7 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship 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 8 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship 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 9 with AbstractRelationship

use of org.structr.core.entity.AbstractRelationship 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)

Example 10 with AbstractRelationship

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

the class BasicTest method test06CascadeDeleteBidirectional.

/**
 * DELETE_INCOMING + DELETE_OUTGOING should trigger delete cascade from start to end node
 * and from end node to start node
 */
@Test
public void test06CascadeDeleteBidirectional() {
    try {
        // Create a relationship with DELETE_INCOMING
        AbstractRelationship rel = cascadeRel(TestOne.class, TestTwo.class, Relation.TARGET_TO_SOURCE | Relation.SOURCE_TO_TARGET);
        NodeInterface sourceNode;
        NodeInterface targetNode;
        String startNodeId;
        String endNodeId;
        try (final Tx tx = app.tx()) {
            startNodeId = rel.getSourceNode().getUuid();
            endNodeId = rel.getTargetNode().getUuid();
            sourceNode = rel.getSourceNode();
        }
        deleteCascade(sourceNode);
        try (final Tx tx = app.tx()) {
            // Start node should not be found after deletion
            assertNodeNotFound(startNodeId);
            // End node should not be found after deletion of start node
            assertNodeNotFound(endNodeId);
        }
        // Create a relationship with DELETE_INCOMING
        rel = cascadeRel(TestOne.class, TestTwo.class, Relation.TARGET_TO_SOURCE | Relation.SOURCE_TO_TARGET);
        try (final Tx tx = app.tx()) {
            startNodeId = rel.getSourceNode().getUuid();
            endNodeId = rel.getTargetNode().getUuid();
            targetNode = rel.getTargetNode();
        }
        deleteCascade(targetNode);
        try (final Tx tx = app.tx()) {
            // End node should not be found after deletion
            assertNodeNotFound(endNodeId);
            // Start node should not be found after deletion of end node
            assertNodeNotFound(startNodeId);
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestTwo(org.structr.core.entity.TestTwo) AbstractRelationship(org.structr.core.entity.AbstractRelationship) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Aggregations

AbstractRelationship (org.structr.core.entity.AbstractRelationship)31 NodeInterface (org.structr.core.graph.NodeInterface)15 FrameworkException (org.structr.common.error.FrameworkException)11 AbstractNode (org.structr.core.entity.AbstractNode)9 SecurityContext (org.structr.common.SecurityContext)7 GraphObject (org.structr.core.GraphObject)6 Tx (org.structr.core.graph.Tx)6 Test (org.junit.Test)4 DatabaseService (org.structr.api.DatabaseService)4 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 ArrayList (java.util.ArrayList)3 TestOne (org.structr.core.entity.TestOne)3 PropertyKey (org.structr.core.property.PropertyKey)3 Href (org.structr.api.util.html.attr.Href)2 TestTwo (org.structr.core.entity.TestTwo)2 CreationContainer (org.structr.core.graph.CreationContainer)2 DOMNode (org.structr.web.entity.dom.DOMNode)2 FileOutputStream (java.io.FileOutputStream)1 HashSet (java.util.HashSet)1