use of nl.knaw.huygens.timbuctoo.rdf.LinkTriple in project timbuctoo by HuygensING.
the class TriplePropertyConverter method to.
@Override
public Tuple<String, List<Triple>> to(HyperLinksProperty property) throws IOException {
String blankNode = "_:" + UUID.randomUUID();
String propertyName = property.getName();
String pred = createMetadata(propertyName);
List<Triple> triples = Lists.newArrayList(new LinkTriple(subjectUri, pred, blankNode));
JsonNode jsonNode = objectMapper.readTree(property.getValue());
for (Iterator<JsonNode> elements = jsonNode.elements(); elements.hasNext(); ) {
JsonNode jn = elements.next();
jn.fieldNames().forEachRemaining(field -> {
String predicate = pred + field;
String fieldValue = jn.get(field).asText();
String object = StringUtils.isBlank(fieldValue) ? "\"\"" : fieldValue;
triples.add(new LinkTriple(blankNode, predicate, object));
});
}
return tuple(propertyName, triples);
}
use of nl.knaw.huygens.timbuctoo.rdf.LinkTriple in project timbuctoo by HuygensING.
the class SingleEntityNTriple method get.
@GET
public Response get(@PathParam("collection") String collectionName, @PathParam("id") UUIDParam id, @QueryParam("rev") Integer rev) {
return transactionEnforcer.executeAndReturn(timbuctooActions -> {
try {
Collection collection = timbuctooActions.getCollectionMetadata(collectionName);
ReadEntity entity = timbuctooActions.getEntity(collection, id.get(), rev);
URI rdfUri = entity.getRdfUri();
String rdfString = rdfUri == null ? uriHelper.fromResourceUri(makeUrl(collectionName, id.get())).toString() : rdfUri.toString();
StringBuilder sb = new StringBuilder();
addRdfProp(rdfString, sb, "id", entity.getId() + "", "https://www.ietf.org/rfc/rfc4122.txt");
entity.getRdfAlternatives().forEach(alt -> sb.append(asNtriples(new LinkTriple(rdfString, SAME_AS_PRED, alt))));
TriplePropertyConverter converter = new TriplePropertyConverter(collection, rdfString);
for (TimProperty<?> timProperty : entity.getProperties()) {
try {
timProperty.convert(converter).getRight().forEach(triple -> sb.append(asNtriples(triple)));
} catch (IOException e) {
LOG.error("Could not convert property with name '{}' and value '{}'", timProperty.getName(), timProperty.getValue());
}
}
entity.getRelations().forEach(rel -> sb.append(asNtriples(new LinkTriple(rdfString, getRelationRdfUri(rel), getEntityRdfUri(rel)))));
return commitAndReturn(Response.ok(sb.toString()).build());
} catch (InvalidCollectionException e) {
return commitAndReturn(Response.status(BAD_REQUEST).entity(e.getMessage()).build());
} catch (NotFoundException e) {
return commitAndReturn(Response.status(NOT_FOUND).build());
}
});
}
Aggregations