Search in sources :

Example 1 with LocalProperty

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));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexDuplicator.duplicateVertex(nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex) TinkerPopPropertyConverter(nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter) LocalProperty(nl.knaw.huygens.timbuctoo.model.properties.LocalProperty) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 VertexDuplicator.duplicateVertex (nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex)1 TinkerPopPropertyConverter (nl.knaw.huygens.timbuctoo.database.tinkerpop.conversion.TinkerPopPropertyConverter)1 LocalProperty (nl.knaw.huygens.timbuctoo.model.properties.LocalProperty)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1