Skip to content

Commit

Permalink
Added extra triples associated to root node
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 14, 2013
1 parent b37e182 commit 48e8858
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
Expand Up @@ -17,34 +17,58 @@
package org.fcrepo.kernel.rdf.impl;

import static com.google.common.base.Predicates.not;
import static com.google.common.collect.ImmutableSet.builder;
import static com.google.common.collect.Iterators.concat;
import static com.google.common.collect.Iterators.filter;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.NodeFactory.createURI;
import static com.hp.hpl.jena.graph.Triple.create;
import static org.fcrepo.jcr.FedoraJcrTypes.ROOT;
import static org.fcrepo.kernel.RdfLexicon.HAS_CONTENT;
import static org.fcrepo.kernel.RdfLexicon.HAS_FIXITY_CHECK_COUNT;
import static org.fcrepo.kernel.RdfLexicon.HAS_FIXITY_ERROR_COUNT;
import static org.fcrepo.kernel.RdfLexicon.HAS_FIXITY_REPAIRED_COUNT;
import static org.fcrepo.kernel.RdfLexicon.HAS_LOCATION;
import static org.fcrepo.kernel.RdfLexicon.HAS_NODE_TYPE;
import static org.fcrepo.kernel.RdfLexicon.HAS_OBJECT_COUNT;
import static org.fcrepo.kernel.RdfLexicon.HAS_OBJECT_SIZE;
import static org.fcrepo.kernel.RdfLexicon.IS_CONTENT_OF;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getRepositoryCount;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getRepositorySize;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.isBinaryProperty;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.property2values;
import static org.fcrepo.metrics.RegistryService.getMetrics;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.NodeTypeManager;

import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.rdf.NodeRdfContext;
import org.fcrepo.kernel.rdf.impl.mappings.PropertyToTriple;
import org.fcrepo.kernel.rdf.impl.mappings.ZippingIterator;
import org.fcrepo.kernel.services.LowLevelStorageService;
import org.fcrepo.kernel.services.functions.GetClusterConfiguration;
import org.fcrepo.kernel.utils.LowLevelCacheEntry;
import org.fcrepo.kernel.utils.iterators.PropertyIterator;
import org.modeshape.jcr.JcrRepository;
import org.slf4j.Logger;

import com.codahale.metrics.Counter;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import com.hp.hpl.jena.graph.Node;
Expand Down Expand Up @@ -81,7 +105,9 @@ public PropertiesRdfContext(final javax.jcr.Node node,

private void putPropertiesIntoContext() throws RepositoryException {

LOGGER.debug("Pushing RDF triples into context for properties of node: {}", node());
LOGGER.debug(
"Pushing RDF triples into context for properties of node: {}",
node());

// this node's own properties
context().concat(triplesFromProperties(node()));
Expand Down Expand Up @@ -122,6 +148,79 @@ public Triple apply(
}));

}

if (node().getPrimaryNodeType().getName().equals(ROOT)) {
context().concat(triplesForRootNode());
}

}

private Set<Triple> triplesForRootNode() throws RepositoryException {
// a rdf description of the root node
LOGGER.debug("Creating RDF triples for repository description");
final Repository repository = node().getSession().getRepository();
// retrieve the metrics from the service
final SortedMap<String, Counter> counters = getMetrics().getCounters();
final ImmutableSet.Builder<Triple> b = builder();
final Node subject = graphSubjects().getGraphSubject(node()).asNode();
for (final String key : repository.getDescriptorKeys()) {
final String descriptor = repository.getDescriptor(key);
if (descriptor != null) {
final String uri = REPOSITORY_NAMESPACE + "repository/" + key;
b.add(create(subject, createURI(uri), createLiteral(descriptor)));
}
}
final NodeTypeManager nodeTypeManager =
node().getSession().getWorkspace().getNodeTypeManager();

final NodeTypeIterator nodeTypes = nodeTypeManager.getAllNodeTypes();
while (nodeTypes.hasNext()) {
final NodeType nodeType = nodeTypes.nextNodeType();
b.add(create(subject, HAS_NODE_TYPE.asNode(),
createLiteral(nodeType.getName())));
}

b.add(create(subject, HAS_OBJECT_COUNT.asNode(), createLiteral(String
.valueOf(getRepositoryCount(repository)))));
b.add(create(subject, HAS_OBJECT_SIZE.asNode(), createLiteral(String
.valueOf(getRepositorySize(repository)))));
// Get the cluster configuration for the RDF response, if available
// this ugly test checks to see whether this is an ordinary JCR repository
// or a ModeShape repo, which will possess the extra info
if (JcrRepository.class.isAssignableFrom(repository.getClass())) {
final Map<String, String> config =
new GetClusterConfiguration().apply(repository);
assert (config != null);

for (final Map.Entry<String, String> entry : config.entrySet()) {
b.add(create(subject, createURI(REPOSITORY_NAMESPACE
+ entry.getKey()), createLiteral(entry.getValue())));
}
}

// and add the repository metrics to the RDF model
if (counters.containsKey("LowLevelStorageService.fixity-check-counter")) {
b.add(create(subject, HAS_FIXITY_CHECK_COUNT.asNode(),
createLiteral(String.valueOf(counters.get(
"org.fcrepo.services." + "LowLevelStorageService."
+ "fixity-check-counter").getCount()))));
}

if (counters.containsKey("LowLevelStorageService.fixity-error-counter")) {
b.add(create(subject, HAS_FIXITY_ERROR_COUNT.asNode(),
createLiteral(String.valueOf(counters.get(
"org.fcrepo.services." + "LowLevelStorageService."
+ "fixity-error-counter").getCount()))));
}

if (counters
.containsKey("LowLevelStorageService.fixity-repaired-counter")) {
b.add(create(subject, HAS_FIXITY_REPAIRED_COUNT.asNode(),
createLiteral(String.valueOf(counters.get(
"org.fcrepo.services." + "LowLevelStorageService."
+ "fixity-repaired-counter").getCount()))));
}
return b.build();
}

private
Expand Down
Expand Up @@ -290,11 +290,6 @@ public void testGetPropertiesModelForRootNode() throws RepositoryException {
actual.createProperty(REPOSITORY_NAMESPACE +
"repository/some-descriptor-key"),
actual.createLiteral("some-descriptor-value")));
assertTrue(actual
.contains(
testSubjects.getGraphSubject(mockNode),
actual.createProperty(REPOSITORY_NAMESPACE + "a"),
actual.createLiteral("b")));
}

@Test
Expand Down

0 comments on commit 48e8858

Please sign in to comment.