Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moving iteration all the way into domain models (e.g. FedoraResource)
  • Loading branch information
ajs6f committed Oct 28, 2013
1 parent 858b3d1 commit f679d4c
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 113 deletions.
Expand Up @@ -136,7 +136,7 @@ public class FedoraNodes extends AbstractResource {
@Produces({TURTLE, N3, N3_ALT1, N3_ALT2, RDF_XML, RDF_JSON, NTRIPLES,
TEXT_HTML})
public Dataset describe(@PathParam("path") final List<PathSegment> pathList,
@QueryParam("offset") @DefaultValue("0") final long offset,
@QueryParam("offset") @DefaultValue("0") final int offset,
@QueryParam("limit") @DefaultValue("-1") final int limit,
@QueryParam("non-member-properties") final String nonMemberProperties,
@Context final Request request,
Expand Down Expand Up @@ -285,6 +285,8 @@ public Response updateSparql(@PathParam("path")
final Dataset properties = resource.updatePropertiesDataset(new HttpGraphSubjects(
session, FedoraNodes.class, uriInfo), IOUtils
.toString(requestBodyStream));


final Model problems = properties.getNamedModel(PROBLEMS_MODEL_NAME);
if (problems.size() > 0) {
logger.info(
Expand Down Expand Up @@ -571,9 +573,9 @@ public Response copyObject(@PathParam("path") final List<PathSegment> path,
nodeService.copyObject(session, toPath(path), destination);
session.save();
return created(new URI(destinationUri)).build();
} catch (ItemExistsException e) {
} catch (final ItemExistsException e) {
return status(SC_PRECONDITION_FAILED).entity("Destination resource already exists").build();
} catch (PathNotFoundException e) {
} catch (final PathNotFoundException e) {
return status(SC_CONFLICT).entity("There is no node that will serve as the parent of the moved item").build();
} finally {
session.logout();
Expand Down Expand Up @@ -609,9 +611,9 @@ public Response moveObject(@PathParam("path") final List<PathSegment> path,
nodeService.moveObject(session, toPath(path), destination);
session.save();
return created(new URI(destinationUri)).build();
} catch (ItemExistsException e) {
} catch (final ItemExistsException e) {
return status(SC_PRECONDITION_FAILED).entity("Destination resource already exists").build();
} catch (PathNotFoundException e) {
} catch (final PathNotFoundException e) {
return status(SC_CONFLICT).entity("There is no node that will serve as the parent of the moved item").build();
} finally {
session.logout();
Expand Down
Expand Up @@ -103,7 +103,7 @@ public Response getVersionList(@PathParam("path")

return Response.ok(
new GraphStoreStreamingOutput(resource
.getVersionDataset(new HttpGraphSubjects(session,
.getVersionTriples(new HttpGraphSubjects(session,
FedoraNodes.class,
uriInfo)),
bestPossibleResponse.getMediaType())).build();
Expand Down
Expand Up @@ -85,7 +85,7 @@ public class FedoraRepositoryWorkspaces extends AbstractResource {
public Dataset getWorkspaces() throws RepositoryException {

final Model workspaceModel =
JcrRdfTools.withContext(null, session).getJcrPropertiesModel().asModel();
JcrRdfTools.withContext(null, session).getNamespaceTriples().asModel();

final String[] workspaces =
session.getWorkspace().getAccessibleWorkspaceNames();
Expand Down
Expand Up @@ -40,9 +40,11 @@
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.services.NodeService;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;

@Ignore("Destined for death.")
public class FedoraFieldSearchTest {

private FedoraFieldSearch testObj;
Expand Down
Expand Up @@ -34,7 +34,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
Expand Down Expand Up @@ -262,7 +261,7 @@ public void testDescribeObject() throws RepositoryException, IOException {
when(mockObject.getEtagValue()).thenReturn("");
when(
mockObject.getPropertiesDataset(any(GraphSubjects.class),
anyLong(), anyInt())).thenReturn(mockDataset);
anyInt(), anyInt())).thenReturn(mockDataset);
when(mockNodes.getObject(isA(Session.class), isA(String.class)))
.thenReturn(mockObject);
final Request mockRequest = mock(Request.class);
Expand All @@ -285,7 +284,7 @@ public void testDescribeObjectNoInlining() throws RepositoryException, IOExcepti
when(mockObject.getEtagValue()).thenReturn("");
when(
mockObject.getPropertiesDataset(any(GraphSubjects.class),
anyLong(), eq(-2))).thenReturn(mockDataset);
anyInt(), eq(-2))).thenReturn(mockDataset);
when(mockNodes.getObject(isA(Session.class), isA(String.class)))
.thenReturn(mockObject);
final Request mockRequest = mock(Request.class);
Expand Down
Expand Up @@ -25,7 +25,6 @@
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -89,7 +88,7 @@ public void testGetVersionList() throws RepositoryException {
mockVariant);
when(mockNodes.getObject(any(Session.class), anyString())).thenReturn(
mockResource);
when(mockResource.getVersionDataset(any(HttpGraphSubjects.class)))
when(mockResource.getVersionTriples(any(HttpGraphSubjects.class)))
.thenReturn(new RdfStream());
when(mockVariant.getMediaType()).thenReturn(
new MediaType("text", "turtle"));
Expand Down Expand Up @@ -124,7 +123,7 @@ public void testGetVersion() throws RepositoryException, IOException {
testObj.getVersion(createPathList(pid), versionLabel, TestHelpers
.getUriInfoImpl());
verify(mockResource).getPropertiesDataset(any(HttpGraphSubjects.class),
anyLong(), anyInt());
anyInt(), anyInt());
}

}
Expand Up @@ -40,10 +40,12 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.junit.Ignore;
import org.junit.Test;

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

@Ignore("Destined for death.")
public class FedoraFieldSearchIT extends AbstractResourceIT {

@Test
Expand Down
57 changes: 37 additions & 20 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/FedoraResource.java
Expand Up @@ -15,9 +15,11 @@
*/
package org.fcrepo.kernel;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.update.UpdateAction.execute;
import static com.hp.hpl.jena.update.UpdateFactory.create;
import static org.apache.commons.codec.digest.DigestUtils.shaHex;
import static org.fcrepo.kernel.rdf.GraphProperties.PROBLEMS_MODEL_NAME;
import static org.fcrepo.kernel.rdf.GraphProperties.URI_SYMBOL;
import static org.fcrepo.kernel.services.ServiceHelpers.getObjectSize;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getBaseVersion;
Expand All @@ -39,15 +41,15 @@
import javax.jcr.version.VersionHistory;

import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.rdf.GraphProperties;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.impl.JcrGraphProperties;
import org.fcrepo.kernel.utils.JcrPropertyStatementListener;
import org.fcrepo.kernel.utils.JcrRdfTools;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;

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

Expand All @@ -59,20 +61,14 @@ public class FedoraResource extends JcrTools implements FedoraJcrTypes {

private static final Logger LOGGER = getLogger(FedoraResource.class);

private static final GraphProperties DEFAULT_PROPERTY_FACTORY =
new JcrGraphProperties();

protected Node node;

private GraphProperties properties;

/**
* Construct a FedoraObject without a backing JCR Node
*/
public FedoraResource() {
super(false);
node = null;
this.properties = DEFAULT_PROPERTY_FACTORY;
}

/**
Expand Down Expand Up @@ -216,7 +212,7 @@ public Collection<String> getModels() throws RepositoryException {
*/
public Dataset updatePropertiesDataset(final GraphSubjects subjects,
final String sparqlUpdateStatement) throws RepositoryException {
final Dataset dataset = getPropertiesDataset(subjects, 0, 0);
final Dataset dataset = getPropertiesDataset(subjects);
final UpdateRequest request =
create(sparqlUpdateStatement, dataset.getContext().getAsString(
URI_SYMBOL));
Expand All @@ -235,16 +231,37 @@ public Dataset updatePropertiesDataset(final GraphSubjects subjects,
* @return
* @throws RepositoryException
*/
public Dataset getPropertiesDataset(final GraphSubjects subjects,
final long offset, final int limit)
public Dataset getPropertiesDataset(final GraphSubjects graphSubjects,
final int offset, final int limit)
throws RepositoryException {

if (this.properties != null) {
return this.properties.getProperties(
node, subjects, offset, limit);
} else {
return null;
}
final JcrRdfTools jcrRdfTools =
JcrRdfTools.withContext(graphSubjects, getNode().getSession());

final RdfStream propertiesStream =
jcrRdfTools.getJcrTriples(getNode()).limit(limit).skip(offset);

propertiesStream.concat(jcrRdfTools.getTreeTriples(getNode(), offset,
limit));

final Dataset dataset = DatasetFactory.create(propertiesStream.asModel());

final Model problemsModel = createDefaultModel();

final JcrPropertyStatementListener listener =
JcrPropertyStatementListener.getListener(graphSubjects, node
.getSession(), problemsModel);

dataset.getDefaultModel().register(listener);

dataset.addNamedModel(PROBLEMS_MODEL_NAME, problemsModel);

dataset.getContext().set(URI_SYMBOL,
graphSubjects.getGraphSubject(getNode()));



return dataset;
}

/**
Expand All @@ -263,10 +280,10 @@ public Dataset getPropertiesDataset(final GraphSubjects subjects)
* @return
* @throws RepositoryException
*/
public RdfStream getVersionDataset(final GraphSubjects subjects)
public RdfStream getVersionTriples(final GraphSubjects graphSubjects)
throws RepositoryException {
return JcrRdfTools.withContext(subjects, node.getSession())
.getJcrVersionPropertiesModel(node);
return JcrRdfTools.withContext(graphSubjects, node.getSession())
.getVersionTriples(node);
}

/**
Expand Down
Expand Up @@ -55,7 +55,7 @@ public interface GraphProperties {
* @throws RepositoryException
*/
Dataset getProperties(final Node node, final GraphSubjects subjects,
final long offset, final int limit) throws RepositoryException;
final int offset, final int limit) throws RepositoryException;

/**
* Return a list of all properties generated by this factory
Expand Down
Expand Up @@ -46,11 +46,11 @@ public String getPropertyModelName() {

@Override
public Dataset getProperties(final Node node, final GraphSubjects subjects,
final long offset, final int limit)
final int offset, final int limit)
throws RepositoryException {
final JcrRdfTools jcrRdfTools = JcrRdfTools.withContext(subjects, node.getSession());
final Model model = jcrRdfTools.getJcrPropertiesModel(node).asModel();
final Model treeModel = jcrRdfTools.getJcrTreeModel(node, offset, limit).asModel();
final Model model = jcrRdfTools.getJcrTriples(node).asModel();
final Model treeModel = jcrRdfTools.getTreeTriples(node, offset, limit).asModel();
final Model problemModel = JcrRdfTools.getProblemsModel();

final JcrPropertyStatementListener listener =
Expand Down
Expand Up @@ -195,7 +195,7 @@ public Dataset getFixityResultsModel(final GraphSubjects subjects,
final Model model =
JcrRdfTools
.withContext(subjects, datastream.getNode().getSession())
.getJcrPropertiesModel(datastream.getNode(), blobs).asModel();
.getJcrTriples(datastream.getNode(), blobs).asModel();

final Dataset dataset = GraphStoreFactory.create(model).toDataset();

Expand Down
Expand Up @@ -184,7 +184,7 @@ public Dataset getNamespaceRegistryGraph(final Session session)
throws RepositoryException {

final Model model =
JcrRdfTools.withContext(null, session).getJcrNamespaceModel()
JcrRdfTools.withContext(null, session).getNamespaceTriples()
.asModel();

model.register(new NamespaceChangedStatementListener(session));
Expand Down
Expand Up @@ -226,17 +226,6 @@ public JcrRdfTools withSession(final Session session) {
return new JcrRdfTools(graphSubjects, session);
}

/**
* Create a default {@link RdfStream} populated with the registered JCR
* namespaces
*
* @return
* @throws RepositoryException
*/
public RdfStream getJcrPropertiesModel() throws RepositoryException {
return new NamespaceRdfContext(session);
}

/**
* Get an {@link RdfStream} for the given JCR NodeIterator
*
Expand Down Expand Up @@ -271,7 +260,7 @@ public RdfStream getJcrPropertiesModel(final Iterator<Node> nodeIterator,
* @return
* @throws RepositoryException
*/
public RdfStream getJcrPropertiesModel(final Node node) throws RepositoryException {
public RdfStream getJcrTriples(final Node node) throws RepositoryException {
return new PropertiesRdfContext(node, graphSubjects, llstore);
}

Expand All @@ -282,7 +271,7 @@ public RdfStream getJcrPropertiesModel(final Node node) throws RepositoryExcepti
* @return
* @throws RepositoryException
*/
public RdfStream getJcrVersionPropertiesModel(final Node node)
public RdfStream getVersionTriples(final Node node)
throws RepositoryException {
return new VersionsRdfContext(node, graphSubjects, llstore);
}
Expand All @@ -295,7 +284,7 @@ public RdfStream getJcrVersionPropertiesModel(final Node node)
* @return
* @throws RepositoryException
*/
public RdfStream getJcrPropertiesModel(final Node node,
public RdfStream getJcrTriples(final Node node,
final Iterable<FixityResult> blobs) throws RepositoryException {
return new FixityRdfContext(node, graphSubjects, llstore, blobs);
}
Expand All @@ -306,7 +295,7 @@ public RdfStream getJcrPropertiesModel(final Node node,
* @return
* @throws RepositoryException
*/
public RdfStream getJcrNamespaceModel() throws RepositoryException {
public RdfStream getNamespaceTriples() throws RepositoryException {
return new NamespaceRdfContext(session);
}

Expand All @@ -318,9 +307,10 @@ public RdfStream getJcrNamespaceModel() throws RepositoryException {
* @param offset
* @param limit @throws RepositoryException
*/
public RdfStream getJcrTreeModel(final Node node, final long offset,
public RdfStream getTreeTriples(final Node node, final int offset,
final int limit) throws RepositoryException {
return new HierarchyRdfContext(node, graphSubjects, llstore);
return new HierarchyRdfContext(node, graphSubjects, llstore).limit(
limit).skip(offset);
}

/**
Expand Down

0 comments on commit f679d4c

Please sign in to comment.