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;
}
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.");
}
}
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();
}
}
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());
}
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;
}
Aggregations