Search in sources :

Example 1 with StructrAndSpatialPredicate

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

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

the class SchemaImporter method analyzeSchema.

public void analyzeSchema() {
    final App app = StructrApp.getInstance();
    final FileBasedHashLongMap<NodeInfo> nodeIdMap = new FileBasedHashLongMap<>(userHome + File.separator + ".structrSchemaAnalyzer");
    final DatabaseService graphDb = app.getDatabaseService();
    final ConfigurationProvider configuration = Services.getInstance().getConfigurationProvider();
    final Set<NodeInfo> nodeTypes = new LinkedHashSet<>();
    final Set<RelationshipInfo> relationships = new LinkedHashSet<>();
    final Map<String, SchemaNode> schemaNodes = new LinkedHashMap<>();
    final Map<String, List<TypeInfo>> typeInfoTypeMap = new LinkedHashMap<>();
    final List<TypeInfo> reducedTypeInfos = new LinkedList<>();
    final List<TypeInfo> typeInfos = new LinkedList<>();
    Iterator<Relationship> relIterator = null;
    Iterator<Node> nodeIterator = null;
    info("Fetching all nodes iterator..");
    try (final Tx tx = app.tx()) {
        nodeIterator = Iterables.filter(new StructrAndSpatialPredicate(false, false, true), graphDb.getAllNodes()).iterator();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Starting to analyze nodes..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), nodeIterator, 100000, "Analyzing nodes", new BulkGraphOperation<Node>() {

        @Override
        public void handleGraphObject(final SecurityContext securityContext, final Node node) throws FrameworkException {
            final NodeInfo nodeInfo = new NodeInfo(node);
            // hashcode of nodeInfo is derived from its property and type signature!
            nodeTypes.add(nodeInfo);
            // add node ID to our new test datastructure
            nodeIdMap.add(nodeInfo, node.getId());
        }
    });
    info("Identifying common base classes..");
    try (final Tx tx = app.tx(true, false, false)) {
        // nodeTypes now contains all existing node types and their property sets
        identifyCommonBaseClasses(app, nodeTypes, nodeIdMap, typeInfos);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Collecting type information..");
    try (final Tx tx = app.tx(true, false, false)) {
        // group type infos by type
        collectTypeInfos(typeInfos, typeInfoTypeMap);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Aggregating type information..");
    try (final Tx tx = app.tx(true, false, false)) {
        // reduce type infos with more than one type
        reduceTypeInfos(typeInfoTypeMap, reducedTypeInfos);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Identifying property sets..");
    try (final Tx tx = app.tx(true, false, false)) {
        // intersect property sets of type infos
        intersectPropertySets(reducedTypeInfos);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Sorting result..");
    try (final Tx tx = app.tx(true, false, false)) {
        // sort type infos
        Collections.sort(reducedTypeInfos, new HierarchyComparator(false));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    final Map<String, TypeInfo> reducedTypeInfoMap = new LinkedHashMap<>();
    for (final TypeInfo info : reducedTypeInfos) {
        final String type = info.getPrimaryType();
        // map TypeInfo to type for later use
        reducedTypeInfoMap.put(type, info);
        info("Starting with setting of type and ID for type {}", type);
        bulkGraphOperation(SecurityContext.getSuperUserInstance(), info.getNodeIds().iterator(), 10000, "Setting type and ID", new BulkGraphOperation<Long>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, Long nodeId) throws FrameworkException {
                final Node node = graphDb.getNodeById(nodeId);
                node.setProperty(GraphObject.id.dbName(), NodeServiceCommand.getNextUuid());
                node.setProperty(GraphObject.type.dbName(), type);
            }
        });
    }
    info("Fetching all relationships iterator..");
    try (final Tx tx = app.tx(true, false, false)) {
        relIterator = Iterables.filter(new StructrAndSpatialPredicate(false, false, true), graphDb.getAllRelationships()).iterator();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Starting with analyzing relationships..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), relIterator, 10000, "Analyzing relationships", new BulkGraphOperation<Relationship>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, Relationship rel) throws FrameworkException {
            final Node startNode = rel.getStartNode();
            final Node endNode = rel.getEndNode();
            // make sure node has been successfully identified above
            if (startNode.hasProperty("type") && endNode.hasProperty("type")) {
                final String relationshipType = rel.getType().name();
                final String startNodeType = (String) startNode.getProperty("type");
                final String endNodeType = (String) endNode.getProperty("type");
                relationships.add(new RelationshipInfo(startNodeType, endNodeType, relationshipType));
                // create combined type on imported relationship
                if (startNodeType != null && endNodeType != null) {
                    final String combinedType = getCombinedType(startNodeType, relationshipType, endNodeType);
                    logger.debug("Combined relationship type {} found for rel type {}, start node type {}, end node type {}", new Object[] { combinedType, relationshipType, startNodeType, endNodeType });
                    rel.setProperty(GraphObject.type.dbName(), combinedType);
                }
                // create ID on imported relationship
                rel.setProperty(GraphObject.id.dbName(), NodeServiceCommand.getNextUuid());
            }
        }
    });
    info("Grouping relationships..");
    // group relationships by type
    final Map<String, List<RelationshipInfo>> relTypeInfoMap = new LinkedHashMap<>();
    for (final RelationshipInfo relInfo : relationships) {
        // final String relType         = relInfo.getRelType();
        final String combinedType = getCombinedType(relInfo.getStartNodeType(), relInfo.getRelType(), relInfo.getEndNodeType());
        List<RelationshipInfo> infos = relTypeInfoMap.get(combinedType);
        if (infos == null) {
            infos = new LinkedList<>();
            relTypeInfoMap.put(combinedType, infos);
        }
        infos.add(relInfo);
    }
    info("Aggregating relationship information..");
    final List<RelationshipInfo> reducedRelationshipInfos = new ArrayList<>();
    if (Settings.InheritanceDetection.getValue()) {
        // reduce relationship infos into one
        for (final List<RelationshipInfo> infos : relTypeInfoMap.values()) {
            reducedRelationshipInfos.addAll(reduceNodeTypes(infos, reducedTypeInfoMap));
        }
    } else {
        reducedRelationshipInfos.addAll(relationships);
    }
    info("Starting with schema node creation..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), reducedTypeInfos.iterator(), 100000, "Creating schema nodes", new BulkGraphOperation<TypeInfo>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, TypeInfo typeInfo) throws FrameworkException {
            final String type = typeInfo.getPrimaryType();
            if (!"ReferenceNode".equals(type)) {
                final Map<String, Class> props = typeInfo.getPropertySet();
                final PropertyMap propertyMap = new PropertyMap();
                // add properties
                for (final Map.Entry<String, Class> propertyEntry : props.entrySet()) {
                    final String propertyName = propertyEntry.getKey();
                    final Class propertyType = propertyEntry.getValue();
                    // handle array types differently
                    String propertyTypeName = propertyType.getSimpleName();
                    if (propertyType.isArray()) {
                        // remove "[]" from the end and append "Array" to match the appropriate parser
                        propertyTypeName = propertyTypeName.substring(0, propertyTypeName.length() - 2).concat("Array");
                    }
                    propertyMap.put(new StringProperty("_".concat(propertyName)), propertyTypeName);
                }
                // set node type which is in "name" property
                propertyMap.put(AbstractNode.name, type);
                // check if there is an existing Structr entity with the same type
                // and make the dynamic class extend the existing class if yes.
                final Class existingType = configuration.getNodeEntityClass(type);
                if (existingType != null) {
                    propertyMap.put(SchemaNode.extendsClass, existingType.getName());
                } else if (!typeInfo.getOtherTypes().isEmpty()) {
                    // only the first supertype is supported
                    propertyMap.put(SchemaNode.extendsClass, typeInfo.getSuperclass(reducedTypeInfoMap));
                }
                final SchemaNode existingNode = app.nodeQuery(SchemaNode.class).andName(type).getFirst();
                if (existingNode != null) {
                    for (final Entry<PropertyKey, Object> entry : propertyMap.entrySet()) {
                        existingNode.setProperty(entry.getKey(), entry.getValue());
                    }
                    schemaNodes.put(type, existingNode);
                } else {
                    // create schema node
                    schemaNodes.put(type, app.create(SchemaNode.class, propertyMap));
                }
            }
        }
    });
    info("Starting with schema relationship creation..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), reducedRelationshipInfos.iterator(), 100000, "Creating schema relationships", new BulkGraphOperation<RelationshipInfo>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, RelationshipInfo template) throws FrameworkException {
            final String startNodeType = template.getStartNodeType();
            final String endNodeType = template.getEndNodeType();
            if (startNodeType != null && endNodeType != null) {
                final SchemaNode startNode = schemaNodes.get(startNodeType);
                final SchemaNode endNode = schemaNodes.get(endNodeType);
                if (startNode != null && endNode != null) {
                    final String relationshipType = template.getRelType();
                    final PropertyMap propertyMap = new PropertyMap();
                    propertyMap.put(SchemaRelationshipNode.sourceId, startNode.getUuid());
                    propertyMap.put(SchemaRelationshipNode.targetId, endNode.getUuid());
                    propertyMap.put(SchemaRelationshipNode.relationshipType, relationshipType);
                    app.create(SchemaRelationshipNode.class, propertyMap);
                } else {
                    info("Unable to create schema relationship node for {} -> {}, no schema nodes found", startNodeType, endNodeType);
                }
            }
        }
    });
    info("Starting with index rebuild..");
    // rebuild index
    app.command(BulkRebuildIndexCommand.class).execute(Collections.EMPTY_MAP);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) LinkedHashSet(java.util.LinkedHashSet) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) SchemaNode(org.structr.core.entity.SchemaNode) ArrayList(java.util.ArrayList) StructrAndSpatialPredicate(org.structr.common.StructrAndSpatialPredicate) StringProperty(org.structr.core.property.StringProperty) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList) BulkRebuildIndexCommand(org.structr.core.graph.BulkRebuildIndexCommand) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) Relationship(org.structr.api.graph.Relationship) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map)

Example 3 with StructrAndSpatialPredicate

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

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

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

Aggregations

DatabaseService (org.structr.api.DatabaseService)5 SecurityContext (org.structr.common.SecurityContext)5 StructrAndSpatialPredicate (org.structr.common.StructrAndSpatialPredicate)5 AbstractNode (org.structr.core.entity.AbstractNode)4 FrameworkException (org.structr.common.error.FrameworkException)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Node (org.structr.api.graph.Node)1 Relationship (org.structr.api.graph.Relationship)1 GraphObject (org.structr.core.GraphObject)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 AbstractRelationship (org.structr.core.entity.AbstractRelationship)1 SchemaNode (org.structr.core.entity.SchemaNode)1 SchemaRelationshipNode (org.structr.core.entity.SchemaRelationshipNode)1