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());
}
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;
}
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;
}
Aggregations