Skip to content

Commit

Permalink
Merge pull request #214 from futures/MixinsForTransforms
Browse files Browse the repository at this point in the history
Let transforms consider mixin types
  • Loading branch information
cbeer committed Dec 15, 2013
2 parents fa28e75 + e8d0953 commit 83ac74e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
Expand Up @@ -22,28 +22,36 @@
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

import org.apache.marmotta.ldpath.LDPath;
import org.apache.marmotta.ldpath.backend.jena.GenericJenaBackend;
import org.apache.marmotta.ldpath.exception.LDPathParseException;
import org.fcrepo.transform.Transformation;
import org.slf4j.Logger;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.WebApplicationException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.ImmutableSortedSet.orderedBy;
import static com.google.common.collect.Maps.transformValues;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
import static org.fcrepo.kernel.rdf.SerializationUtils.getDatasetSubject;
import static org.fcrepo.kernel.rdf.SerializationUtils.unifyDatasetModel;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
import static org.slf4j.LoggerFactory.getLogger;

/**
* Utilities for working with LDPath
Expand All @@ -57,6 +65,17 @@ public class LDPathTransform implements Transformation {
public static final String APPLICATION_RDF_LDPATH = "application/rdf+ldpath";
private final InputStream query;

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

private static final Comparator<NodeType> nodeTypeComp = new Comparator<NodeType>() {

@Override
public int compare(final NodeType o1, final NodeType o2) {
return o1.getName().compareTo(o2.getName());

}
};

/**
* Construct a new Transform from the InputStream
* @param query
Expand All @@ -77,23 +96,43 @@ public static LDPathTransform getNodeTypeTransform(final Node node,

final Node programNode = node.getSession().getNode(CONFIGURATION_FOLDER + key);

LOGGER.debug("Found program node: {}", programNode.getPath());

final NodeType primaryNodeType = node.getPrimaryNodeType();

final NodeType[] supertypes = primaryNodeType.getSupertypes();
final Set<NodeType> supertypes =
orderedBy(nodeTypeComp).add(primaryNodeType.getSupertypes())
.build();
final Set<NodeType> mixinTypes =
orderedBy(nodeTypeComp).add(node.getMixinNodeTypes()).build();

// start with mixins, primary type, and supertypes of primary type
final ImmutableList.Builder<NodeType> nodeTypesB =
new ImmutableList.Builder<NodeType>().addAll(mixinTypes).add(
primaryNodeType).addAll(supertypes);

// add supertypes of mixins
for (final NodeType mixin : mixinTypes) {
nodeTypesB.addAll(orderedBy(nodeTypeComp).add(
mixin.getDeclaredSupertypes()).build());
}

final List<NodeType> nodeTypes = nodeTypesB.build();

final Iterable<NodeType> nodeTypes =
concat(of(primaryNodeType), copyOf(supertypes));
LOGGER.debug("Discovered node types: {}", nodeTypes);

for (final NodeType nodeType : nodeTypes) {
if (programNode.hasNode(nodeType.toString())) {
return new LDPathTransform(programNode.getNode(nodeType.toString())
.getNode("jcr:content")
.getProperty("jcr:data")
.getNode(JCR_CONTENT)
.getProperty(JCR_DATA)
.getBinary().getStream());
}
}

return null;
throw new WebApplicationException(new Exception(
"Couldn't find transformation for " + node.getPath()
+ " and transformation key " + key), SC_BAD_REQUEST);
}

@Override
Expand Down
Expand Up @@ -31,16 +31,19 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.WebApplicationException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.fcrepo.transform.transformations.LDPathTransform.CONFIGURATION_FOLDER;
import static org.fcrepo.transform.transformations.LDPathTransform.getNodeTypeTransform;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
Expand All @@ -62,36 +65,36 @@ public void setUp() throws RepositoryException {
when(mockNode.getSession()).thenReturn(mockSession);
}

@Test
@Test(expected=WebApplicationException.class)
public void testGetNodeTypeSpecificLdpathProgramForMissingProgram() throws RepositoryException {
final Node mockConfigNode = mock(Node.class);
when(mockSession.getNode(LDPathTransform.CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);
when(mockSession.getNode(CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);

when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[]{});
final NodeType mockNodeType = mock(NodeType.class);
final NodeType mockNtBase = mock(NodeType.class);
when(mockNodeType.getSupertypes()).thenReturn(new NodeType[] { mockNtBase });
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
final LDPathTransform nodeTypeSpecificLdpathProgramStream = LDPathTransform.getNodeTypeTransform(mockNode, "some-program");

assertNull(nodeTypeSpecificLdpathProgramStream);
getNodeTypeTransform(mockNode, "some-program");
}

@Test
public void testGetNodeTypeSpecificLdpathProgramForNodeTypeProgram() throws RepositoryException {
final Node mockConfigNode = mock(Node.class);
final Node mockTypeConfigNode = mock(Node.class, Mockito.RETURNS_DEEP_STUBS);
when(mockSession.getNode(LDPathTransform.CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);
final Node mockTypeConfigNode = mock(Node.class, RETURNS_DEEP_STUBS);
when(mockSession.getNode(CONFIGURATION_FOLDER + "some-program")).thenReturn(mockConfigNode);

final NodeType mockNodeType = mock(NodeType.class);
final NodeType mockNtBase = mock(NodeType.class);
when(mockNodeType.getSupertypes()).thenReturn(new NodeType[] { mockNtBase });
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[] {});
when(mockNodeType.toString()).thenReturn("custom:type");
when(mockConfigNode.hasNode("custom:type")).thenReturn(true);
when(mockConfigNode.getNode("custom:type")).thenReturn(mockTypeConfigNode);
final InputStream mockInputStream = mock(InputStream.class);
when(mockTypeConfigNode.getNode("jcr:content").getProperty("jcr:data").getBinary().getStream()).thenReturn(mockInputStream);
final LDPathTransform nodeTypeSpecificLdpathProgramStream = LDPathTransform.getNodeTypeTransform(mockNode, "some-program");
final LDPathTransform nodeTypeSpecificLdpathProgramStream = getNodeTypeTransform(mockNode, "some-program");

assertEquals(new LDPathTransform(mockInputStream), nodeTypeSpecificLdpathProgramStream);
}
Expand All @@ -108,7 +111,7 @@ public void testGetNodeTypeSpecificLdpathProgramForSupertypeProgram() throws Rep
when(mockNodeType.toString()).thenReturn("custom:type");
when(mockNtBase.toString()).thenReturn("nt:base");
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);

when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[]{});
when(mockConfigNode.hasNode("custom:type")).thenReturn(false);

when(mockConfigNode.hasNode("nt:base")).thenReturn(true);
Expand Down

0 comments on commit 83ac74e

Please sign in to comment.