Search in sources :

Example 66 with NodeInterface

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

the class AbstractRelationship method setSourceNodeId.

@Override
public final void setSourceNodeId(final String sourceNodeId) throws FrameworkException {
    // Do nothing if new id equals old
    if (getSourceNodeId().equals(sourceNodeId)) {
        return;
    }
    final App app = StructrApp.getInstance(securityContext);
    final NodeInterface newStartNode = app.getNodeById(sourceNodeId);
    final NodeInterface endNode = getTargetNode();
    final Class relationType = getClass();
    final PropertyMap _props = getProperties();
    final String type = this.getClass().getSimpleName();
    if (newStartNode == null) {
        throw new FrameworkException(404, "Node with ID " + sourceNodeId + " not found", new IdNotFoundToken(type, sourceNodeId));
    }
    // delete this as the new rel will be the container afterwards
    app.delete(this);
    // create new relationship
    app.create(newStartNode, endNode, relationType, _props);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) IdNotFoundToken(org.structr.common.error.IdNotFoundToken) NodeInterface(org.structr.core.graph.NodeInterface)

Example 67 with NodeInterface

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

the class AbstractRelationship method setTargetNodeId.

@Override
public final void setTargetNodeId(final String targetNodeId) throws FrameworkException {
    // Do nothing if new id equals old
    if (getTargetNodeId().equals(targetNodeId)) {
        return;
    }
    final App app = StructrApp.getInstance(securityContext);
    final NodeInterface newTargetNode = app.getNodeById(targetNodeId);
    final NodeInterface startNode = getSourceNode();
    final Class relationType = getClass();
    final PropertyMap _props = getProperties();
    final String type = this.getClass().getSimpleName();
    if (newTargetNode == null) {
        throw new FrameworkException(404, "Node with ID " + targetNodeId + " not found", new IdNotFoundToken(type, targetNodeId));
    }
    // delete this as the new rel will be the container afterwards
    app.delete(this);
    // create new relationship and store here
    app.create(startNode, newTargetNode, relationType, _props);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) IdNotFoundToken(org.structr.common.error.IdNotFoundToken) NodeInterface(org.structr.core.graph.NodeInterface)

Example 68 with NodeInterface

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

the class OtherNodeTypeRelationFilter method accept.

@Override
public boolean accept(final Relation rel) {
    final NodeInterface otherNode = nodeFactory.instantiate(rel.getRelationship().getOtherNode(thisNode));
    final Class otherNodeType = otherNode.getClass();
    return desiredType.isAssignableFrom(otherNodeType) || otherNodeType.isAssignableFrom(desiredType);
}
Also used : NodeInterface(org.structr.core.graph.NodeInterface)

Example 69 with NodeInterface

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

the class ManyEndpoint method set.

@Override
public Object set(final SecurityContext securityContext, final NodeInterface sourceNode, final Iterable<T> collection) throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    final List<Relation> createdRelationships = new LinkedList<>();
    final PropertyMap properties = new PropertyMap();
    final T actualSourceNode = (T) unwrap(securityContext, relation.getClass(), sourceNode, properties);
    final Set<T> toBeDeleted = new LinkedHashSet<>(Iterables.toList(get(securityContext, actualSourceNode, null)));
    final Set<T> toBeCreated = new LinkedHashSet<>();
    if (collection != null) {
        Iterables.addAll(toBeCreated, collection);
    }
    // create intersection of both sets
    final Set<T> intersection = new HashSet<>(toBeCreated);
    intersection.retainAll(toBeDeleted);
    // intersection needs no change
    toBeCreated.removeAll(intersection);
    toBeDeleted.removeAll(intersection);
    if (actualSourceNode != null) {
        // remove existing relationships
        for (T targetNode : toBeDeleted) {
            for (Iterator<AbstractRelationship> it = actualSourceNode.getOutgoingRelationships(relation.getClass()).iterator(); it.hasNext(); ) {
                final AbstractRelationship rel = it.next();
                if (actualSourceNode.equals(targetNode)) {
                    logger.warn("Preventing deletion of self relationship {}-[{}]->{}. If you experience issue with this, please report to team@structr.com.", new Object[] { actualSourceNode, rel.getRelType(), targetNode });
                    // skip self relationships
                    continue;
                }
                if (rel.getTargetNode().equals(targetNode)) {
                    app.delete(rel);
                }
            }
        }
        // create new relationships
        for (T targetNode : toBeCreated) {
            if (targetNode != null) {
                properties.clear();
                final NodeInterface actualTargetNode = (NodeInterface) unwrap(securityContext, relation.getClass(), targetNode, properties);
                relation.ensureCardinality(securityContext, actualSourceNode, actualTargetNode);
                final PropertyMap notionProperties = getNotionProperties(securityContext, relation.getClass(), actualSourceNode.getName() + relation.name() + actualTargetNode.getName());
                if (notionProperties != null) {
                    properties.putAll(notionProperties);
                }
                createdRelationships.add(app.create(actualSourceNode, actualTargetNode, relation.getClass(), properties));
            }
        }
    }
    return createdRelationships;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) LinkedHashSet(java.util.LinkedHashSet) PropertyMap(org.structr.core.property.PropertyMap) LinkedList(java.util.LinkedList) NodeInterface(org.structr.core.graph.NodeInterface) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 70 with NodeInterface

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

the class OneEndpoint method set.

@Override
public Object set(final SecurityContext securityContext, final NodeInterface sourceNode, final T targetNode) throws FrameworkException {
    final PropertyMap properties = new PropertyMap();
    final NodeInterface actualTargetNode = (NodeInterface) unwrap(securityContext, relation.getClass(), targetNode, properties);
    final T actualSourceNode = (T) unwrap(securityContext, relation.getClass(), sourceNode, properties);
    // let relation check multiplicity
    relation.ensureCardinality(securityContext, actualSourceNode, actualTargetNode);
    if (actualSourceNode != null && actualTargetNode != null) {
        final String storageKey = actualSourceNode.getName() + relation.name() + actualTargetNode.getName();
        final PropertyMap notionProperties = getNotionProperties(securityContext, relation.getClass(), storageKey);
        if (notionProperties != null) {
            properties.putAll(notionProperties);
        }
        // create new relationship
        return StructrApp.getInstance(securityContext).create(actualSourceNode, actualTargetNode, relation.getClass(), properties);
    }
    return null;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) NodeInterface(org.structr.core.graph.NodeInterface)

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