Search in sources :

Example 1 with LinkTriple

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);
}
Also used : Triple(nl.knaw.huygens.timbuctoo.rdf.Triple) LiteralTriple(nl.knaw.huygens.timbuctoo.rdf.LiteralTriple) LinkTriple(nl.knaw.huygens.timbuctoo.rdf.LinkTriple) LinkTriple(nl.knaw.huygens.timbuctoo.rdf.LinkTriple) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with LinkTriple

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());
        }
    });
}
Also used : ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) LinkTriple(nl.knaw.huygens.timbuctoo.rdf.LinkTriple) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) IOException(java.io.IOException) TriplePropertyConverter(nl.knaw.huygens.timbuctoo.rdf.conversion.TriplePropertyConverter) URI(java.net.URI) GET(javax.ws.rs.GET)

Aggregations

LinkTriple (nl.knaw.huygens.timbuctoo.rdf.LinkTriple)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 URI (java.net.URI)1 GET (javax.ws.rs.GET)1 NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)1 ReadEntity (nl.knaw.huygens.timbuctoo.core.dto.ReadEntity)1 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)1 InvalidCollectionException (nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)1 LiteralTriple (nl.knaw.huygens.timbuctoo.rdf.LiteralTriple)1 Triple (nl.knaw.huygens.timbuctoo.rdf.Triple)1 TriplePropertyConverter (nl.knaw.huygens.timbuctoo.rdf.conversion.TriplePropertyConverter)1