Skip to content

Commit

Permalink
Moved iterating RDF generation to the outer layer of JcrRdfTools
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 25, 2013
1 parent f63bf3c commit 6a22fd7
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 122 deletions.
Expand Up @@ -85,7 +85,7 @@ public class FedoraRepositoryWorkspaces extends AbstractResource {
public Dataset getWorkspaces() throws RepositoryException {

final Model workspaceModel =
JcrRdfTools.withContext(null, session).getJcrPropertiesModel();
JcrRdfTools.withContext(null, session).getJcrPropertiesModel().asModel();

final String[] workspaces =
session.getWorkspace().getAccessibleWorkspaceNames();
Expand Down
Expand Up @@ -267,7 +267,7 @@ public Dataset getVersionDataset(final GraphSubjects subjects)
throws RepositoryException {
final Model model =
JcrRdfTools.withContext(subjects, node.getSession())
.getJcrVersionPropertiesModel(node);
.getJcrVersionPropertiesModel(node).asModel();

final Dataset dataset = DatasetFactory.create(model);

Expand Down
@@ -0,0 +1,33 @@
/**
* 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.exception;

import javax.jcr.RepositoryException;

/**
* Represents the case where a property definition has been requested but does
* not exist. Typically, this happens when a new property is added to a node
* that does not restrict its property types.
*
* @author ajs6f
* @date Oct 25, 2013
*/
public class NoSuchPropertyDefinitionException extends RepositoryException {

private static final long serialVersionUID = 1L;

}
Expand Up @@ -49,8 +49,8 @@ public Dataset getProperties(final Node node, final GraphSubjects subjects,
final long offset, final int limit)
throws RepositoryException {
final JcrRdfTools jcrRdfTools = JcrRdfTools.withContext(subjects, node.getSession());
final Model model = jcrRdfTools.getJcrPropertiesModel(node);
final Model treeModel = jcrRdfTools.getJcrTreeModel(node, offset, limit);
final Model model = jcrRdfTools.getJcrPropertiesModel(node).asModel();
final Model treeModel = jcrRdfTools.getJcrTreeModel(node, offset, limit).asModel();
final Model problemModel = JcrRdfTools.getProblemsModel();

final JcrPropertyStatementListener listener =
Expand Down
Expand Up @@ -193,8 +193,9 @@ public Dataset getFixityResultsModel(final GraphSubjects subjects,
runFixityAndFixProblems(datastream);

final Model model =
JcrRdfTools.withContext(subjects, datastream.getNode().getSession()).getJcrPropertiesModel(datastream
.getNode(), blobs);
JcrRdfTools
.withContext(subjects, datastream.getNode().getSession())
.getJcrPropertiesModel(datastream.getNode(), blobs).asModel();

final Dataset dataset = GraphStoreFactory.create(model).toDataset();

Expand Down
Expand Up @@ -71,7 +71,7 @@

/**
* Repository-global helper methods
*
*
* @author Chris Beer
* @date Mar 11, 2013
*/
Expand All @@ -88,7 +88,7 @@ public class RepositoryService extends JcrTools implements FedoraJcrTypes {

/**
* Test whether a node exists in the JCR store
*
*
* @param path
* @return whether a node exists at the given path
* @throws RepositoryException
Expand All @@ -100,7 +100,7 @@ public boolean exists(final Session session, final String path)

/**
* Calculate the total size of all the binary properties in the repository
*
*
* @return size in bytes
*/
public Long getRepositorySize() {
Expand All @@ -122,7 +122,7 @@ public Long getRepositorySize() {

/**
* Calculate the number of objects in the repository
*
*
* @return
*/
public Long getRepositoryObjectCount() {
Expand All @@ -135,7 +135,7 @@ public Long getRepositoryObjectCount() {

/**
* Get the full list of node types in the repository
*
*
* @param session
* @return
* @throws RepositoryException
Expand All @@ -149,7 +149,7 @@ public NodeTypeIterator getAllNodeTypes(final Session session)

/**
* Get a map of JCR prefixes to their URI namespaces
*
*
* @param session
* @return
* @throws RepositoryException
Expand All @@ -175,15 +175,17 @@ public String apply(final String p) {

/**
* Serialize the JCR namespace information as an RDF Dataset
*
*
* @param session
* @return
* @throws RepositoryException
*/
public Dataset getNamespaceRegistryGraph(final Session session)
throws RepositoryException {

final Model model = JcrRdfTools.withContext(null, session).getJcrNamespaceModel();
final Model model =
JcrRdfTools.withContext(null, session).getJcrNamespaceModel()
.asModel();

model.register(new NamespaceChangedStatementListener(session));

Expand All @@ -196,7 +198,7 @@ public Dataset getNamespaceRegistryGraph(final Session session)
/**
* Perform a full-text search on the whole repository and return the
* information as an RDF Dataset
*
*
* @param subjectFactory
* @param searchSubject RDF resource to use as the subject of the search
* @param session
Expand Down Expand Up @@ -246,8 +248,10 @@ public Dataset searchRepository(final GraphSubjects subjectFactory,
limit(new org.fcrepo.kernel.utils.iterators.NodeIterator(nodeIterator),
limit);

model = JcrRdfTools.withContext(subjectFactory, session).getJcrPropertiesModel(limitedIterator,
searchSubject);
model =
JcrRdfTools.withContext(subjectFactory, session)
.getJcrPropertiesModel(limitedIterator, searchSubject)
.asModel();

/* add the result description to the RDF model */

Expand Down Expand Up @@ -281,11 +285,11 @@ public Dataset searchRepository(final GraphSubjects subjectFactory,
*/
public Problems backupRepository(final Session session,
final File backupDirectory) throws RepositoryException {
RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session)
final RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session)
.getWorkspace()
.getRepositoryManager();

Problems problems = repoMgr.backupRepository(backupDirectory);
final Problems problems = repoMgr.backupRepository(backupDirectory);

return problems;
}
Expand All @@ -300,18 +304,18 @@ public Problems backupRepository(final Session session,
*/
public Problems restoreRepository(final Session session,
final File backupDirectory) throws RepositoryException {
RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session)
final RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session)
.getWorkspace()
.getRepositoryManager();

Problems problems = repoMgr.restoreRepository(backupDirectory);
final Problems problems = repoMgr.restoreRepository(backupDirectory);

return problems;
}

/**
* Set the repository to back this RepositoryService
*
*
* @param repository
*/
public void setRepository(final Repository repository) {
Expand Down
Expand Up @@ -227,24 +227,25 @@ public JcrRdfTools withSession(final Session session) {
}

/**
* Create a default Jena Model populated with the registered JCR namespaces
* Create a default {@link RdfStream} populated with the registered JCR
* namespaces
*
* @return
* @throws RepositoryException
*/
public Model getJcrPropertiesModel() throws RepositoryException {
return new NamespaceContext(session).asModel();
public RdfStream getJcrPropertiesModel() throws RepositoryException {
return new NamespaceContext(session);
}

/**
* Get an RDF model for the given JCR NodeIterator
* Get an {@link RdfStream} for the given JCR NodeIterator
*
* @param nodeIterator
* @param iteratorSubject
* @return
* @throws RepositoryException
*/
public Model getJcrPropertiesModel(final Iterator<Node> nodeIterator,
public RdfStream getJcrPropertiesModel(final Iterator<Node> nodeIterator,
final Resource iteratorSubject) throws RepositoryException {

final RdfStream results = new RdfStream();
Expand All @@ -258,74 +259,68 @@ public Model getJcrPropertiesModel(final Iterator<Node> nodeIterator,
.getGraphSubject(node).asNode())));
}
}
return results.asModel();
return results;
}

/**
* Get an {@link Model} for a node that includes all its own JCR properties,
* Get an {@link RdfStream} for a node that includes all its own JCR properties,
* as well as the properties of its immediate children. TODO add triples for
* root node, ala addRepositoryMetricsToModel()
*
* @param node
* @return
* @throws RepositoryException
*/
public Model getJcrPropertiesModel(final Node node) throws RepositoryException {
final RdfStream namespaceContext = new NamespaceContext(session);
return namespaceContext.concat(
new PropertiesRdfContext(node, graphSubjects, llstore))
.asModel();
public RdfStream getJcrPropertiesModel(final Node node) throws RepositoryException {
return new PropertiesRdfContext(node, graphSubjects, llstore);
}

/**
* Get a Jena RDF model for the JCR version history information for a node
* Get an {@link RdfStream} for the JCR version history information for a node
*
* @param node
* @return
* @throws RepositoryException
*/
public Model getJcrVersionPropertiesModel(final Node node)
public RdfStream getJcrVersionPropertiesModel(final Node node)
throws RepositoryException {
return new VersionsRdfContext(node, graphSubjects, llstore).asModel();
return new VersionsRdfContext(node, graphSubjects, llstore);
}

/**
* Serialize the JCR fixity information in a Jena Model
* Serialize the JCR fixity information in an {@link RdfStream}
*
* @param node
* @param blobs
* @return
* @throws RepositoryException
*/
public Model getJcrPropertiesModel(final Node node,
final Iterable<FixityResult> blobs) throws RepositoryException {
return new NamespaceContext(session).concat(
new FixityRdfContext(node, graphSubjects, llstore, blobs))
.concat(new PropertiesRdfContext(node, graphSubjects, llstore))
.asModel();
public RdfStream getJcrPropertiesModel(final Node node,
final Iterable<FixityResult> blobs) throws RepositoryException {
return new FixityRdfContext(node, graphSubjects, llstore, blobs);
}

/**
* Get an RDF model of the registered JCR namespaces
* Get an {@link RdfStream} of the registered JCR namespaces
*
* @return
* @throws RepositoryException
*/
public Model getJcrNamespaceModel() throws RepositoryException {
return new NamespaceContext(session).asModel();
public RdfStream getJcrNamespaceModel() throws RepositoryException {
return new NamespaceContext(session);
}

/**
* Add the properties of a Node's parent and immediate children (as well as
* the jcr:content of children) to the given RDF model
* the jcr:content of children) to the given {@link RdfStream}
*
* @param node
* @param offset
* @param limit @throws RepositoryException
*/
public Model getJcrTreeModel(final Node node, final long offset,
public RdfStream getJcrTreeModel(final Node node, final long offset,
final int limit) throws RepositoryException {
return new HierarchyRdfContext(node, graphSubjects, llstore).asModel();
return new HierarchyRdfContext(node, graphSubjects, llstore);
}

/**
Expand Down
Expand Up @@ -28,6 +28,7 @@
import javax.jcr.Value;
import javax.jcr.nodetype.PropertyDefinition;

import org.fcrepo.kernel.exception.NoSuchPropertyDefinitionException;
import org.slf4j.Logger;

/**
Expand Down Expand Up @@ -83,7 +84,14 @@ public static void appendOrReplaceNodeProperty(final Node node,
property.setValue(newValue);
}
} else {
if (isMultivaluedProperty(node, propertyName)) {
boolean isMultiple = false;
try {
isMultiple = isMultivaluedProperty(node, propertyName);

} catch (final NoSuchPropertyDefinitionException e) {
// simply represents a new kind of property on this node
}
if (isMultiple) {
logger.debug("Creating new multivalued {} property {} with " +
"initial value [{}]",
PropertyType.nameFromValue(newValue.getType()),
Expand Down Expand Up @@ -197,8 +205,7 @@ public static boolean isMultivaluedProperty(final Node node,
getDefinitionForPropertyName(node, propertyName);

if (def == null) {
throw new RepositoryException("No such property as " + propertyName
+ " in definitions for " + node.getPath());
throw new NoSuchPropertyDefinitionException();
}

return def.isMultiple();
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.fcrepo.kernel.utils.iterators;

import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Iterators.singletonIterator;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
Expand Down Expand Up @@ -178,4 +179,24 @@ public Triple apply(final Statement s) {

};

/*
* We ignore duplicated triples for equality.
*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object o) {
if (o instanceof RdfStream) {
final RdfStream rdfo = (RdfStream) o;
final boolean triplesEqual =
copyOf(rdfo.triples).equals(copyOf(this.triples));
final boolean namespaceMappingsEqual =
rdfo.namespaces().equals(this.namespaces());
return triplesEqual && namespaceMappingsEqual;
} else {
return false;
}
}

}

0 comments on commit 6a22fd7

Please sign in to comment.