Search in sources :

Example 26 with AbstractRelationship

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

the class BasicTest method test05CascadeDeleteOutgoing.

/**
 * DELETE_OUTGOING should trigger delete cascade from start to end node,
 * but not from end to start node.
 */
@Test
public void test05CascadeDeleteOutgoing() {
    try {
        // Create a relationship with DELETE_OUTGOING
        AbstractRelationship rel = cascadeRel(TestOne.class, TestTwo.class, 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
            assertNodeNotFound(endNodeId);
        }
        // Create another relationship with DELETE_OUTGOING
        rel = cascadeRel(TestOne.class, TestTwo.class, 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 still exist deletion of end node
            assertNodeExists(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)

Example 27 with AbstractRelationship

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

the class RemoveCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String id = webSocketData.getId();
    if (id != null) {
        final NodeInterface node = getNode(id);
        if (node != null) {
            if (node instanceof DOMNode) {
                // Use new DOM interface
                DOMNode domNode = (DOMNode) node;
                try {
                    domNode.getParentNode().removeChild(domNode);
                    // remove pageId from node and all children ("move to trash")
                    recursivelyRemoveNodesFromPage(domNode, securityContext);
                } catch (DOMException | FrameworkException ex) {
                    logger.error("Could not remove node from page " + domNode, ex);
                    getWebSocket().send(MessageBuilder.status().code(422).message(ex.getMessage()).build(), true);
                }
            } else {
                // is this even used any more?
                logger.warn("Deprecated use of RemoveCommand, please report this error and the following stack trace to the Structr team on https://github.com/structr/structr. Thanks!");
                Thread.dumpStack();
                final App app = StructrApp.getInstance(securityContext);
                try {
                    // Old style: Delete all incoming CONTAINS rels
                    for (AbstractRelationship rel : node.getIncomingRelationships()) {
                        if ("CONTAINS".equals(rel.getType())) {
                            app.delete(rel);
                        }
                    }
                } catch (Throwable t) {
                    logger.error("Could not delete relationship", t);
                    getWebSocket().send(MessageBuilder.status().code(400).message("Error in RemoveCommand: " + t.getMessage()).build(), true);
                }
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(400).message("RemoveCommand called with empty id").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) DOMException(org.w3c.dom.DOMException) FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface)

Example 28 with AbstractRelationship

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

the class Property method index.

@Override
public void index(final GraphObject entity, final Object value) {
    if (entity instanceof AbstractNode) {
        final NodeService nodeService = Services.getInstance().getService(NodeService.class);
        final AbstractNode node = (AbstractNode) entity;
        final Node dbNode = node.getNode();
        final Index<Node> index = nodeService.getNodeIndex();
        if (index != null) {
            try {
                index.remove(dbNode, dbName);
                if (value != null || isIndexedWhenEmpty()) {
                    index.add(dbNode, dbName, value, valueType());
                }
            } catch (Throwable t) {
                logger.info("Unable to index property with dbName {} and value {} of type {} on {}: {}", new Object[] { dbName, value, this.getClass().getSimpleName(), entity, t });
                logger.warn("", t);
            }
        }
    } else if (entity instanceof AbstractRelationship) {
        final NodeService nodeService = Services.getInstance().getService(NodeService.class);
        final AbstractRelationship rel = (AbstractRelationship) entity;
        final Relationship dbRel = rel.getRelationship();
        final Index<Relationship> index = nodeService.getRelationshipIndex();
        if (index != null) {
            try {
                index.remove(dbRel, dbName);
                if (value != null || isIndexedWhenEmpty()) {
                    index.add(dbRel, dbName, value, valueType());
                }
            } catch (Throwable t) {
                logger.info("Unable to index property with dbName {} and value {} of type {} on {}: {}", new Object[] { dbName, value, this.getClass().getSimpleName(), entity, t });
            }
        }
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) NodeService(org.structr.core.graph.NodeService) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) GraphObject(org.structr.core.GraphObject) AbstractCypherIndex(org.structr.bolt.index.AbstractCypherIndex) Index(org.structr.api.index.Index)

Example 29 with AbstractRelationship

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

the class ReferenceGroup method getGroupedProperties.

// ----- interface PropertyGroup -----
@Override
public PropertyMap getGroupedProperties(SecurityContext securityContext, GraphObject source) {
    if (source instanceof AbstractRelationship) {
        AbstractRelationship rel = (AbstractRelationship) source;
        PropertyMap properties = new PropertyMap();
        for (PropertyKey key : propertyKeys.values()) {
            Reference reference = (Reference) key;
            GraphObject referencedEntity = reference.getReferencedEntity(rel);
            PropertyKey referenceKey = reference.getReferenceKey();
            PropertyKey propertyKey = reference.getPropertyKey();
            if (referencedEntity != null) {
                properties.put(propertyKey, referencedEntity.getProperty(referenceKey));
            }
        }
        return properties;
    }
    return null;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) GraphObject(org.structr.core.GraphObject)

Example 30 with AbstractRelationship

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

the class ReferenceGroup method setGroupedProperties.

@Override
public void setGroupedProperties(SecurityContext securityContext, PropertyMap source, GraphObject destination) throws FrameworkException {
    if (destination instanceof AbstractRelationship) {
        AbstractRelationship rel = (AbstractRelationship) destination;
        for (PropertyKey key : propertyKeys.values()) {
            Reference reference = (Reference) key;
            GraphObject referencedEntity = reference.getReferencedEntity(rel);
            PropertyKey referenceKey = reference.getReferenceKey();
            PropertyKey propertyKey = reference.getPropertyKey();
            if (referencedEntity != null && !reference.isReadOnly()) {
                Object value = source.get(propertyKey);
                referencedEntity.setProperty(referenceKey, value);
            }
        }
    }
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject)

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