Search in sources :

Example 21 with RelationshipInterface

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

the class BasicTest method testRelationshipsOnNodeCreation.

@Test
public void testRelationshipsOnNodeCreation() {
    Principal user = null;
    TestOne test = null;
    // create user
    try (final Tx tx = app.tx()) {
        user = app.create(Principal.class, "tester");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final SecurityContext ctx = SecurityContext.getInstance(user, AccessMode.Backend);
    final App app = StructrApp.getInstance(ctx);
    // create object with user context
    try (final Tx tx = app.tx()) {
        test = app.create(TestOne.class);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // query for relationships
    try (final Tx tx = app.tx()) {
        final List<? extends RelationshipInterface> rels1 = app.relationshipQuery().and(AbstractRelationship.sourceId, user.getUuid()).getAsList();
        final List<Class> classes1 = rels1.stream().map(r -> r.getClass()).collect(Collectors.toList());
        assertEquals("Invalid number of relationships after object creation", 2, rels1.size());
        assertTrue("Invalid relationship type after object creation", classes1.contains(Security.class));
        assertTrue("Invalid relationship type after object creation", classes1.contains(PrincipalOwnsNode.class));
        final List<? extends RelationshipInterface> rels2 = app.relationshipQuery().and(AbstractRelationship.targetId, test.getUuid()).getAsList();
        final List<Class> classes2 = rels2.stream().map(r -> r.getClass()).collect(Collectors.toList());
        assertEquals("Invalid number of relationships after object creation", 2, rels2.size());
        assertTrue("Invalid relationship type after object creation", classes2.contains(Security.class));
        assertTrue("Invalid relationship type after object creation", classes2.contains(PrincipalOwnsNode.class));
        final List<? extends RelationshipInterface> rels3 = Iterables.toList(test.getIncomingRelationships());
        final List<Class> classes3 = rels3.stream().map(r -> r.getClass()).collect(Collectors.toList());
        assertEquals("Invalid number of relationships after object creation", 2, rels3.size());
        assertTrue("Invalid relationship type after object creation", classes3.contains(Security.class));
        assertTrue("Invalid relationship type after object creation", classes3.contains(PrincipalOwnsNode.class));
        final Security sec = app.relationshipQuery(Security.class).getFirst();
        assertNotNull("Relationship caching on node creation is broken", sec);
        final PrincipalOwnsNode owns = app.relationshipQuery(PrincipalOwnsNode.class).getFirst();
        assertNotNull("Relationship caching on node creation is broken", owns);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) LoggerFactory(org.slf4j.LoggerFactory) RelationshipType(org.structr.api.graph.RelationshipType) TestTen(org.structr.core.entity.TestTen) StringUtils(org.apache.commons.lang3.StringUtils) GenericNode(org.structr.core.entity.GenericNode) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) StringProperty(org.structr.core.property.StringProperty) Assert.fail(org.junit.Assert.fail) Location(org.structr.core.entity.Location) ResourceAccess(org.structr.core.entity.ResourceAccess) OneThreeOneToOne(org.structr.core.entity.OneThreeOneToOne) NodeHasLocation(org.structr.core.entity.relationship.NodeHasLocation) AbstractRelationship(org.structr.core.entity.AbstractRelationship) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) NotInTransactionException(org.structr.api.NotInTransactionException) GraphObject(org.structr.core.GraphObject) OneTwoOneToOne(org.structr.core.entity.OneTwoOneToOne) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SixOneManyToMany(org.structr.core.entity.SixOneManyToMany) List(java.util.List) DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) StructrApp(org.structr.core.app.StructrApp) TestOne(org.structr.core.entity.TestOne) Security(org.structr.core.entity.Security) TestSix(org.structr.core.entity.TestSix) PropertyMap(org.structr.core.property.PropertyMap) NotFoundException(org.structr.api.NotFoundException) NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyKey(org.structr.core.property.PropertyKey) TestNine(org.structr.core.entity.TestNine) Result(org.structr.core.Result) LinkedList(java.util.LinkedList) AbstractNode(org.structr.core.entity.AbstractNode) TestTwo(org.structr.core.entity.TestTwo) NodeInterface(org.structr.core.graph.NodeInterface) SixThreeOneToMany(org.structr.core.entity.SixThreeOneToMany) Logger(org.slf4j.Logger) Iterables(org.structr.api.util.Iterables) GenericRelationship(org.structr.core.entity.GenericRelationship) Assert.assertNotNull(org.junit.Assert.assertNotNull) Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) Principal(org.structr.core.entity.Principal) Relation(org.structr.core.entity.Relation) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) TestSeven(org.structr.core.entity.TestSeven) TestThree(org.structr.core.entity.TestThree) TestFour(org.structr.core.entity.TestFour) NodeServiceCommand(org.structr.core.graph.NodeServiceCommand) Localization(org.structr.core.entity.Localization) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SchemaNode(org.structr.core.entity.SchemaNode) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Security(org.structr.core.entity.Security) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 22 with RelationshipInterface

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

the class CloudConnection method storeRelationship.

public RelationshipInterface storeRelationship(final DataContainer receivedData) throws FrameworkException {
    final RelationshipDataContainer receivedRelationshipData = (RelationshipDataContainer) receivedData;
    final String sourceStartNodeId = receivedRelationshipData.getSourceStartNodeId();
    final String sourceEndNodeId = receivedRelationshipData.getSourceEndNodeId();
    final String uuid = receivedRelationshipData.getRelationshipId();
    // if end node ID was not found in the ID map,
    // assume it already exists in the database
    // (i.e. it was created earlier)
    String targetStartNodeId = idMap.get(sourceStartNodeId);
    if (targetStartNodeId == null) {
        targetStartNodeId = sourceStartNodeId;
    }
    // if end node ID was not found in the ID map,
    // assume it already exists in the database
    // (i.e. it was created earlier)
    String targetEndNodeId = idMap.get(sourceEndNodeId);
    if (targetEndNodeId == null) {
        targetEndNodeId = sourceEndNodeId;
    }
    if (targetStartNodeId != null && targetEndNodeId != null) {
        // Get new start and end node
        final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
        final NodeInterface targetStartNode = app.getNodeById(targetStartNodeId);
        final NodeInterface targetEndNode = app.getNodeById(targetEndNodeId);
        final String typeName = receivedRelationshipData.getType();
        final Class relType = config.getRelationshipEntityClass(typeName);
        if (targetStartNode != null && targetEndNode != null) {
            final RelationshipInterface existingCandidate = app.relationshipQuery().and(GraphObject.id, uuid).includeDeletedAndHidden().getFirst();
            count++;
            total++;
            if (existingCandidate != null) {
                // merge properties?
                existingCandidate.setProperties(securityContext, PropertyMap.databaseTypeToJavaType(securityContext, relType, receivedRelationshipData.getProperties()));
                return existingCandidate;
            } else {
                final PropertyMap properties = PropertyMap.databaseTypeToJavaType(securityContext, relType, receivedRelationshipData.getProperties());
                return app.create(targetStartNode, targetEndNode, relType, properties);
            }
        } else {
            logger.warn("Could not store relationship {} -> {}", new Object[] { targetStartNode, targetEndNode });
        }
    }
    logger.warn("Could not store relationship {} -> {}", new Object[] { sourceStartNodeId, sourceEndNodeId });
    return null;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) RelationshipDataContainer(org.structr.cloud.message.RelationshipDataContainer) SecurityContext(org.structr.common.SecurityContext) RelationshipInterface(org.structr.core.graph.RelationshipInterface) NodeInterface(org.structr.core.graph.NodeInterface)

Example 23 with RelationshipInterface

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

the class UpdateTransmission method doRemote.

@Override
public Boolean doRemote(final CloudConnection client) throws IOException, FrameworkException {
    // send synchronization request first
    client.send(new Synchronize());
    // send all node and relationship data
    final DatabaseService graphDb = StructrApp.getInstance().getDatabaseService();
    final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
    final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
    for (final Node neo4jNode : graphDb.getAllNodes()) {
        final NodeInterface node = nodeFactory.instantiate(neo4jNode);
        if (node instanceof File) {
            PushTransmission.sendFile(client, (File) node, CloudService.CHUNK_SIZE);
        } else {
            client.send(new NodeDataContainer(node, 0));
        }
    }
    for (final Relationship relationship : graphDb.getAllRelationships()) {
        final RelationshipInterface relationshipInterface = relFactory.instantiate(relationship);
        client.send(new RelationshipDataContainer(relationshipInterface, 0));
    }
    // wait for end of transmission
    client.waitForTransmission();
    return true;
}
Also used : NodeFactory(org.structr.core.graph.NodeFactory) RelationshipDataContainer(org.structr.cloud.message.RelationshipDataContainer) RelationshipFactory(org.structr.core.graph.RelationshipFactory) Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) RelationshipInterface(org.structr.core.graph.RelationshipInterface) NodeDataContainer(org.structr.cloud.message.NodeDataContainer) DatabaseService(org.structr.api.DatabaseService) File(org.structr.dynamic.File) NodeInterface(org.structr.core.graph.NodeInterface)

Example 24 with RelationshipInterface

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

the class PullRelationship method onRequest.

@Override
public void onRequest(CloudConnection serverConnection) throws IOException, FrameworkException {
    final Object value = serverConnection.getValue(key + "Rels");
    if (value instanceof List) {
        final List<RelationshipInterface> relationships = (List<RelationshipInterface>) value;
        final RelationshipInterface relationship = relationships.get(nodeIndex);
        serverConnection.send(new RelationshipDataContainer(relationship, nodeIndex));
    }
}
Also used : RelationshipInterface(org.structr.core.graph.RelationshipInterface) List(java.util.List)

Example 25 with RelationshipInterface

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

the class UiSyncCommand method collectDataRecursively.

private void collectDataRecursively(final App app, final GraphObject root, final Set<NodeInterface> nodes, final Set<RelationshipInterface> rels, final Set<String> files) throws FrameworkException {
    if (root.isNode()) {
        final NodeInterface node = root.getSyncNode();
        if (node instanceof File) {
            final String fileUuid = node.getUuid();
            files.add(fileUuid);
        }
        // add node to set, recurse if not already present
        if (nodes.add(node)) {
            final List<GraphObject> syncData = node.getSyncData();
            if (syncData != null) {
                for (final GraphObject obj : syncData) {
                    // syncData can contain null objects!
                    if (obj != null) {
                        collectDataRecursively(app, obj, nodes, rels, files);
                    }
                }
            } else {
                logger.warn("Node {} returned null syncData!", node);
            }
        }
    } else if (root.isRelationship()) {
        final RelationshipInterface rel = root.getSyncRelationship();
        // add node to set, recurse if not already present
        if (rels.add(rel)) {
            final List<GraphObject> syncData = rel.getSyncData();
            if (syncData != null) {
                for (final GraphObject obj : syncData) {
                    // syncData can contain null objects!
                    if (obj != null) {
                        collectDataRecursively(app, obj, nodes, rels, files);
                    }
                }
            } else {
                logger.warn("Relationship {} returned null syncData!", rel);
            }
        }
    }
}
Also used : RelationshipInterface(org.structr.core.graph.RelationshipInterface) LinkedList(java.util.LinkedList) List(java.util.List) GraphObject(org.structr.core.GraphObject) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

RelationshipInterface (org.structr.core.graph.RelationshipInterface)28 NodeInterface (org.structr.core.graph.NodeInterface)15 GraphObject (org.structr.core.GraphObject)8 LinkedList (java.util.LinkedList)6 FrameworkException (org.structr.common.error.FrameworkException)6 PropertyMap (org.structr.core.property.PropertyMap)6 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 List (java.util.List)4 SecurityContext (org.structr.common.SecurityContext)4 AbstractNode (org.structr.core.entity.AbstractNode)4 GenericNode (org.structr.core.entity.GenericNode)4 Tx (org.structr.core.graph.Tx)4 RelationshipDataContainer (org.structr.cloud.message.RelationshipDataContainer)3 Result (org.structr.core.Result)3 PropertyKey (org.structr.core.property.PropertyKey)3 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 NotFoundException (org.structr.api.NotFoundException)2 RelationshipType (org.structr.api.graph.RelationshipType)2