001/**
002 * Copyright 2014 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.impl.rdf.impl;
017
018import com.google.common.collect.Iterators;
019import com.hp.hpl.jena.graph.Triple;
020import com.hp.hpl.jena.rdf.model.Resource;
021import org.fcrepo.kernel.models.FedoraResource;
022import org.fcrepo.kernel.identifiers.IdentifierConverter;
023import org.fcrepo.kernel.impl.rdf.impl.mappings.PropertyToTriple;
024import org.fcrepo.kernel.utils.iterators.PropertyIterator;
025
026import javax.jcr.Property;
027import javax.jcr.RepositoryException;
028
029import java.util.Iterator;
030
031/**
032 * Accumulate inbound references to a given resource
033 *
034 * @author cabeer
035 */
036public class ReferencesRdfContext extends NodeRdfContext {
037
038    private PropertyToTriple property2triple;
039
040    /**
041     * Add the inbound references from other nodes to this resource to the stream
042     *
043     * @param resource
044     * @param idTranslator
045     * @throws javax.jcr.RepositoryException
046     */
047
048    public ReferencesRdfContext(final FedoraResource resource,
049                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
050        throws RepositoryException {
051        super(resource, idTranslator);
052        property2triple = new PropertyToTriple(resource.getNode().getSession(), idTranslator);
053        concat(putStrongReferencePropertiesIntoContext());
054        concat(putWeakReferencePropertiesIntoContext());
055    }
056
057    private Iterator<Triple> putWeakReferencePropertiesIntoContext() throws RepositoryException {
058        final Iterator<Property> properties = new PropertyIterator(resource().getNode().getWeakReferences());
059
060        return Iterators.concat(Iterators.transform(properties, property2triple));
061    }
062
063    private Iterator<Triple> putStrongReferencePropertiesIntoContext() throws RepositoryException {
064        final Iterator<Property> properties = new PropertyIterator(resource().getNode().getReferences());
065
066        return Iterators.concat(Iterators.transform(properties, property2triple));
067
068    }
069
070}