Search in sources :

Example 1 with DirectionalRelationType

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);
    }
}
Also used : DirectionalRelationType(nl.knaw.huygens.timbuctoo.core.dto.DirectionalRelationType) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexDuplicator.duplicateVertex(nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex) ImmutableEntityRelation(nl.knaw.huygens.timbuctoo.core.dto.ImmutableEntityRelation) EntityRelation(nl.knaw.huygens.timbuctoo.core.dto.EntityRelation) Instant(java.time.Instant) RelationType(nl.knaw.huygens.timbuctoo.core.dto.RelationType) DirectionalRelationType(nl.knaw.huygens.timbuctoo.core.dto.DirectionalRelationType) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) CreateCollection(nl.knaw.huygens.timbuctoo.core.dto.CreateCollection) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) UUID(java.util.UUID)

Aggregations

Instant (java.time.Instant)1 UUID (java.util.UUID)1 NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)1 CreateCollection (nl.knaw.huygens.timbuctoo.core.dto.CreateCollection)1 DirectionalRelationType (nl.knaw.huygens.timbuctoo.core.dto.DirectionalRelationType)1 EntityRelation (nl.knaw.huygens.timbuctoo.core.dto.EntityRelation)1 ImmutableEntityRelation (nl.knaw.huygens.timbuctoo.core.dto.ImmutableEntityRelation)1 RelationType (nl.knaw.huygens.timbuctoo.core.dto.RelationType)1 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)1 VertexDuplicator.duplicateVertex (nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1