001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.rdf.impl;
019
020import static java.util.stream.Stream.of;
021import static org.apache.jena.graph.Triple.create;
022import static org.fcrepo.kernel.api.RdfLexicon.DESCRIBES;
023import static org.fcrepo.kernel.api.RdfLexicon.DESCRIBED_BY;
024
025import org.fcrepo.kernel.api.models.NonRdfSourceDescription;
026import org.fcrepo.kernel.api.models.FedoraBinary;
027import org.fcrepo.kernel.api.models.FedoraResource;
028import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
029
030import org.apache.jena.graph.Node;
031import org.apache.jena.rdf.model.Resource;
032
033/**
034 * @author cabeer
035 * @author ajs6f
036 * @since 10/16/14
037 */
038public class ContentRdfContext extends NodeRdfContext {
039
040    /**
041     * Default constructor.
042     *
043     * @param resource the resource
044     * @param idTranslator the idTranslator
045     */
046    public ContentRdfContext(final FedoraResource resource,
047                             final IdentifierConverter<Resource, FedoraResource> idTranslator) {
048        super(resource, idTranslator);
049
050        // if there's an accessible jcr:content node, include information about it
051        if (resource instanceof NonRdfSourceDescription) {
052            // describedby property should already be in response
053            if (resource.isMemento()) {
054                return;
055            }
056            final FedoraResource contentNode = resource().getDescribedResource();
057            final FedoraResource descNode = resource();
058            final Node subject = uriFor(contentNode);
059            final Node descObject = uriFor(descNode);
060            // add triples representing parent-to-content-child relationship
061            concat(of(create(subject, DESCRIBED_BY.asNode(), descObject)));
062        } else if (resource instanceof FedoraBinary) {
063            final FedoraResource contentNode = resource.getOriginalResource();
064            concat(of(create(uriFor(contentNode), DESCRIBES.asNode(), uriFor(contentNode))));
065        }
066    }
067}