Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use FedoraResource#getContainer/getChildren when serializating resources
Also, update getContainer + getChildren to be pairtree aware.
  • Loading branch information
cbeer committed Oct 16, 2014
1 parent d67a15b commit 44788bc
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 88 deletions.
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Iterators.singletonIterator;
import static com.google.common.collect.Lists.newArrayList;
import static com.hp.hpl.jena.update.UpdateAction.execute;
import static com.hp.hpl.jena.update.UpdateFactory.create;
Expand Down Expand Up @@ -46,10 +47,12 @@
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;

import com.google.common.base.Converter;
import com.google.common.base.Function;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.Datastream;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.exception.PathNotFoundRuntimeException;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
Expand Down Expand Up @@ -128,28 +131,72 @@ public String getPath() {
@Override
public Iterator<FedoraResource> getChildren() {
try {
return Iterators.transform(new NodeIterator(node.getNodes()), new Function<Node, FedoraResource>() {

@Override
public FedoraResource apply(final Node node) {
final FedoraResource fedoraResource = nodeConverter.convert(node);

if (fedoraResource instanceof Datastream) {
return ((Datastream) fedoraResource).getBinary();
} else {
return fedoraResource;
}
}
});
return Iterators.concat(getTransform(node));
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

private Iterator<Iterator<FedoraResource>> getTransform(final Node input) throws RepositoryException {
return Iterators.transform(new NodeIterator(input.getNodes()), new Function<Node, Iterator<FedoraResource>>() {

@Override
public Iterator<FedoraResource> apply(final Node input) {
try {
if (input.isNodeType(FEDORA_PAIRTREE)) {
return Iterators.concat(getTransform(input));
} else {
return singletonIterator(localConverter.convert(input));
}
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}
});
}

private static final Converter<FedoraResource, FedoraResource> datastreamToBinary
= new Converter<FedoraResource, FedoraResource>() {

@Override
protected FedoraResource doForward(final FedoraResource fedoraResource) {
if (fedoraResource instanceof Datastream) {
return ((Datastream) fedoraResource).getBinary();
} else {
return fedoraResource;
}
}

@Override
protected FedoraResource doBackward(final FedoraResource fedoraResource) {
if (fedoraResource instanceof FedoraBinary) {
return ((FedoraBinary) fedoraResource).getDescription();
} else {
return fedoraResource;
}
}
};

private static final Converter<Node, FedoraResource> localConverter = nodeConverter.andThen(datastreamToBinary);

@Override
public FedoraResource getParent() {
public FedoraResource getContainer() {
try {
return nodeConverter.convert(getNode().getParent());

if (getNode().getDepth() == 0) {
return null;
}

Node container = getNode().getParent();
while (container.getDepth() > 0) {
if (container.isNodeType(FEDORA_PAIRTREE) || container.isNodeType(FEDORA_DATASTREAM)) {
container = container.getParent();
} else {
return nodeConverter.convert(container);
}
}

return nodeConverter.convert(container);
} catch (RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
Expand Down
Expand Up @@ -110,25 +110,21 @@ private Iterator<Triple> memberRelations(final FedoraResource resource) throws R
memberRelation = LDP_MEMBER.asNode();
}

if (resource.getNode().hasNodes()) {
final Iterator<FedoraResource> memberNodes = resource.getChildren();
return Iterators.transform(memberNodes, new Function<FedoraResource, Triple>() {

@Override
public Triple apply(final FedoraResource child) {
final com.hp.hpl.jena.graph.Node childSubject;
if (child instanceof Datastream) {
childSubject = graphSubjects().reverse().convert(((Datastream) child).getBinary()).asNode();
} else {
childSubject = graphSubjects().reverse().convert(child).asNode();
}
return create(subject(), memberRelation, childSubject);
final Iterator<FedoraResource> memberNodes = resource.getChildren();
return Iterators.transform(memberNodes, new Function<FedoraResource, Triple>() {

@Override
public Triple apply(final FedoraResource child) {
final com.hp.hpl.jena.graph.Node childSubject;
if (child instanceof Datastream) {
childSubject = graphSubjects().reverse().convert(((Datastream) child).getBinary()).asNode();
} else {
childSubject = graphSubjects().reverse().convert(child).asNode();
}
});
} else {
return Collections.emptyIterator();
}
return create(subject(), memberRelation, childSubject);

}
});
}

}
Expand Up @@ -17,7 +17,6 @@

import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import org.fcrepo.kernel.FedoraBinary;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;

Expand Down Expand Up @@ -48,48 +47,26 @@ public LdpIsMemberOfRdfContext(final FedoraResource resource,
throws RepositoryException {
super(resource, graphSubjects);

if (!isRoot(resource)) {
final FedoraResource container = getContainer(resource);
final FedoraResource container = resource.getContainer();

if (container.hasProperty(LDP_IS_MEMBER_OF_RELATION)) {
final Property property = container.getProperty(LDP_IS_MEMBER_OF_RELATION);
if (container != null) {

final Resource memberRelation = ResourceFactory.createResource(property.getString());

final Resource membershipResource = getMemberResource(container);

if (membershipResource != null) {
concat(create(subject(), memberRelation.asNode(), membershipResource.asNode()));
}
}
concatIsMemberOfRelation(container);
}
}

/**
* Check if the resource is the root resource (and therefore can't be within a container)
* @param resource
* @return
* @throws RepositoryException
*/
private boolean isRoot(final FedoraResource resource) throws RepositoryException {
return resource.getNode().getDepth() == 0;
}
private void concatIsMemberOfRelation(final FedoraResource container) throws RepositoryException {
if (container.hasProperty(LDP_IS_MEMBER_OF_RELATION)) {
final Property property = container.getProperty(LDP_IS_MEMBER_OF_RELATION);

/**
* Get the LDP container for this resource
* @param resource
* @return
* @throws RepositoryException
*/
private FedoraResource getContainer(final FedoraResource resource) throws RepositoryException {
final FedoraResource parent;
final Resource memberRelation = ResourceFactory.createResource(property.getString());

if (resource instanceof FedoraBinary) {
parent = ((FedoraBinary) resource).getDescription().getParent();
} else {
parent = resource.getParent();
final Resource membershipResource = getMemberResource(container);

if (membershipResource != null) {
concat(create(subject(), memberRelation.asNode(), membershipResource.asNode()));
}
}
return parent;
}

/**
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.kernel.impl.rdf.impl;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.kernel.FedoraResource;
Expand Down Expand Up @@ -57,12 +58,10 @@ public ParentRdfContext(final FedoraResource resource,
}

private Iterator<Triple> parentContext() throws RepositoryException {
final javax.jcr.Node parentNode = resource().getNode().getParent();
final com.hp.hpl.jena.graph.Node parentNodeSubject = nodeConverter().convert(parentNode).asNode();

final RdfStream parentStream = new RdfStream();

parentStream.concat(create(subject(), HAS_PARENT.asNode(), parentNodeSubject));
final Node containerSubject = graphSubjects().reverse().convert(resource().getContainer()).asNode();
parentStream.concat(create(subject(), HAS_PARENT.asNode(), containerSubject));

return parentStream;
}
Expand Down
Expand Up @@ -600,4 +600,49 @@ public void testDeleteObjectWithInboundReferences() throws RepositoryException {
assertFalse(session.nodeExists("/" + pid + "/b"));

}

@Test
public void testGetContainer() throws RepositoryException {
final String pid = getRandomPid();
final FedoraObject container = objectService.findOrCreateObject(session, "/" + pid);
final FedoraResource resource = objectService.findOrCreateObject(session, "/" + pid + "/a");

assertEquals(container, resource.getContainer());
}

@Test
public void testGetChildren() throws RepositoryException {
final String pid = getRandomPid();
final FedoraObject container = objectService.findOrCreateObject(session, "/" + pid);
final FedoraResource resource = objectService.findOrCreateObject(session, "/" + pid + "/a");

assertEquals(resource, container.getChildren().next());
}

@Test
public void testGetContainerForBinary() throws RepositoryException {
final String pid = getRandomPid();
final FedoraObject container = objectService.findOrCreateObject(session, "/" + pid);
final FedoraResource resource = binaryService.findOrCreateBinary(session, "/" + pid + "/a");

assertEquals(container, resource.getContainer());
}

@Test
public void testGetContainerWithHierarchy() throws RepositoryException {
final String pid = getRandomPid();
final FedoraObject container = objectService.findOrCreateObject(session, "/" + pid);
final FedoraResource resource = objectService.findOrCreateObject(session, "/" + pid + "/a/b/c/d");

assertEquals(container, resource.getContainer());
}

@Test
public void testGetChildrenWithHierarchy() throws RepositoryException {
final String pid = getRandomPid();
final FedoraObject container = objectService.findOrCreateObject(session, "/" + pid);
final FedoraResource resource = objectService.findOrCreateObject(session, "/" + pid + "/a/b/c/d");

assertEquals(resource, container.getChildren().next());
}
}
Expand Up @@ -121,4 +121,5 @@ public void testGetDatastreamContentInputStream() throws Exception {
session.logout();
}


}
Expand Up @@ -20,6 +20,7 @@
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.util.Calendar.JULY;
import static org.apache.commons.codec.digest.DigestUtils.shaHex;
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_PAIRTREE;
import static org.fcrepo.jcr.FedoraJcrTypes.JCR_CREATED;
import static org.fcrepo.jcr.FedoraJcrTypes.JCR_LASTMODIFIED;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -83,6 +84,9 @@ public class FedoraResourceImplTest {
@Mock
private Node mockChild;

@Mock
private Node mockContainer;

@Mock
private Session mockSession;

Expand Down Expand Up @@ -130,7 +134,7 @@ public void testGetCreatedDate() throws RepositoryException {
when(mockNode.hasProperty(JCR_CREATED)).thenReturn(true);
when(mockNode.getProperty(JCR_CREATED)).thenReturn(mockProp);
assertEquals(someDate.getTimeInMillis(), testObj.getCreatedDate()
.getTime());
.getTime());
}

@Test
Expand Down Expand Up @@ -330,10 +334,23 @@ public void shouldGetEtagForAnObject() throws RepositoryException {
}

@Test
public void testGetParent() throws RepositoryException {
when(mockNode.getParent()).thenReturn(mockRoot);
final FedoraResource actual = testObj.getParent();
assertEquals(new FedoraResourceImpl(mockRoot), actual);
public void testGetContainer() throws RepositoryException {
when(mockNode.getParent()).thenReturn(mockContainer);
when(mockNode.getDepth()).thenReturn(1);
final FedoraResource actual = testObj.getContainer();
assertEquals(new FedoraResourceImpl(mockContainer), actual);
}

@Test
public void testGetContainerForNestedResource() throws RepositoryException {
when(mockNode.getParent()).thenReturn(mockChild);
when(mockNode.getDepth()).thenReturn(3);
when(mockChild.getParent()).thenReturn(mockContainer);
when(mockChild.getDepth()).thenReturn(2);
when(mockChild.isNodeType(FEDORA_PAIRTREE)).thenReturn(true);
when(mockContainer.getDepth()).thenReturn(1);
final FedoraResource actual = testObj.getContainer();
assertEquals(new FedoraResourceImpl(mockContainer), actual);
}

@Test
Expand Down
Expand Up @@ -51,9 +51,6 @@ public class LdpContainerRdfContextTest {
@Mock
private FedoraResource mockResource;

@Mock
private FedoraResource mockContainer;

@Mock
private Node mockContainerNode;

Expand Down Expand Up @@ -82,7 +79,6 @@ public void setUp() throws RepositoryException {
initMocks(this);
when(mockResource.getPath()).thenReturn("/a");
when(mockResource.getNode()).thenReturn(mockNode);
when(mockContainer.getNode()).thenReturn(mockContainerNode);
subjects = new DefaultIdentifierTranslator(mockSession);
}

Expand All @@ -103,6 +99,7 @@ public void testLdpResourceWithEmptyContainer() throws RepositoryException {

when(mockNode.getReferences(LDP_MEMBER_RESOURCE)).thenReturn(new TestPropertyIterator(mockProperty));
when(mockProperty.getParent()).thenReturn(mockContainerNode);
when(mockContainerNode.getNodes()).thenReturn(new TestNodeIterator());
testObj = new LdpContainerRdfContext(mockResource, subjects);

final Model model = testObj.asModel();
Expand All @@ -116,7 +113,6 @@ public void testLdpResourceWithContainer() throws RepositoryException {

when(mockNode.getReferences(LDP_MEMBER_RESOURCE)).thenReturn(new TestPropertyIterator(mockProperty));
when(mockProperty.getParent()).thenReturn(mockContainerNode);
when(mockContainerNode.hasNodes()).thenReturn(true);
when(mockContainerNode.getNodes()).thenReturn(new TestNodeIterator(mockChild));
when(mockChild.getPath()).thenReturn("/b");
testObj = new LdpContainerRdfContext(mockResource, subjects);
Expand All @@ -135,7 +131,6 @@ public void testLdpResourceWithContainerAssertingRelation() throws RepositoryExc

when(mockNode.getReferences(LDP_MEMBER_RESOURCE)).thenReturn(new TestPropertyIterator(mockProperty));
when(mockProperty.getParent()).thenReturn(mockContainerNode);
when(mockContainerNode.hasNodes()).thenReturn(true);
when(mockContainerNode.getNodes()).thenReturn(new TestNodeIterator(mockChild));
when(mockChild.getPath()).thenReturn("/b");
final Property mockRelation = mock(Property.class);
Expand Down

0 comments on commit 44788bc

Please sign in to comment.