Navigation Menu

Skip to content

Commit

Permalink
Update RdfAdder and RdfRemover to use #getJcrNameForRdfNode machinery…
Browse files Browse the repository at this point in the history
… for translating a RDF resource into a JCR name
  • Loading branch information
cbeer committed May 2, 2014
1 parent 21d0b88 commit 9caddef
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 91 deletions.
64 changes: 38 additions & 26 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/rdf/JcrRdfTools.java
Expand Up @@ -505,56 +505,69 @@ public String getPropertyNameFromPredicate(final Node node, final com.hp.hpl.jen
final NamespaceRegistry namespaceRegistry =
getNamespaceRegistry.apply(node);

return getPropertyNameFromPredicate(namespaceRegistry, predicate, namespaceMapping);
return getJcrNameForRdfNode(namespaceRegistry,
predicate.getNameSpace(),
predicate.getLocalName(),
namespaceMapping);
}

/**
* Get the property name for an RDF predicate
* Get a property name for an RDF predicate
* @param predicate
* @param namespaceMapping
* @return
* @throws RepositoryException
*/
public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Property predicate,
final Map<String, String> namespaceMapping) throws RepositoryException {
public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Property predicate)
throws RepositoryException {

final NamespaceRegistry namespaceRegistry = (org.modeshape.jcr.api.NamespaceRegistry) session.getWorkspace()
.getNamespaceRegistry();
final NamespaceRegistry namespaceRegistry =
(org.modeshape.jcr.api.NamespaceRegistry) session.getWorkspace().getNamespaceRegistry();

return getPropertyNameFromPredicate(namespaceRegistry, predicate, namespaceMapping);
final Map<String, String> namespaceMapping = emptyMap();
return getJcrNameForRdfNode(namespaceRegistry,
predicate.getNameSpace(),
predicate.getLocalName(),
namespaceMapping);
}

/**
* Get a property name for an RDF predicate
* @param predicate
* Get the JCR name for the given RDF resource
* @param node
* @param resource
* @param namespaces
* @return
* @throws RepositoryException
*/
public String getPropertyNameFromPredicate(final com.hp.hpl.jena.rdf.model.Property predicate)
throws RepositoryException {


final Map<String, String> emptyNamespaceMapping = emptyMap();
return getPropertyNameFromPredicate(predicate, emptyNamespaceMapping);
public String getPropertyNameFromPredicate(final Node node,
final Resource resource,
final Map<String,String> namespaces) throws RepositoryException {
final NamespaceRegistry namespaceRegistry = getNamespaceRegistry.apply(node);
return getJcrNameForRdfNode(namespaceRegistry,
resource.getNameSpace(),
resource.getLocalName(),
namespaces);
}


/**
* Get the JCR property name for an RDF predicate
*
* @param namespaceRegistry
* @param predicate
* @param namespace
* @param localname
* @param namespaceMapping
* @return
* @throws RepositoryException
*/
public String getPropertyNameFromPredicate(final NamespaceRegistry namespaceRegistry,
final com.hp.hpl.jena.rdf.model.Property predicate,
final Map<String, String> namespaceMapping) throws RepositoryException {
public String getJcrNameForRdfNode(final NamespaceRegistry namespaceRegistry,
final String rdfNamespace,
final String rdfLocalname,
final Map<String, String> namespaceMapping)
throws RepositoryException {

final String prefix;

final String namespace =
getJcrNamespaceForRDFNamespace(predicate.getNameSpace());
getJcrNamespaceForRDFNamespace(rdfNamespace);

assert (namespaceRegistry != null);

Expand All @@ -575,11 +588,9 @@ public String getPropertyNameFromPredicate(final NamespaceRegistry namespaceRegi
}
}

final String localName = predicate.getLocalName();
final String propertyName = prefix + ":" + rdfLocalname;

final String propertyName = prefix + ":" + localName;

LOGGER.debug("Took RDF predicate {} and translated it to JCR property {}", predicate, propertyName);
LOGGER.debug("Took RDF predicate {} and translated it to JCR property {}", namespace, propertyName);

return propertyName;

Expand Down Expand Up @@ -653,4 +664,5 @@ public com.hp.hpl.jena.rdf.model.Property apply(

}
};

}
Expand Up @@ -16,25 +16,19 @@

package org.fcrepo.kernel.utils.iterators;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static org.fcrepo.kernel.rdf.JcrRdfTools.getJcrNamespaceForRDFNamespace;
import static org.fcrepo.kernel.rdf.ManagedRdf.isManagedMixin;
import static org.fcrepo.kernel.rdf.ManagedRdf.isManagedTriple;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Map;

import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;

import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
import org.fcrepo.kernel.rdf.JcrRdfTools;
import org.slf4j.Logger;
Expand Down Expand Up @@ -147,49 +141,16 @@ protected String getPropertyNameFromPredicate(final Node subjectNode,
stream.namespaces());
}

protected String getPropertyNameFromPredicate(final Node subjectNode,
final Resource predicate) throws RepositoryException {
return jcrRdfTools().getPropertyNameFromPredicate(subjectNode, predicate, stream.namespaces());
}

protected Value createValue(final Node subjectNode, final RDFNode object,
final Integer propertyType) throws RepositoryException {
return jcrRdfTools().createValue(subjectNode, object, propertyType);
}

protected String jcrMixinNameFromRdfResource(final Resource mixinResource) throws RepositoryException {

final String namespace = getJcrNamespaceForRDFNamespace(mixinResource.getNameSpace());
String namespacePrefix = null;
final Map<String, String> streamNSMap =
checkNotNull(stream().namespaces(),
"Use an empty map of namespaces, not null!");
if (streamNSMap.containsValue(namespace)) {
LOGGER.debug("Found namespace: {} in stream namespace mapping.",
namespace);
for (final Map.Entry<String, String> entry : streamNSMap.entrySet()) {
final String streamNamespace = entry.getValue();
if (namespace.equals(streamNamespace)) {
LOGGER.debug(
"Found namespace: {} in stream namespace mapping with prefix: {}.",
namespace, namespacePrefix);
namespacePrefix = entry.getKey();
}
}
} else {
try {
namespacePrefix = session().getNamespacePrefix(namespace);
LOGGER.debug(
"Found namespace: {} in repository namespace mapping with prefix: {}.",
namespace, namespacePrefix);
} catch (final NamespaceException e) {
throw new MalformedRdfException(
"Unable to resolve registered namespace for resource "
+ mixinResource.toString(), e);

}
}
final String mixinName =
namespacePrefix + ":" + mixinResource.getLocalName();
LOGGER.debug("Constructed JCR mixin name: {}", mixinName);
return mixinName;
}

protected abstract void operateOnProperty(final Statement t,
final Node subjectNode) throws RepositoryException;

Expand Down
Expand Up @@ -22,6 +22,8 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;

import org.fcrepo.kernel.exception.MalformedRdfException;
import org.fcrepo.kernel.rdf.IdentifierTranslator;
Expand Down Expand Up @@ -61,25 +63,26 @@ public RdfAdder(final IdentifierTranslator graphSubjects, final Session session,
protected void operateOnMixin(final Resource mixinResource,
final Node subjectNode) throws RepositoryException {

final String mixinName = jcrMixinNameFromRdfResource(mixinResource);
if (session().getWorkspace().getNodeTypeManager()
.hasNodeType(mixinName)) {
if (subjectNode.canAddMixin(mixinName)) {
LOGGER.debug("Adding mixin: {} to node: {}.", mixinName,
subjectNode.getPath());
subjectNode.addMixin(mixinName);
} else {
throw new MalformedRdfException(
"Could not persist triple containing type assertion:"
+ mixinResource.toString()
+ " because no such mixin/type can be added to this node: "
+ subjectNode.getPath() + "!");
}
final String mixinName = getPropertyNameFromPredicate(subjectNode, mixinResource);
if (!session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
final NodeTypeManager mgr = session().getWorkspace().getNodeTypeManager();
NodeTypeTemplate type = mgr.createNodeTypeTemplate();
type.setName(mixinName);
type.setMixin(true);
type.setQueryable(true);
mgr.registerNodeType(type, false);
}

if (subjectNode.canAddMixin(mixinName)) {
LOGGER.debug("Adding mixin: {} to node: {}.", mixinName,
subjectNode.getPath());
subjectNode.addMixin(mixinName);
} else {
throw new MalformedRdfException(
"Could not persist triple containing type assertion:"
+ mixinResource.toString()
+ " because no such mixin/type can be found in the repository!");
"Could not persist triple containing type assertion:"
+ mixinResource.toString()
+ " because no such mixin/type can be added to this node: "
+ subjectNode.getPath() + "!");
}
}

Expand Down
Expand Up @@ -60,7 +60,7 @@ public RdfRemover(final IdentifierTranslator graphSubjects, final Session sessio
protected void operateOnMixin(final Resource mixinResource,
final Node subjectNode) throws RepositoryException {

final String mixinName = jcrMixinNameFromRdfResource(mixinResource);
final String mixinName = getPropertyNameFromPredicate(subjectNode, mixinResource);
if (session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
LOGGER.debug("Removing mixin: {} from node: {}.", mixinName,
subjectNode.getPath());
Expand Down
Expand Up @@ -44,6 +44,7 @@
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;
import javax.jcr.nodetype.PropertyDefinition;

import org.fcrepo.kernel.exception.MalformedRdfException;
Expand Down Expand Up @@ -121,8 +122,8 @@ public void testAddingModelWithStreamNamespace() throws Exception {
verify(mockNode).addMixin(anyString());
}

@Test(expected = MalformedRdfException.class)
public void testAddingWithBadNamespace() throws Exception {
@Test
public void testAddingWithNotYetDefinedNamespace() throws Exception {
// we drop our stream namespace map
testStream = new RdfStream(mockTriples);
when(
Expand Down Expand Up @@ -152,11 +153,14 @@ public void testAddingWithBadMixinOnNode() throws Exception {
testAdder.operateOnMixin(mixinStmnt.getObject().asResource(), mockNode);
}

@Test(expected = MalformedRdfException.class)
@Test
public void testAddingWithBadMixinForRepo() throws Exception {
when(mockNodeTypeManager.hasNodeType(mixinShortName)).thenReturn(false);
testAdder = new RdfAdder(mockGraphSubjects, mockSession, testStream);
testAdder.operateOnMixin(mixinStmnt.getObject().asResource(), mockNode);
verify(mockNodeTypeManager).registerNodeType(mockNodeTypeTemplate, false);
verify(mockNodeTypeTemplate).setName(mixinShortName);
verify(mockNodeTypeTemplate).setMixin(true);
}

@Before
Expand Down Expand Up @@ -190,6 +194,7 @@ public void setUp() throws RepositoryException {
.thenReturn(mockNodeTypeManager);
when(mockNodeTypeManager.getNodeType(FEDORA_RESOURCE)).thenReturn(
mockNodeType);
when(mockNodeTypeManager.createNodeTypeTemplate()).thenReturn(mockNodeTypeTemplate);
when(mockNodeTypeManager.hasNodeType(mixinShortName)).thenReturn(true);
when(mockNode.getPrimaryNodeType()).thenReturn(mockNodeType);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[] {});
Expand Down Expand Up @@ -225,6 +230,9 @@ public void setUp() throws RepositoryException {
@Mock
private NodeTypeManager mockNodeTypeManager;

@Mock
private NodeTypeTemplate mockNodeTypeTemplate;

@Mock
private NodeType mockNodeType;

Expand Down

0 comments on commit 9caddef

Please sign in to comment.