Skip to content

Commit

Permalink
Adding topic URI to dataset context and velocity template, using to f…
Browse files Browse the repository at this point in the history
…ix template choice
  • Loading branch information
escowles committed May 22, 2013
1 parent eca6d4a commit 4a8d3bf
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
24 changes: 22 additions & 2 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraNodes.java
Expand Up @@ -53,13 +53,17 @@
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.FedoraJcrTypes;
import org.fcrepo.utils.JcrRdfTools;
import org.modeshape.common.collection.Problems;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.sparql.util.Symbol;
import com.hp.hpl.jena.update.UpdateAction;

@Component
Expand Down Expand Up @@ -102,8 +106,24 @@ public Dataset describe(@PathParam("path")
throw new WebApplicationException(builder.cacheControl(cc)
.lastModified(date).build());
}
return resource.getGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)).toDataset();

//return resource.getGraphStore(new HttpGraphSubjects(FedoraNodes.class, uriInfo)).toDataset();

// store the topic uri in the dataset context
HttpGraphSubjects subjects =
new HttpGraphSubjects(FedoraNodes.class, uriInfo);
Dataset dataset = resource.getGraphStore(subjects).toDataset();
String uri = null;
try {
uri = JcrRdfTools.getGraphSubject(
subjects,session.getNode(path)).getURI();
} catch ( Exception ex ) { uri = null; }

com.hp.hpl.jena.sparql.util.Context context =
dataset.getContext();
if ( context == null ) { context = new com.hp.hpl.jena.sparql.util.Context(); }
context.set(Symbol.create("uri"),uri);

return dataset;
} finally {
session.logout();
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_HTML_TYPE;
import static org.fcrepo.responses.RdfSerializationUtils.getFirstValueForPredicate;
import static org.fcrepo.responses.RdfSerializationUtils.getDatasetSubject;
import static org.fcrepo.responses.RdfSerializationUtils.primaryTypePredicate;
import static org.fcrepo.responses.RdfSerializationUtils.setCachingHeaders;
import static org.slf4j.LoggerFactory.getLogger;
Expand Down Expand Up @@ -147,14 +148,16 @@ public void writeTo(final Dataset rdf, final Class<?> type,
WebApplicationException {

logger.debug("Writing an HTML response for: {}", rdf);
logger.trace("Attempting to discover our subject");
Node subject = getDatasetSubject(rdf);

// add standard headers
httpHeaders.put("Content-type", of((Object) TEXT_HTML));
setCachingHeaders(httpHeaders, rdf);

logger.trace("Attempting to discover the primary type of the node for the resource in question...");
final String nodeType =
getFirstValueForPredicate(rdf, primaryTypePredicate);
getFirstValueForPredicate(rdf, subject, primaryTypePredicate);
logger.debug("Found primary node type: {}", nodeType);
final Template nodeTypeTemplate = templatesMap.get(nodeType);
logger.debug("Choosing template: {}", nodeTypeTemplate.getName());
Expand All @@ -163,6 +166,7 @@ public void writeTo(final Dataset rdf, final Class<?> type,
context.put("rdf", rdf.asDatasetGraph());
context.put("subjects", rdf.getDefaultModel().listSubjects());
context.put("nodeany", Node.ANY);
context.put("topic", subject);

// the contract of MessageBodyWriter<T> is _not_ to close the stream
// after writing to it
Expand Down
Expand Up @@ -18,6 +18,8 @@

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.sparql.util.Context;
import com.hp.hpl.jena.sparql.util.Symbol;
import com.hp.hpl.jena.sparql.core.Quad;

public class RdfSerializationUtils {
Expand All @@ -42,9 +44,9 @@ public class RdfSerializationUtils {
forPattern("EEE, dd MMM yyyy HH:mm:ss Z");

static String getFirstValueForPredicate(final Dataset rdf,
final Node predicate) {
final Node subject, final Node predicate) {
final Iterator<Quad> statements =
rdf.asDatasetGraph().find(ANY, ANY, predicate, ANY);
rdf.asDatasetGraph().find(ANY, subject, predicate, ANY);
// we'll take the first one we get
if (statements.hasNext()) {
final Quad statement = statements.next();
Expand All @@ -55,7 +57,13 @@ static String getFirstValueForPredicate(final Dataset rdf,
logger.trace("No value found for predicate: {}", predicate);
return null;
}

}
static Node getDatasetSubject(final Dataset rdf) {
Context context = rdf.getContext();
String uri = context.getAsString(Symbol.create("uri"));
logger.debug("uri from context: {}", uri);
if ( uri != null ) { return createURI(uri); }
else { return null; }
}

static void
Expand All @@ -66,7 +74,8 @@ static String getFirstValueForPredicate(final Dataset rdf,

logger.trace("Attempting to discover the last-modified date of the node for the resource in question...");
final String lastModifiedinXSDStyle =
getFirstValueForPredicate(rdf, lastModifiedPredicate);
getFirstValueForPredicate(rdf, getDatasetSubject(rdf),
lastModifiedPredicate);
if (lastModifiedinXSDStyle != null) {
logger.debug("Found last-modified date: {}", lastModifiedinXSDStyle);
final String lastModified =
Expand Down
72 changes: 72 additions & 0 deletions fcrepo-http-commons/src/main/resources/views/mode:root.vsl
@@ -0,0 +1,72 @@
#* @vtlvariable name="rdf" type="com.hp.hpl.jena.sparql.core.DatasetGraph" *#
#* @vtlvariable name="subjects" type="com.hp.hpl.jena.rdf.model.ResIterator" *#
#* @vtlvariable name="nodeany" type="com.hp.hpl.jena.graph.Node" *#
#* @vtlvariable name="topic" type="com.hp.hpl.jena.graph.Node" *#
<html>
<head>
<title>mode:root profile</title>
</head>

<body>
<h1>mode:root: $topic</h1>

## output triples for the topic node
#triples($topic)

## output other nodes
#foreach($subject in $subjects)
#if( $subject != $topic )
#triples($subject)
#end
#end

#*
<div>
#if($subject.isURIResource())
<h2><a href="$subject.getURI()">$subject</a></h2>
#else
<h2>$subject</h2>
#end

<dl>
#foreach($quad in $rdf.find($nodeany, $subject.asNode(), $nodeany, $nodeany))
<dt>$quad.getPredicate()</dt>

<dd>
#if($quad.getObject().isURIResource())
<a href="$quad.getObject().getURI()">$quad.getObject()</a>
#else
$quad.getObject()
#end
</dd>
#end
</dl>
</div>
#end
*#
</body>

</html>
#macro( triples $sub )
<div>
#if($sub.isURIResource())
<h2><a href="$sub.getURI()">$sub</a></h2>
#else
<h2>$sub</h2>
#end

<dl>
#foreach($quad in $rdf.find($nodeany, $sub.asNode(), $nodeany, $nodeany))
<dt>$quad.getPredicate()</dt>

<dd>
#if($quad.getObject().isURIResource())
<a href="$quad.getObject().getURI()">$quad.getObject()</a>
#else
$quad.getObject()
#end
</dd>
#end
</dl>
</div>
#end
@@ -1,6 +1,7 @@

package org.fcrepo.responses;

import static com.hp.hpl.jena.graph.Node.ANY;
import static com.hp.hpl.jena.graph.Node.createLiteral;
import static com.hp.hpl.jena.graph.Node.createURI;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
Expand Down Expand Up @@ -33,9 +34,9 @@ public class RdfSerializationUtilsTest {
@Test
public void testGetFirstValueForPredicate() {
final String foundValue =
getFirstValueForPredicate(testData, createURI("test:predicate"));
getFirstValueForPredicate(testData,ANY,createURI("test:predicate"));
assertEquals("Didn't find correct value for predicate!", foundValue,
"test:object");
"test:object");
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit 4a8d3bf

Please sign in to comment.