use of nl.knaw.huygens.timbuctoo.model.properties.LocalProperty in project timbuctoo by HuygensING.
the class TinkerPopOperations method createEntity.
@Override
public void createEntity(Collection col, Optional<Collection> baseCollection, CreateEntity input) throws IOException {
requireCommit = true;
Map<String, LocalProperty> mapping = col.getWriteableProperties();
TinkerPopPropertyConverter colConverter = new TinkerPopPropertyConverter(col);
Map<String, LocalProperty> baseMapping = baseCollection.isPresent() ? baseCollection.get().getWriteableProperties() : Maps.newHashMap();
TinkerPopPropertyConverter baseColConverter = // converter not needed without mapping
baseCollection.isPresent() ? new TinkerPopPropertyConverter(baseCollection.get()) : null;
GraphTraversal<Vertex, Vertex> traversalWithVertex = traversal.addV();
Vertex vertex = traversalWithVertex.next();
for (TimProperty<?> property : input.getProperties()) {
String fieldName = property.getName();
if (mapping.containsKey(fieldName)) {
try {
String dbName = mapping.get(fieldName).getDatabasePropertyName();
Tuple<String, Object> convertedProp = property.convert(colConverter);
vertex.property(dbName, convertedProp.getRight());
} catch (IOException e) {
throw new IOException(fieldName + " could not be saved. " + e.getMessage(), e);
}
} else {
throw new IOException(String.format("Items of %s have no property %s", col.getCollectionName(), fieldName));
}
if (baseMapping.containsKey(fieldName)) {
try {
property.convert(baseColConverter);
Tuple<String, Object> convertedProp = property.convert(baseColConverter);
baseMapping.get(fieldName).setValue(vertex, convertedProp.getRight());
} catch (IOException e) {
LOG.error(configurationFailure, "Field could not be parsed by Admin VRE converter {}_{}", baseCollection.get().getCollectionName(), fieldName);
}
}
}
setAdministrativeProperties(col, vertex, input);
Vertex duplicate = duplicateVertex(traversal, vertex, indexHandler);
listener.onCreate(col, duplicate);
// not passing oldVertex because old has never been passed to addToCollection so it doesn't need to be removed
listener.onAddToCollection(col, Optional.empty(), duplicate);
baseCollection.ifPresent(baseCol -> listener.onAddToCollection(baseCol, Optional.empty(), duplicate));
}
Aggregations