Permalink
Browse files

Code factoring for JcrPropertyStatementListener and RdfAdder/Remover

- Add unit test for FedoraTypesUtils

Resolves: https://www.pivotaltracker.com/story/show/71846066
  • Loading branch information...
giuliah authored and awoods committed Sep 5, 2014
1 parent be7d793 commit 8a97cfde666457314d4b211664144cf30e116b65
@@ -394,4 +394,15 @@ public static boolean propertyContains(final Property p, final String value) thr
return value.equals(p.getString());
}
+ /**
+ * Check if there is a node of given type
+ * @param subjectNode
+ * @param mixinName
+ */
+ public static boolean nodeHasType(final Node subjectNode, final String mixinName) throws RepositoryException {
+ if (subjectNode == null) {
+ return false;
+ }
+ return getNodeTypeManager(subjectNode).hasNodeType(mixinName);
+ }
}
@@ -41,6 +41,8 @@
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.vocabulary.RDF;
import java.util.HashMap;
@@ -138,24 +140,26 @@ public void addedStatement(final Statement s) {
// special logic for handling rdf:type updates.
// if the object is an already-existing mixin, update
// the node's mixins. If it isn't, just treat it normally.
- if (s.getPredicate().equals(RDF.type)
- && s.getObject().isResource()) {
- final Resource mixinResource = s.getObject().asResource();
+ final Property property = s.getPredicate();
+ final RDFNode objectNode = s.getObject();
+ if (property.equals(RDF.type) && objectNode.isResource()) {
+ final Resource mixinResource = objectNode.asResource();
+ final String nameSpace = mixinResource.getNameSpace();
- if (mixinResource.getNameSpace().equals(LDP_NAMESPACE)) {
+ if (nameSpace.equals(LDP_NAMESPACE)) {
return;
}
try {
- final String namespacePrefix = session.getNamespacePrefix(mixinResource.getNameSpace());
+ final String namespacePrefix = session.getNamespacePrefix(nameSpace);
final String mixinName = namespacePrefix + ":" + mixinResource.getLocalName();
- if (FedoraTypesUtils.getNodeTypeManager(subjectNode).hasNodeType(mixinName)) {
+ if (FedoraTypesUtils.nodeHasType(subjectNode, mixinName)) {
if (subjectNode.canAddMixin(mixinName)) {
subjectNode.addMixin(mixinName);
} else {
- problems.add(subject, COULD_NOT_STORE_PROPERTY, s.getPredicate().getURI());
+ problems.add(subject, COULD_NOT_STORE_PROPERTY, property.getURI());
}
return;
@@ -169,18 +173,13 @@ public void addedStatement(final Statement s) {
// extract the JCR propertyName from the predicate
final String propertyName =
jcrRdfTools.getPropertyNameFromPredicate(subjectNode,
- s.getPredicate(),
- s.getModel()
- .getNsPrefixMap());
-
- if (validateModificationsForPropertyName(
- subject, subjectNode, s.getPredicate())) {
- final Value v =
- jcrRdfTools.createValue(subjectNode, s.getObject(),
- propertiesTools.getPropertyType(subjectNode,
- propertyName));
- propertiesTools.appendOrReplaceNodeProperty(subjects,
- subjectNode, propertyName, v);
+ property,
+ s.getModel().getNsPrefixMap());
+
+ if (validateModificationsForPropertyName(subject, subjectNode, property)) {
+ final Value v = createValue(subjectNode, objectNode, propertyName);
+
+ propertiesTools.appendOrReplaceNodeProperty(subjects, subjectNode, propertyName, v);
}
} catch (final RepositoryException e) {
throw propagate(e);
@@ -210,31 +209,35 @@ public void removedStatement(final Statement s) {
// special logic for handling rdf:type updates.
// if the object is an already-existing mixin, update
// the node's mixins. If it isn't, just treat it normally.
- if (s.getPredicate().equals(RDF.type)
- && s.getObject().isResource()) {
- final Resource mixinResource = s.getObject().asResource();
+ final Property property = s.getPredicate();
+ final RDFNode objectNode = s.getObject();
+
+ if (property.equals(RDF.type) && objectNode.isResource()) {
+ final Resource mixinResource = objectNode.asResource();
+ final String nameSpace = mixinResource.getNameSpace();
final String errorPrefix = "Error removing node type";
try {
- final String namespacePrefix = session.getNamespacePrefix(mixinResource.getNameSpace());
+ final String namespacePrefix = session.getNamespacePrefix(nameSpace);
final String mixinName = namespacePrefix + ":" + mixinResource.getLocalName();
- if (FedoraTypesUtils.getNodeTypeManager(subjectNode).hasNodeType(mixinName)) {
+ if (FedoraTypesUtils.nodeHasType(subjectNode, mixinName)) {
try {
subjectNode.removeMixin(mixinName);
} catch (final RepositoryException e) {
LOGGER.info(
"problem with removing <{}> <{}> <{}>: {}",
subject.getURI(),
RdfLexicon.COULD_NOT_STORE_PROPERTY,
- s.getPredicate().getURI(),
+ property.getURI(),
e);
String errorMessage = e.getMessage();
+ final String className = e.getClass().getName();
if (StringUtils.isNotBlank(errorMessage)) {
errorMessage = errorPrefix + " '" + mixinName +
- "': \n" + e.getClass().getName() + ": " + errorMessage;
+ "': \n" + className + ": " + errorMessage;
} else {
errorMessage = errorPrefix + " '" + mixinName +
- "': \n" + e.getClass().getName();
+ "': \n" + className;
}
problems.add(subject, RdfLexicon.COULD_NOT_STORE_PROPERTY, errorMessage);
}
@@ -245,29 +248,27 @@ public void removedStatement(final Statement s) {
LOGGER.trace("Unable to resolve registered namespace for resource {}: {}", mixinResource, e);
String errorMessage = e.getMessage();
+ final String className = e.getClass().getName();
if (StringUtils.isNotBlank(errorMessage)) {
errorMessage = errorPrefix + " " +
- e.getClass().getName() + ": " + errorMessage;
+ className + ": " + errorMessage;
} else {
- errorMessage = errorPrefix + ": " + e.getClass().getName();
+ errorMessage = errorPrefix + ": " + className;
}
problems.add(subject, RdfLexicon.COULD_NOT_STORE_PROPERTY, errorMessage);
}
}
final String propertyName =
- jcrRdfTools.getPropertyNameFromPredicate(subjectNode, s.getPredicate());
+ jcrRdfTools.getPropertyNameFromPredicate(subjectNode, property);
// if the property doesn't exist, we don't need to worry about it.
if (subjectNode.hasProperty(propertyName) &&
- validateModificationsForPropertyName(subject, subjectNode,
- s.getPredicate())) {
- final Value v =
- jcrRdfTools.createValue(subjectNode, s.getObject(),
- propertiesTools.getPropertyType(subjectNode,
- propertyName));
+ validateModificationsForPropertyName(subject, subjectNode, property) ) {
+ final Value v = createValue(subjectNode, objectNode, propertyName);
+
propertiesTools.removeNodeProperty(subjects, subjectNode,
propertyName, v);
}
@@ -292,6 +293,14 @@ private boolean validateModificationsForPropertyName(
return true;
}
+ private Value createValue(final Node subjectNode,
+ final RDFNode RDFNode,
+ final String propertyName) throws RepositoryException {
+ return jcrRdfTools.createValue(subjectNode,
+ RDFNode,
+ propertiesTools.getPropertyType(subjectNode, propertyName));
+ }
+
/**
* Get a list of any problems from trying to apply the statement changes to
* the node's properties
@@ -23,6 +23,7 @@
import static org.fcrepo.kernel.impl.rdf.ManagedRdf.isManagedMixin;
import static org.fcrepo.kernel.impl.rdf.ManagedRdf.isManagedTriple;
import static org.slf4j.LoggerFactory.getLogger;
+import org.fcrepo.kernel.impl.utils.NodePropertiesTools;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
@@ -186,6 +187,19 @@ protected Value createValue(final Node subjectNode, final RDFNode object,
return jcrRdfTools().createValue(subjectNode, object, propertyType);
}
+
+ protected Value createValue(final Node n, final Statement t, final String propertyName) throws RepositoryException {
+ final NodePropertiesTools propertiesTools = new NodePropertiesTools();
+ return jcrRdfTools().createValue(n, t.getObject(), propertiesTools.getPropertyType(n, propertyName));
+ }
+
+ protected boolean sessionHasType(final Session session, final String mixinName) throws RepositoryException {
+ if (session == null) {
+ return false;
+ }
+ return session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName);
+ }
+
protected abstract void operateOnProperty(final Statement t,
final Node subjectNode) throws RepositoryException;
@@ -64,7 +64,7 @@ protected void operateOnMixin(final Resource mixinResource,
final Node subjectNode) throws RepositoryException {
final String mixinName = getPropertyNameFromPredicate(subjectNode, mixinResource);
- if (!session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
+ if (!sessionHasType(session(), mixinName)) {
final NodeTypeManager mgr = session().getWorkspace().getNodeTypeManager();
final NodeTypeTemplate type = mgr.createNodeTypeTemplate();
type.setName(mixinName);
@@ -94,11 +94,10 @@ protected void operateOnMixin(final Resource mixinResource,
protected void operateOnProperty(final Statement t, final Node n) throws RepositoryException {
LOGGER.debug("Adding property from triple: {} to node: {}.", t, n
.getPath());
+
final String propertyName =
getPropertyNameFromPredicate(n, t.getPredicate());
- final Value v =
- createValue(n, t.getObject(), propertiesTools.getPropertyType(n,
- propertyName));
+ final Value v = createValue(n, t, propertyName);
propertiesTools.appendOrReplaceNodeProperty(idTranslator(), n, propertyName, v);
}
}
@@ -61,7 +61,7 @@ protected void operateOnMixin(final Resource mixinResource,
final Node subjectNode) throws RepositoryException {
final String mixinName = getPropertyNameFromPredicate(subjectNode, mixinResource);
- if (session().getWorkspace().getNodeTypeManager().hasNodeType(mixinName)) {
+ if (sessionHasType(session(), mixinName)) {
LOGGER.debug("Removing mixin: {} from node: {}.", mixinName,
subjectNode.getPath());
@@ -84,14 +84,12 @@ protected void operateOnProperty(final Statement t, final Node n)
throws RepositoryException {
LOGGER.debug("Trying to remove property from triple: {} on node: {}.", t, n
.getPath());
+
final String propertyName =
getPropertyNameFromPredicate(n, t.getPredicate());
if (n.hasProperty(propertyName)) {
- final Value v =
- jcrRdfTools().createValue(n, t.getObject(),
- propertiesTools.getPropertyType(n, propertyName));
- propertiesTools.removeNodeProperty(idTranslator(), n, propertyName,
- v);
+ final Value v = createValue(n, t, propertyName);
+ propertiesTools.removeNodeProperty(idTranslator(), n, propertyName, v);
}
}
}
@@ -28,6 +28,7 @@
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.isFedoraResource;
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.isInternalNode;
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.isMultipleValuedProperty;
+import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.nodeHasType;
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.propertyContains;
import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.value2string;
import static org.fcrepo.kernel.impl.utils.NodePropertiesTools.REFERENCE_PROPERTY_SUFFIX;
@@ -383,4 +384,35 @@ public void testMultiValuedPropertyContains() throws RepositoryException {
assertTrue(propertyContains(mockProperty, "some-string"));
assertFalse(propertyContains(mockProperty, "some-other-string"));
}
+
+ @Test
+ public void testNodeHasTypeNo() throws RepositoryException {
+ when(mockNode.getSession()).thenReturn(mockSession);
+ when(mockSession.getWorkspace()).thenReturn(mockWS);
+ when(mockWS.getNodeTypeManager()).thenReturn(mockNTM);
+ assertFalse(nodeHasType(mockNode, "mixin"));
+ }
+
+ @Test
+ public void testNodeHasTypeYes() throws RepositoryException {
+ when(mockNode.getSession()).thenReturn(mockSession);
+ when(mockSession.getWorkspace()).thenReturn(mockWS);
+ when(mockWS.getNodeTypeManager()).thenReturn(mockNTM);
+ when(mockNTM.hasNodeType("mixin")).thenReturn(true);
+ assertTrue(nodeHasType(mockNode, "mixin"));
+ }
+
+ @Test
+ public void testNodeHasTypeNullMixin() throws RepositoryException {
+ when(mockNode.getSession()).thenReturn(mockSession);
+ when(mockSession.getWorkspace()).thenReturn(mockWS);
+ when(mockWS.getNodeTypeManager()).thenReturn(mockNTM);
+ assertFalse(nodeHasType(mockNode, null));
+ }
+
+ @Test
+ public void testNodeHasTypeNull() throws RepositoryException {
+ assertFalse(nodeHasType(null, "mixin"));
+ }
+
}

0 comments on commit 8a97cfd

Please sign in to comment.