Search in sources :

Example 1 with ReadEntityImpl

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

the class EntityToJsonMapperTest method mapEntityAddsADisplayNameWhenItIsKnown.

@Test
public void mapEntityAddsADisplayNameWhenItIsKnown() {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    UUID id = UUID.randomUUID();
    readEntity.setId(id);
    String userId = "userId";
    Change change = new Change(Instant.now().toEpochMilli(), userId, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    readEntity.setProperties(Lists.newArrayList());
    readEntity.setRelations(Lists.newArrayList());
    String displayName = "displayName";
    readEntity.setDisplayName(displayName);
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, true, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(resutlJson.has("@displayName"), is(true));
    assertThat(resutlJson.get("@displayName"), is(jsn(displayName)));
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with ReadEntityImpl

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

the class EntityToJsonMapperTest method mapEntityMapsTheTypeAndId.

@Test
public void mapEntityMapsTheTypeAndId() throws Exception {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    UUID id = UUID.randomUUID();
    readEntity.setId(id);
    Change change = new Change(Instant.now().toEpochMilli(), USER_ID, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    readEntity.setProperties(Lists.newArrayList());
    EntityToJsonMapper instance = new EntityToJsonMapper(userValidator, (collection, id1, rev) -> URI.create("www.example.com"));
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, false, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(resutlJson.toString(), sameJSONAs(jsnO("_id", jsn(id.toString()), "@type", jsn(type)).toString()).allowingExtraUnexpectedFields());
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EntityToJsonMapper(nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with ReadEntityImpl

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

the class EntityToJsonMapperTest method mapEntityMapsTheRelations.

@Test
public void mapEntityMapsTheRelations() throws Exception {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    UUID id = UUID.randomUUID();
    readEntity.setId(id);
    String userId = "userId";
    Change change = new Change(Instant.now().toEpochMilli(), userId, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    readEntity.setProperties(Lists.newArrayList());
    String otherEntity = UUID.randomUUID().toString();
    String relType = "relType";
    readEntity.setRelations(Lists.newArrayList(new RelationRef(otherEntity, "rdfUri", new String[] { "origUri" }, "otherColl", "otherType", true, "relId", "rdfUri", 1, relType, "displayName")));
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, true, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(resutlJson.toString(), sameJSONAs(jsnO("@relationCount", jsn(1), "@relations", jsnO(relType, jsnA(jsnO("id", jsn(otherEntity))))).toString()).allowingExtraUnexpectedFields());
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) UUID(java.util.UUID) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) Test(org.junit.Test)

Example 4 with ReadEntityImpl

use of nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl 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 5 with ReadEntityImpl

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

the class EntityToJsonMapperTest method mapEntityDoesNotAddADisplayPropertyWhenTheDisplayNameIsNull.

@Test
public void mapEntityDoesNotAddADisplayPropertyWhenTheDisplayNameIsNull() {
    ReadEntityImpl readEntity = new ReadEntityImpl();
    UUID id = UUID.randomUUID();
    readEntity.setId(id);
    String userId = "userId";
    Change change = new Change(Instant.now().toEpochMilli(), userId, null);
    readEntity.setCreated(change);
    readEntity.setModified(change);
    String type = "otherType";
    readEntity.setTypes(Lists.newArrayList("type", type));
    readEntity.setDeleted(false);
    readEntity.setRev(1);
    readEntity.setPid("pid");
    readEntity.setProperties(Lists.newArrayList());
    readEntity.setRelations(Lists.newArrayList());
    Collection collection = mock(Collection.class);
    when(collection.getEntityTypeName()).thenReturn(type);
    ObjectNode resutlJson = instance.mapEntity(collection, readEntity, true, (readEntity1, resultJson) -> {
    }, (relationRef, resultJson) -> {
    });
    assertThat(resutlJson.has("@displayName"), is(false));
}
Also used : ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Change(nl.knaw.huygens.timbuctoo.model.Change) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ReadEntityImpl (nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl)7 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)7 Change (nl.knaw.huygens.timbuctoo.model.Change)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 Test (org.junit.Test)6 UUID (java.util.UUID)5 TimProperty (nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty)3 EntityToJsonMapper (nl.knaw.huygens.timbuctoo.crud.conversion.EntityToJsonMapper)3 RelationRef (nl.knaw.huygens.timbuctoo.core.dto.RelationRef)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 UnknownPropertyException (nl.knaw.huygens.timbuctoo.core.UnknownPropertyException)1