Search in sources :

Example 6 with SecurityContext

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

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

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

the class BulkSetRelationshipPropertiesCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relationshipFactory = new RelationshipFactory(securityContext);
    if (graphDb != null) {
        Iterator<AbstractRelationship> relIterator = null;
        final String typeName = "type";
        if (properties.containsKey(typeName)) {
            relIterator = StructrApp.getInstance(securityContext).relationshipQuery(SchemaHelper.getEntityClassForRawType(typeName)).getAsList().iterator();
            properties.remove(typeName);
        } else {
            relIterator = Iterables.map(relationshipFactory, graphDb.getAllRelationships()).iterator();
        }
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "SetRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" nodes
                if (rel.getProperty(AbstractRelationship.id) != null) {
                    for (Entry entry : properties.entrySet()) {
                        String key = (String) entry.getKey();
                        Object val = entry.getValue();
                        PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(rel.getClass(), key);
                        if (propertyKey != null) {
                            try {
                                rel.setProperty(propertyKey, val);
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to set relationship property {} of relationship {} to {}: {}", new Object[] { propertyKey, rel.getUuid(), val, fex.getMessage() });
                            }
                        }
                    }
                }
            }

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

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

Example 9 with SecurityContext

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

Example 10 with SecurityContext

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

the class SetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        Class type = null;
        PropertyMap propertyMap = null;
        if (sources[0] instanceof GraphObject) {
            final GraphObject source = (GraphObject) sources[0];
            type = source.getEntityType();
        }
        if (type == null) {
            throw new FrameworkException(422, "Can't get type of object '" + sources[0].toString() + "' in set() method!");
        }
        final GraphObject sourceObject = (GraphObject) sources[0];
        if (sources.length == 2 && sources[1] instanceof Map) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
        } else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
        } else if (sources.length == 2 && sources[1] instanceof String) {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> values = deserialize(gson, sources[1].toString());
            if (values != null) {
                propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, values);
            }
        } else if (sources.length > 2) {
            propertyMap = new PropertyMap();
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_SET_JS : ERROR_MESSAGE_SET));
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = config.getPropertyKeyForJSONName(type, sources[c].toString());
                if (key != null) {
                    final PropertyConverter inputConverter = key.inputConverter(securityContext);
                    Object value = sources[c + 1];
                    if (inputConverter != null) {
                        value = inputConverter.convert(value);
                    }
                    propertyMap.put(key, value);
                }
            }
        } else {
            throw new FrameworkException(422, "Invalid use of builtin method set, usage: set(entity, params..)");
        }
        if (propertyMap != null) {
            sourceObject.setProperties(securityContext, propertyMap);
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Gson(com.google.gson.Gson) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) 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