/** * Get a graph from the dataset. * * @param graphName the graph name * @return the graph */ public Optional<Graph> getGraph(final IRI graphName) { return dataset.getGraph(graphName); }
private static void updateAcl(final Handle handle, final int resourceId, final Dataset dataset, final int batchSize) { dataset.getGraph(PreferAccessControl).ifPresent(graph -> batchUpdateTriples(handle, resourceId, "acl", graph, batchSize)); }
@Override public Collection<IRI> getTypes() { return data.getGraph(Trellis.PreferAudit) .map(graph -> graph.stream(null, type, null).map(Triple::getObject) .filter(term -> term instanceof IRI).map(term -> (IRI) term).collect(toList())) .orElseGet(Collections::emptyList); }
@Override public Collection<IRI> getAgents() { return data.getGraph(Trellis.PreferAudit) .map(graph -> graph.stream(null, PROV.wasAssociatedWith, null).map(Triple::getObject) .filter(term -> term instanceof IRI).map(term -> (IRI) term).collect(toList())) .orElseGet(Collections::emptyList); }
private static void updateDescription(final Handle handle, final int resourceId, final Dataset dataset, final int batchSize) { dataset.getGraph(PreferUserManaged).ifPresent(graph -> batchUpdateTriples(handle, resourceId, "description", graph, batchSize)); }
@Override public Optional<IRI> getInbox() { return data.getGraph(Trellis.PreferUserManaged) .flatMap(graph -> graph.stream(null, LDP.inbox, null).map(Triple::getObject) .filter(term -> term instanceof IRI).map(term -> (IRI) term).findFirst()); } }
protected Metadata.Builder metadataBuilder(final IRI identifier, final IRI ixnModel, final TrellisDataset mutable) { final Metadata.Builder builder = Metadata.builder(identifier).interactionModel(ixnModel); mutable.asDataset().getGraph(Trellis.PreferUserManaged).ifPresent(graph -> { graph.stream(identifier, LDP.membershipResource, null).findFirst().map(Triple::getObject) .filter(IRI.class::isInstance).map(IRI.class::cast).ifPresent(builder::membershipResource); graph.stream(identifier, LDP.hasMemberRelation, null).findFirst().map(Triple::getObject) .filter(IRI.class::isInstance).map(IRI.class::cast).ifPresent(builder::memberRelation); graph.stream(identifier, LDP.isMemberOfRelation, null).findFirst().map(Triple::getObject) .filter(IRI.class::isInstance).map(IRI.class::cast).ifPresent(builder::memberOfRelation); graph.stream(identifier, LDP.insertedContentRelation, null).findFirst().map(Triple::getObject) .filter(IRI.class::isInstance).map(IRI.class::cast).ifPresent(builder::insertedContentRelation); }); return builder; }
@Test public void getGraphNull() throws Exception { assertTrue(dataset.getGraph(null).get() instanceof DummyGraph); }
private static void updateExtra(final Handle handle, final int resourceId, final IRI identifier, final Dataset dataset) { dataset.getGraph(PreferUserManaged).ifPresent(graph -> { final String query = "INSERT INTO extra (resource_id, predicate, object) VALUES (?, ?, ?)"; try (final PreparedBatch batch = handle.prepareBatch(query)) { graph.stream(identifier, LDP.inbox, null).map(Triple::getObject).filter(t -> t instanceof IRI) .map(t -> ((IRI) t).getIRIString()).findFirst().ifPresent(iri -> batch.bind(0, resourceId) .bind(1, LDP.inbox.getIRIString()) .bind(2, iri) .add()); graph.stream(identifier, OA.annotationService, null).map(Triple::getObject) .filter(t -> t instanceof IRI).map(t -> ((IRI) t).getIRIString()).findFirst().ifPresent(iri -> batch.bind(0, resourceId) .bind(1, OA.annotationService.getIRIString()) .bind(2, iri).add()); if (batch.size() > 0) { batch.execute(); } } }); }
@Test public void getGraph() throws Exception { assertTrue(dataset.getGraph() instanceof DummyGraph); }
@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(asJenaDataset(data))); } catch (final Exception ex) { throw new RuntimeTrellisException("Error storing audit dataset for " + id, ex); } }); }
@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(asJenaDataset(data))); } catch (final Exception ex) { throw new RuntimeTrellisException("Error storing audit dataset for " + id, ex); } }); }
@Test public void getGraphNamed() throws Exception { assertFalse(dataset.getGraph(new DummyIRI(0)).isPresent()); }
@Test public void testAuditUpdate() { final Dataset dataset = rdf.createDataset(); final AuditService svc = new DefaultAuditService() {}; svc.update(subject, mockSession).forEach(dataset::add); assertTrue(dataset.getGraph(Trellis.PreferAudit).filter(graph -> graph.size() == dataset.size()).isPresent()); assertTrue(dataset.contains(null, null, type, AS.Update)); assertAll("Event property check", checkEventProperties(dataset)); }
@Test public void testAuditCreation() { final Dataset dataset = rdf.createDataset(); final AuditService svc = new DefaultAuditService() {}; svc.creation(subject, mockSession).forEach(dataset::add); assertTrue(dataset.getGraph(Trellis.PreferAudit).filter(graph -> graph.size() == dataset.size()).isPresent(), "Graph and dataset sizes don't match for creation event!"); assertTrue(dataset.contains(null, null, type, AS.Create), "as:Create type not in create dataset!"); assertAll("Event property check", checkEventProperties(dataset)); }
@Test public void testAuditDeletion() { final Dataset dataset = rdf.createDataset(); final AuditService svc = new DefaultAuditService() {}; svc.deletion(subject, mockSession).forEach(dataset::add); assertTrue(dataset.getGraph(Trellis.PreferAudit).filter(graph -> graph.size() == dataset.size()).isPresent(), "Graph and dataset sizes don't match for deletion event!"); assertTrue(dataset.contains(null, null, type, AS.Delete), "as:Delete type not in delete dataset!"); assertAll("Event property check", checkEventProperties(dataset)); }
@Test public void getGraphNull() throws Exception { // Default graph should be present try (final Graph defaultGraph = dataset.getGraph(null).get()) { // TODO: Can we assume the default graph was empty before our new triples? assertEquals(2, defaultGraph.size()); assertTrue(defaultGraph.contains(alice, isPrimaryTopicOf, graph1)); // NOTE: wildcard as graph2 is a (potentially mapped) BlankNode assertTrue(defaultGraph.contains(bob, isPrimaryTopicOf, null)); } }
@Test public void getGraph2() throws Exception { // graph2 should be present, even if is named by a BlankNode // We'll look up the potentially mapped graph2 blanknode final BlankNodeOrIRI graph2Name = (BlankNodeOrIRI) dataset.stream(Optional.empty(), bob, isPrimaryTopicOf, null) .map(Quad::getObject).findAny().get(); try (final Graph g2 = dataset.getGraph(graph2Name).get()) { assertEquals(4, g2.size()); final Triple bobNameTriple = bobNameQuad.asTriple(); assertTrue(g2.contains(bobNameTriple)); assertTrue(g2.contains(bob, member, bnode1)); assertTrue(g2.contains(bob, member, bnode2)); assertFalse(g2.contains(bnode1, name, secretClubName)); assertTrue(g2.contains(bnode2, name, companyName)); } }
@Test public void getGraph() throws Exception { try (final Graph defaultGraph = dataset.getGraph()) { // TODO: Can we assume the default graph was empty before our new triples? assertEquals(2, defaultGraph.size()); assertTrue(defaultGraph.contains(alice, isPrimaryTopicOf, graph1)); // NOTE: graph2 is a BlankNode assertTrue(defaultGraph.contains(bob, isPrimaryTopicOf, null)); } }
@Test public void getGraph1() throws Exception { // graph1 should be present try (final Graph g1 = dataset.getGraph(graph1).get()) { assertEquals(4, g1.size()); assertTrue(g1.contains(alice, name, aliceName)); assertTrue(g1.contains(alice, knows, bob)); assertTrue(g1.contains(alice, member, null)); assertTrue(g1.contains(null, name, secretClubName)); } }