Search in sources :

Example 6 with TimProperty

use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.

the class JsonCrudService method createEntity.

private UUID createEntity(Collection collection, ObjectNode input, User user) throws IOException, PermissionFetchingException {
    List<TimProperty<?>> timProperties = jsonToEntityMapper.getDataProperties(collection, input);
    Optional<Collection> baseCollection = mappings.getCollectionForType(collection.getAbstractType());
    UUID id = timDbAccess.createEntity(collection, baseCollection, timProperties, user);
    return id;
}
Also used : TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) UUID(java.util.UUID)

Example 7 with TimProperty

use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.

the class JsonToEntityMapper method getDataProperties.

/**
 * Retrieve all the properties that contain client data.
 */
public List<TimProperty<?>> getDataProperties(Collection collection, ObjectNode input) throws IOException {
    JsonPropertyConverter converter = new JsonPropertyConverter(collection);
    Set<String> fieldNames = getDataFields(input);
    List<TimProperty<?>> properties = Lists.newArrayList();
    for (String fieldName : fieldNames) {
        try {
            TimProperty<?> property = converter.from(fieldName, input.get(fieldName));
            // This is the only place to filter the empty strings.
            if (!(property.getValue() instanceof String) || !StringUtils.isBlank((String) property.getValue())) {
                properties.add(property);
            }
        } catch (UnknownPropertyException e) {
            LOG.error("Property with name '{}' is unknown for collection '{}'.", fieldName, collection.getCollectionName());
            throw new IOException(String.format("Items of %s have no property %s", collection.getCollectionName(), fieldName));
        } catch (IOException e) {
            LOG.error("Property '{}' with value '{}' could not be converted", fieldName, input.get(fieldName));
            throw new IOException(String.format("Property '%s' could not be converted. %s", fieldName, e.getMessage()), e);
        }
    }
    return properties;
}
Also used : TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) IOException(java.io.IOException) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException)

Example 8 with TimProperty

use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.

the class TinkerPopToEntityMapper method mapEntity.

public ReadEntity mapEntity(GraphTraversal<Vertex, Vertex> entityT, boolean withRelations) {
    final List<TimProperty<?>> properties = Lists.newArrayList();
    TinkerPopPropertyConverter dbPropertyConverter = new TinkerPopPropertyConverter(collection);
    String entityTypeName = collection.getEntityTypeName();
    GraphTraversal[] propertyGetters = collection.getReadableProperties().entrySet().stream().map(prop -> prop.getValue().traversalRaw().sideEffect(x -> x.get().onSuccess(value -> {
        try {
            properties.add(dbPropertyConverter.from(prop.getKey(), value));
        } catch (UnknownPropertyException e) {
            LOG.error("Unknown property", e);
        } catch (IOException e) {
            LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
        }
    }).onFailure(e -> {
        if (e.getCause() instanceof IOException) {
            LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
        } else {
            LOG.error("Something went wrong while reading the property '" + prop.getKey() + "'.", e.getCause());
        }
    }))).toArray(GraphTraversal[]::new);
    entityT.asAdmin().clone().union(propertyGetters).forEachRemaining(x -> {
    // Force side effects to happen
    });
    ReadEntityImpl entity = new ReadEntityImpl();
    entity.setProperties(properties);
    Vertex entityVertex = entityT.asAdmin().clone().next();
    // TODO make use conversion for the types
    entity.setRev(getProp(entityVertex, "rev", Integer.class).orElse(-1));
    entity.setDeleted(getProp(entityVertex, "deleted", Boolean.class).orElse(false));
    entity.setPid(getProp(entityVertex, "pid", String.class).orElse(null));
    URI rdfUri = getProp(entityVertex, RDF_URI_PROP, String.class).map(x -> {
        try {
            return new URI(x);
        } catch (URISyntaxException e) {
            return null;
        }
    }).orElse(null);
    entity.setRdfUri(rdfUri);
    Property<String[]> rdfAlternativesProp = entityVertex.property(RDF_SYNONYM_PROP);
    if (rdfAlternativesProp.isPresent()) {
        try {
            entity.setRdfAlternatives(Lists.newArrayList(rdfAlternativesProp.value()));
        } catch (Exception e) {
            LOG.error(databaseInvariant, "Error while reading rdfAlternatives", e);
        }
    }
    Optional<String> typesOptional = getProp(entityVertex, "types", String.class);
    if (typesOptional.isPresent()) {
        try {
            List<String> types = new ObjectMapper().readValue(typesOptional.get(), new TypeReference<List<String>>() {
            });
            entity.setTypes(types);
        } catch (Exception e) {
            LOG.error(databaseInvariant, "Error while generating variation refs", e);
            entity.setTypes(Lists.newArrayList(entityTypeName));
        }
    } else {
        entity.setTypes(Lists.newArrayList(entityTypeName));
    }
    Optional<String> modifiedStringOptional = getProp(entityVertex, "modified", String.class);
    if (modifiedStringOptional.isPresent()) {
        try {
            entity.setModified(new ObjectMapper().readValue(modifiedStringOptional.get(), Change.class));
        } catch (IOException e) {
            LOG.error(databaseInvariant, "Change cannot be converted", e);
            entity.setModified(new Change());
        }
    } else {
        entity.setModified(new Change());
    }
    Optional<String> createdStringOptional = getProp(entityVertex, "created", String.class);
    if (createdStringOptional.isPresent()) {
        try {
            entity.setCreated(new ObjectMapper().readValue(createdStringOptional.get(), Change.class));
        } catch (IOException e) {
            LOG.error(databaseInvariant, "Change cannot be converted", e);
            entity.setCreated(new Change());
        }
    } else {
        entity.setCreated(new Change());
    }
    entity.setDisplayName(DisplayNameHelper.getDisplayname(traversalSource, entityVertex, collection).orElse(""));
    entity.setId(getIdOrDefault(entityVertex));
    if (withRelations) {
        entity.setRelations(getRelations(entityVertex, traversalSource, collection));
    }
    customEntityProperties.execute(entity, entityVertex);
    return entity;
}
Also used : StreamIterator.stream(nl.knaw.huygens.timbuctoo.util.StreamIterator.stream) LabelP(org.apache.tinkerpop.gremlin.neo4j.process.traversal.LabelP) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) RDF_SYNONYM_PROP(nl.knaw.huygens.timbuctoo.rdf.Database.RDF_SYNONYM_PROP) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) CustomEntityProperties(nl.knaw.huygens.timbuctoo.database.tinkerpop.CustomEntityProperties) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Lists(com.google.common.collect.Lists) Map(java.util.Map) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) P(org.apache.tinkerpop.gremlin.process.traversal.P) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Logmarkers.databaseInvariant(nl.knaw.huygens.timbuctoo.logging.Logmarkers.databaseInvariant) Property(org.apache.tinkerpop.gremlin.structure.Property) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) Logger(org.slf4j.Logger) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CustomRelationProperties(nl.knaw.huygens.timbuctoo.database.tinkerpop.CustomRelationProperties) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) IOException(java.io.IOException) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) UUID(java.util.UUID) T(org.apache.tinkerpop.gremlin.structure.T) GraphReadUtils.getEntityTypesOrDefault(nl.knaw.huygens.timbuctoo.model.GraphReadUtils.getEntityTypesOrDefault) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Vres(nl.knaw.huygens.timbuctoo.model.vre.Vres) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) EmptyGraph(org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph) RDF_URI_PROP(nl.knaw.huygens.timbuctoo.rdf.Database.RDF_URI_PROP) DisplayNameHelper(nl.knaw.huygens.timbuctoo.core.dto.DisplayNameHelper) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) Optional(java.util.Optional) GraphReadUtils.getProp(nl.knaw.huygens.timbuctoo.model.GraphReadUtils.getProp) Change(nl.knaw.huygens.timbuctoo.model.Change) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Change(nl.knaw.huygens.timbuctoo.model.Change) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 9 with TimProperty

use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.

the class TinkerPopOperationsTest method createEntitySetsCreatedAndModified.

@Test
public void createEntitySetsCreatedAndModified() throws Exception {
    TinkerPopGraphManager graphManager = newGraph().wrap();
    Vres vres = createConfiguration();
    Collection collection = vres.getCollection("testthings").get();
    TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
    List<TimProperty<?>> properties = Lists.newArrayList();
    String userId = "userId";
    long timeStamp = Instant.now().toEpochMilli();
    CreateEntity createEntity = withProperties(properties, userId, timeStamp);
    instance.createEntity(collection, Optional.empty(), createEntity);
    assertThat(graphManager.getGraph().traversal().V().has("tim_id", createEntity.getId().toString()).next().value("created"), sameJSONAs(String.format("{\"timeStamp\": %s,\"userId\": \"%s\"}", timeStamp, userId)));
    assertThat(graphManager.getGraph().traversal().V().has("tim_id", createEntity.getId().toString()).next().value("modified"), sameJSONAs(String.format("{\"timeStamp\": %s,\"userId\": \"%s\"}", timeStamp, userId)));
}
Also used : CreateEntity(nl.knaw.huygens.timbuctoo.core.dto.CreateEntity) Vres(nl.knaw.huygens.timbuctoo.model.vre.Vres) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) CreateCollection(nl.knaw.huygens.timbuctoo.core.dto.CreateCollection) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 10 with TimProperty

use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.

the class TinkerPopOperationsTest method createEntitySetsTypesWithTheCollectionAndTheBaseCollection.

@Test
public void createEntitySetsTypesWithTheCollectionAndTheBaseCollection() throws Exception {
    TinkerPopGraphManager graphManager = newGraph().wrap();
    Vres vres = createConfiguration();
    Collection collection = vres.getCollection("testthings").get();
    TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
    List<TimProperty<?>> properties = Lists.newArrayList();
    CreateEntity createEntity = withProperties(properties);
    instance.createEntity(collection, Optional.empty(), createEntity);
    assertThat(graphManager.getGraph().traversal().V().has("tim_id", createEntity.getId().toString()).next().value("types"), allOf(containsString("testthing"), containsString("thing")));
}
Also used : CreateEntity(nl.knaw.huygens.timbuctoo.core.dto.CreateEntity) Vres(nl.knaw.huygens.timbuctoo.model.vre.Vres) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) CreateCollection(nl.knaw.huygens.timbuctoo.core.dto.CreateCollection) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Test(org.junit.Test)

Aggregations

TimProperty (nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty)21 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)20 Test (org.junit.Test)17 Vres (nl.knaw.huygens.timbuctoo.model.vre.Vres)13 CreateCollection (nl.knaw.huygens.timbuctoo.core.dto.CreateCollection)12 CreateEntity (nl.knaw.huygens.timbuctoo.core.dto.CreateEntity)12 TinkerPopGraphManager (nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager)12 IOException (java.io.IOException)10 UUID (java.util.UUID)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 List (java.util.List)9 UpdateEntity (nl.knaw.huygens.timbuctoo.core.dto.UpdateEntity)8 Change (nl.knaw.huygens.timbuctoo.model.Change)8 JsonBuilder.jsn (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn)8 PropertyTypes.localProperty (nl.knaw.huygens.timbuctoo.model.properties.PropertyTypes.localProperty)7 VresBuilder (nl.knaw.huygens.timbuctoo.model.vre.vres.VresBuilder)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Matchers.allOf (org.hamcrest.Matchers.allOf)7 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)7 Matchers.equalTo (org.hamcrest.Matchers.equalTo)7