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.hp.hpl.jena.graph.Node;
019import com.hp.hpl.jena.graph.Triple;
020import com.hp.hpl.jena.rdf.model.Resource;
021import org.fcrepo.kernel.FedoraJcrTypes;
022import org.fcrepo.kernel.models.FedoraResource;
023import org.fcrepo.kernel.identifiers.IdentifierConverter;
024import org.fcrepo.kernel.utils.iterators.RdfStream;
025import org.slf4j.Logger;
026
027import javax.jcr.RepositoryException;
028
029import java.util.Iterator;
030
031import static com.hp.hpl.jena.graph.Triple.create;
032import static org.fcrepo.kernel.RdfLexicon.HAS_PARENT;
033import static org.slf4j.LoggerFactory.getLogger;
034
035/**
036 * @author cabeer
037 * @since 9/16/14
038 */
039public class ParentRdfContext extends NodeRdfContext {
040
041    private static final Logger LOGGER = getLogger(ParentRdfContext.class);
042
043    /**
044     * Default constructor.
045     *
046     * @param resource the resource
047     * @param idTranslator the id translator
048     * @throws javax.jcr.RepositoryException if repository exception occurred
049     */
050    public ParentRdfContext(final FedoraResource resource,
051                            final IdentifierConverter<Resource, FedoraResource> idTranslator)
052            throws RepositoryException {
053        super(resource, idTranslator);
054
055        if (resource.getNode().getDepth() > 0) {
056            LOGGER.trace("Determined that this resource has a parent.");
057            concat(parentContext());
058        }
059    }
060
061    private Iterator<Triple> parentContext() {
062        final RdfStream parentStream = new RdfStream();
063        // The parent node of a frozen node for a versionable resource in the
064        // jcr:system space is not a node we want to link to.
065        if (!resource().isFrozenResource() || !resource().getUnfrozenResource().hasType(FedoraJcrTypes.VERSIONABLE)) {
066            final Node containerSubject = translator().reverse().convert(resource().getContainer()).asNode();
067            parentStream.concat(create(subject(), HAS_PARENT.asNode(), containerSubject));
068        }
069        return parentStream;
070    }
071}