Search in sources :

Example 6 with DatabaseService

use of org.structr.api.DatabaseService in project structr by structr.

the class FindRelationshipCommand method execute.

// ~--- methods --------------------------------------------------------
public T execute(Relationship relationship) throws FrameworkException {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    RelationshipFactory<T> relationshipFactory = new RelationshipFactory<>(securityContext);
    if (graphDb != null) {
        return relationshipFactory.instantiate(relationship);
    }
    return (null);
}
Also used : DatabaseService(org.structr.api.DatabaseService)

Example 7 with DatabaseService

use of org.structr.api.DatabaseService in project structr by structr.

the class GetAllNodes method execute.

public List<AbstractNode> execute() throws FrameworkException {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    NodeFactory nodeFactory = new NodeFactory(securityContext);
    if (graphDb != null) {
        return nodeFactory.bulkInstantiate(graphDb.getAllNodes());
    }
    return Collections.emptyList();
}
Also used : DatabaseService(org.structr.api.DatabaseService)

Example 8 with DatabaseService

use of org.structr.api.DatabaseService 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 9 with DatabaseService

use of org.structr.api.DatabaseService in project structr by structr.

the class BulkCopyRelationshipPropertyCommand method execute.

@Override
public void execute(final Map<String, Object> map) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    final String sourceKey = (String) map.get("sourceKey");
    final String destKey = (String) map.get("destKey");
    if (sourceKey == null || destKey == null) {
        throw new IllegalArgumentException("This command requires one argument of type Map. Map must contain values for 'sourceKey' and 'destKey'.");
    }
    if (graphDb != null) {
        final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "CopyRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" rels
                if (rel.getProperty(GraphObject.id) != null) {
                    Class type = rel.getClass();
                    PropertyKey destPropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, destKey);
                    PropertyKey sourcePropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sourceKey);
                    try {
                        // copy properties
                        rel.setProperty(destPropertyKey, rel.getProperty(sourcePropertyKey));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to copy relationship property {} of relationship {} to {}: {}", new Object[] { sourcePropertyKey, rel.getUuid(), destPropertyKey, fex.getMessage() });
                    }
                }
            }

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

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to copy relationship properties: {}", t.getMessage());
            }
        });
        logger.info("Finished setting properties on {} nodes", count);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 10 with DatabaseService

use of org.structr.api.DatabaseService 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)

Aggregations

DatabaseService (org.structr.api.DatabaseService)31 SecurityContext (org.structr.common.SecurityContext)12 FrameworkException (org.structr.common.error.FrameworkException)12 AbstractNode (org.structr.core.entity.AbstractNode)8 Node (org.structr.api.graph.Node)7 GraphObject (org.structr.core.GraphObject)7 Tx (org.structr.core.graph.Tx)7 Relationship (org.structr.api.graph.Relationship)6 PropertyKey (org.structr.core.property.PropertyKey)6 StructrAndSpatialPredicate (org.structr.common.StructrAndSpatialPredicate)5 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 AbstractRelationship (org.structr.core.entity.AbstractRelationship)5 LinkedHashSet (java.util.LinkedHashSet)4 LinkedList (java.util.LinkedList)4 Entry (java.util.Map.Entry)4 Test (org.junit.Test)4 List (java.util.List)3 Label (org.structr.api.graph.Label)3 StructrTest (org.structr.common.StructrTest)3