Skip to content

Commit

Permalink
support LDPC representations
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jul 28, 2013
1 parent aacc0ef commit cdf0dc0
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 12 deletions.
46 changes: 36 additions & 10 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/JcrRdfTools.java
Expand Up @@ -68,6 +68,9 @@
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import org.fcrepo.RdfLexicon;
import org.fcrepo.rdf.GraphSubjects;
import org.fcrepo.services.LowLevelStorageService;
Expand Down Expand Up @@ -428,10 +431,19 @@ public Model getJcrTreeModel(final Node node, final long offset, final int limit
}

final Resource subject = graphSubjects.getGraphSubject(node);
final Resource pageContext = graphSubjects.getContext();

model.add(pageContext, RDF.type, model.createResource("http://www.w3.org/ns/ldp#Page"));
model.add(pageContext, PAGE_OF, subject);

model.add(graphSubjects.getContext(), RDF.type, model.createResource("http://www.w3.org/ns/ldp#Page"));
model.add(graphSubjects.getContext(), PAGE_OF, subject);
if (isContainer(node)) {
model.add(pageContext, model.createProperty("http://www.w3.org/ns/ldp#membersInlined"), model.createTypedLiteral(true));

model.add(subject, RDF.type, model.createResource("http://www.w3.org/ns/ldp#Container"));
model.add(subject, model.createProperty("http://www.w3.org/ns/ldp#membershipSubject"), subject);
model.add(subject, model.createProperty("http://www.w3.org/ns/ldp#membershipPredicate"), RdfLexicon.HAS_CHILD);
model.add(subject, model.createProperty("http://www.w3.org/ns/ldp#membershipObject"), model.createResource("http://www.w3.org/ns/ldp#MemberSubject"));
}

// don't do this if the node is the root node.
if (node.getDepth() > 0) {
Expand All @@ -442,21 +454,22 @@ public Model getJcrTreeModel(final Node node, final long offset, final int limit
model.add(subject, RdfLexicon.HAS_PARENT, parentNodeSubject);
model.add(parentNodeSubject, RdfLexicon.HAS_CHILD, subject);
addJcrPropertiesToModel(parentNode, model);
model.add(graphSubjects.getContext(),
model.add(pageContext,
model.createProperty("http://www.w3.org/ns/ldp#inlinedResource"),
parentNodeSubject);
}

final javax.jcr.NodeIterator nodeIterator = node.getNodes();

int i = 0;
long excludedNodeCount = 0;
if (node.hasNodes()) {

if (nodeIterator.hasNext()) {
if (limit == -1) {
model.add(graphSubjects.getContext(), PAGE, RDF.nil);
model.add(pageContext, PAGE, RDF.nil);
}

final javax.jcr.NodeIterator nodeIterator = node.getNodes();

int i = 0;
long excludedNodeCount = 0;

while (nodeIterator.hasNext()) {
final Node childNode = nodeIterator.nextNode();

Expand All @@ -470,7 +483,7 @@ public Model getJcrTreeModel(final Node node, final long offset, final int limit

if (i >= offset && (limit == -1 || i < (offset + limit))) {
addJcrPropertiesToModel(childNode, model);
model.add(graphSubjects.getContext(), model.createProperty("http://www.w3.org/ns/ldp#inlinedResource"), childNodeSubject);
model.add(pageContext, model.createProperty("http://www.w3.org/ns/ldp#inlinedResource"), childNodeSubject);
model.add(childNodeSubject, RdfLexicon.HAS_PARENT, subject);
}

Expand All @@ -488,6 +501,19 @@ public Model getJcrTreeModel(final Node node, final long offset, final int limit
return model;
}

private boolean isContainer(final Node node) throws RepositoryException {
return HAS_CHILD_NODE_DEFINITIONS.apply(node.getPrimaryNodeType())
|| Iterables.any(ImmutableList.copyOf(node.getMixinNodeTypes()),
HAS_CHILD_NODE_DEFINITIONS);
}

Predicate<NodeType> HAS_CHILD_NODE_DEFINITIONS = new Predicate<NodeType>() {
@Override
public boolean apply(NodeType input) {
return input.getChildNodeDefinitions().length > 0;
}
};

/**
* Determine if a predicate is an internal property of a node (and
* should not be modified from external sources)
Expand Down
131 changes: 129 additions & 2 deletions fcrepo-kernel/src/test/java/org/fcrepo/utils/JcrRdfToolsTest.java
Expand Up @@ -91,6 +91,7 @@
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.NodeTypeManager;
Expand Down Expand Up @@ -481,12 +482,122 @@ public void shouldBeAbleToDisableResourceInlining() throws RepositoryException {
verify(mockNode, never()).getNodes();
}

@Test
public void shouldIncludeLinkedDataPlatformContainerInformation() throws RepositoryException {
when(mockParent.getPath()).thenReturn("/test");
when(mockNode.getPath()).thenReturn("/test/jcr");
when(mockNode.getParent()).thenReturn(mockParent);

NodeType mockPrimaryNodeType = mock(NodeType.class);
when(mockPrimaryNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { mock(NodeDefinition.class) }
);
when(mockNode.getPrimaryNodeType()).thenReturn(mockPrimaryNodeType);

when(mockParent.getProperties()).thenReturn(mockProperties);
when(mockProperties.hasNext()).thenReturn(false);

when(mockNode.getDepth()).thenReturn(0);
when(mockNodes.hasNext()).thenReturn(false);
when(mockNode.getNodes()).thenReturn(mockNodes);
final Model actual = testObj.getJcrTreeModel(mockNode, 0, -1);
assertTrue(actual.contains(testSubjects.getContext(), RDF.type, actual.createProperty("http://www.w3.org/ns/ldp#Page")));
assertTrue(actual.contains(testSubjects.getContext(), actual.createProperty("http://www.w3.org/ns/ldp#membersInlined"), actual.createTypedLiteral(true)));

final Resource graphSubject = testSubjects.getGraphSubject(mockNode);
assertTrue(actual.contains(graphSubject, RDF.type, actual.createProperty("http://www.w3.org/ns/ldp#Container")));

assertTrue(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipSubject"), graphSubject));
assertTrue(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipPredicate"), RdfLexicon.HAS_CHILD));
assertTrue(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipObject"), actual.createResource("http://www.w3.org/ns/ldp#MemberSubject")));

}

@Test
public void shouldIncludeContainerInfoWithMixinTypeContainer() throws RepositoryException {
when(mockParent.getPath()).thenReturn("/test");
when(mockNode.getPath()).thenReturn("/test/jcr");
when(mockNode.getParent()).thenReturn(mockParent);

NodeType mockPrimaryNodeType = mock(NodeType.class);
NodeType mockMixinNodeType = mock(NodeType.class);
when(mockPrimaryNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { }
);

when(mockMixinNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { mock(NodeDefinition.class) }
);
when(mockNode.getPrimaryNodeType()).thenReturn(mockPrimaryNodeType);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[] { mockMixinNodeType });

when(mockParent.getProperties()).thenReturn(mockProperties);
when(mockProperties.hasNext()).thenReturn(false);

when(mockNode.getDepth()).thenReturn(0);
when(mockNodes.hasNext()).thenReturn(false);
when(mockNode.getNodes()).thenReturn(mockNodes);
final Model actual = testObj.getJcrTreeModel(mockNode, 0, -1);
assertTrue(actual.contains(testSubjects.getContext(), RDF.type, actual.createProperty("http://www.w3.org/ns/ldp#Page")));
assertTrue(actual.contains(testSubjects.getContext(), actual.createProperty("http://www.w3.org/ns/ldp#membersInlined"), actual.createTypedLiteral(true)));

final Resource graphSubject = testSubjects.getGraphSubject(mockNode);
assertTrue(actual.contains(graphSubject, RDF.type, actual.createProperty("http://www.w3.org/ns/ldp#Container")));

assertTrue(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipSubject"), graphSubject));
assertTrue(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipPredicate"), RdfLexicon.HAS_CHILD));
assertTrue(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipObject"), actual.createResource("http://www.w3.org/ns/ldp#MemberSubject")));
}

@Test
public void shouldNotIncludeContainerInfoIfItIsntContainer() throws RepositoryException {
when(mockParent.getPath()).thenReturn("/test");
when(mockNode.getPath()).thenReturn("/test/jcr");
when(mockNode.getParent()).thenReturn(mockParent);

NodeType mockPrimaryNodeType = mock(NodeType.class);
NodeType mockMixinNodeType = mock(NodeType.class);
when(mockPrimaryNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { }
);

when(mockMixinNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { }
);
when(mockNode.getPrimaryNodeType()).thenReturn(mockPrimaryNodeType);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[] { mockMixinNodeType });

when(mockParent.getProperties()).thenReturn(mockProperties);
when(mockProperties.hasNext()).thenReturn(false);

when(mockNode.getDepth()).thenReturn(0);
when(mockNodes.hasNext()).thenReturn(false);
when(mockNode.getNodes()).thenReturn(mockNodes);
final Model actual = testObj.getJcrTreeModel(mockNode, 0, -1);
assertTrue(actual.contains(testSubjects.getContext(), RDF.type, actual.createProperty("http://www.w3.org/ns/ldp#Page")));
assertFalse(actual.contains(testSubjects.getContext(), actual.createProperty("http://www.w3.org/ns/ldp#membersInlined"), actual.createTypedLiteral(true)));

final Resource graphSubject = testSubjects.getGraphSubject(mockNode);
assertFalse(actual.contains(graphSubject, RDF.type, actual.createProperty("http://www.w3.org/ns/ldp#Container")));

assertFalse(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipSubject"), graphSubject));
assertFalse(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipPredicate"), RdfLexicon.HAS_CHILD));
assertFalse(actual.contains(graphSubject, actual.createProperty("http://www.w3.org/ns/ldp#membershipObject"), actual.createResource("http://www.w3.org/ns/ldp#MemberSubject")));

}

@Test
public void shouldIncludeParentNodeInformation() throws RepositoryException {
when(mockParent.getPath()).thenReturn("/test");
when(mockNode.getPath()).thenReturn("/test/jcr");
when(mockNode.getParent()).thenReturn(mockParent);

NodeType mockPrimaryNodeType = mock(NodeType.class);
when(mockPrimaryNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { mock(NodeDefinition.class) }
);
when(mockNode.getPrimaryNodeType()).thenReturn(mockPrimaryNodeType);

when(mockParent.getProperties()).thenReturn(mockProperties);
when(mockProperties.hasNext()).thenReturn(false);

Expand All @@ -503,15 +614,22 @@ public void shouldIncludeChildNodeInformation() throws RepositoryException {
when(mockNode.getPath()).thenReturn("/test/jcr");
when(mockNode.getParent()).thenReturn(mockParent);

NodeType mockPrimaryNodeType = mock(NodeType.class);
when(mockPrimaryNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { mock(NodeDefinition.class) }
);
when(mockNode.getPrimaryNodeType()).thenReturn(mockPrimaryNodeType);

when(mockParent.getProperties()).thenReturn(mockProperties);
when(mockProperties.hasNext()).thenReturn(false);

when(mockNode.getDepth()).thenReturn(0);
when(mockChildNode.getName()).thenReturn("some-name");
when(mockChildNode.getPath()).thenReturn("/test/jcr/1", "/test/jcr/2",
"/test/jcr/3", "/test/jcr/4", "/test/jcr/5");
when(mockNodes.hasNext()).thenReturn(true, true, true, true, true, true,
when(mockNodes.hasNext()).thenReturn(true, true, true, true, true,
false);
when(mockNode.hasNodes()).thenReturn(true);
when(mockNodes.nextNode()).thenReturn(mockChildNode);

when(mockNode.getNodes()).thenReturn(mockNodes);
Expand All @@ -526,6 +644,14 @@ public void shouldIncludeFullChildNodeInformationInsideWindow()
when(mockNode.getPath()).thenReturn("/test/jcr");
when(mockNode.getParent()).thenReturn(mockParent);
when(mockNode.getDepth()).thenReturn(0);


NodeType mockPrimaryNodeType = mock(NodeType.class);
when(mockPrimaryNodeType.getChildNodeDefinitions()).thenReturn(
new NodeDefinition[] { mock(NodeDefinition.class) }
);
when(mockNode.getPrimaryNodeType()).thenReturn(mockPrimaryNodeType);

when(mockChildNode.getName()).thenReturn("some-name");
when(mockChildNode.getPath()).thenReturn("/test/jcr/1", "/test/jcr/4",
"/test/jcr/5");
Expand All @@ -534,8 +660,9 @@ public void shouldIncludeFullChildNodeInformationInsideWindow()
"/test/jcr/3");
when(mockFullChildNode.getProperties()).thenReturn(mockProperties);
when(mockProperties.hasNext()).thenReturn(false);
when(mockNodes.hasNext()).thenReturn(true, true, true, true, true,
when(mockNodes.hasNext()).thenReturn(true, true, true, true,
false);
when(mockNode.hasNodes()).thenReturn(true);
when(mockNodes.nextNode()).thenReturn(mockChildNode, mockFullChildNode,
mockFullChildNode, mockChildNode, mockChildNode);
when(mockNode.getNodes()).thenReturn(mockNodes);
Expand Down

0 comments on commit cdf0dc0

Please sign in to comment.