Search in sources :

Example 31 with NodeInterface

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

the class IncomingFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 2)) {
        final RelationshipFactory factory = new RelationshipFactory(ctx.getSecurityContext());
        final Object source = sources[0];
        if (source instanceof NodeInterface) {
            final NodeInterface node = (NodeInterface) source;
            if (sources.length > 1) {
                final Object relType = sources[1];
                if (relType != null && relType instanceof String) {
                    final String relTypeName = (String) relType;
                    return factory.bulkInstantiate(node.getNode().getRelationships(Direction.INCOMING, RelationshipType.forName(relTypeName)));
                }
            } else {
                return factory.bulkInstantiate(node.getNode().getRelationships(Direction.INCOMING));
            }
        } else {
            logger.warn("Error: entity is not a node. Parameters: {}", getParametersAsString(sources));
            return "Error: entity is not a node.";
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : RelationshipFactory(org.structr.core.graph.RelationshipFactory) NodeInterface(org.structr.core.graph.NodeInterface)

Example 32 with NodeInterface

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

the class AccessControlTest method test07ResultCount.

@Test
public void test07ResultCount() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        final Class type = TestOne.class;
        final List<NodeInterface> nodes = createTestNodes(type, 10);
        try (final Tx tx = app.tx()) {
            nodes.get(3).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(5).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(7).setProperty(AbstractNode.visibleToPublicUsers, true);
            tx.success();
        }
        SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(publicContext).nodeQuery(type).sort(AbstractNode.createdDate).getResult();
            assertEquals(3, result.size());
            assertEquals(3, (int) result.getRawResultCount());
        // do not test order of elements
        // assertEquals(nodes.get(3).getUuid(), result.get(0).getUuid());
        // assertEquals(nodes.get(5).getUuid(), result.get(1).getUuid());
        // assertEquals(nodes.get(7).getUuid(), result.get(2).getUuid());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) Result(org.structr.core.Result) Test(org.junit.Test)

Example 33 with NodeInterface

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

the class AccessControlTest method test07ResultCountWithPaging.

@Test
public void test07ResultCountWithPaging() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        final Class type = TestOne.class;
        final List<NodeInterface> nodes = createTestNodes(type, 10);
        int count = 0;
        try (final Tx tx = app.tx()) {
            // add names to make sorting work...
            for (final NodeInterface node : nodes) {
                node.setProperty(AbstractNode.name, "node0" + count++);
            }
            nodes.get(3).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(5).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(7).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(9).setProperty(AbstractNode.visibleToPublicUsers, true);
            tx.success();
        }
        SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
        PropertyKey sortKey = AbstractNode.name;
        boolean sortDesc = false;
        int pageSize = 2;
        int page = 1;
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(publicContext).nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            assertEquals(2, result.size());
            assertEquals(4, (int) result.getRawResultCount());
            assertEquals(nodes.get(3).getUuid(), result.get(0).getUuid());
            assertEquals(nodes.get(5).getUuid(), result.get(1).getUuid());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 34 with NodeInterface

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

the class BasicTest method test01DeleteNode.

/**
 * Test successful deletion of a node.
 *
 * The node shouldn't be found afterwards.
 * Creation and deletion are executed in two different transactions.
 */
@Test
public void test01DeleteNode() {
    try {
        final PropertyMap props = new PropertyMap();
        final String type = "GenericNode";
        final String name = "GenericNode-name";
        NodeInterface node = null;
        String uuid = null;
        props.put(AbstractNode.type, type);
        props.put(AbstractNode.name, name);
        try (final Tx tx = app.tx()) {
            node = app.create(GenericNode.class, props);
            tx.success();
        }
        assertTrue(node != null);
        try (final Tx tx = app.tx()) {
            uuid = node.getUuid();
        }
        try (final Tx tx = app.tx()) {
            app.delete(node);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery().uuid(uuid).getResult();
            assertEquals("Node should have been deleted", 0, result.size());
        } catch (FrameworkException fe) {
        }
    } 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) GenericNode(org.structr.core.entity.GenericNode) NodeInterface(org.structr.core.graph.NodeInterface) Result(org.structr.core.Result) Test(org.junit.Test)

Example 35 with NodeInterface

use of org.structr.core.graph.NodeInterface 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

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