Search in sources :

Example 16 with AbstractRelationship

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

the class DeleteRelationshipCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final AbstractRelationship obj = getRelationship(webSocketData.getId());
    if (obj != null) {
        StructrApp.getInstance(securityContext).delete(obj);
    }
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext)

Example 17 with AbstractRelationship

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

the class AbstractCommand method getRelationship.

/**
 * Returns the relationship to which the uuid parameter
 * of this command refers to.
 *
 * @param id
 * @return the node
 */
public AbstractRelationship getRelationship(final String id) {
    if (id == null) {
        return null;
    }
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        final AbstractRelationship rel = (AbstractRelationship) app.getRelationshipById(id);
        tx.success();
        return rel;
    } catch (FrameworkException fex) {
        logger.warn("Unable to get relationship", fex);
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext)

Example 18 with AbstractRelationship

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

the class GraphObject method setProperties.

/**
 * Sets the given properties.
 *
 * @param securityContext
 * @param properties
 * @throws FrameworkException
 */
default void setProperties(final SecurityContext securityContext, final PropertyMap properties) throws FrameworkException {
    final CreationContainer container = new CreationContainer(this);
    boolean atLeastOnePropertyChanged = false;
    for (final Entry<PropertyKey, Object> attr : properties.entrySet()) {
        final PropertyKey key = attr.getKey();
        final Object value = attr.getValue();
        if (key.indexable(value)) {
            final Object oldValue = getProperty(key);
            if (!value.equals(oldValue)) {
                atLeastOnePropertyChanged = true;
                // bulk set possible, store in container
                key.setProperty(securityContext, container, value);
                if (isNode()) {
                    if (!key.isUnvalidated()) {
                        TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) this, key, getProperty(key), value);
                    }
                    if (key instanceof TypeProperty) {
                        if (this instanceof NodeInterface) {
                            final Class type = StructrApp.getConfiguration().getNodeEntityClass((String) value);
                            TypeProperty.updateLabels(StructrApp.getInstance().getDatabaseService(), (NodeInterface) this, type, true);
                        }
                    }
                } else if (isRelationship()) {
                    if (!key.isUnvalidated()) {
                        TransactionCommand.relationshipModified(securityContext.getCachedUser(), (AbstractRelationship) this, key, getProperty(key), value);
                    }
                    if (key instanceof TypeProperty) {
                        if (this instanceof NodeInterface) {
                            final Class type = StructrApp.getConfiguration().getNodeEntityClass((String) value);
                            TypeProperty.updateLabels(StructrApp.getInstance().getDatabaseService(), (NodeInterface) this, type, true);
                        }
                    }
                }
            }
        } else {
            // bulk set NOT possible, set on entity
            if (key.isSystemInternal()) {
                unlockSystemPropertiesOnce();
            }
            setProperty(key, value);
        }
    }
    if (atLeastOnePropertyChanged) {
        // set primitive values directly for better performance
        getPropertyContainer().setProperties(container.getData());
    }
}
Also used : TypeProperty(org.structr.core.property.TypeProperty) AbstractRelationship(org.structr.core.entity.AbstractRelationship) CreationContainer(org.structr.core.graph.CreationContainer) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Example 19 with AbstractRelationship

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

the class BulkRebuildIndexCommand method rebuildRelationshipIndex.

private void rebuildRelationshipIndex(final String relType) {
    final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getRelationshipsByType(relType))).iterator();
    if (relType == null) {
        info("Relationship type not set, starting (re-)indexing all relationships");
    } else {
        info("Starting (re-)indexing all relationships of type {}", new Object[] { relType });
    }
    long count = bulkGraphOperation(securityContext, relIterator, 1000, "RebuildRelIndex", new BulkGraphOperation<AbstractRelationship>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
            rel.updateInIndex();
        }

        @Override
        public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
            logger.warn("Unable to index relationship {}: {}", new Object[] { rel, t.getMessage() });
        }

        @Override
        public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
            logger.warn("Unable to index relationship: {}", t.getMessage());
        }
    });
    info("Done with (re-)indexing {} relationships", count);
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext) StructrAndSpatialPredicate(org.structr.common.StructrAndSpatialPredicate) DatabaseService(org.structr.api.DatabaseService)

Example 20 with AbstractRelationship

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

the class DeleteNodeCommand method doDeleteNode.

private void doDeleteNode(final NodeInterface node) {
    if (TransactionCommand.isDeleted(node.getNode())) {
        return;
    }
    try {
        if (!deletedNodes.contains(node) && node.getUuid() == null) {
            logger.warn("Will not delete node which has no UUID, dumping stack.");
            Thread.dumpStack();
            return;
        }
    } catch (java.lang.IllegalStateException ise) {
        logger.warn("Trying to delete a node which is already deleted", ise.getMessage());
        return;
    } catch (org.structr.api.NotFoundException nfex) {
    // exception can be ignored, node is already deleted
    }
    deletedNodes.add(node);
    App app = StructrApp.getInstance(securityContext);
    try {
        List<NodeInterface> nodesToCheckAfterDeletion = new LinkedList<>();
        // by relationships which are marked with DELETE_OUTGOING
        for (AbstractRelationship rel : node.getOutgoingRelationships()) {
            // deleted rels can be null..
            if (rel != null) {
                int cascadeDelete = rel.cascadeDelete();
                NodeInterface endNode = rel.getTargetNode();
                if ((cascadeDelete & Relation.CONSTRAINT_BASED) == Relation.CONSTRAINT_BASED) {
                    nodesToCheckAfterDeletion.add(endNode);
                }
                if (!deletedNodes.contains(endNode) && ((cascadeDelete & Relation.SOURCE_TO_TARGET) == Relation.SOURCE_TO_TARGET)) {
                    // remove end node from index
                    endNode.removeFromIndex();
                    doDeleteNode(endNode);
                }
            }
        }
        // by relationships which are marked with DELETE_INCOMING
        for (AbstractRelationship rel : node.getIncomingRelationships()) {
            // deleted rels can be null
            if (rel != null) {
                final int cascadeDelete = rel.cascadeDelete();
                final NodeInterface startNode = rel.getSourceNode();
                if ((cascadeDelete & Relation.CONSTRAINT_BASED) == Relation.CONSTRAINT_BASED) {
                    nodesToCheckAfterDeletion.add(startNode);
                }
                if (!deletedNodes.contains(startNode) && ((cascadeDelete & Relation.TARGET_TO_SOURCE) == Relation.TARGET_TO_SOURCE)) {
                    // remove start node from index
                    startNode.removeFromIndex();
                    doDeleteNode(startNode);
                }
            }
        }
        // deletion callback, must not prevent node deletion!
        node.onNodeDeletion();
        // Delete any relationship (this is PASSIVE DELETION)
        for (AbstractRelationship r : node.getRelationships()) {
            if (r != null) {
                app.delete(r);
            }
        }
        // still valid after node deletion
        for (NodeInterface nodeToCheck : nodesToCheckAfterDeletion) {
            ErrorBuffer errorBuffer = new ErrorBuffer();
            if (!deletedNodes.contains(nodeToCheck) && !nodeToCheck.isValid(errorBuffer)) {
                // remove end node from index
                nodeToCheck.removeFromIndex();
                doDeleteNode(nodeToCheck);
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        logger.warn("Exception while deleting node {}: {}", node, t.getMessage());
    }
    return;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) AbstractRelationship(org.structr.core.entity.AbstractRelationship) LinkedList(java.util.LinkedList) ErrorBuffer(org.structr.common.error.ErrorBuffer)

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