Search in sources :

Example 16 with RelationshipInterface

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

the class InvertibleModificationOperation method collectNodes.

private static void collectNodes(final DOMNode node, final Map<String, DOMNode> indexMappedNodes, final Map<String, DOMNode> hashMappedNodes, final Map<DOMNode, Integer> depthMap, final int depth, final Map<Integer, Integer> childIndexMap) {
    // early exit
    if (node == null) {
        return;
    }
    Integer pos = childIndexMap.get(depth);
    if (pos == null) {
        pos = 0;
    }
    int position = pos;
    childIndexMap.put(depth, ++position);
    // store node with its tree index
    final String hash = "[" + depth + ":" + position + "]";
    indexMappedNodes.put(hash, node);
    // store node with its data hash
    String dataHash = node.getDataHash();
    if (dataHash == null) {
        dataHash = node.getIdHash();
    }
    hashMappedNodes.put(dataHash, node);
    depthMap.put(node, depth);
    // recurse
    for (final RelationshipInterface childRel : node.getChildRelationships()) {
        collectNodes((DOMNode) childRel.getTargetNode(), indexMappedNodes, hashMappedNodes, depthMap, depth + 1, childIndexMap);
    }
}
Also used : RelationshipInterface(org.structr.core.graph.RelationshipInterface)

Example 17 with RelationshipInterface

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

the class SearchCommand method getAllSubtypesAsStringSet.

public static synchronized Set<String> getAllSubtypesAsStringSet(final String type) {
    Set<String> allSubtypes = subtypeMapForType.get(type);
    if (allSubtypes == null) {
        logger.debug("Subtype map cache miss.");
        allSubtypes = new LinkedHashSet<>();
        subtypeMapForType.put(type, allSubtypes);
        final ConfigurationProvider configuration = StructrApp.getConfiguration();
        final Map<String, Class<? extends NodeInterface>> nodeEntities = configuration.getNodeEntities();
        final Map<String, Class<? extends RelationshipInterface>> relEntities = configuration.getRelationshipEntities();
        // add type first (this is neccesary because two class objects of the same dynamic type node are not equal
        // to each other and not assignable, if the schema node was modified in the meantime)
        allSubtypes.add(type);
        // scan all node entities for subtypes
        for (final Map.Entry<String, Class<? extends NodeInterface>> entity : nodeEntities.entrySet()) {
            final Class entityType = entity.getValue();
            final Set<Class> ancestors = typeAndAllSupertypes(entityType);
            for (final Class superClass : ancestors) {
                final String superClasSimpleName = superClass.getSimpleName();
                final String superClassFullName = superClass.getName();
                if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
                    allSubtypes.add(entityType.getSimpleName());
                }
            }
        }
        // scan all relationship entities for subtypes
        for (final Map.Entry<String, Class<? extends RelationshipInterface>> entity : relEntities.entrySet()) {
            final Class entityType = entity.getValue();
            final Set<Class> ancestors = typeAndAllSupertypes(entityType);
            for (final Class superClass : ancestors) {
                final String superClasSimpleName = superClass.getSimpleName();
                final String superClassFullName = superClass.getName();
                if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
                    allSubtypes.add(entityType.getSimpleName());
                }
            }
        }
    } else {
        logger.debug("Subtype map cache hit.");
    }
    return Collections.unmodifiableSet(allSubtypes);
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) RelationshipInterface(org.structr.core.graph.RelationshipInterface) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) NodeInterface(org.structr.core.graph.NodeInterface)

Example 18 with RelationshipInterface

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

the class StructrUiTest method createTestRelationships.

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

Example 19 with RelationshipInterface

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

the class TypeResource method doPost.

@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
    // virtual type?
    if (virtualType != null) {
        virtualType.transformInput(securityContext, entityClass, propertySet);
    }
    if (isNode) {
        final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_CREATED);
        final NodeInterface newNode = createNode(propertySet);
        if (newNode != null) {
            result.addHeader("Location", buildLocationHeader(newNode));
            result.addContent(newNode);
        }
        result.serializeAsPrimitiveArray(true);
        // finally: return 201 Created
        return result;
    } else {
        final App app = StructrApp.getInstance(securityContext);
        final Relation template = getRelationshipTemplate();
        final ErrorBuffer errorBuffer = new ErrorBuffer();
        if (template != null) {
            final NodeInterface sourceNode = identifyStartNode(template, propertySet);
            final NodeInterface targetNode = identifyEndNode(template, propertySet);
            final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, entityClass, propertySet);
            RelationshipInterface newRelationship = null;
            if (sourceNode == null) {
                errorBuffer.add(new EmptyPropertyToken(entityClass.getSimpleName(), template.getSourceIdProperty()));
            }
            if (targetNode == null) {
                errorBuffer.add(new EmptyPropertyToken(entityClass.getSimpleName(), template.getTargetIdProperty()));
            }
            if (errorBuffer.hasError()) {
                throw new FrameworkException(422, "Source node ID and target node ID of relationsips must be set", errorBuffer);
            }
            template.ensureCardinality(securityContext, sourceNode, targetNode);
            newRelationship = app.create(sourceNode, targetNode, entityClass, properties);
            RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_CREATED);
            if (newRelationship != null) {
                result.addHeader("Location", buildLocationHeader(newRelationship));
                result.addContent(newRelationship);
            }
            result.serializeAsPrimitiveArray(true);
            // finally: return 201 Created
            return result;
        }
        // shouldn't happen
        throw new NotFoundException("Type" + rawType + " does not exist");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) EmptyPropertyToken(org.structr.common.error.EmptyPropertyToken) Relation(org.structr.core.entity.Relation) ErrorBuffer(org.structr.common.error.ErrorBuffer) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) RelationshipInterface(org.structr.core.graph.RelationshipInterface) NotFoundException(org.structr.rest.exception.NotFoundException) RestMethodResult(org.structr.rest.RestMethodResult) NodeInterface(org.structr.core.graph.NodeInterface)

Example 20 with RelationshipInterface

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

the class DeleteFunction method deleteObject.

// ----- private methods -----
private void deleteObject(final App app, final Object obj) throws FrameworkException {
    if (obj instanceof NodeInterface) {
        app.delete((NodeInterface) obj);
    }
    if (obj instanceof RelationshipInterface) {
        app.delete((RelationshipInterface) obj);
    }
    if (obj instanceof Iterable) {
        if (batched) {
            final Iterable iterable = (Iterable) obj;
            final Iterator iterator = iterable.iterator();
            int count = 0;
            while (iterator.hasNext()) {
                try (final Tx tx = app.tx()) {
                    while (iterator.hasNext()) {
                        deleteObject(app, iterator.next());
                        if ((++count % batchSize) == 0) {
                            break;
                        }
                    }
                    tx.success();
                }
                logger.debug("Committing batch after {} objects", count);
                // reset count
                count = 0;
            }
        } else {
            for (final Object o : (Iterable) obj) {
                deleteObject(app, o);
            }
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Iterator(java.util.Iterator) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

RelationshipInterface (org.structr.core.graph.RelationshipInterface)28 NodeInterface (org.structr.core.graph.NodeInterface)15 GraphObject (org.structr.core.GraphObject)8 LinkedList (java.util.LinkedList)6 FrameworkException (org.structr.common.error.FrameworkException)6 PropertyMap (org.structr.core.property.PropertyMap)6 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 List (java.util.List)4 SecurityContext (org.structr.common.SecurityContext)4 AbstractNode (org.structr.core.entity.AbstractNode)4 GenericNode (org.structr.core.entity.GenericNode)4 Tx (org.structr.core.graph.Tx)4 RelationshipDataContainer (org.structr.cloud.message.RelationshipDataContainer)3 Result (org.structr.core.Result)3 PropertyKey (org.structr.core.property.PropertyKey)3 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 NotFoundException (org.structr.api.NotFoundException)2 RelationshipType (org.structr.api.graph.RelationshipType)2