001/**
002 * Copyright 2015 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 javax.jcr.Property;
025import javax.jcr.RepositoryException;
026
027import java.util.Iterator;
028
029/**
030 * Accumulate inbound references to a given resource
031 *
032 * @author cabeer
033 */
034public class ReferencesRdfContext extends NodeRdfContext {
035
036    private final PropertyToTriple property2triple;
037
038    /**
039     * Add the inbound references from other nodes to this resource to the stream
040     *
041     * @param resource the resource
042     * @param idTranslator the id translator
043     * @throws RepositoryException if repository exception occurred
044     */
045
046    public ReferencesRdfContext(final FedoraResource resource,
047                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
048        throws RepositoryException {
049        super(resource, idTranslator);
050        property2triple = new PropertyToTriple(resource.getNode().getSession(), idTranslator);
051        concat(putStrongReferencePropertiesIntoContext());
052        concat(putWeakReferencePropertiesIntoContext());
053    }
054
055    private Iterator<Triple> putWeakReferencePropertiesIntoContext() throws RepositoryException {
056        final Iterator<Property> properties = resource().getNode().getWeakReferences();
057
058        return Iterators.concat(Iterators.transform(properties, property2triple));
059    }
060
061    private Iterator<Triple> putStrongReferencePropertiesIntoContext() throws RepositoryException {
062        final Iterator<Property> properties = resource().getNode().getReferences();
063
064        return Iterators.concat(Iterators.transform(properties, property2triple));
065
066    }
067
068}