use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class BasicTest method testRelationshipsOnNodeCreation.
@Test
public void testRelationshipsOnNodeCreation() {
Principal user = null;
TestOne test = null;
// create user
try (final Tx tx = app.tx()) {
user = app.create(Principal.class, "tester");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final SecurityContext ctx = SecurityContext.getInstance(user, AccessMode.Backend);
final App app = StructrApp.getInstance(ctx);
// create object with user context
try (final Tx tx = app.tx()) {
test = app.create(TestOne.class);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// query for relationships
try (final Tx tx = app.tx()) {
final List<? extends RelationshipInterface> rels1 = app.relationshipQuery().and(AbstractRelationship.sourceId, user.getUuid()).getAsList();
final List<Class> classes1 = rels1.stream().map(r -> r.getClass()).collect(Collectors.toList());
assertEquals("Invalid number of relationships after object creation", 2, rels1.size());
assertTrue("Invalid relationship type after object creation", classes1.contains(Security.class));
assertTrue("Invalid relationship type after object creation", classes1.contains(PrincipalOwnsNode.class));
final List<? extends RelationshipInterface> rels2 = app.relationshipQuery().and(AbstractRelationship.targetId, test.getUuid()).getAsList();
final List<Class> classes2 = rels2.stream().map(r -> r.getClass()).collect(Collectors.toList());
assertEquals("Invalid number of relationships after object creation", 2, rels2.size());
assertTrue("Invalid relationship type after object creation", classes2.contains(Security.class));
assertTrue("Invalid relationship type after object creation", classes2.contains(PrincipalOwnsNode.class));
final List<? extends RelationshipInterface> rels3 = Iterables.toList(test.getIncomingRelationships());
final List<Class> classes3 = rels3.stream().map(r -> r.getClass()).collect(Collectors.toList());
assertEquals("Invalid number of relationships after object creation", 2, rels3.size());
assertTrue("Invalid relationship type after object creation", classes3.contains(Security.class));
assertTrue("Invalid relationship type after object creation", classes3.contains(PrincipalOwnsNode.class));
final Security sec = app.relationshipQuery(Security.class).getFirst();
assertNotNull("Relationship caching on node creation is broken", sec);
final PrincipalOwnsNode owns = app.relationshipQuery(PrincipalOwnsNode.class).getFirst();
assertNotNull("Relationship caching on node creation is broken", owns);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class CloudConnection method storeRelationship.
public RelationshipInterface storeRelationship(final DataContainer receivedData) throws FrameworkException {
final RelationshipDataContainer receivedRelationshipData = (RelationshipDataContainer) receivedData;
final String sourceStartNodeId = receivedRelationshipData.getSourceStartNodeId();
final String sourceEndNodeId = receivedRelationshipData.getSourceEndNodeId();
final String uuid = receivedRelationshipData.getRelationshipId();
// if end node ID was not found in the ID map,
// assume it already exists in the database
// (i.e. it was created earlier)
String targetStartNodeId = idMap.get(sourceStartNodeId);
if (targetStartNodeId == null) {
targetStartNodeId = sourceStartNodeId;
}
// if end node ID was not found in the ID map,
// assume it already exists in the database
// (i.e. it was created earlier)
String targetEndNodeId = idMap.get(sourceEndNodeId);
if (targetEndNodeId == null) {
targetEndNodeId = sourceEndNodeId;
}
if (targetStartNodeId != null && targetEndNodeId != null) {
// Get new start and end node
final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
final NodeInterface targetStartNode = app.getNodeById(targetStartNodeId);
final NodeInterface targetEndNode = app.getNodeById(targetEndNodeId);
final String typeName = receivedRelationshipData.getType();
final Class relType = config.getRelationshipEntityClass(typeName);
if (targetStartNode != null && targetEndNode != null) {
final RelationshipInterface existingCandidate = app.relationshipQuery().and(GraphObject.id, uuid).includeDeletedAndHidden().getFirst();
count++;
total++;
if (existingCandidate != null) {
// merge properties?
existingCandidate.setProperties(securityContext, PropertyMap.databaseTypeToJavaType(securityContext, relType, receivedRelationshipData.getProperties()));
return existingCandidate;
} else {
final PropertyMap properties = PropertyMap.databaseTypeToJavaType(securityContext, relType, receivedRelationshipData.getProperties());
return app.create(targetStartNode, targetEndNode, relType, properties);
}
} else {
logger.warn("Could not store relationship {} -> {}", new Object[] { targetStartNode, targetEndNode });
}
}
logger.warn("Could not store relationship {} -> {}", new Object[] { sourceStartNodeId, sourceEndNodeId });
return null;
}
use of org.structr.core.graph.RelationshipInterface 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;
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class PullRelationship method onRequest.
@Override
public void onRequest(CloudConnection serverConnection) throws IOException, FrameworkException {
final Object value = serverConnection.getValue(key + "Rels");
if (value instanceof List) {
final List<RelationshipInterface> relationships = (List<RelationshipInterface>) value;
final RelationshipInterface relationship = relationships.get(nodeIndex);
serverConnection.send(new RelationshipDataContainer(relationship, nodeIndex));
}
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class UiSyncCommand method collectDataRecursively.
private void collectDataRecursively(final App app, final GraphObject root, final Set<NodeInterface> nodes, final Set<RelationshipInterface> rels, final Set<String> files) throws FrameworkException {
if (root.isNode()) {
final NodeInterface node = root.getSyncNode();
if (node instanceof File) {
final String fileUuid = node.getUuid();
files.add(fileUuid);
}
// add node to set, recurse if not already present
if (nodes.add(node)) {
final List<GraphObject> syncData = node.getSyncData();
if (syncData != null) {
for (final GraphObject obj : syncData) {
// syncData can contain null objects!
if (obj != null) {
collectDataRecursively(app, obj, nodes, rels, files);
}
}
} else {
logger.warn("Node {} returned null syncData!", node);
}
}
} else if (root.isRelationship()) {
final RelationshipInterface rel = root.getSyncRelationship();
// add node to set, recurse if not already present
if (rels.add(rel)) {
final List<GraphObject> syncData = rel.getSyncData();
if (syncData != null) {
for (final GraphObject obj : syncData) {
// syncData can contain null objects!
if (obj != null) {
collectDataRecursively(app, obj, nodes, rels, files);
}
}
} else {
logger.warn("Relationship {} returned null syncData!", rel);
}
}
}
}
Aggregations