Search in sources :

Example 31 with AbstractNode

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

the class NodeRelationshipStatisticsCommand method execute.

public Map<String, Long> execute(AbstractNode sNode, Direction dir) throws FrameworkException {
    final Map<String, Long> statistics = new LinkedHashMap<>();
    final Node node = sNode.getNode();
    Iterable<Relationship> rels = null;
    if (dir != null) {
        rels = node.getRelationships(dir);
    } else {
        rels = node.getRelationships();
    }
    try {
        // use temporary map to avoid frequent construction of Long values when increasing..
        Map<String, LongValueHolder> values = new LinkedHashMap<>();
        for (Relationship r : rels) {
            final String relType = r.getType().name();
            LongValueHolder count = values.get(relType);
            if (count == null) {
                count = new LongValueHolder();
                values.put(relType, count);
            }
            count.inc();
        }
        // create results from temporary map
        for (Entry<String, LongValueHolder> entry : values.entrySet()) {
            final String key = entry.getKey();
            LongValueHolder value = entry.getValue();
            statistics.put(key, value.getValue());
        }
    } catch (RuntimeException e) {
        logger.warn("Exception occured.", e);
    }
    return statistics;
}
Also used : Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) Relationship(org.structr.api.graph.Relationship) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with AbstractNode

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

the class BulkDeleteSoftDeletedNodesCommand 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, true, false);
    if (graphDb != null) {
        final Iterator<AbstractNode> nodeIterator = Iterables.map(nodeFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getAllNodes())).iterator();
        final boolean erase;
        if (properties.containsKey("erase") && properties.get("erase").equals("true")) {
            erase = true;
        } else {
            erase = false;
        }
        bulkGraphOperation(securityContext, nodeIterator, 1000, "DeleteSoftDeletedNodes", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                if (node.isDeleted()) {
                    logger.info("Found deleted node: {}", node);
                    if (erase) {
                        try {
                            StructrApp.getInstance(securityContext).delete(node);
                        } catch (FrameworkException ex) {
                            logger.warn("Could not delete node " + node, ex);
                        }
                    }
                }
            }

            @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("Done");
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) StructrAndSpatialPredicate(org.structr.common.StructrAndSpatialPredicate) DatabaseService(org.structr.api.DatabaseService)

Example 33 with AbstractNode

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

the class BulkFixNodePropertiesCommand method execute.

@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final String propertyName = (String) attributes.get("name");
    final String entityTypeName = (String) attributes.get("type");
    if (entityTypeName != null) {
        final Class type = SchemaHelper.getEntityClassForRawType(entityTypeName);
        if (type != null) {
            final DatabaseService db = StructrApp.getInstance(securityContext).getDatabaseService();
            final NodeFactory factory = new NodeFactory(securityContext);
            final Iterator<AbstractNode> nodeIterator = Iterables.map(factory, db.getNodesByLabel(entityTypeName)).iterator();
            logger.info("Trying to fix properties of all {} nodes", type.getSimpleName());
            long nodeCount = bulkGraphOperation(securityContext, nodeIterator, 100, "FixNodeProperties", new BulkGraphOperation<AbstractNode>() {

                private void fixProperty(AbstractNode node, Property propertyToFix) {
                    Node databaseNode = node.getNode();
                    if (databaseNode.hasProperty(propertyToFix.dbName())) {
                        // check value with property converter
                        PropertyConverter converter = propertyToFix.databaseConverter(securityContext, node);
                        if (converter != null) {
                            try {
                                Object value = databaseNode.getProperty(propertyToFix.dbName());
                                converter.revert(value);
                            } catch (ClassCastException cce) {
                                // exception, needs fix
                                String databaseName = propertyToFix.dbName();
                                Object databaseValue = databaseNode.getProperty(databaseName);
                                Object correctedValue = propertyToFix.fixDatabaseProperty(databaseValue);
                                if (databaseValue != null && correctedValue != null) {
                                    try {
                                        // try to set database value to corrected value
                                        databaseNode.setProperty(databaseName, correctedValue);
                                    } catch (Throwable t) {
                                        logger.warn("Unable to fix property {} of {} with UUID {} which is of type {}", new Object[] { propertyToFix.dbName(), type.getSimpleName(), node.getUuid(), databaseValue != null ? databaseValue.getClass() : "null" });
                                    }
                                }
                            } catch (Throwable t) {
                                // log exceptions of other types
                                logger.warn("", t);
                            }
                        }
                    }
                }

                @Override
                public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                    if (propertyName != null) {
                        PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, propertyName);
                        if (key != null) {
                            // needs type cast to Property to use fixDatabaseProperty method
                            if (key instanceof Property) {
                                fixProperty(node, (Property) key);
                            }
                        }
                    } else {
                        for (PropertyKey key : node.getPropertyKeys(PropertyView.All)) {
                            // needs type cast to Property to use fixDatabaseProperty method
                            if (key instanceof Property) {
                                fixProperty(node, (Property) key);
                            }
                        }
                    }
                }
            });
            logger.info("Fixed {} nodes", nodeCount);
            return;
        }
    }
    logger.info("Unable to determine property and/or entity type to fix.");
}
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) PropertyConverter(org.structr.core.converter.PropertyConverter) SecurityContext(org.structr.common.SecurityContext) Property(org.structr.core.property.Property) PropertyKey(org.structr.core.property.PropertyKey)

Example 34 with AbstractNode

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

the class BulkRebuildIndexCommand method rebuildNodeIndex.

// ----- private methods -----
private void rebuildNodeIndex(final String entityType) {
    final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    Iterator<AbstractNode> nodeIterator = null;
    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 (re-)indexing all nodes");
    } else {
        info("Starting (re-)indexing all nodes of type {}", entityType);
    }
    long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "RebuildNodeIndex", new BulkGraphOperation<AbstractNode>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
            node.updateInIndex();
        }

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

        @Override
        public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
            logger.warn("Unable to index node: {}", t.getMessage());
        }
    });
    info("Done with (re-)indexing {} 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 35 with AbstractNode

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

the class BulkSetNodePropertiesCommand 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);
    final String type = (String) properties.get("type");
    if (StringUtils.isBlank(type)) {
        throw new FrameworkException(422, "Type must not be empty");
    }
    final Class cls = SchemaHelper.getEntityClassForRawType(type);
    if (cls == null) {
        throw new FrameworkException(422, "Invalid type " + type);
    }
    if (graphDb != null) {
        final App app = StructrApp.getInstance(securityContext);
        Iterator<AbstractNode> nodeIterator = null;
        if (properties.containsKey(AbstractNode.type.dbName())) {
            try (final Tx tx = app.tx()) {
                nodeIterator = app.nodeQuery(cls).getResult().getResults().iterator();
                properties.remove(AbstractNode.type.dbName());
                tx.success();
            }
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
        }
        // remove "type" so it won't be set later
        properties.remove("type");
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "SetNodeProperties", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                // Treat only "our" nodes
                if (node.getProperty(GraphObject.id) != null) {
                    for (Entry entry : properties.entrySet()) {
                        String key = (String) entry.getKey();
                        Object val = null;
                        // allow to set new type
                        if (key.equals("newType")) {
                            key = "type";
                        }
                        PropertyConverter inputConverter = StructrApp.key(cls, key).inputConverter(securityContext);
                        if (inputConverter != null) {
                            try {
                                val = inputConverter.convert(entry.getValue());
                            } catch (FrameworkException ex) {
                                logger.error("", ex);
                            }
                        } else {
                            val = entry.getValue();
                        }
                        PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
                        if (propertyKey != null) {
                            try {
                                node.unlockSystemPropertiesOnce();
                                node.setProperty(propertyKey, val);
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, node.getUuid(), val, fex.getMessage() });
                            }
                        }
                    }
                }
            }

            @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);
    }
    logger.info("Done");
}
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) DatabaseService(org.structr.api.DatabaseService) Entry(java.util.Map.Entry) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

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