Skip to content

Commit

Permalink
use Jena Datasets consistently, and add a context element to identify…
Browse files Browse the repository at this point in the history
… the oriobject that produced the graph
  • Loading branch information
cbeer committed May 22, 2013
1 parent d5be12f commit 9fdc5f1
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 96 deletions.
34 changes: 7 additions & 27 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -53,17 +53,13 @@
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.FedoraJcrTypes;
import org.fcrepo.utils.JcrRdfTools;
import org.modeshape.common.collection.Problems;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.sparql.util.Symbol;
import com.hp.hpl.jena.update.UpdateAction;

@Component
Expand Down Expand Up @@ -106,24 +102,8 @@ public Dataset describe(@PathParam("path")
throw new WebApplicationException(builder.cacheControl(cc)
.lastModified(date).build());
}
//return resource.getGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)).toDataset();

// store the topic uri in the dataset context
HttpGraphSubjects subjects =
new HttpGraphSubjects(FedoraNodes.class, uriInfo);
Dataset dataset = resource.getGraphStore(subjects).toDataset();
String uri = null;
try {
uri = JcrRdfTools.getGraphSubject(
subjects,session.getNode(path)).getURI();
} catch ( Exception ex ) { uri = null; }

com.hp.hpl.jena.sparql.util.Context context =
dataset.getContext();
if ( context == null ) { context = new com.hp.hpl.jena.sparql.util.Context(); }
context.set(Symbol.create("uri"),uri);

return dataset;
return resource.getPropertiesDataset(new HttpGraphSubjects(FedoraNodes.class, uriInfo));

} finally {
session.logout();
}
Expand Down Expand Up @@ -154,8 +134,8 @@ public Response modifyObject(@PathParam("path")

if (requestBodyStream != null) {
UpdateAction.parseExecute(IOUtils.toString(requestBodyStream),
result.getGraphStore(new HttpGraphSubjects(
FedoraNodes.class, uriInfo)));
result.getPropertiesDataset(new HttpGraphSubjects(
FedoraNodes.class, uriInfo)));
}
session.save();

Expand Down Expand Up @@ -194,9 +174,9 @@ public Response updateSparql(@PathParam("path")
final FedoraResource result =
nodeService.getObject(session, path);

result.updateGraph(new HttpGraphSubjects(FedoraNodes.class,
uriInfo), IOUtils.toString(requestBodyStream));
final Problems problems = result.getGraphProblems();
result.updatePropertiesDataset(new HttpGraphSubjects(FedoraNodes.class,
uriInfo), IOUtils.toString(requestBodyStream));
final Problems problems = result.getDatasetProblems();
if (problems != null && problems.hasProblems()) {
logger.info(
"Found these problems updating the properties for {}: {}",
Expand Down
Expand Up @@ -65,7 +65,7 @@ public Response getVersionList(@PathParam("path")

return Response.ok(
new GraphStoreStreamingOutput(resource
.getVersionGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)), bestPossibleResponse
.getVersionDataset(new HttpGraphSubjects(FedoraNodes.class, uriInfo)), bestPossibleResponse
.getMediaType())).build();

} finally {
Expand Down Expand Up @@ -112,7 +112,7 @@ public Dataset getVersion(@PathParam("path")
throw new WebApplicationException(status(NOT_FOUND).build());
} else {

return resource.getGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)).toDataset();
return resource.getPropertiesDataset(new HttpGraphSubjects(FedoraNodes.class, uriInfo));
}

} finally {
Expand Down
Expand Up @@ -50,8 +50,8 @@ public Response updateSparql(final InputStream requestBodyStream) throws Reposit

final FedoraResource result = nodeService.getObject(session, "/");

result.updateGraph(IOUtils.toString(requestBodyStream));
Problems problems = result.getGraphProblems();
result.updatePropertiesDataset(IOUtils.toString(requestBodyStream));
Problems problems = result.getDatasetProblems();
if (problems != null && problems.hasProblems()) {
logger.info("Found these problems updating the properties for {}: {}", "/", problems.toString());
return Response.status(Response.Status.FORBIDDEN).entity(problems.toString()).build();
Expand Down
Expand Up @@ -43,7 +43,6 @@

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;

public class FedoraNodesTest {

Expand Down Expand Up @@ -169,14 +168,12 @@ public void testDescribeObject() throws RepositoryException, IOException {
final String path = "/" + pid;

final FedoraObject mockObject = mock(FedoraObject.class);
final GraphStore mockStore = mock(GraphStore.class);
final Dataset mockDataset = mock(Dataset.class);
final Model mockModel = mock(Model.class);
when(mockStore.toDataset()).thenReturn(mockDataset);
when(mockDataset.getDefaultModel()).thenReturn(mockModel);

when(mockObject.getLastModifiedDate()).thenReturn(null);
when(mockObject.getGraphStore(any(GraphSubjects.class))).thenReturn(mockStore);
when(mockObject.getPropertiesDataset(any(GraphSubjects.class))).thenReturn(mockDataset);
when(
mockNodes.getObject(Mockito.isA(Session.class), Mockito
.isA(String.class))).thenReturn(mockObject);
Expand All @@ -194,16 +191,16 @@ public void testSparqlUpdate() throws RepositoryException, IOException {

final FedoraObject mockObject = mock(FedoraObject.class);

when(mockObject.getGraphProblems()).thenReturn(null);
when(mockObject.getDatasetProblems()).thenReturn(null);
final InputStream mockStream =
new ByteArrayInputStream("my-sparql-statement".getBytes());
when(mockNodes.getObject(mockSession, path)).thenReturn(mockObject);

testObj.updateSparql(createPathList(pid), TestHelpers.getUriInfoImpl(),
mockStream);

verify(mockObject).updateGraph(any(GraphSubjects.class),
eq("my-sparql-statement"));
verify(mockObject).updatePropertiesDataset(any(GraphSubjects.class),
eq("my-sparql-statement"));
verify(mockSession).save();
verify(mockSession).logout();
}
Expand Down
Expand Up @@ -38,14 +38,14 @@ public void setUp() throws RepositoryException {
public void testSparqlUpdate() throws RepositoryException, IOException {
final FedoraObject mockObject = mock(FedoraObject.class);

when(mockObject.getGraphProblems()).thenReturn(null);
when(mockObject.getDatasetProblems()).thenReturn(null);
final InputStream mockStream =
new ByteArrayInputStream("my-sparql-statement".getBytes());
when(mockNodes.getObject(mockSession, "/")).thenReturn(mockObject);

testObj.updateSparql(mockStream);

verify(mockObject).updateGraph("my-sparql-statement");
verify(mockObject).updatePropertiesDataset("my-sparql-statement");
verify(mockSession).save();
verify(mockSession).logout();
}
Expand Down
Expand Up @@ -132,8 +132,8 @@ protected FedoraResource createObjectOrDatastreamFromRequestContent(
requestContentType != null &&
requestContentType.toString().equals(
WebContent.contentTypeSPARQLUpdate)) {
result.updateGraph(new HttpGraphSubjects(pathsRelativeTo,
uriInfo), IOUtils.toString(requestBodyStream));
result.updatePropertiesDataset(new HttpGraphSubjects(pathsRelativeTo,
uriInfo), IOUtils.toString(requestBodyStream));
}

} else if (FedoraJcrTypes.FEDORA_DATASTREAM.equals(mixin)) {
Expand Down
51 changes: 30 additions & 21 deletions fcrepo-kernel/src/main/java/org/fcrepo/FedoraResource.java
Expand Up @@ -18,6 +18,9 @@
import javax.jcr.nodetype.NodeType;
import javax.jcr.version.VersionHistory;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.sparql.util.Symbol;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.rdf.impl.DefaultGraphSubjects;
import org.fcrepo.utils.FedoraJcrTypes;
Expand All @@ -29,8 +32,6 @@
import org.slf4j.Logger;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.GraphStoreFactory;
import com.hp.hpl.jena.update.UpdateAction;

public class FedoraResource extends JcrTools implements FedoraJcrTypes {
Expand Down Expand Up @@ -166,28 +167,26 @@ public Collection<String> getModels() throws RepositoryException {
return map(node.getMixinNodeTypes(), nodetype2name);
}

public Problems getGraphProblems() throws RepositoryException {
public Problems getDatasetProblems() throws RepositoryException {
if (listener != null) {
return listener.getProblems();
} else {
return null;
}
}

public GraphStore updateGraph(final GraphSubjects subjects,
final String sparqlUpdateStatement) throws RepositoryException {
final GraphStore store = getGraphStore(subjects);
UpdateAction.parseExecute(sparqlUpdateStatement, store);

return store;
public void updatePropertiesDataset(final GraphSubjects subjects,
final String sparqlUpdateStatement) throws RepositoryException {
final Dataset dataset = getPropertiesDataset(subjects);
UpdateAction.parseExecute(sparqlUpdateStatement, dataset);
}

public GraphStore updateGraph(final String sparqlUpdateStatement)
public void updatePropertiesDataset(final String sparqlUpdateStatement)
throws RepositoryException {
return updateGraph(DEFAULT_SUBJECT_FACTORY, sparqlUpdateStatement);
updatePropertiesDataset(DEFAULT_SUBJECT_FACTORY, sparqlUpdateStatement);
}

public GraphStore getGraphStore(final GraphSubjects subjects)
public Dataset getPropertiesDataset(final GraphSubjects subjects)
throws RepositoryException {

final Model model = JcrRdfTools.getJcrPropertiesModel(subjects, node);
Expand All @@ -197,26 +196,36 @@ public GraphStore getGraphStore(final GraphSubjects subjects)

model.register(listener);

final GraphStore graphStore = GraphStoreFactory.create(model);
final Dataset dataset = DatasetFactory.create(model);

String uri = JcrRdfTools.getGraphSubject(subjects, node).getURI();
com.hp.hpl.jena.sparql.util.Context context = dataset.getContext();
if ( context == null ) { context = new com.hp.hpl.jena.sparql.util.Context(); }
context.set(Symbol.create("uri"),uri);

return graphStore;
return dataset;
}

public GraphStore getGraphStore() throws RepositoryException {
return getGraphStore(DEFAULT_SUBJECT_FACTORY);
public Dataset getPropertiesDataset() throws RepositoryException {
return getPropertiesDataset(DEFAULT_SUBJECT_FACTORY);
}

public GraphStore getVersionGraphStore(final GraphSubjects subjects)
public Dataset getVersionDataset(final GraphSubjects subjects)
throws RepositoryException {
final Model model = JcrRdfTools.getJcrVersionsModel(subjects, node);

final GraphStore graphStore = GraphStoreFactory.create(model);
final Dataset dataset = DatasetFactory.create(model);

String uri = JcrRdfTools.getGraphSubject(subjects, node).getURI();
com.hp.hpl.jena.sparql.util.Context context = dataset.getContext();
if ( context == null ) { context = new com.hp.hpl.jena.sparql.util.Context(); }
context.set(Symbol.create("uri"),uri);

return graphStore;
return dataset;
}

public GraphStore getVersionGraphStore() throws RepositoryException {
return getVersionGraphStore(DEFAULT_SUBJECT_FACTORY);
public Dataset getVersionDataset() throws RepositoryException {
return getVersionDataset(DEFAULT_SUBJECT_FACTORY);
}

public void addVersionLabel(final String label) throws RepositoryException {
Expand Down
Expand Up @@ -158,12 +158,12 @@ public void testGetLastModifiedDate() throws RepositoryException {

@Test
public void testGetGraphProblems() throws RepositoryException {
final Problems actual = testObj.getGraphProblems();
final Problems actual = testObj.getDatasetProblems();
assertEquals(null, actual);
final JcrPropertyStatementListener mockListener =
mock(JcrPropertyStatementListener.class);
setField("listener", FedoraResource.class, mockListener, testObj);
testObj.getGraphProblems();
testObj.getDatasetProblems();
verify(mockListener).getProblems();
}

Expand Down
Expand Up @@ -15,12 +15,12 @@
import javax.jcr.Session;
import javax.jcr.Value;

import com.hp.hpl.jena.query.Dataset;
import org.fcrepo.FedoraObject;
import org.fcrepo.services.ObjectService;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.update.UpdateAction;

@ContextConfiguration({"/spring-test/repo.xml"})
Expand Down Expand Up @@ -61,7 +61,7 @@ public void testGetSizeWhenInATree() throws Exception {
public void testObjectGraph() throws Exception {
final Session session = repo.login();
final FedoraObject object = objectService.createObject(session, "/graphObject");
final GraphStore graphStore = object.getGraphStore();
final Dataset graphStore = object.getPropertiesDataset();

final String graphSubject = "info:fedora/graphObject";

Expand Down

0 comments on commit 9fdc5f1

Please sign in to comment.