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