Search in sources :

Example 21 with AbstractRelationship

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

the class AbstractPrimitiveProperty method setProperty.

@Override
public Object setProperty(final SecurityContext securityContext, final GraphObject obj, final T value) throws FrameworkException {
    final PropertyConverter converter = databaseConverter(securityContext, PropertyMap.unwrap(obj));
    Object convertedValue = value;
    if (converter != null) {
        convertedValue = converter.convert(value);
    }
    // use transformators from property
    for (final String fqcn : transformators) {
        // first test, use caching here later..
        final Transformer transformator = getTransformator(fqcn);
        if (transformator != null) {
            convertedValue = transformator.setProperty(PropertyMap.unwrap(obj), this, convertedValue);
        }
    }
    final PropertyContainer propertyContainer = obj.getPropertyContainer();
    if (propertyContainer != null) {
        if (!TransactionCommand.inTransaction()) {
            throw new NotInTransactionException("setProperty outside of transaction");
        }
        boolean internalSystemPropertiesUnlocked = (obj instanceof CreationContainer);
        // collect modified properties
        if (obj instanceof AbstractNode) {
            if (!unvalidated) {
                TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) obj, AbstractPrimitiveProperty.this, propertyContainer.hasProperty(dbName()) ? propertyContainer.getProperty(dbName()) : null, value);
            }
            internalSystemPropertiesUnlocked = ((AbstractNode) obj).internalSystemPropertiesUnlocked;
        } else if (obj instanceof AbstractRelationship) {
            if (!unvalidated) {
                TransactionCommand.relationshipModified(securityContext.getCachedUser(), (AbstractRelationship) obj, AbstractPrimitiveProperty.this, propertyContainer.hasProperty(dbName()) ? propertyContainer.getProperty(dbName()) : null, value);
            }
            internalSystemPropertiesUnlocked = ((AbstractRelationship) obj).internalSystemPropertiesUnlocked;
        }
        // catch all sorts of errors and wrap them in a FrameworkException
        try {
            // save space
            if (convertedValue == null) {
                propertyContainer.removeProperty(dbName());
            } else {
                if (!isSystemInternal() || internalSystemPropertiesUnlocked) {
                    propertyContainer.setProperty(dbName(), convertedValue);
                } else {
                    logger.warn("Tried to set internal system property {} to {}. Action was denied.", new Object[] { dbName(), convertedValue });
                }
            }
            updateAccessInformation(securityContext, propertyContainer);
        } catch (final RetryException rex) {
            // don't catch RetryException here
            throw rex;
        } catch (Throwable t) {
            // throw FrameworkException with the given cause
            final FrameworkException fex = new FrameworkException(500, "Unable to set property " + jsonName() + " on entity with ID " + obj.getUuid() + ": " + t.toString());
            fex.initCause(t);
            throw fex;
        }
        if (isIndexed()) {
            // work
            if (!isPassivelyIndexed()) {
                index(obj, convertedValue);
            }
        }
    }
    return null;
}
Also used : PropertyContainer(org.structr.api.graph.PropertyContainer) Transformer(org.structr.schema.Transformer) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) CreationContainer(org.structr.core.graph.CreationContainer) RetryException(org.structr.api.RetryException) NotInTransactionException(org.structr.api.NotInTransactionException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 22 with AbstractRelationship

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

the class GetOutgoingRelationshipsFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final List<AbstractRelationship> list = new ArrayList<>();
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && s.equals(sourceNode) && t.equals(targetNode)) {
                    list.add(rel);
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && rel.getRelType().name().equals(relType) && s.equals(sourceNode) && t.equals(targetNode)) {
                    list.add(rel);
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return list;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) ArrayList(java.util.ArrayList) NodeInterface(org.structr.core.graph.NodeInterface)

Example 23 with AbstractRelationship

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

the class GetRelationshipsFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final List<AbstractRelationship> list = new ArrayList<>();
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
        final Object source = sources[0];
        final Object target = sources[1];
        NodeInterface sourceNode = null;
        NodeInterface targetNode = null;
        if (source instanceof NodeInterface && target instanceof NodeInterface) {
            sourceNode = (NodeInterface) source;
            targetNode = (NodeInterface) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: Entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && ((s.equals(sourceNode) && t.equals(targetNode)) || (s.equals(targetNode) && t.equals(sourceNode)))) {
                    list.add(rel);
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null && t != null && rel.getRelType().name().equals(relType) && ((s.equals(sourceNode) && t.equals(targetNode)) || (s.equals(targetNode) && t.equals(sourceNode)))) {
                    list.add(rel);
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return list;
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) ArrayList(java.util.ArrayList) NodeInterface(org.structr.core.graph.NodeInterface)

Example 24 with AbstractRelationship

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

the class HasRelationshipFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
        final Object source = sources[0];
        final Object target = sources[1];
        AbstractNode sourceNode = null;
        AbstractNode targetNode = null;
        if (source instanceof AbstractNode && target instanceof AbstractNode) {
            sourceNode = (AbstractNode) source;
            targetNode = (AbstractNode) target;
        } else {
            logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
        if (sources.length == 2) {
            for (final AbstractRelationship rel : sourceNode.getRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && ((s.equals(sourceNode) && t.equals(targetNode)) || (s.equals(targetNode) && t.equals(sourceNode)))) {
                    return true;
                }
            }
        } else if (sources.length == 3) {
            // dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
            final String relType = (String) sources[2];
            for (final AbstractRelationship rel : sourceNode.getRelationships()) {
                final NodeInterface s = rel.getSourceNode();
                final NodeInterface t = rel.getTargetNode();
                // We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
                if (s != null & t != null && rel.getRelType().name().equals(relType) && ((s.equals(sourceNode) && t.equals(targetNode)) || (s.equals(targetNode) && t.equals(sourceNode)))) {
                    return true;
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return false;
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface)

Example 25 with AbstractRelationship

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

the class BasicTest method cascadeRel.

// ----- private methods -----
private AbstractRelationship cascadeRel(final Class type1, final Class type2, final int cascadeDeleteFlag) throws FrameworkException {
    try (final Tx tx = app.tx()) {
        NodeInterface start = createTestNode(type1);
        NodeInterface end = createTestNode(type2);
        AbstractRelationship rel = createTestRelationship(start, end, NodeHasLocation.class);
        rel.setProperty(AbstractRelationship.cascadeDelete, cascadeDeleteFlag);
        tx.success();
        return rel;
    }
}
Also used : Tx(org.structr.core.graph.Tx) AbstractRelationship(org.structr.core.entity.AbstractRelationship) NodeInterface(org.structr.core.graph.NodeInterface)

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