use of nl.knaw.huygens.timbuctoo.core.AlreadyUpdatedException in project timbuctoo by HuygensING.
the class SingleEntity method put.
@PUT
public Response put(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, @PathParam("id") UUIDParam id, ObjectNode body) {
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 {
UpdateMessage updateMessage = transactionEnforcer.executeAndReturn(timbuctooActions -> {
JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
try {
crudService.replace(collectionName, id.get(), body, newUser.get());
return commitAndReturn(UpdateMessage.success());
} catch (InvalidCollectionException e) {
return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.NOT_FOUND));
} catch (NotFoundException e) {
return rollbackAndReturn(UpdateMessage.failure("not found", Response.Status.NOT_FOUND));
} catch (IOException e) {
return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.BAD_REQUEST));
} catch (AlreadyUpdatedException e) {
return rollbackAndReturn(UpdateMessage.failure("Entry was already updated", Response.Status.EXPECTATION_FAILED));
} catch (PermissionFetchingException e) {
return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.FORBIDDEN));
}
});
// committed in the database
if (updateMessage.isSuccess()) {
return transactionEnforcer.executeAndReturn(timbuctooActions -> {
JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
try {
JsonNode jsonNode = crudService.get(collectionName, id.get());
return commitAndReturn(Response.ok(jsonNode).build());
} catch (InvalidCollectionException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("Collection '" + collectionName + "' was available a moment ago, but not anymore: " + e.getMessage()))).build());
} catch (NotFoundException e) {
return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("not found"))).build());
}
});
} else {
return Response.status(updateMessage.getResponseStatus()).entity(jsnO("message", jsn(updateMessage.getException().get()))).build();
}
}
}
use of nl.knaw.huygens.timbuctoo.core.AlreadyUpdatedException 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;
}
Aggregations