Search in sources :

Example 26 with DatabaseService

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

the class CypherQueryCommand method execute.

public List<GraphObject> execute(String query, Map<String, Object> parameters, boolean includeHiddenAndDeleted, boolean publicOnly) throws FrameworkException {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    NodeFactory nodeFactory = new NodeFactory(securityContext);
    List<GraphObject> resultList = new LinkedList<>();
    // graphdb can be null..
    if (graphDb != null) {
        try (final NativeResult result = graphDb.execute(query, parameters != null ? parameters : Collections.emptyMap())) {
            while (result.hasNext()) {
                final Map<String, Object> row = result.next();
                for (Entry<String, Object> entry : row.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    final Object obj = handleObject(nodeFactory, relFactory, key, value, includeHiddenAndDeleted, publicOnly, 0);
                    if (obj != null) {
                        if (obj instanceof GraphObject) {
                            resultList.add((GraphObject) obj);
                        } else if (obj instanceof Collection) {
                            final List<Object> nonGraphObjectResult = new LinkedList<>();
                            for (final Object item : ((Collection) obj)) {
                                if (item instanceof GraphObject) {
                                    resultList.add((GraphObject) item);
                                } else {
                                    nonGraphObjectResult.add(item);
                                }
                            }
                            if (!nonGraphObjectResult.isEmpty()) {
                                // Wrap non-graph-objects in simple list
                                final GraphObjectMap graphObject = new GraphObjectMap();
                                graphObject.setProperty(new GenericProperty(key), nonGraphObjectResult);
                                resultList.add(graphObject);
                            }
                        } else {
                            logger.warn("Unable to handle Cypher query result object of type {}, ignoring.", obj.getClass().getName());
                        }
                    }
                }
            }
        }
    }
    return resultList;
}
Also used : NativeResult(org.structr.api.NativeResult) DatabaseService(org.structr.api.DatabaseService) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList)

Example 27 with DatabaseService

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

the class MaintenanceTest method testSyncCommandInheritance.

@Test
public void testSyncCommandInheritance() {
    try {
        // create test nodes
        final List<TestEleven> testNodes = createTestNodes(TestEleven.class, 10);
        final String tenantIdentifier = app.getDatabaseService().getTenantIdentifier();
        int labelCount = 7;
        // one additional label
        if (tenantIdentifier != null) {
            labelCount += 1;
        }
        try (final Tx tx = app.tx()) {
            for (final TestEleven node : testNodes) {
                Iterable<Label> labels = node.getNode().getLabels();
                assertEquals(labelCount, Iterables.count(labels));
                for (final Label label : labels) {
                    System.out.print(label.name() + " ");
                }
                System.out.println();
                final Set<String> names = Iterables.toSet(labels).stream().map(Label::name).collect(Collectors.toSet());
                assertEquals("Number of labels must be 7", labelCount, names.size());
                assertTrue("Set of labels must contain AbstractNode", names.contains("AbstractNode"));
                assertTrue("Set of labels must contain NodeInterface", names.contains("NodeInterface"));
                assertTrue("Set of labels must contain AccessControllable", names.contains("AccessControllable"));
                assertTrue("Set of labels must contain CMISInfo", names.contains("CMISInfo"));
                assertTrue("Set of labels must contain CMISItemInfo", names.contains("CMISItemInfo"));
                assertTrue("Set of labels must contain TestOne", names.contains("TestOne"));
                assertTrue("Set of labels must contain TestEleven", names.contains("TestEleven"));
                if (tenantIdentifier != null) {
                    assertTrue("Set of labels must contain custom tenant identifier if set", names.contains(tenantIdentifier));
                }
            }
            tx.success();
        }
        // test export
        app.command(SyncCommand.class).execute(toMap("mode", "export", "file", EXPORT_FILENAME));
        final Path exportFile = Paths.get(EXPORT_FILENAME);
        assertTrue("Export file doesn't exist!", Files.exists(exportFile));
        // stop existing and start new database
        stopSystem();
        startSystem();
        // test import
        app.command(SyncCommand.class).execute(toMap("mode", "import", "file", EXPORT_FILENAME));
        final DatabaseService db = app.getDatabaseService();
        try (final Tx tx = app.tx()) {
            final Result<TestEleven> result = app.nodeQuery(TestEleven.class).getResult();
            assertEquals(10, result.size());
            for (final TestEleven node : result.getResults()) {
                Iterable<Label> labels = node.getNode().getLabels();
                final Set<Label> set = new HashSet<>(Iterables.toList(labels));
                assertEquals(labelCount, set.size());
                assertTrue("First label has to be AbstractNode", set.contains(db.forName(Label.class, "AbstractNode")));
                assertTrue("Second label has to be NodeInterface", set.contains(db.forName(Label.class, "NodeInterface")));
                assertTrue("Third label has to be AccessControllable", set.contains(db.forName(Label.class, "AccessControllable")));
                assertTrue("Fourth label has to be CMISInfo", set.contains(db.forName(Label.class, "CMISInfo")));
                assertTrue("Firth label has to be CMISItemInfo", set.contains(db.forName(Label.class, "CMISItemInfo")));
                assertTrue("Sixth label has to be TestEleven", set.contains(db.forName(Label.class, "TestEleven")));
                assertTrue("Seventh label has to be TestOne", set.contains(db.forName(Label.class, "TestOne")));
                if (tenantIdentifier != null) {
                    assertTrue("Set of labels must contain custom tenant identifier if set", set.contains(db.forName(Label.class, tenantIdentifier)));
                }
            }
            tx.success();
        }
        // clean-up after test
        Files.delete(exportFile);
    } catch (Exception ex) {
        ex.printStackTrace();
        logger.warn("", ex);
        fail("Unexpected exception.");
    }
}
Also used : Path(java.nio.file.Path) TestEleven(org.structr.core.entity.TestEleven) Tx(org.structr.core.graph.Tx) SyncCommand(org.structr.core.graph.SyncCommand) Label(org.structr.api.graph.Label) DatabaseService(org.structr.api.DatabaseService) FrameworkException(org.structr.common.error.FrameworkException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 28 with DatabaseService

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

the class DeleteSpatialIndexCommand method execute.

@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final DatabaseService graphDb = StructrApp.getInstance().getService(NodeService.class).getGraphDb();
    final List<Node> toDelete = new LinkedList<>();
    for (final Node node : graphDb.getAllNodes()) {
        try {
            if (node.hasProperty("bbox") && node.hasProperty("gtype") && node.hasProperty("id") && node.hasProperty("latitude") && node.hasProperty("longitude")) {
                toDelete.add(node);
            }
        } catch (Throwable t) {
        }
    }
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        for (Node node : toDelete) {
            logger.info("Deleting node {}", node);
            try {
                for (Relationship rel : node.getRelationships()) {
                    rel.delete();
                }
                node.delete();
            } catch (Throwable t) {
                logger.warn("", t);
            }
        }
        tx.success();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) NodeService(org.structr.core.graph.NodeService) Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList)

Example 29 with DatabaseService

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

the class Synchronize method onRequest.

@Override
public void onRequest(final CloudConnection serverConnection) throws IOException, FrameworkException {
    final DatabaseService graphDb = StructrApp.getInstance().getDatabaseService();
    final String uuidPropertyName = GraphObject.id.dbName();
    final Set<Long> visitedObjectIDs = new HashSet<>();
    for (final Node node : graphDb.getAllNodes()) {
        if (!visitedObjectIDs.contains(node.getId())) {
            final String hash = contentHashCode(node, visitedObjectIDs);
            final Object uuid = node.getProperty(uuidPropertyName, null);
            if (uuid != null && uuid instanceof String) {
                serverConnection.send(new Diff(uuid.toString(), hash));
            }
        }
    }
    // clear set of visited objects because node and relationship IDs are offsets and can overlap.
    visitedObjectIDs.clear();
    for (final Relationship relationship : graphDb.getAllRelationships()) {
        if (!visitedObjectIDs.contains(relationship.getId())) {
            final String hash = contentHashCode(relationship, visitedObjectIDs);
            final Object uuid = relationship.getProperty(uuidPropertyName, null);
            if (uuid != null && uuid instanceof String) {
                serverConnection.send(new Diff(uuid.toString(), hash));
            }
        }
    }
    serverConnection.send(new Finish());
}
Also used : Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) GraphObject(org.structr.core.GraphObject) DatabaseService(org.structr.api.DatabaseService) Finish(org.structr.cloud.message.Finish) HashSet(java.util.HashSet)

Example 30 with DatabaseService

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

the class UpdateTransmission method doRemote.

@Override
public Boolean doRemote(final CloudConnection client) throws IOException, FrameworkException {
    // send synchronization request first
    client.send(new Synchronize());
    // send all node and relationship data
    final DatabaseService graphDb = StructrApp.getInstance().getDatabaseService();
    final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
    final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
    for (final Node neo4jNode : graphDb.getAllNodes()) {
        final NodeInterface node = nodeFactory.instantiate(neo4jNode);
        if (node instanceof File) {
            PushTransmission.sendFile(client, (File) node, CloudService.CHUNK_SIZE);
        } else {
            client.send(new NodeDataContainer(node, 0));
        }
    }
    for (final Relationship relationship : graphDb.getAllRelationships()) {
        final RelationshipInterface relationshipInterface = relFactory.instantiate(relationship);
        client.send(new RelationshipDataContainer(relationshipInterface, 0));
    }
    // wait for end of transmission
    client.waitForTransmission();
    return true;
}
Also used : NodeFactory(org.structr.core.graph.NodeFactory) RelationshipDataContainer(org.structr.cloud.message.RelationshipDataContainer) RelationshipFactory(org.structr.core.graph.RelationshipFactory) Node(org.structr.api.graph.Node) Relationship(org.structr.api.graph.Relationship) RelationshipInterface(org.structr.core.graph.RelationshipInterface) NodeDataContainer(org.structr.cloud.message.NodeDataContainer) DatabaseService(org.structr.api.DatabaseService) File(org.structr.dynamic.File) NodeInterface(org.structr.core.graph.NodeInterface)

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