Search in sources :

Example 11 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class SyncCommand method checkAndMerge.

private static boolean checkAndMerge(final NodeInterface node, final Set<Long> deletedNodes, final Set<Long> deletedRels) throws FrameworkException {
    final Class type = node.getClass();
    final String name = node.getName();
    final List<NodeInterface> existingNodes = StructrApp.getInstance().nodeQuery(type).andName(name).getAsList();
    for (NodeInterface existingNode : existingNodes) {
        final Node sourceNode = node.getNode();
        final Node targetNode = existingNode.getNode();
        // skip newly created node
        if (sourceNode.getId() == targetNode.getId()) {
            continue;
        }
        logger.info("Found existing schema node {}, merging!", name);
        copyProperties(sourceNode, targetNode);
        // handle outgoing rels
        for (final Relationship outRel : sourceNode.getRelationships(Direction.OUTGOING)) {
            final Node otherNode = outRel.getEndNode();
            final Relationship newRel = targetNode.createRelationshipTo(otherNode, outRel.getType());
            copyProperties(outRel, newRel);
            // report deletion
            deletedRels.add(outRel.getId());
            // remove previous relationship
            outRel.delete();
            System.out.println("############################################ Deleting relationship " + outRel.getId());
        }
        // handle incoming rels
        for (final Relationship inRel : sourceNode.getRelationships(Direction.INCOMING)) {
            final Node otherNode = inRel.getStartNode();
            final Relationship newRel = otherNode.createRelationshipTo(targetNode, inRel.getType());
            copyProperties(inRel, newRel);
            // report deletion
            deletedRels.add(inRel.getId());
            // remove previous relationship
            inRel.delete();
            System.out.println("############################################ Deleting relationship " + inRel.getId());
        }
        // merge properties, views and methods
        final Map<String, List<Node>> groupedNodes = groupByTypeAndName(Iterables.toList(Iterables.map(new EndNodes(), targetNode.getRelationships(Direction.OUTGOING))));
        for (final List<Node> nodes : groupedNodes.values()) {
            final int size = nodes.size();
            if (size > 1) {
                final Node groupTargetNode = nodes.get(0);
                for (final Node groupSourceNode : nodes.subList(1, size)) {
                    copyProperties(groupSourceNode, groupTargetNode);
                    // delete relationships of merged node
                    for (final Relationship groupRel : groupSourceNode.getRelationships()) {
                        deletedRels.add(groupRel.getId());
                        groupRel.delete();
                    }
                    // delete merged node
                    deletedNodes.add(groupSourceNode.getId());
                    groupSourceNode.delete();
                    System.out.println("############################################ Deleting node " + groupSourceNode.getId());
                }
            }
        }
        // report deletion
        deletedNodes.add(sourceNode.getId());
        // delete
        sourceNode.delete();
        System.out.println("############################################ Deleting node " + sourceNode.getId());
        return true;
    }
    return false;
}
Also used : Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) List(java.util.List) LinkedList(java.util.LinkedList)

Example 12 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class SyncCommand method exportDatabase.

private static void exportDatabase(final ZipOutputStream zos, final OutputStream outputStream, final Iterable<? extends NodeInterface> nodes, final Iterable<? extends RelationshipInterface> relationships) throws IOException, FrameworkException {
    // start database zip entry
    final ZipEntry dbEntry = new ZipEntry(STRUCTR_ZIP_DB_NAME);
    final DataOutputStream dos = new DataOutputStream(outputStream);
    final String uuidPropertyName = GraphObject.id.dbName();
    int nodeCount = 0;
    int relCount = 0;
    zos.putNextEntry(dbEntry);
    for (NodeInterface nodeObject : nodes) {
        // skip schema
        if (nodeObject instanceof AbstractSchemaNode) {
            continue;
        }
        final Node node = nodeObject.getNode();
        // ignore non-structr nodes
        if (node.hasProperty(GraphObject.id.dbName())) {
            outputStream.write('N');
            for (String key : node.getPropertyKeys()) {
                serialize(dos, key);
                serialize(dos, node.getProperty(key));
            }
            // do not use platform-specific line ending here!
            dos.write('\n');
            nodeCount++;
        }
    }
    dos.flush();
    for (RelationshipInterface relObject : relationships) {
        final Relationship rel = relObject.getRelationship();
        // ignore non-structr nodes
        if (rel.hasProperty(GraphObject.id.dbName())) {
            final Node startNode = rel.getStartNode();
            final Node endNode = rel.getEndNode();
            if (startNode.hasProperty(uuidPropertyName) && endNode.hasProperty(uuidPropertyName)) {
                String startId = (String) startNode.getProperty(uuidPropertyName);
                String endId = (String) endNode.getProperty(uuidPropertyName);
                outputStream.write('R');
                serialize(dos, startId);
                serialize(dos, endId);
                serialize(dos, rel.getType().name());
                for (String key : rel.getPropertyKeys()) {
                    serialize(dos, key);
                    serialize(dos, rel.getProperty(key));
                }
                // do not use platform-specific line ending here!
                dos.write('\n');
                relCount++;
            }
        }
    }
    dos.flush();
    // finish db entry
    zos.closeEntry();
    logger.info("Exported {} nodes and {} rels", new Object[] { nodeCount, relCount });
}
Also used : DataOutputStream(java.io.DataOutputStream) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) ZipEntry(java.util.zip.ZipEntry) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship)

Example 13 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class SyncCommand method importDatabase.

private static void importDatabase(final DatabaseService graphDb, final SecurityContext securityContext, final ZipInputStream zis, boolean doValidation, final Long batchSize) throws FrameworkException, IOException {
    final App app = StructrApp.getInstance();
    final DataInputStream dis = new DataInputStream(new BufferedInputStream(zis));
    final long internalBatchSize = batchSize != null ? batchSize : 200;
    final String uuidPropertyName = GraphObject.id.dbName();
    final Map<String, Node> uuidMap = new LinkedHashMap<>();
    final Set<Long> deletedNodes = new HashSet<>();
    double t0 = System.nanoTime();
    PropertyContainer currentObject = null;
    String currentKey = null;
    boolean finished = false;
    long totalNodeCount = 0;
    long totalRelCount = 0;
    do {
        try (final Tx tx = app.tx(doValidation)) {
            final List<Relationship> rels = new LinkedList<>();
            final List<Node> nodes = new LinkedList<>();
            long nodeCount = 0;
            long relCount = 0;
            do {
                try {
                    // store current position
                    dis.mark(4);
                    // read one byte
                    byte objectType = dis.readByte();
                    // skip newlines
                    if (objectType == '\n') {
                        continue;
                    }
                    if (objectType == 'N') {
                        // break loop after 200 objects, commit and restart afterwards
                        if (nodeCount + relCount >= internalBatchSize) {
                            dis.reset();
                            break;
                        }
                        currentObject = graphDb.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
                        nodeCount++;
                        // store for later use
                        nodes.add((Node) currentObject);
                    } else if (objectType == 'R') {
                        // break look after 200 objects, commit and restart afterwards
                        if (nodeCount + relCount >= internalBatchSize) {
                            dis.reset();
                            break;
                        }
                        String startId = (String) deserialize(dis);
                        String endId = (String) deserialize(dis);
                        String relTypeName = (String) deserialize(dis);
                        Node endNode = uuidMap.get(endId);
                        Node startNode = uuidMap.get(startId);
                        if (startNode != null && endNode != null) {
                            if (deletedNodes.contains(startNode.getId()) || deletedNodes.contains(endNode.getId())) {
                                System.out.println("NOT creating relationship between deleted nodes..");
                                currentObject = null;
                                currentKey = null;
                            } else {
                                RelationshipType relType = RelationshipType.forName(relTypeName);
                                currentObject = startNode.createRelationshipTo(endNode, relType);
                                // store for later use
                                rels.add((Relationship) currentObject);
                                relCount++;
                            }
                        } else {
                            System.out.println("NOT creating relationship of type " + relTypeName + ", start: " + startId + ", end: " + endId);
                            currentObject = null;
                            currentKey = null;
                        }
                    } else {
                        // reset if not at the beginning of a line
                        dis.reset();
                        if (currentKey == null) {
                            try {
                                currentKey = (String) deserialize(dis);
                            } catch (Throwable t) {
                                logger.warn("", t);
                            }
                        } else {
                            final Object obj = deserialize(dis);
                            if (obj != null && currentObject != null) {
                                if (uuidPropertyName.equals(currentKey) && currentObject instanceof Node) {
                                    final String uuid = (String) obj;
                                    uuidMap.put(uuid, (Node) currentObject);
                                }
                                if (currentKey.length() != 0) {
                                    // store object in DB
                                    currentObject.setProperty(currentKey, obj);
                                    // set type label
                                    if (currentObject instanceof Node && NodeInterface.type.dbName().equals(currentKey)) {
                                        ((Node) currentObject).addLabel(graphDb.forName(Label.class, (String) obj));
                                    }
                                } else {
                                    logger.error("Invalid property key for value {}, ignoring", obj);
                                }
                            } else {
                                logger.warn("No current object to store property in.");
                            }
                            currentKey = null;
                        }
                    }
                } catch (EOFException eofex) {
                    finished = true;
                }
            } while (!finished);
            totalNodeCount += nodeCount;
            totalRelCount += relCount;
            logger.info("Imported {} nodes and {} rels, committing transaction..", new Object[] { totalNodeCount, totalRelCount });
            tx.success();
        }
    } while (!finished);
    // build schema
    try (final Tx tx = app.tx()) {
        SchemaHelper.reloadSchema(new ErrorBuffer(), securityContext.getSessionId());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    final Map<String, Object> params = new HashMap<>();
    params.put("removeUnused", false);
    // set correct labels after schema has been compiled
    app.command(BulkCreateLabelsCommand.class).execute(params);
    double t1 = System.nanoTime();
    double time = ((t1 - t0) / 1000000000.0);
    DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    logger.info("Import done in {} s", decimalFormat.format(time));
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyContainer(org.structr.api.graph.PropertyContainer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) DecimalFormat(java.text.DecimalFormat) RelationshipType(org.structr.api.graph.RelationshipType) LinkedHashMap(java.util.LinkedHashMap) BufferedInputStream(java.io.BufferedInputStream) EOFException(java.io.EOFException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) FrameworkException(org.structr.common.error.FrameworkException) DataInputStream(java.io.DataInputStream) LinkedList(java.util.LinkedList) ErrorBuffer(org.structr.common.error.ErrorBuffer) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) GraphObject(org.structr.core.GraphObject)

Example 14 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class CreateRelationshipCommand method createRelationship.

private synchronized <A extends NodeInterface, B extends NodeInterface, R extends Relation<A, B, ?, ?>> R createRelationship(final A fromNode, final B toNode, final Class<R> relType, final PropertyMap attributes) throws FrameworkException {
    // disable updating access time when creating relationships
    securityContext.disableModificationOfAccessTime();
    final RelationshipFactory<R> factory = new RelationshipFactory(securityContext);
    final PropertyMap properties = new PropertyMap(attributes);
    final CreationContainer tmp = new CreationContainer();
    final R template = instantiate(relType);
    final Node startNode = fromNode.getNode();
    final Node endNode = toNode.getNode();
    final Date now = new Date();
    final Principal user = securityContext.getCachedUser();
    template.ensureCardinality(securityContext, fromNode, toNode);
    // date properties need converter
    AbstractRelationship.createdDate.setProperty(securityContext, tmp, now);
    AbstractRelationship.lastModifiedDate.setProperty(securityContext, tmp, now);
    // set initial properties manually (caution, this can only be used for primitive properties!)
    tmp.getData().put(GraphObject.id.jsonName(), getNextUuid());
    tmp.getData().put(GraphObject.type.jsonName(), relType.getSimpleName());
    tmp.getData().put(AbstractRelationship.relType.jsonName(), template.name());
    tmp.getData().put(AbstractRelationship.sourceId.jsonName(), fromNode.getUuid());
    tmp.getData().put(AbstractRelationship.targetId.jsonName(), toNode.getUuid());
    tmp.getData().put(AbstractRelationship.visibleToPublicUsers.jsonName(), false);
    tmp.getData().put(AbstractRelationship.visibleToAuthenticatedUsers.jsonName(), false);
    tmp.getData().put(AbstractRelationship.cascadeDelete.jsonName(), template.getCascadingDeleteFlag());
    if (user != null) {
        tmp.getData().put(AbstractRelationship.createdBy.jsonName(), user.getUuid());
    }
    // create relationship including initial properties
    final Relationship rel = startNode.createRelationshipTo(endNode, template, tmp.getData());
    final R newRel = factory.instantiateWithType(rel, relType, null, true);
    if (newRel != null) {
        newRel.setProperties(securityContext, properties);
        // notify transaction handler
        TransactionCommand.relationshipCreated(user, newRel);
        // notify relationship of its creation
        newRel.onRelationshipCreation();
        // iterate post creation transformations
        for (Transformation<GraphObject> transformation : StructrApp.getConfiguration().getEntityCreationTransformations(newRel.getClass())) {
            transformation.apply(securityContext, newRel);
        }
    }
    // enable access time update again for subsequent calls
    securityContext.enableModificationOfAccessTime();
    return newRel;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Node(org.structr.api.graph.Node) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) GraphObject(org.structr.core.GraphObject) Date(java.util.Date) Principal(org.structr.core.entity.Principal)

Example 15 with Relationship

use of org.structr.api.graph.Relationship in project structr by structr.

the class DeleteSpatialIndexCommand method execute.

@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final DatabaseService graphDb = StructrApp.getInstance().getService(NodeService.class).getGraphDb();
    final List<Node> toDelete = new LinkedList<>();
    for (final Node node : graphDb.getAllNodes()) {
        try {
            if (node.hasProperty("bbox") && node.hasProperty("gtype") && node.hasProperty("id") && node.hasProperty("latitude") && node.hasProperty("longitude")) {
                toDelete.add(node);
            }
        } catch (Throwable t) {
        }
    }
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        for (Node node : toDelete) {
            logger.info("Deleting node {}", node);
            try {
                for (Relationship rel : node.getRelationships()) {
                    rel.delete();
                }
                node.delete();
            } catch (Throwable t) {
                logger.warn("", t);
            }
        }
        tx.success();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) NodeService(org.structr.core.graph.NodeService) Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList)

Aggregations

Relationship (org.structr.api.graph.Relationship)26 Node (org.structr.api.graph.Node)13 LinkedList (java.util.LinkedList)7 LinkedHashMap (java.util.LinkedHashMap)6 DatabaseService (org.structr.api.DatabaseService)6 GraphObject (org.structr.core.GraphObject)6 AbstractNode (org.structr.core.entity.AbstractNode)6 AbstractRelationship (org.structr.core.entity.AbstractRelationship)6 RelationshipFactory (org.structr.core.graph.RelationshipFactory)5 HashMap (java.util.HashMap)4 FrameworkException (org.structr.common.error.FrameworkException)4 List (java.util.List)3 Test (org.junit.Test)3 SessionTransaction (org.structr.bolt.SessionTransaction)3 RelationshipRelationshipMapper (org.structr.bolt.mapper.RelationshipRelationshipMapper)3 App (org.structr.core.app.App)3 StructrApp (org.structr.core.app.StructrApp)3 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)3 NodeFactory (org.structr.core.graph.NodeFactory)3 Tx (org.structr.core.graph.Tx)3