Search in sources :

Example 1 with RelationRef

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

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

the class TinkerPopToEntityMapper method getRelations.

private List<RelationRef> getRelations(Vertex entity, GraphTraversalSource traversalSource, Collection collection) {
    final Vre vre = collection.getVre();
    Vre adminVre = mappings.getVre("Admin");
    Map<String, Collection> collectionsOfVre = vre.getCollections();
    Object[] relationTypes = traversalSource.V().has(T.label, LabelP.of("relationtype")).id().toList().toArray();
    GraphTraversal<Vertex, RelationRef> realRelations = collectionsOfVre.values().stream().filter(Collection::isRelationCollection).findAny().map(Collection::getEntityTypeName).map(ownRelationType -> traversalSource.V(entity.id()).union(__.outE().as("edge").label().as("label").select("edge"), __.inE().as("edge").label().as("edgeLabel").V(relationTypes).has("relationtype_regularName", __.where(P.eq("edgeLabel"))).properties("relationtype_inverseName").value().as("label").select("edge")).where(// FIXME move to strategy
    __.has("isLatest", true).not(__.has("deleted", true)).not(__.hasLabel("VERSION_OF")).not(__.has(ownRelationType + "_accepted", false))).otherV().as("vertex").select("edge", "vertex", "label").map(r -> {
        try {
            Map<String, Object> val = r.get();
            Edge edge = (Edge) val.get("edge");
            Vertex target = (Vertex) val.get("vertex");
            String label = (String) val.get("label");
            String targetEntityType = vre.getOwnType(getEntityTypesOrDefault(target));
            Collection targetCollection = vre.getCollectionForTypeName(targetEntityType);
            if (targetEntityType == null) {
                // this means that the edge is of this VRE, but the
                // Vertex it points to is of another VRE
                // In that case we use the admin vre
                targetEntityType = adminVre.getOwnType(getEntityTypesOrDefault(target));
                targetCollection = adminVre.getCollectionForTypeName(targetEntityType);
            }
            String displayName = DisplayNameHelper.getDisplayname(traversalSource, target, targetCollection).orElse("<No displayname found>");
            String targetId = getProp(target, "tim_id", String.class).orElse("");
            String targetRdfUri = getProp(target, RDF_URI_PROP, String.class).orElse("");
            String[] targetAlternativeUris = getProp(target, RDF_SYNONYM_PROP, String[].class).orElse(new String[0]);
            boolean accepted = getProp(edge, "accepted", Boolean.class).orElse(true);
            String relationId = getProp(edge, "tim_id", String.class).orElse("");
            String relationRdfUri = getProp(edge, "rdfUri", String.class).orElse("");
            int relationRev = getProp(edge, "rev", Integer.class).orElse(1);
            RelationRef relationRef = new RelationRef(targetId, targetRdfUri, targetAlternativeUris, targetCollection.getCollectionName(), targetEntityType, accepted, relationId, relationRdfUri, relationRev, label, displayName);
            customRelationProperties.execute(traversalSource, vre, target, relationRef);
            return relationRef;
        } catch (Exception e) {
            LOG.error(databaseInvariant, "Something went wrong while formatting the entity", e);
            return null;
        }
    })).orElse(EmptyGraph.instance().traversal().V().map(x -> null));
    List<RelationRef> relations = stream(realRelations).filter(x -> x != null).collect(toList());
    return relations;
}
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) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 3 with RelationRef

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

the class EntityToJsonMapper method mapRelations.

private JsonNode mapRelations(List<RelationRef> relations, ExtraRelationMappingOptions relationMappingOptions) {
    ObjectNode relationsNode = jsnO();
    relations.stream().map(relationRef -> mapRelationRef(relationRef, relationMappingOptions)).collect(groupingBy(x -> x.get("relationType").asText())).entrySet().forEach(relationsType -> relationsNode.set(relationsType.getKey(), jsnA(relationsType.getValue().stream())));
    return relationsNode;
}
Also used : UserValidator(nl.knaw.huygens.timbuctoo.v5.security.UserValidator) JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) JsonBuilder(nl.knaw.huygens.timbuctoo.util.JsonBuilder) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) UrlGenerator(nl.knaw.huygens.timbuctoo.crud.UrlGenerator) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Strings(com.google.common.base.Strings) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) Logmarkers.databaseInvariant(nl.knaw.huygens.timbuctoo.logging.Logmarkers.databaseInvariant) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) Logger(org.slf4j.Logger) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) UUID(java.util.UUID) ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Change(nl.knaw.huygens.timbuctoo.model.Change) Tuple.tuple(nl.knaw.huygens.timbuctoo.util.Tuple.tuple) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode)

Aggregations

UUID (java.util.UUID)3 RelationRef (nl.knaw.huygens.timbuctoo.core.dto.RelationRef)3 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)3 Change (nl.knaw.huygens.timbuctoo.model.Change)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IOException (java.io.IOException)2 List (java.util.List)2 ReadEntity (nl.knaw.huygens.timbuctoo.core.dto.ReadEntity)2 ReadEntityImpl (nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl)2 TimProperty (nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty)2 Logmarkers.databaseInvariant (nl.knaw.huygens.timbuctoo.logging.Logmarkers.databaseInvariant)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1