use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.
the class TinkerPopOperations method replaceEntity.
@Override
public int replaceEntity(Collection collection, UpdateEntity updateEntity) throws NotFoundException, AlreadyUpdatedException, IOException {
requireCommit = true;
GraphTraversal<Vertex, Vertex> entityTraversal = entityFetcher.getEntity(this.traversal, updateEntity.getId(), null, collection.getCollectionName());
if (!entityTraversal.hasNext()) {
throw new NotFoundException();
}
Vertex entityVertex = entityTraversal.next();
int curRev = getProp(entityVertex, "rev", Integer.class).orElse(1);
if (curRev != updateEntity.getRev()) {
throw new AlreadyUpdatedException();
}
int newRev = updateEntity.getRev() + 1;
entityVertex.property("rev", newRev);
// update properties
TinkerPopPropertyConverter tinkerPopPropertyConverter = new TinkerPopPropertyConverter(collection);
for (TimProperty<?> property : updateEntity.getProperties()) {
try {
Tuple<String, Object> nameValue = property.convert(tinkerPopPropertyConverter);
collection.getWriteableProperties().get(nameValue.getLeft()).setValue(entityVertex, nameValue.getRight());
} catch (IOException e) {
throw new IOException(property.getName() + " could not be saved. " + e.getMessage(), e);
}
}
// Set removed values to null.
Set<String> propertyNames = updateEntity.getProperties().stream().map(prop -> prop.getName()).collect(Collectors.toSet());
for (String name : Sets.difference(collection.getWriteableProperties().keySet(), propertyNames)) {
collection.getWriteableProperties().get(name).setJson(entityVertex, null);
}
String entityTypesStr = getProp(entityVertex, "types", String.class).orElse("[]");
boolean wasAddedToCollection = false;
if (!entityTypesStr.contains("\"" + collection.getEntityTypeName() + "\"")) {
try {
ArrayNode entityTypes = arrayToEncodedArray.tinkerpopToJson(entityTypesStr);
entityTypes.add(collection.getEntityTypeName());
entityVertex.property("types", entityTypes.toString());
wasAddedToCollection = true;
} catch (IOException e) {
// FIXME potential bug?
LOG.error(Logmarkers.databaseInvariant, "property 'types' was not parseable: " + entityTypesStr);
}
}
setModified(entityVertex, updateEntity.getModified());
entityVertex.property("pid").remove();
Vertex duplicate = duplicateVertex(traversal, entityVertex, indexHandler);
listener.onPropertyUpdate(collection, Optional.of(entityVertex), duplicate);
if (wasAddedToCollection) {
listener.onAddToCollection(collection, Optional.of(entityVertex), duplicate);
}
return newRev;
}
use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.
the class TinkerPopOperations method getEntity.
@Override
public ReadEntity getEntity(UUID id, Integer rev, Collection collection, CustomEntityProperties customEntityProperties, CustomRelationProperties customRelationProperties) throws NotFoundException {
GraphTraversal<Vertex, Vertex> fetchedEntity = entityFetcher.getEntity(traversal, id, rev, collection.getCollectionName());
if (!fetchedEntity.hasNext()) {
throw new NotFoundException();
}
Vertex entityVertex = entityFetcher.getEntity(traversal, id, rev, collection.getCollectionName()).next();
GraphTraversal<Vertex, Vertex> entityT = traversal.V(entityVertex.id());
if (!entityT.asAdmin().clone().hasNext()) {
throw new NotFoundException();
}
String entityTypesStr = getProp(entityT.asAdmin().clone().next(), "types", String.class).orElse("[]");
if (!entityTypesStr.contains("\"" + collection.getEntityTypeName() + "\"")) {
throw new NotFoundException();
}
return new TinkerPopToEntityMapper(collection, traversal, mappings, customEntityProperties, customRelationProperties).mapEntity(entityT, true);
}
use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.
the class TinkerPopOperations method replaceRelation.
private void replaceRelation(Collection collection, UUID id, int rev, boolean accepted, String userId, Instant instant) throws NotFoundException {
requireCommit = true;
// FIXME: string concatenating methods like this should be delegated to a configuration class
final String acceptedPropName = collection.getEntityTypeName() + "_accepted";
// FIXME: throw a AlreadyUpdatedException when the rev of the client is not the latest
Optional<Edge> origEdgeOpt = indexHandler.findEdgeById(id);
if (!origEdgeOpt.isPresent()) {
throw new NotFoundException();
}
Edge origEdge = origEdgeOpt.get();
if (!origEdge.property("isLatest").isPresent() || !(origEdge.value("isLatest") instanceof Boolean) || !origEdge.<Boolean>value("isLatest")) {
LOG.error("edge {} is not the latest edge, or it has no valid isLatest property.", origEdge.id());
}
// FIXME: throw a distinct Exception when the client tries to save a relation with wrong source, target or type.
Edge edge = duplicateEdge(origEdge);
edge.property(acceptedPropName, accepted);
edge.property("rev", getProp(origEdge, "rev", Integer.class).orElse(1) + 1);
setModified(edge, userId, instant);
listener.onEdgeUpdate(collection, origEdge, edge);
}
use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.
the class SingleEntity method delete.
@DELETE
public Response delete(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, @PathParam("id") UUIDParam id) {
Optional<User> user;
try {
user = userValidator.getUserFromAccessToken(authHeader);
} catch (UserValidationException e) {
user = Optional.empty();
}
Optional<User> newUser = user;
if (!newUser.isPresent()) {
return Response.status(Response.Status.UNAUTHORIZED).build();
} else {
return transactionEnforcer.executeAndReturn(timbuctooActions -> {
JsonCrudService jsonCrudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
try {
jsonCrudService.delete(collectionName, id.get(), newUser.get());
return commitAndReturn(Response.noContent().build());
} catch (InvalidCollectionException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn(e.getMessage()))).build());
} catch (NotFoundException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("not found"))).build());
} catch (PermissionFetchingException e) {
return rollbackAndReturn(Response.status(Response.Status.FORBIDDEN).entity(jsnO("message", jsn(e.getMessage()))).build());
} catch (IOException e) {
return rollbackAndReturn(Response.status(Response.Status.BAD_REQUEST).entity(jsnO("message", jsn(e.getMessage()))).build());
}
});
}
}
Aggregations