Search in sources :

Example 6 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException 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)

Example 7 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.

the class TinkerPopOperations method deleteEntity.

@Override
public int deleteEntity(Collection collection, UUID id, Change modified) throws NotFoundException {
    requireCommit = true;
    GraphTraversal<Vertex, Vertex> entityTraversal = entityFetcher.getEntity(traversal, id, null, collection.getCollectionName());
    if (!entityTraversal.hasNext()) {
        throw new NotFoundException();
    }
    Vertex entity = entityTraversal.next();
    String entityTypesStr = getProp(entity, "types", String.class).orElse("[]");
    boolean wasRemoved = false;
    if (entityTypesStr.contains("\"" + collection.getEntityTypeName() + "\"")) {
        try {
            ArrayNode entityTypes = arrayToEncodedArray.tinkerpopToJson(entityTypesStr);
            if (entityTypes.size() == 1) {
                entity.property("deleted", true);
                wasRemoved = true;
            } else {
                for (int i = entityTypes.size() - 1; i >= 0; i--) {
                    JsonNode val = entityTypes.get(i);
                    if (val != null && val.asText("").equals(collection.getEntityTypeName())) {
                        entityTypes.remove(i);
                        wasRemoved = true;
                    }
                }
                entity.property("types", entityTypes.toString());
            }
        } catch (IOException e) {
            LOG.error(Logmarkers.databaseInvariant, "property 'types' was not parseable: " + entityTypesStr);
        }
    } else {
        throw new NotFoundException();
    }
    int newRev = getProp(entity, "rev", Integer.class).orElse(1) + 1;
    entity.property("rev", newRev);
    entity.edges(Direction.BOTH).forEachRemaining(edge -> {
        // Skip the hasEntity and the VERSION_OF edge, which are not real relations, but system edges
        if (edge.label().equals(HAS_ENTITY_RELATION_NAME) || edge.label().equals(VERSION_OF)) {
            return;
        }
        Optional<Collection> ownEdgeCol = getOwnCollectionOfElement(collection.getVre(), edge);
        if (ownEdgeCol.isPresent()) {
            edge.property(ownEdgeCol.get().getEntityTypeName() + "_accepted", false);
        }
    });
    setModified(entity, modified);
    entity.property("pid").remove();
    Vertex duplicate = duplicateVertex(traversal, entity, indexHandler);
    if (wasRemoved) {
        listener.onRemoveFromCollection(collection, Optional.of(entity), duplicate);
    }
    return newRev;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexDuplicator.duplicateVertex(nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) CreateCollection(nl.knaw.huygens.timbuctoo.core.dto.CreateCollection) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException)

Example 8 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.

the class D3GraphGeneratorService method get.

public D3Graph get(String type, UUID uuid, List<String> relationNames, int depth) throws NotFoundException {
    final String vreId = (String) graphWrapper.getGraph().traversal().V().hasLabel(Collection.DATABASE_LABEL).has(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME, type).in(Vre.HAS_COLLECTION_RELATION_NAME).next().property(Vre.VRE_NAME_PROPERTY_NAME).value();
    final String relationTypeName = (String) graphWrapper.getGraph().traversal().V().hasLabel(Collection.DATABASE_LABEL).has(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME, type).in(Vre.HAS_COLLECTION_RELATION_NAME).out(Vre.HAS_COLLECTION_RELATION_NAME).where(__.has(Collection.IS_RELATION_COLLECTION_PROPERTY_NAME, true)).next().property(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME).value();
    GraphTraversal<Vertex, Vertex> result = graphWrapper.getGraph().traversal().V().has("tim_id", uuid.toString()).filter(x -> ((String) x.get().property("types").value()).contains("\"" + type + "\"")).has("isLatest", true).not(__.has("deleted", true));
    if (!result.hasNext()) {
        throw new NotFoundException();
    }
    Vertex vertex = result.next();
    D3Graph d3Graph = new D3Graph();
    generateD3Graph(relationTypeName, vreId, d3Graph, vertex, relationNames, depth, 1);
    return d3Graph;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException)

Example 9 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.

the class HandleAdder method actualExecution.

private void actualExecution(HandleAdderParameters params) {
    try {
        URI uri = params.getUrlToRedirectTo();
        LOG.info(String.format("Retrieving persistent url for '%s'", uri));
        String persistentId = (manager.persistURL(uri.toString()));
        URI persistentUrl = new URI(manager.getPersistentURL(persistentId));
        transactionEnforcer.execute(timbuctooActions -> {
            try {
                timbuctooActions.addPid(persistentUrl, params.getEntityLookup());
                LOG.info("committed pid");
                return TransactionState.commit();
            } catch (NotFoundException e) {
                LOG.warn("Entity for entityLookup '{}' cannot be found", params.getEntityLookup());
                try {
                    manager.deletePersistentId(persistentId);
                } catch (PersistenceException e1) {
                    LOG.error("Cannot remove handle with id '{}'", persistentId);
                }
                return TransactionState.rollback();
            }
        });
    } catch (PersistenceException | URISyntaxException e) {
        LOG.error(Logmarkers.serviceUnavailable, "Could not create handle", e);
        if (params.getRetries() < 5) {
            LOG.warn(String.format("Re-adding %s%s job to the queue for '%s' '%s'", params.getRetries() + 1, getOrdinalSuffix(params.getRetries() + 1), params.getEntityLookup(), params.getUrlToRedirectTo()));
            this.sender.send(params.nextTry());
        }
    }
}
Also used : PersistenceException(nl.knaw.huygens.persistence.PersistenceException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 10 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.

the class TimbuctooActionsAddPidTest method addPidThrowsANotFoundExceptionWhenTheEntityCannotBeFound.

@Test(expected = NotFoundException.class)
public void addPidThrowsANotFoundExceptionWhenTheEntityCannotBeFound() throws Exception {
    doThrow(new NotFoundException()).when(dataStoreOperations).addPid(id, REV, pidUri);
    instance.addPid(pidUri, ImmutableEntityLookup.builder().collection("someCollection").timId(id).rev(REV).build());
}
Also used : NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) Test(org.junit.Test)

Aggregations

NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)14 IOException (java.io.IOException)6 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 URI (java.net.URI)5 UUID (java.util.UUID)5 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)4 InvalidCollectionException (nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)4 VertexDuplicator.duplicateVertex (nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex)4 Optional (java.util.Optional)3 CreateCollection (nl.knaw.huygens.timbuctoo.core.dto.CreateCollection)3 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)3 UserValidationException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Test (org.junit.Test)3 Mockito.mock (org.mockito.Mockito.mock)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Lists (com.google.common.collect.Lists)2 Instant (java.time.Instant)2 List (java.util.List)2