Search in sources :

Example 1 with PreferAudit

use of org.trellisldp.vocabulary.Trellis.PreferAudit in project trellis by trellis-ldp.

the class TriplestoreResourceService method buildUpdateRequest.

/**
 * This is equivalent to the SPARQL below.
 *
 * <p><pre><code>
 * DELETE WHERE { GRAPH IDENTIFIER { ?s ?p ?o } };
 * DELETE WHERE { GRAPH IDENTIFIER?ext=acl { ?s ?p ?o } };
 * DELETE WHERE { GRAPH trellis:PreferServerManaged {
 *   IDENTIFIER a ldp:NonRDFSource .
 *   IDENTIFIER dc:hasPart ?s .
 *   ?s ?p ?o .
 * };
 * DELETE WHERE { GRAPH trellis:PreferServerManaged { IDENTIFIER ?p ?o } };
 * INSERT DATA {
 *   GRAPH IDENTIFIER { ... }
 *   GRAPH IDENTIFIER?ext=acl { ... }
 *   GRAPH trellis:PreferServerManaged { ... }
 *   GRAPH IDENTIFIER?ext=audit { ... }
 * }
 * </code></pre></p>
 */
private UpdateRequest buildUpdateRequest(final IRI identifier, final Literal time, final Dataset dataset, final OperationType operation) {
    // Set the time
    dataset.add(PreferServerManaged, identifier, DC.modified, time);
    final UpdateRequest req = new UpdateRequest();
    req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(identifier), SUBJECT, PREDICATE, OBJECT)))));
    extensions.forEach((ext, graph) -> req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(getExtIRI(identifier, ext), SUBJECT, PREDICATE, OBJECT))))));
    req.add(new UpdateDeleteWhere(new QuadAcc(asList(new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(type), toJena(LDP.NonRDFSource)), new Quad(toJena(PreferServerManaged), toJena(identifier), toJena(DC.hasPart), SUBJECT), new Quad(toJena(PreferServerManaged), SUBJECT, PREDICATE, OBJECT)))));
    req.add(new UpdateDeleteWhere(new QuadAcc(singletonList(new Quad(toJena(PreferServerManaged), toJena(identifier), PREDICATE, OBJECT)))));
    final QuadDataAcc sink = new QuadDataAcc(synchronizedList(new ArrayList<>()));
    if (operation == OperationType.DELETE) {
        dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
    } else {
        dataset.stream().filter(q -> q.getGraphName().filter(PreferServerManaged::equals).isPresent()).map(JenaCommonsRDF::toJena).forEach(sink::addQuad);
        dataset.getGraph(PreferUserManaged).ifPresent(g -> g.stream().map(t -> new Quad(toJena(identifier), toJena(t))).forEach(sink::addQuad));
        dataset.getGraph(PreferAudit).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, "audit"), toJena(t))).forEach(sink::addQuad));
        extensions.forEach((ext, graph) -> dataset.getGraph(graph).ifPresent(g -> g.stream().map(t -> new Quad(getExtIRI(identifier, ext), toJena(t))).forEach(sink::addQuad)));
    }
    req.add(new UpdateDataInsert(sink));
    return req;
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) XSD(org.trellisldp.vocabulary.XSD) DeletedResource(org.trellisldp.vocabulary.Trellis.DeletedResource) DatabaseMgr.connectDatasetGraph(org.apache.jena.tdb2.DatabaseMgr.connectDatasetGraph) RDF.type(org.trellisldp.vocabulary.RDF.type) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc) Resource(org.trellisldp.api.Resource) Collections.singletonList(java.util.Collections.singletonList) SUBJECT(org.trellisldp.triplestore.TriplestoreUtils.SUBJECT) Txn.executeWrite(org.apache.jena.system.Txn.executeWrite) TrellisUtils.normalizeIdentifier(org.trellisldp.api.TrellisUtils.normalizeIdentifier) 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) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) Collections.synchronizedList(java.util.Collections.synchronizedList) DC(org.trellisldp.vocabulary.DC) Set(java.util.Set) Literal(org.apache.commons.rdf.api.Literal) Instant(java.time.Instant) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Var(org.apache.jena.sparql.core.Var) Stream.builder(java.util.stream.Stream.builder) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Query(org.apache.jena.query.Query) FOAF(org.trellisldp.vocabulary.FOAF) Arrays.stream(java.util.Arrays.stream) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Instant.now(java.time.Instant.now) NodeFactory.createURI(org.apache.jena.graph.NodeFactory.createURI) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) UpdateDeleteInsert(org.apache.jena.sparql.modify.request.UpdateDeleteInsert) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Supplier(java.util.function.Supplier) RDF(org.apache.commons.rdf.api.RDF) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) Quad(org.apache.jena.sparql.core.Quad) ResourceService(org.trellisldp.api.ResourceService) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) Logger(org.slf4j.Logger) PREDICATE(org.trellisldp.triplestore.TriplestoreUtils.PREDICATE) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) JenaCommonsRDF(org.apache.jena.commonsrdf.JenaCommonsRDF) TriplestoreUtils.getObject(org.trellisldp.triplestore.TriplestoreUtils.getObject) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) IRI(org.apache.commons.rdf.api.IRI) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) Node(org.apache.jena.graph.Node) DatasetFactory.createTxnMem(org.apache.jena.query.DatasetFactory.createTxnMem) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) UpdateRequest(org.apache.jena.update.UpdateRequest) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) OBJECT(org.trellisldp.triplestore.TriplestoreUtils.OBJECT) Quad(org.apache.jena.sparql.core.Quad) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) UpdateRequest(org.apache.jena.update.UpdateRequest) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) ArrayList(java.util.ArrayList) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc)

Example 2 with PreferAudit

use of org.trellisldp.vocabulary.Trellis.PreferAudit in project trellis by trellis-ldp.

the class TriplestoreResourceService method add.

@Override
public CompletionStage<Void> add(final IRI id, final Dataset dataset) {
    return runAsync(() -> {
        final IRI graphName = rdf.createIRI(id.getIRIString() + "?ext=audit");
        try (final Dataset data = rdf.createDataset()) {
            dataset.getGraph(PreferAudit).ifPresent(g -> g.stream().forEach(t -> data.add(graphName, t.getSubject(), t.getPredicate(), t.getObject())));
            executeWrite(rdfConnection, () -> rdfConnection.loadDataset(wrap(toJena(data))));
        } catch (final Exception ex) {
            throw new TrellisRuntimeException("Error storing audit dataset for " + id, ex);
        }
    });
}
Also used : IdentifierService(org.trellisldp.api.IdentifierService) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) XSD(org.trellisldp.vocabulary.XSD) DeletedResource(org.trellisldp.vocabulary.Trellis.DeletedResource) DatabaseMgr.connectDatasetGraph(org.apache.jena.tdb2.DatabaseMgr.connectDatasetGraph) RDF.type(org.trellisldp.vocabulary.RDF.type) QuadAcc(org.apache.jena.sparql.modify.request.QuadAcc) Resource(org.trellisldp.api.Resource) Collections.singletonList(java.util.Collections.singletonList) SUBJECT(org.trellisldp.triplestore.TriplestoreUtils.SUBJECT) Txn.executeWrite(org.apache.jena.system.Txn.executeWrite) TrellisUtils.normalizeIdentifier(org.trellisldp.api.TrellisUtils.normalizeIdentifier) 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) PreferServerManaged(org.trellisldp.vocabulary.Trellis.PreferServerManaged) Collections.synchronizedList(java.util.Collections.synchronizedList) DC(org.trellisldp.vocabulary.DC) Set(java.util.Set) Literal(org.apache.commons.rdf.api.Literal) Instant(java.time.Instant) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Var(org.apache.jena.sparql.core.Var) Stream.builder(java.util.stream.Stream.builder) TRELLIS_DATA_PREFIX(org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) RDFConnection(org.apache.jena.rdfconnection.RDFConnection) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Query(org.apache.jena.query.Query) FOAF(org.trellisldp.vocabulary.FOAF) Arrays.stream(java.util.Arrays.stream) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Instant.now(java.time.Instant.now) NodeFactory.createURI(org.apache.jena.graph.NodeFactory.createURI) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) RDFTerm(org.apache.commons.rdf.api.RDFTerm) ACL(org.trellisldp.vocabulary.ACL) PreferAccessControl(org.trellisldp.vocabulary.Trellis.PreferAccessControl) UpdateDeleteInsert(org.apache.jena.sparql.modify.request.UpdateDeleteInsert) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Supplier(java.util.function.Supplier) RDF(org.apache.commons.rdf.api.RDF) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Metadata(org.trellisldp.api.Metadata) Quad(org.apache.jena.sparql.core.Quad) ResourceService(org.trellisldp.api.ResourceService) LDP(org.trellisldp.vocabulary.LDP) DatasetFactory.wrap(org.apache.jena.query.DatasetFactory.wrap) Dataset(org.apache.commons.rdf.api.Dataset) RDFFactory(org.trellisldp.api.RDFFactory) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) RDFConnectionFactory.connect(org.apache.jena.rdfconnection.RDFConnectionFactory.connect) Logger(org.slf4j.Logger) PREDICATE(org.trellisldp.triplestore.TriplestoreUtils.PREDICATE) QuadDataAcc(org.apache.jena.sparql.modify.request.QuadDataAcc) JenaCommonsRDF(org.apache.jena.commonsrdf.JenaCommonsRDF) TriplestoreUtils.getObject(org.trellisldp.triplestore.TriplestoreUtils.getObject) UpdateDeleteWhere(org.apache.jena.sparql.modify.request.UpdateDeleteWhere) IRI(org.apache.commons.rdf.api.IRI) UpdateDataInsert(org.apache.jena.sparql.modify.request.UpdateDataInsert) Node(org.apache.jena.graph.Node) DatasetFactory.createTxnMem(org.apache.jena.query.DatasetFactory.createTxnMem) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) UpdateRequest(org.apache.jena.update.UpdateRequest) PreferUserManaged(org.trellisldp.vocabulary.Trellis.PreferUserManaged) OBJECT(org.trellisldp.triplestore.TriplestoreUtils.OBJECT) IRI(org.apache.commons.rdf.api.IRI) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Dataset(org.apache.commons.rdf.api.Dataset) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException)

Aggregations

Instant (java.time.Instant)2 Instant.now (java.time.Instant.now)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 Arrays.stream (java.util.Arrays.stream)2 Collections.singletonList (java.util.Collections.singletonList)2 Collections.synchronizedList (java.util.Collections.synchronizedList)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 CompletableFuture.runAsync (java.util.concurrent.CompletableFuture.runAsync)2 CompletionStage (java.util.concurrent.CompletionStage)2 Supplier (java.util.function.Supplier)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Stream (java.util.stream.Stream)2 Stream.builder (java.util.stream.Stream.builder)2 PostConstruct (javax.annotation.PostConstruct)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Inject (javax.inject.Inject)2 Dataset (org.apache.commons.rdf.api.Dataset)2