use of nl.knaw.huygens.timbuctoo.logging.Logmarkers.databaseInvariant 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