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);
}
}
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;
}
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;
}
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());
}
}
}
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());
}
Aggregations