Search in sources :

Example 1 with AbstractNode

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

the class SyncCommand method exportToFile.

// ----- static methods -----
/**
 * Exports the whole structr database to a file with the given name.
 *
 * @param graphDb
 * @param fileName
 * @param includeFiles
 * @throws FrameworkException
 */
public static void exportToFile(final DatabaseService graphDb, final String fileName, final String query, final boolean includeFiles) throws FrameworkException {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
        final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
        final Set<AbstractNode> nodes = new HashSet<>();
        final Set<AbstractRelationship> rels = new HashSet<>();
        boolean conditionalIncludeFiles = includeFiles;
        if (query != null) {
            logger.info("Using Cypher query {} to determine export set, disabling export of files", query);
            conditionalIncludeFiles = false;
            final List<GraphObject> result = StructrApp.getInstance().cypher(query, null);
            for (final GraphObject obj : result) {
                if (obj.isNode()) {
                    nodes.add((AbstractNode) obj.getSyncNode());
                } else {
                    rels.add((AbstractRelationship) obj.getSyncRelationship());
                }
            }
            logger.info("Query returned {} nodes and {} relationships.", new Object[] { nodes.size(), rels.size() });
        } else {
            nodes.addAll(nodeFactory.bulkInstantiate(graphDb.getAllNodes()));
            rels.addAll(relFactory.bulkInstantiate(graphDb.getAllRelationships()));
        }
        try (final FileOutputStream fos = new FileOutputStream(fileName)) {
            exportToStream(fos, nodes, rels, null, conditionalIncludeFiles);
        }
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
        throw new FrameworkException(500, t.getMessage());
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) GraphObject(org.structr.core.GraphObject) FileOutputStream(java.io.FileOutputStream) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with AbstractNode

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

the class BulkChangeNodePropertyKeyCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    String type = null;
    final String oldKey = (String) properties.get("oldKey");
    final String newKey = (String) properties.get("newKey");
    if (graphDb != null && StringUtils.isNotBlank(oldKey) && StringUtils.isNotBlank(newKey)) {
        Iterator<AbstractNode> nodeIterator = null;
        if (properties.containsKey(AbstractNode.type.dbName())) {
            type = (String) properties.get(AbstractNode.type.dbName());
            nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByLabel(type)).iterator();
            properties.remove(AbstractNode.type.dbName());
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
        }
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "ChangeNodePropertyKey", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                for (Entry entry : properties.entrySet()) {
                    String key = (String) entry.getKey();
                    PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
                    if (propertyKey != null) {
                        Node dbNode = node.getNode();
                        if (dbNode.hasProperty(newKey)) {
                            logger.error("Node {} has already a property with key {}", new Object[] { node, newKey });
                            throw new IllegalStateException("Node has already a property of the new key");
                        }
                        if (dbNode.hasProperty(oldKey)) {
                            dbNode.setProperty(newKey, dbNode.getProperty(oldKey));
                            dbNode.removeProperty(oldKey);
                        }
                        node.updateInIndex();
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
                logger.warn("Unable to set properties of node {}: {}", new Object[] { node.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set node properties: {}", t.getMessage());
            }
        });
        logger.info("Fixed {} nodes ...", count);
    } else {
        logger.info("No values for oldKey and/or newKey found, aborting.");
    }
    logger.info("Done");
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) DatabaseService(org.structr.api.DatabaseService) Entry(java.util.Map.Entry) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 3 with AbstractNode

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

the class BulkCreateLabelsCommand method execute.

@Override
public void execute(Map<String, Object> attributes) {
    final String entityType = (String) attributes.get("type");
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    final boolean removeUnused = !attributes.containsKey("removeUnused");
    final Iterator<AbstractNode> nodeIterator = Iterables.map(nodeFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getNodesByTypeProperty(entityType))).iterator();
    if (entityType == null) {
        info("Node type not set or no entity class found. Starting creation of labels for all nodes.");
    } else {
        info("Starting creation of labels for all nodes of type {}", entityType);
    }
    final long count = bulkGraphOperation(securityContext, nodeIterator, 10000, "CreateLabels", new BulkGraphOperation<AbstractNode>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
            TypeProperty.updateLabels(graphDb, node, node.getClass(), removeUnused);
        }

        @Override
        public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
            warn("Unable to create labels for node {}: {}", node, t.getMessage());
        }

        @Override
        public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
            warn("Unable to create labels for node: {}", t.getMessage());
        }
    });
    info("Done with creating labels on {} nodes", count);
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) StructrAndSpatialPredicate(org.structr.common.StructrAndSpatialPredicate) DatabaseService(org.structr.api.DatabaseService)

Example 4 with AbstractNode

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

the class BulkDeleteCommand method bulkDelete.

public void bulkDelete(final Iterator<GraphObject> iterator) throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    final long count = bulkGraphOperation(securityContext, iterator, 1000, "DeleteObjects", new BulkGraphOperation<GraphObject>() {

        @Override
        public void handleGraphObject(final SecurityContext securityContext, final GraphObject obj) {
            try {
                if (obj.isNode()) {
                    final AbstractNode node = (AbstractNode) obj;
                    if (!node.isGranted(Permission.delete, securityContext)) {
                        logger.warn("Could not delete {} because {} has no delete permission", obj.getUuid(), securityContext.getUser(false));
                    } else {
                        app.delete((NodeInterface) obj);
                    }
                } else {
                    app.delete((RelationshipInterface) obj);
                }
            } catch (FrameworkException fex) {
                logger.warn("Unable to delete node {}: {}", obj.getUuid(), fex.toString());
            }
        }

        @Override
        public void handleThrowable(SecurityContext securityContext, Throwable t, GraphObject node) {
            logger.warn("Unable to delete node {}", node.getUuid());
        }

        @Override
        public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
            logger.warn("Unable to delete nodes {}", t.toString());
        }
    });
    info("Done with deleting {} nodes", count);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject)

Example 5 with AbstractNode

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

the class BulkSetUuidCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final String nodeType = (String) attributes.get("type");
    final String relType = (String) attributes.get("relType");
    final Boolean allNodes = (Boolean) attributes.get("allNodes");
    final Boolean allRels = (Boolean) attributes.get("allRels");
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    final RelationshipFactory relFactory = new RelationshipFactory(superUserContext);
    if (nodeType != null || Boolean.TRUE.equals(allNodes)) {
        Iterator<AbstractNode> nodeIterator = null;
        if (Boolean.TRUE.equals(allNodes)) {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
            info("Start setting UUID on all nodes");
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByTypeProperty(nodeType)).iterator();
            info("Start setting UUID on nodes of type {}", new Object[] { nodeType });
        }
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "SetNodeUuid", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(final SecurityContext securityContext, final AbstractNode node) {
                try {
                    if (node.getProperty(GraphObject.id) == null) {
                        node.unlockSystemPropertiesOnce();
                        node.setProperty(GraphObject.id, NodeServiceCommand.getNextUuid());
                    }
                } catch (FrameworkException fex) {
                    logger.warn("Unable to set UUID of node {}", node, fex);
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
                logger.warn("Unable to set UUID of node {}", node, t);
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set UUID on node", t);
            }

            @Override
            public boolean doValidation() {
                return false;
            }
        });
        info("Done with setting UUID on {} nodes", count);
        return;
    }
    if (relType != null || Boolean.TRUE.equals(allRels)) {
        Iterator<AbstractRelationship> relIterator = null;
        if (Boolean.TRUE.equals(allRels)) {
            relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
            info("Start setting UUID on all rels", new Object[] { relType });
        } else {
            relIterator = Iterables.map(relFactory, graphDb.getRelationshipsByType(relType)).iterator();
            info("Start setting UUID on rels of type {}", new Object[] { relType });
        }
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "SetRelationshipUuid", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                try {
                    if (rel.getProperty(GraphObject.id) == null) {
                        rel.unlockSystemPropertiesOnce();
                        rel.setProperty(GraphObject.id, NodeServiceCommand.getNextUuid());
                    }
                } catch (FrameworkException fex) {
                    logger.warn("Unable to set UUID of relationship {}: {}", new Object[] { rel, fex.getMessage() });
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to set UUID of relationship {}: {}", rel, t.getMessage());
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set UUID on relationships {}", t.toString());
            }

            @Override
            public boolean doValidation() {
                return false;
            }
        });
        info("Done with setting UUID on {} relationships", count);
        return;
    }
    info("Unable to determine entity type to set UUID.");
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext)

Aggregations

AbstractNode (org.structr.core.entity.AbstractNode)62 FrameworkException (org.structr.common.error.FrameworkException)31 Tx (org.structr.core.graph.Tx)20 App (org.structr.core.app.App)18 StructrApp (org.structr.core.app.StructrApp)18 GraphObject (org.structr.core.GraphObject)17 SecurityContext (org.structr.common.SecurityContext)16 PropertyMap (org.structr.core.property.PropertyMap)12 Result (org.structr.core.Result)10 Test (org.junit.Test)9 AbstractRelationship (org.structr.core.entity.AbstractRelationship)9 LinkedList (java.util.LinkedList)8 TestOne (org.structr.core.entity.TestOne)8 DatabaseService (org.structr.api.DatabaseService)7 NodeInterface (org.structr.core.graph.NodeInterface)7 PropertyKey (org.structr.core.property.PropertyKey)7 Principal (org.structr.core.entity.Principal)6 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)5 DOMNode (org.structr.web.entity.dom.DOMNode)5 LinkedHashSet (java.util.LinkedHashSet)4