use of nl.knaw.huygens.timbuctoo.core.dto.DirectionalRelationType 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);
}
}
Aggregations