Search in sources :

Example 66 with SecurityContext

use of org.structr.common.SecurityContext 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 67 with SecurityContext

use of org.structr.common.SecurityContext 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 68 with SecurityContext

use of org.structr.common.SecurityContext 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 69 with SecurityContext

use of org.structr.common.SecurityContext in project structr by structr.

the class BulkRebuildIndexCommand method rebuildRelationshipIndex.

private void rebuildRelationshipIndex(final String relType) {
    final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, Iterables.filter(new StructrAndSpatialPredicate(true, false, false), graphDb.getRelationshipsByType(relType))).iterator();
    if (relType == null) {
        info("Relationship type not set, starting (re-)indexing all relationships");
    } else {
        info("Starting (re-)indexing all relationships of type {}", new Object[] { relType });
    }
    long count = bulkGraphOperation(securityContext, relIterator, 1000, "RebuildRelIndex", new BulkGraphOperation<AbstractRelationship>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
            rel.updateInIndex();
        }

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

        @Override
        public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
            logger.warn("Unable to index relationship: {}", t.getMessage());
        }
    });
    info("Done with (re-)indexing {} relationships", count);
}
Also used : AbstractRelationship(org.structr.core.entity.AbstractRelationship) SecurityContext(org.structr.common.SecurityContext) StructrAndSpatialPredicate(org.structr.common.StructrAndSpatialPredicate) DatabaseService(org.structr.api.DatabaseService)

Example 70 with SecurityContext

use of org.structr.common.SecurityContext 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

SecurityContext (org.structr.common.SecurityContext)131 FrameworkException (org.structr.common.error.FrameworkException)76 App (org.structr.core.app.App)56 StructrApp (org.structr.core.app.StructrApp)56 Tx (org.structr.core.graph.Tx)36 GraphObject (org.structr.core.GraphObject)35 PropertyKey (org.structr.core.property.PropertyKey)26 PropertyMap (org.structr.core.property.PropertyMap)26 AbstractNode (org.structr.core.entity.AbstractNode)19 IOException (java.io.IOException)18 Map (java.util.Map)17 File (org.structr.web.entity.File)14 LinkedList (java.util.LinkedList)13 DatabaseService (org.structr.api.DatabaseService)12 DOMNode (org.structr.web.entity.dom.DOMNode)12 Result (org.structr.core.Result)11 PropertyConverter (org.structr.core.converter.PropertyConverter)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 Query (org.structr.core.app.Query)10 Principal (org.structr.core.entity.Principal)10