Search in sources :

Example 16 with NodeInterface

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

the class JavaParserModuleTest method createTestRelationships.

protected <T extends Relation> List<T> createTestRelationships(final Class<T> relType, final int number) throws FrameworkException {
    List<GenericNode> nodes = createTestNodes(GenericNode.class, 2);
    final NodeInterface startNode = nodes.get(0);
    final NodeInterface endNode = nodes.get(1);
    try (final Tx tx = app.tx()) {
        List<T> rels = new LinkedList<>();
        for (int i = 0; i < number; i++) {
            rels.add((T) app.create(startNode, endNode, relType));
        }
        tx.success();
        return rels;
    }
}
Also used : Tx(org.structr.core.graph.Tx) GenericNode(org.structr.core.entity.GenericNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 17 with NodeInterface

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

the class ValidationHelper method isValidGloballyUniqueProperty.

public static synchronized boolean isValidGloballyUniqueProperty(final GraphObject object, final PropertyKey key, final ErrorBuffer errorBuffer) {
    if (key != null) {
        final Object value = object.getProperty(key);
        List<? extends GraphObject> result = null;
        try {
            if (object instanceof NodeInterface) {
                result = StructrApp.getInstance().nodeQuery(NodeInterface.class).and(key, value).disableSorting().getAsList();
            } else if (object instanceof RelationshipInterface) {
                result = StructrApp.getInstance().relationshipQuery(RelationshipInterface.class).and(key, value).disableSorting().getAsList();
            } else {
                logger.error("GraphObject is neither NodeInterface nor RelationshipInterface");
                return false;
            }
        } catch (FrameworkException fex) {
            logger.warn("Unable to fetch list of nodes for uniqueness check", fex);
        // handle error
        }
        if (result != null) {
            for (final GraphObject foundNode : result) {
                if (foundNode.getId() != object.getId()) {
                    // validation is aborted when the first validation failure occurs, so
                    // we can assume that the object currently exmained is the first
                    // existing object, hence all others get the error message with the
                    // UUID of the first one.
                    errorBuffer.add(new UniqueToken(object.getType(), key, object.getUuid()));
                    // error!
                    return false;
                }
            }
        }
    }
    // no error
    return true;
}
Also used : UniqueToken(org.structr.common.error.UniqueToken) FrameworkException(org.structr.common.error.FrameworkException) RelationshipInterface(org.structr.core.graph.RelationshipInterface) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) NodeInterface(org.structr.core.graph.NodeInterface)

Example 18 with NodeInterface

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

the class GraphMergeHelper method merge.

/**
 * Merge new nodes into original nodes including all relationships and
 * properties
 *
 * @param <T>
 * @param newNodes
 * @param origNodes
 * @param shadowIdPropertyKey
 */
public static <T extends NodeInterface> void merge(final Set<T> origNodes, final Set<T> newNodes, final PropertyKey shadowIdPropertyKey) {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        // and mark all original nodes as deleted which are not contained in new nodes list anymore
        for (final NodeInterface origNode : origNodes) {
            origNode.setProperty(NodeInterface.deleted, true);
            for (final NodeInterface newNode : newNodes) {
                final String shadowId = (String) newNode.getProperty(shadowIdPropertyKey);
                logger.info("New node shadow id: {}", shadowId);
                if (origNode.getUuid().equals(shadowId)) {
                    origNode.setProperty(NodeInterface.deleted, false);
                }
            }
        }
        // Delete all original nodes which are marked as deleted
        for (final NodeInterface origNode : origNodes) {
            if (origNode.getProperty(NodeInterface.deleted)) {
                app.delete(origNode);
            }
        }
        // Delete all new nodes
        for (final NodeInterface newNode : newNodes) {
            app.delete(newNode);
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
}
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) NodeInterface(org.structr.core.graph.NodeInterface)

Example 19 with NodeInterface

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

the class EndNodes method getSearchAttribute.

@Override
public SearchAttribute getSearchAttribute(SecurityContext securityContext, Occurrence occur, List<T> searchValue, boolean exactMatch, final Query query) {
    final Predicate<GraphObject> predicate = query != null ? query.toPredicate() : null;
    final SourceSearchAttribute attr = new SourceSearchAttribute(occur);
    final Set<GraphObject> intersectionResult = new LinkedHashSet<>();
    boolean alreadyAdded = false;
    if (searchValue != null && !StringUtils.isBlank(searchValue.toString())) {
        if (exactMatch) {
            for (NodeInterface node : searchValue) {
                switch(occur) {
                    case REQUIRED:
                        if (!alreadyAdded) {
                            // the first result is the basis of all subsequent intersections
                            intersectionResult.addAll(getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                            // the next additions are intersected with this one
                            alreadyAdded = true;
                        } else {
                            intersectionResult.retainAll(getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                        }
                        break;
                    case OPTIONAL:
                        intersectionResult.addAll(getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                        break;
                    case FORBIDDEN:
                        break;
                }
            }
        } else {
            // loose search behaves differently, all results must be combined
            for (NodeInterface node : searchValue) {
                intersectionResult.addAll(getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
            }
        }
        attr.setResult(intersectionResult);
    } else {
        // value in the given field
        return new EmptySearchAttribute(this, null);
    }
    return attr;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceSearchAttribute(org.structr.core.graph.search.SourceSearchAttribute) EmptySearchAttribute(org.structr.core.graph.search.EmptySearchAttribute) GraphObject(org.structr.core.GraphObject) NodeInterface(org.structr.core.graph.NodeInterface)

Example 20 with NodeInterface

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

the class EndNodeGroup method getGroupedProperties.

@Override
public PropertyMap getGroupedProperties(SecurityContext securityContext, GraphObject source) {
    if (source instanceof RelationshipInterface) {
        RelationshipInterface rel = (RelationshipInterface) source;
        NodeInterface end = rel.getTargetNode();
        return super.getGroupedProperties(securityContext, end);
    }
    return null;
}
Also used : RelationshipInterface(org.structr.core.graph.RelationshipInterface) 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