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.modeshape.rdf.impl;
017
018import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeToResource;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022
023import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
024import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
025import org.fcrepo.kernel.api.models.FedoraResource;
026import org.fcrepo.kernel.api.utils.iterators.RdfStream;
027
028import com.google.common.base.Converter;
029import com.hp.hpl.jena.rdf.model.Resource;
030
031/**
032 * {@link RdfStream} that holds contexts related to a specific {@link Node}.
033 *
034 * @author ajs6f
035 * @since Oct 10, 2013
036 */
037public class NodeRdfContext extends RdfStream {
038
039    private final FedoraResource resource;
040
041    private final IdentifierConverter<Resource, FedoraResource> idTranslator;
042
043    private final com.hp.hpl.jena.graph.Node subject;
044
045
046    /**
047     * Default constructor.
048     *
049     * @param resource the resource
050     * @param idTranslator the id translator
051     */
052    public NodeRdfContext(final FedoraResource resource,
053                          final IdentifierConverter<Resource, FedoraResource> idTranslator) {
054        super();
055        this.resource = resource;
056        this.idTranslator = idTranslator;
057        this.subject = uriFor(resource);
058        try {
059            session(resource.getNode().getSession());
060        } catch (final RepositoryException ex) {
061            throw new RepositoryRuntimeException(ex);
062        }
063    }
064
065    /**
066     * @return The {@link Node} in question
067     */
068    public FedoraResource resource() {
069        return resource;
070    }
071
072    /**
073     * @return local {@link org.fcrepo.kernel.api.identifiers.IdentifierConverter}
074     */
075    public IdentifierConverter<Resource, FedoraResource> translator() {
076        return idTranslator;
077    }
078
079    /**
080     * @param resource a Fedora model instance that must be identified by an URI
081     * @return a translated URI for that resource
082     */
083    protected com.hp.hpl.jena.graph.Node uriFor(final FedoraResource resource) {
084        return translator().reverse().convert(resource).asNode();
085    }
086
087    /**
088     * @return local {@link org.fcrepo.kernel.api.identifiers.IdentifierConverter}
089     */
090    public Converter<Node, Resource> nodeConverter() {
091        return nodeToResource(idTranslator);
092    }
093
094    /**
095     * @return the RDF subject at the center of this context
096     */
097    public com.hp.hpl.jena.graph.Node subject() {
098        return subject;
099    }
100}