Search in sources :

Example 1 with Graph

use of org.apache.commons.rdf.api.Graph in project trellis by trellis-ldp.

the class DBResourceService method storeResource.

private void storeResource(final Metadata metadata, final Dataset dataset, final Instant time, final OperationType opType) {
    try {
        jdbi.useTransaction(handle -> {
            final int resourceId = updateResource(handle, metadata, dataset, time, opType == OperationType.DELETE);
            updateDescription(handle, resourceId, dataset, batchSize);
            updateAcl(handle, resourceId, dataset, batchSize);
            updateExtra(handle, resourceId, metadata.getIdentifier(), dataset);
            extensions.forEach((ext, graph) -> dataset.getGraph(graph).filter(g -> !"acl".equals(ext)).ifPresent(g -> updateExtension(handle, resourceId, ext, g)));
            if (opType == OperationType.DELETE) {
                // Verify that the container really is empty
                final String query = "SELECT EXISTS(SELECT 1 FROM resource WHERE is_part_of = ?)";
                if (Boolean.TRUE.equals(handle.select(query, metadata.getIdentifier().getIRIString()).map((rs, ctx) -> rs.getBoolean(1)).one())) {
                    throw new StorageConflictException("Cannot delete non-empty containers");
                }
            }
        });
    } catch (final TrellisRuntimeException ex) {
        throw ex;
    } catch (final Exception ex) {
        throw new TrellisRuntimeException("Could not update data for " + metadata.getIdentifier(), ex);
    }
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) Optional.of(java.util.Optional.of) NTRIPLES(org.apache.jena.riot.Lang.NTRIPLES) Resource(org.trellisldp.api.Resource) PreferAudit(org.trellisldp.vocabulary.Trellis.PreferAudit) Collectors.toMap(java.util.stream.Collectors.toMap) Arrays.asList(java.util.Arrays.asList) JenaCommonsRDF.toJena(org.apache.jena.commonsrdf.JenaCommonsRDF.toJena) Map(java.util.Map) Handle(org.jdbi.v3.core.Handle) Graph(org.apache.commons.rdf.api.Graph) Set(java.util.Set) Instant(java.time.Instant) UncheckedIOException(java.io.UncheckedIOException) CompletionStage(java.util.concurrent.CompletionStage) StorageConflictException(org.trellisldp.api.StorageConflictException) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Arrays.stream(java.util.Arrays.stream) Instant.now(java.time.Instant.now) DBUtils.getObjectLang(org.trellisldp.jdbc.DBUtils.getObjectLang) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) Supplier(java.util.function.Supplier) RDF(org.apache.commons.rdf.api.RDF) Triple(org.apache.commons.rdf.api.Triple) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) ResourceService(org.trellisldp.api.ResourceService) DBUtils.getObjectDatatype(org.trellisldp.jdbc.DBUtils.getObjectDatatype) DataSource(javax.sql.DataSource) Collections.singletonMap(java.util.Collections.singletonMap) LDP(org.trellisldp.vocabulary.LDP) Dataset(org.apache.commons.rdf.api.Dataset) DBUtils.getObjectValue(org.trellisldp.jdbc.DBUtils.getObjectValue) Jdbi(org.jdbi.v3.core.Jdbi) BinaryMetadata(org.trellisldp.api.BinaryMetadata) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Logger(org.slf4j.Logger) StringWriter(java.io.StringWriter) IOException(java.io.IOException) TrellisUtils(org.trellisldp.api.TrellisUtils) IRI(org.apache.commons.rdf.api.IRI) OA(org.trellisldp.vocabulary.OA) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) RDFDataMgr(org.apache.jena.riot.RDFDataMgr) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Update(org.jdbi.v3.core.statement.Update) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) StorageConflictException(org.trellisldp.api.StorageConflictException) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) UncheckedIOException(java.io.UncheckedIOException) StorageConflictException(org.trellisldp.api.StorageConflictException) IOException(java.io.IOException)

Example 2 with Graph

use of org.apache.commons.rdf.api.Graph in project trellis by trellis-ldp.

the class JenaIOServiceTest method testUpdate.

@Test
void testUpdate() {
    final Graph graph = rdf.createGraph();
    getTriples().forEach(graph::add);
    assertEquals(3L, graph.size(), "Incorrect graph size!");
    service.update(graph, "DELETE WHERE { ?s <http://purl.org/dc/terms/title> ?o }", SPARQL_UPDATE, "test:info");
    assertEquals(2L, graph.size(), "Incorrect graph size, post update!");
    service.update(graph, "INSERT { " + "<> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}", SPARQL_UPDATE, "trellis:data/resource");
    assertEquals(3L, graph.size(), "Incorrect graph size, after adding triple!");
    service.update(graph, "DELETE WHERE { ?s ?p ?o };" + "INSERT { <> <http://purl.org/dc/terms/title> \"Other title\" } WHERE {}", SPARQL_UPDATE, "trellis:data/");
    assertEquals(1L, graph.size(), "Incorrect graph size after removing triples!");
    assertEquals("<trellis:data/>", graph.stream().findFirst().map(Triple::getSubject).map(RDFTerm::ntriplesString).get(), "Incorrect graph subject from updates!");
}
Also used : Graph(org.apache.commons.rdf.api.Graph) Factory.createDefaultGraph(org.apache.jena.graph.Factory.createDefaultGraph) RDFTerm(org.apache.commons.rdf.api.RDFTerm) Test(org.junit.jupiter.api.Test)

Example 3 with Graph

use of org.apache.commons.rdf.api.Graph in project trellis by trellis-ldp.

the class JenaIOServiceTest method testJsonLdFlattenedSerializer4.

@Test
void testJsonLdFlattenedSerializer4() throws UnsupportedEncodingException {
    when(mockNamespaceService.getNamespaces()).thenReturn(namespaces);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service.write(getTriples(), out, JSONLD, identifier, compacted, flattened);
    final String output = out.toString("UTF-8");
    final Graph graph = rdf.createGraph();
    service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add);
    assertAll("Check flattened serialization", checkCompactSerialization(output, graph));
}
Also used : Graph(org.apache.commons.rdf.api.Graph) Factory.createDefaultGraph(org.apache.jena.graph.Factory.createDefaultGraph) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 4 with Graph

use of org.apache.commons.rdf.api.Graph in project trellis by trellis-ldp.

the class JenaIOServiceTest method testJsonLdFlattenedSerializer.

@Test
void testJsonLdFlattenedSerializer() throws UnsupportedEncodingException {
    when(mockNamespaceService.getNamespaces()).thenReturn(namespaces);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service.write(getTriples(), out, JSONLD, identifier, flattened);
    final String output = out.toString("UTF-8");
    final Graph graph = rdf.createGraph();
    service.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add);
    assertAll("Check flattened serialization", checkFlattenedSerialization(output, graph));
}
Also used : Graph(org.apache.commons.rdf.api.Graph) Factory.createDefaultGraph(org.apache.jena.graph.Factory.createDefaultGraph) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 5 with Graph

use of org.apache.commons.rdf.api.Graph in project trellis by trellis-ldp.

the class JenaIOServiceTest method testJsonLdDefaultSerializer.

@Test
void testJsonLdDefaultSerializer() throws UnsupportedEncodingException {
    when(mockNamespaceService.getNamespaces()).thenReturn(namespaces);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    service3.write(getTriples(), out, JSONLD, identifier);
    final String output = out.toString("UTF-8");
    final Graph graph = rdf.createGraph();
    service3.read(new ByteArrayInputStream(output.getBytes(UTF_8)), JSONLD, null).forEach(graph::add);
    assertAll("Check compact serialization", checkCompactSerialization(output, graph));
}
Also used : Graph(org.apache.commons.rdf.api.Graph) Factory.createDefaultGraph(org.apache.jena.graph.Factory.createDefaultGraph) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Aggregations

Graph (org.apache.commons.rdf.api.Graph)81 RDF (org.apache.commons.rdf.api.RDF)34 IRI (org.apache.commons.rdf.api.IRI)31 Response (javax.ws.rs.core.Response)27 TestUtils.readEntityAsGraph (org.trellisldp.test.TestUtils.readEntityAsGraph)26 Test (org.junit.jupiter.api.Test)22 Factory.createDefaultGraph (org.apache.jena.graph.Factory.createDefaultGraph)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 Test (org.junit.Test)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 TestUtils.getResourceAsString (org.trellisldp.test.TestUtils.getResourceAsString)12 EntityTag (javax.ws.rs.core.EntityTag)9 Dataset (org.apache.commons.rdf.api.Dataset)9 Triple (org.apache.commons.rdf.api.Triple)9 Resource (org.trellisldp.api.Resource)7 InputStream (java.io.InputStream)6 Instant (java.time.Instant)6 RDFParser (org.apache.commons.rdf.experimental.RDFParser)6 TrellisRuntimeException (org.trellisldp.api.TrellisRuntimeException)6 IOException (java.io.IOException)5