Search in sources :

Example 1 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class NodeRelationshipsCommand method execute.

// ~--- methods --------------------------------------------------------
/**
 * Fetch relationships for the given source node.
 *
 * @param sourceNode
 * @param relType can be null
 * @param dir
 *
 * @return a list of relationships
 * @throws FrameworkException
 */
public List<RelationshipInterface> execute(NodeInterface sourceNode, RelationshipType relType, Direction dir) throws FrameworkException {
    RelationshipFactory factory = new RelationshipFactory(securityContext);
    List<RelationshipInterface> result = new LinkedList<>();
    Node node = sourceNode.getNode();
    Iterable<Relationship> rels;
    if (node == null) {
        return Collections.EMPTY_LIST;
    }
    if (relType != null) {
        rels = node.getRelationships(dir, relType);
    } else {
        rels = node.getRelationships(dir);
    }
    try {
        for (Relationship r : rels) {
            result.add(factory.instantiate(r));
        }
    } catch (RuntimeException e) {
        logger.warn("Exception occured: ", e.getMessage());
    /**
     * ********* FIXME
     *
     * Here an exception occurs:
     *
     * org.neo4j.kernel.impl.nioneo.store.InvalidRecordException: Node[5] is neither firstNode[37781] nor secondNode[37782] for Relationship[188125]
     * at org.neo4j.kernel.impl.nioneo.xa.ReadTransaction.getMoreRelationships(ReadTransaction.java:131)
     * at org.neo4j.kernel.impl.nioneo.xa.NioNeoDbPersistenceSource$ReadOnlyResourceConnection.getMoreRelationships(NioNeoDbPersistenceSource.java:280)
     * at org.neo4j.kernel.impl.persistence.PersistenceManager.getMoreRelationships(PersistenceManager.java:100)
     * at org.neo4j.kernel.impl.core.NodeManager.getMoreRelationships(NodeManager.java:585)
     * at org.neo4j.kernel.impl.core.NodeImpl.getMoreRelationships(NodeImpl.java:358)
     * at org.neo4j.kernel.impl.core.IntArrayIterator.hasNext(IntArrayIterator.java:115)
     */
    }
    return result;
}
Also used : Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) LinkedList(java.util.LinkedList)

Example 2 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class CypherQueryCommand method handleObject.

final Object handleObject(final NodeFactory nodeFactory, final RelationshipFactory relFactory, final String key, final Object value, boolean includeHiddenAndDeleted, boolean publicOnly, int level) throws FrameworkException {
    GraphObject graphObject = null;
    if (value instanceof Node) {
        graphObject = nodeFactory.instantiate((Node) value, includeHiddenAndDeleted, publicOnly);
    } else if (value instanceof Relationship) {
        graphObject = relFactory.instantiate((Relationship) value);
    } else if (value instanceof Map) {
        final Map<String, Object> valueMap = (Map<String, Object>) value;
        graphObject = new GraphObjectMap();
        for (final Entry<String, Object> valueEntry : valueMap.entrySet()) {
            final String valueKey = valueEntry.getKey();
            final Object valueValue = valueEntry.getValue();
            graphObject.setProperty(new GenericProperty(valueKey), handleObject(nodeFactory, relFactory, valueKey, valueValue, includeHiddenAndDeleted, publicOnly, level + 1));
        }
    } else if (value instanceof Collection) {
        final Collection<Object> valueCollection = (Collection<Object>) value;
        final List<Object> collection = new LinkedList<>();
        for (final Object valueEntry : valueCollection) {
            collection.add(handleObject(nodeFactory, relFactory, null, valueEntry, includeHiddenAndDeleted, publicOnly, level + 1));
        }
        return collection;
    } else if (level == 0) {
        graphObject = new GraphObjectMap();
        graphObject.setProperty(new GenericProperty(key), value);
    } else {
        return value;
    }
    return graphObject;
}
Also used : Node(org.structr.api.graph.Node) GraphObject(org.structr.core.GraphObject) Relationship(org.structr.api.graph.Relationship) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 3 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class DeleteRelationshipCommand method execute.

public Object execute(final RelationshipInterface rel, final boolean passiveDeletion) {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    if (graphDb != null && rel != null) {
        if (rel.getProperty(AbstractRelationship.id) == null) {
            logger.warn("Will not delete relationship which has no UUID: {} --[:{}]-->{}", new Object[] { rel.getSourceNode(), rel.getType(), rel.getTargetNode() });
            return null;
        }
        final Relationship relToDelete = rel.getRelationship();
        final RelationshipInterface finalRel = rel;
        TransactionCommand.relationshipDeleted(securityContext.getCachedUser(), finalRel, passiveDeletion);
        // callback
        finalRel.onRelationshipDeletion();
        // remove object from index
        finalRel.removeFromIndex();
        // delete node in database
        relToDelete.delete();
    }
    return null;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) DatabaseService(org.structr.api.DatabaseService)

Example 4 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class AccessControlTest method test02SetDifferentPrincipalTypesAsOwner.

@Test
public void test02SetDifferentPrincipalTypesAsOwner() {
    try (final Tx tx = app.tx()) {
        final List<Principal> users = createTestNodes(Principal.class, 2);
        final Principal user1 = (Principal) users.get(0);
        final Group group1 = createTestNode(Group.class, "test group");
        final TestOne t1 = createTestNode(TestOne.class);
        t1.setProperty(AbstractNode.owner, user1);
        t1.setProperty(AbstractNode.owner, group1);
        assertEquals(group1, t1.getProperty(AbstractNode.owner));
        Ownership ownerRel = t1.getIncomingRelationship(PrincipalOwnsNode.class);
        assertNotNull(ownerRel);
        // Do additional low-level check here to ensure cardinality!
        List<Relationship> incomingRels = Iterables.toList(t1.getNode().getRelationships(Direction.INCOMING, new PrincipalOwnsNode()));
        assertEquals(1, incomingRels.size());
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Group(org.structr.core.entity.Group) Ownership(org.structr.core.entity.relationship.Ownership) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relationship(org.structr.api.graph.Relationship) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 5 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class CypherTest method test01DeleteAfterLookupWithCypherInTransaction.

@Test
public void test01DeleteAfterLookupWithCypherInTransaction() {
    try {
        final TestSix testSix = this.createTestNode(TestSix.class);
        final TestOne testOne = this.createTestNode(TestOne.class);
        SixOneOneToOne rel = null;
        assertNotNull(testSix);
        assertNotNull(testOne);
        try (final Tx tx = app.tx()) {
            rel = app.create(testSix, testOne, SixOneOneToOne.class);
            tx.success();
        }
        assertNotNull(rel);
        DatabaseService graphDb = app.command(GraphDatabaseCommand.class).execute();
        try (final Tx tx = app.tx()) {
            NativeResult<Relationship> result = graphDb.execute("MATCH (n)<-[r:ONE_TO_ONE]-() RETURN r");
            final Iterator<Relationship> rels = result.columnAs("r");
            assertTrue(rels.hasNext());
            rels.next().delete();
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            rel.getUuid();
            fail("Accessing a deleted relationship should thow an exception.");
            tx.success();
        } catch (NotFoundException iex) {
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relationship(org.structr.api.graph.Relationship) NotFoundException(org.structr.api.NotFoundException) TestOne(org.structr.core.entity.TestOne) DatabaseService(org.structr.api.DatabaseService) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) GraphDatabaseCommand(org.structr.core.graph.GraphDatabaseCommand) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Aggregations

Relationship (org.structr.api.graph.Relationship)26 Node (org.structr.api.graph.Node)13 LinkedList (java.util.LinkedList)7 LinkedHashMap (java.util.LinkedHashMap)6 DatabaseService (org.structr.api.DatabaseService)6 GraphObject (org.structr.core.GraphObject)6 AbstractNode (org.structr.core.entity.AbstractNode)6 AbstractRelationship (org.structr.core.entity.AbstractRelationship)6 RelationshipFactory (org.structr.core.graph.RelationshipFactory)5 HashMap (java.util.HashMap)4 FrameworkException (org.structr.common.error.FrameworkException)4 List (java.util.List)3 Test (org.junit.Test)3 SessionTransaction (org.structr.bolt.SessionTransaction)3 RelationshipRelationshipMapper (org.structr.bolt.mapper.RelationshipRelationshipMapper)3 App (org.structr.core.app.App)3 StructrApp (org.structr.core.app.StructrApp)3 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)3 NodeFactory (org.structr.core.graph.NodeFactory)3 Tx (org.structr.core.graph.Tx)3