Skip to content

Commit

Permalink
RDF triples from version info now returned from iterating components
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 15, 2013
1 parent 00a0af8 commit edccdc0
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 278 deletions.
10 changes: 5 additions & 5 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/FedoraObject.java
Expand Up @@ -16,6 +16,8 @@
package org.fcrepo.kernel;

import static org.fcrepo.kernel.utils.FedoraTypesUtils.isFedoraObject;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.NT_FOLDER;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.Node;
Expand Down Expand Up @@ -72,14 +74,12 @@ private void mixinTypeSpecificCrap() {
try {
if (node.isNew() || !hasMixin(node)) {
logger.debug("Setting {} properties on a {} node {}...",
FEDORA_OBJECT,
JcrConstants.NT_FOLDER,
node.getPath());
FEDORA_OBJECT, NT_FOLDER, node.getPath());
node.addMixin(FEDORA_OBJECT);
}
} catch (RepositoryException e) {
} catch (final RepositoryException e) {
logger.warn("Could not decorate {} with {} properties: {} ",
JcrConstants.JCR_CONTENT, FEDORA_OBJECT, e);
JCR_CONTENT, FEDORA_OBJECT, e);
}
}

Expand Down
Expand Up @@ -37,7 +37,6 @@
import org.fcrepo.kernel.rdf.GraphProperties;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.impl.JcrGraphProperties;
import org.fcrepo.kernel.utils.FedoraTypesUtils;
import org.fcrepo.kernel.utils.JcrRdfTools;
import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.jcr.api.JcrTools;
Expand Down Expand Up @@ -236,7 +235,7 @@ public Dataset updatePropertiesDataset(final GraphSubjects subjects,
* @throws RepositoryException
*/
public Dataset getPropertiesDataset(final GraphSubjects subjects,
final long offset, final int limit)
final long offset, final int limit)
throws RepositoryException {

if (this.properties != null) {
Expand Down Expand Up @@ -265,12 +264,14 @@ public Dataset getPropertiesDataset(final GraphSubjects subjects)
*/
public Dataset getVersionDataset(final GraphSubjects subjects)
throws RepositoryException {
final Model model = JcrRdfTools.withContext(subjects, node.getSession()).getJcrPropertiesModel(FedoraTypesUtils.getVersionHistory(node), subjects.getGraphSubject(node));
final Model model =
JcrRdfTools.withContext(subjects, node.getSession())
.getJcrVersionPropertiesModel(node);

final Dataset dataset = DatasetFactory.create(model);

String uri = subjects.getGraphSubject(node).getURI();
com.hp.hpl.jena.sparql.util.Context context = dataset.getContext();
final String uri = subjects.getGraphSubject(node).getURI();
final com.hp.hpl.jena.sparql.util.Context context = dataset.getContext();
context.set(GraphProperties.URI_SYMBOL,uri);

return dataset;
Expand Down
Expand Up @@ -66,25 +66,28 @@ public NamespaceContext(final Session session) throws RepositoryException {
final ImmutableCollection.Builder<Triple> nsTriples =
ImmutableSet.builder();
for (String prefix : namespaceRegistry.getPrefixes()) {
final String nsURI = namespaceRegistry.getURI(prefix);
if (prefix.equals("jcr")) {
prefix = "fcrepo";
if (!prefix.isEmpty()) {
final String nsURI = namespaceRegistry.getURI(prefix);
if (prefix.equals("jcr")) {
prefix = "fcrepo";
}
LOGGER.debug(
"Discovered namespace prefix \"{}\" with URI \"{}\"",
prefix, nsURI);
final String rdfNsUri = getRDFNamespaceForJcrNamespace(nsURI);
// first, let's put the namespace in context
namespaces.put(prefix, rdfNsUri);
LOGGER.debug("Added namespace prefix \"{}\" with URI \"{}\"",
prefix, rdfNsUri);
final Node nsSubject = createURI(rdfNsUri);
// now, some triples describing this namespace
nsTriples.add(create(nsSubject, type.asNode(), VOAF_VOCABULARY
.asNode()));
nsTriples.add(create(nsSubject, HAS_NAMESPACE_PREFIX.asNode(),
createLiteral(prefix)));
nsTriples.add(create(nsSubject, HAS_NAMESPACE_URI.asNode(),
createLiteral(nsURI)));
}
LOGGER.debug("Discovered namespace prefix \"{}\" with URI \"{}\"",
prefix, nsURI);
final String rdfNsUri = getRDFNamespaceForJcrNamespace(nsURI);
// first, let's put the namespace in context
namespaces.put(prefix, rdfNsUri);
LOGGER.debug("Added namespace prefix \"{}\" with URI \"{}\"",
prefix, rdfNsUri);
final Node nsSubject = createURI(rdfNsUri);
// now, some triples describing this namespace
nsTriples.add(create(nsSubject, type.asNode(), VOAF_VOCABULARY
.asNode()));
nsTriples.add(create(nsSubject, HAS_NAMESPACE_PREFIX.asNode(),
createLiteral(prefix)));
nsTriples.add(create(nsSubject, HAS_NAMESPACE_URI.asNode(),
createLiteral(nsURI)));
}
context.concat(nsTriples.build()).addNamespaces(namespaces.build());
}
Expand Down
@@ -0,0 +1,93 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.kernel.rdf.impl;

import static com.google.common.collect.ImmutableSet.builder;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.Triple.create;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION_LABEL;

import java.util.Iterator;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;

import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.NodeRdfContext;
import org.fcrepo.kernel.services.LowLevelStorageService;

import com.google.common.collect.ImmutableSet;
import com.hp.hpl.jena.graph.Triple;


/**
* An {@link NodeRdfContext} that supplies {@link Triple}s concerning
* the versions of a selected {@link Node}.
*
* @author ajs6f
* @date Oct 15, 2013
*/
public class VersionsRdfContext extends NodeRdfContext {

/**
* Ordinary constructor.
*
* @param node
* @param graphSubjects
* @param lowLevelStorageService
* @throws RepositoryException
*/
public VersionsRdfContext(final Node node, final GraphSubjects graphSubjects,
final LowLevelStorageService lowLevelStorageService)
throws RepositoryException {
super(node, graphSubjects, lowLevelStorageService);
context().concat(versionTriples());
}

private Iterator<Triple> versionTriples() throws RepositoryException {
final VersionHistory versionHistory =
node().getSession().getWorkspace().getVersionManager()
.getVersionHistory(node().getPath());

final VersionIterator versionIterator = versionHistory.getAllVersions();
final ImmutableSet.Builder<Triple> b = builder();
while (versionIterator.hasNext()) {
final Version version = versionIterator.nextVersion();
final Node frozenNode = version.getFrozenNode();
final com.hp.hpl.jena.graph.Node versionSubject =
graphSubjects().getGraphSubject(frozenNode).asNode();

b.add(create(subject(), HAS_VERSION.asNode(), versionSubject));

final String[] versionLabels =
versionHistory.getVersionLabels(version);
for (final String label : versionLabels) {
b.add(create(versionSubject, HAS_VERSION_LABEL.asNode(), createLiteral(label)));
}
context().concat(
new PropertiesRdfContext(frozenNode, graphSubjects(),
lowLevelStorageService()).context());

}
return b.build().iterator();
}


}
Expand Up @@ -68,24 +68,23 @@ public PropertyToTriple(final GraphSubjects graphSubjects) {
}

/**
* This nightmare of Java verbosity is a curried transformation. We really
* want to go from an iterator of JCR {@link Properties} to an iterator of
* RDF {@link Triple}s. But we run into a thorny annoyance: some properties
* may produce several triples (multi-valued properties). So we cannot
* expect to find a simple Property -> Triple mapping. Instead, we wax much
* too clever and offer here a function from any specific property to a new
* function, one that takes multiple values (such as occur in our
* multi-valued properties) to multiple triples. In other words, this is a
* function the outputs of which are functions specific to a given JCR
* property. Each output knows how to take any specific value of its
* specific property to a triple representing the fact that its specific
* property obtains that specific value on some node. All of this is useful
* because with these operations represented as functions instead of
* ordinary methods, which may have side-effects, we can use elegant,
* efficient machinery to manipulate iterators of the objects in which we
* are interested, and that's exactly what we want to do in this class. See
* {@link PropertiesRdfContext#triplesFromProperties} for an example of the
* use of this class with {@link ZippingIterator}.
* This nightmare of Java signature verbosity is a curried transformation.
* We want to go from an iterator of JCR {@link Properties} to an iterator
* of RDF {@link Triple}s. An annoyance: some properties may produce several
* triples (multi-valued properties). So we cannot find a simple Property ->
* Triple mapping. Instead, we wax clever and offer a function from any
* specific property to a new function, one that takes multiple values (such
* as occur in our multi-valued properties) to multiple triples. In other
* words, this is a function the outputs of which are functions specific to
* a given JCR property. Each output knows how to take any specific value of
* its specific property to a triple representing the fact that its specific
* property obtains that specific value on the node to which that property
* belongs. All of this is useful because with these operations represented
* as functions instead of ordinary methods, which may have side-effects, we
* can use efficient machinery to manipulate iterators of the objects in
* which we are interested, and that's exactly what we want to do in this
* class. See {@link PropertiesRdfContext#triplesFromProperties} for an
* example of the use of this class with {@link ZippingIterator}.
*
* @see <a href="http://en.wikipedia.org/wiki/Currying">Currying</a>
*/
Expand Down Expand Up @@ -117,10 +116,11 @@ public Triple apply(final Value v) {
* @return An RDF {@link Triple} representing that property.
*/
private Triple propertyvalue2triple(final Property p, final Value v) {
LOGGER.debug("Rendering triple for Property: {} with Value: {}", p, v);
try {
final Triple triple =
create(getGraphSubject(p.getNode()), getPredicateForProperty
.apply(p).asNode(), propertyvalue2node(p, p.getValue()));
create(getGraphSubject(p.getParent()), getPredicateForProperty
.apply(p).asNode(), propertyvalue2node(p, v));
LOGGER.debug("Created triple: {} ", triple);
return triple;
} catch (final RepositoryException e) {
Expand All @@ -145,12 +145,10 @@ private Node propertyvalue2node(final Property p, final Value v) {
return createResource(v.getString()).asNode();
case REFERENCE:
case WEAKREFERENCE:
final javax.jcr.Node refNode =
p.getSession().getNodeByIdentifier(v.getString());
return getGraphSubject(refNode);
case PATH:
return getGraphSubject(p.getSession()
.getNode(v.getString()));
final javax.jcr.Node refNode = p.getNode();
return getGraphSubject(refNode);

default:
return literal2node(v.getString());
}
Expand Down

0 comments on commit edccdc0

Please sign in to comment.