use of nl.knaw.huygens.timbuctoo.core.dto.RelationType in project timbuctoo by HuygensING.
the class TinkerPopOperations method acceptRelation.
@Override
public UUID acceptRelation(Collection collection, CreateRelation createRelation) throws RelationNotPossibleException {
UUID typeId = createRelation.getTypeId();
String userId = createRelation.getCreated().getUserId();
Instant instant = Instant.ofEpochMilli(createRelation.getCreated().getTimeStamp());
requireCommit = true;
RelationType descs = getRelationDescription(traversal, typeId).orElseThrow(notPossible("Relation type " + typeId + " does not exist"));
Vertex sourceV = getEntityByFullIteration(traversal, createRelation.getSourceId()).orElseThrow(notPossible("source is not present"));
Vertex targetV = getEntityByFullIteration(traversal, createRelation.getTargetId()).orElseThrow(notPossible("target is not present"));
// check if the relation already exists
final Optional<EntityRelation> existingEdgeOpt = getEntityRelation(sourceV, targetV, typeId, collection);
if (existingEdgeOpt.isPresent()) {
final EntityRelation existingEdge = existingEdgeOpt.get();
if (!existingEdge.isAccepted()) {
// if not already an active relation
try {
replaceRelation(collection, existingEdge.getTimId(), existingEdge.getRevision(), true, userId, instant);
} catch (NotFoundException e) {
LOG.error("Relation with id '{}' not found. This should not happen.", existingEdge.getTimId());
}
}
return existingEdge.getTimId();
} else {
Collection sourceCollection = getOwnCollectionOfElement(collection.getVre(), sourceV).orElseThrow(notPossible("Source vertex is not part of the VRE of " + collection.getCollectionName()));
Collection targetCollection = getOwnCollectionOfElement(collection.getVre(), targetV).orElseThrow(notPossible("Target vertex is not part of the VRE of " + collection.getCollectionName()));
DirectionalRelationType desc = descs.getForDirection(sourceCollection, targetCollection).orElseThrow(notPossible("You can't have a " + descs.getOutName() + " relation from " + sourceCollection.getEntityTypeName() + " to " + targetCollection.getEntityTypeName() + " or vice versa"));
return createRelation(sourceV, targetV, desc, userId, collection, true, instant);
}
}
use of nl.knaw.huygens.timbuctoo.core.dto.RelationType in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method addPidAddsAPidToEachVertexInTheCollectionWithTheIdAndRev.
@Test
public void addPidAddsAPidToEachVertexInTheCollectionWithTheIdAndRev() throws Exception {
Vres vres = createConfiguration();
UUID id = UUID.randomUUID();
int rev = 1;
TinkerPopGraphManager graphManager = newGraph().withVertex(v -> v.withTimId(id.toString()).withType("relationtype").withProperty("relationtype_regularName", "regularName").withProperty("rev", rev).withProperty("isLatest", true).withIncomingRelation("VERSION_OF", "version")).withVertex("version", v -> v.withTimId(id.toString()).withType("relationtype").withProperty("relationtype_regularName", "regularName").withProperty("rev", rev).withProperty("isLatest", false)).wrap();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
URI pidUri = new URI("http://example.com/pid");
instance.addPid(id, rev, pidUri);
assertThat(graphManager.getGraph().traversal().V().has("pid", pidUri.toString()).count().next(), is(2L));
}
use of nl.knaw.huygens.timbuctoo.core.dto.RelationType in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method getRelationTypesReturnsAllTheRelationTypesInTheDatabase.
@Test
public void getRelationTypesReturnsAllTheRelationTypesInTheDatabase() {
UUID id1 = UUID.randomUUID();
UUID id2 = UUID.randomUUID();
TinkerPopGraphManager graphManager = newGraph().withVertex(v -> v.withTimId(id1).withType("relationtype").withProperty("rdfUri", "http://example.com/entity1").withProperty("rev", 2).isLatest(true).withLabel("relationtype")).withVertex(v -> v.withTimId(id2).withType("relationstype").withVre("test").withProperty("rdfUri", "http://example.com/entity1").isLatest(false).withProperty("rev", 1).withLabel("relationtype")).withVertex(v -> v.withTimId(UUID.randomUUID()).withType("othertype").withVre("test").withProperty("rdfUri", "http://example.com/entity1").isLatest(false).withProperty("rev", 1).withLabel("othertype")).wrap();
Vres vres = createConfiguration();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
List<RelationType> relationTypes = instance.getRelationTypes();
assertThat(relationTypes, hasSize(2));
}
use of nl.knaw.huygens.timbuctoo.core.dto.RelationType in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method getEntityOnlyReturnsTheLatestRelations.
@Test
public void getEntityOnlyReturnsTheLatestRelations() throws Exception {
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testthings").get();
UUID id = UUID.randomUUID();
UUID relatedId = UUID.randomUUID();
TinkerPopGraphManager graphManager = newGraph().withVertex("source", v -> v.withOutgoingRelation("isRelatedTo", "relatedThing", rel -> rel.withIsLatest(true).withRev(2)).withOutgoingRelation("isRelatedTo", "relatedThing", rel -> rel.withIsLatest(false).withRev(1)).withVre("test").withVre("").withType("thing").isLatest(true).withTimId(id.toString())).withVertex("relatedThing", v -> v.withVre("test").withVre("").withType("thing").withTimId(relatedId.toString())).wrap();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
ReadEntity entity = instance.getEntity(id, null, collection, (readEntity, vertex) -> {
}, (graphTraversalSource, vre, vertex, relationRef) -> {
});
assertThat(entity.getRelations(), hasSize(1));
assertThat(entity.getRelations(), contains(allOf(hasProperty("entityId", equalTo(relatedId.toString())), hasProperty("relationType", equalTo("isRelatedTo")), hasProperty("relationRev", equalTo(2)))));
}
use of nl.knaw.huygens.timbuctoo.core.dto.RelationType in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method acceptRelationCallTheChangeListener.
@Test
public void acceptRelationCallTheChangeListener() throws Exception {
ChangeListener changeListener = mock(ChangeListener.class);
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testrelations").get();
UUID typeId = UUID.randomUUID();
UUID sourceId = UUID.randomUUID();
UUID targetId = UUID.randomUUID();
TinkerPopGraphManager graphManager = newGraph().withVertex(v -> v.withTimId(typeId.toString()).withType("relationtype").withProperty("relationtype_regularName", "regularName").withProperty("rev", 1).withProperty("isLatest", true)).withVertex(v -> v.withTimId(sourceId.toString()).withProperty("rev", 1).withVre("test").withType("thing").withProperty("isLatest", true)).withVertex(v -> v.withTimId(targetId.toString()).withProperty("rev", 1).withVre("test").withType("thing").withProperty("isLatest", true)).wrap();
TinkerPopOperations instance = forGraphMappingsAndChangeListener(graphManager, vres, changeListener);
CreateRelation createRelation = new CreateRelation(sourceId, typeId, targetId);
createRelation.setCreated(new Change(Instant.now().toEpochMilli(), "userId", null));
instance.acceptRelation(collection, createRelation);
verify(changeListener).onCreateEdge(argThat(is(sameInstance(collection))), argThat(is(likeEdge().withSourceWithId(sourceId).withTargetWithId(targetId).withTypeId(typeId))));
}
Aggregations