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.NodeFactory.createURI;
022import static org.apache.jena.graph.Triple.create;
023import static org.apache.jena.vocabulary.RDF.type;
024import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
025import static org.fcrepo.kernel.modeshape.FedoraJcrConstants.ROOT;
026import static org.slf4j.LoggerFactory.getLogger;
027
028import org.apache.jena.rdf.model.Resource;
029import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
030import org.fcrepo.kernel.api.models.FedoraResource;
031
032import java.util.stream.Stream;
033import org.slf4j.Logger;
034
035import org.apache.jena.graph.Triple;
036
037/**
038 * Assemble {@link Triple}s derived from the root of a repository.
039 *
040 * @author ajs6f
041 * @since Oct 18, 2013
042 */
043public class RootRdfContext extends NodeRdfContext {
044
045    private static final Logger LOGGER = getLogger(RootRdfContext.class);
046
047    /**
048     * Ordinary constructor.
049     *
050     * @param resource the resource
051     * @param idTranslator the id translator
052     */
053    public RootRdfContext(final FedoraResource resource,
054                          final IdentifierConverter<Resource, FedoraResource> idTranslator) {
055        super(resource, idTranslator);
056
057        if (resource().hasType(ROOT)) {
058            concat(getRepositoryTriples());
059        }
060    }
061
062    private Stream<Triple> getRepositoryTriples() {
063        LOGGER.trace("Creating RDF triples for repository description");
064
065        final Stream.Builder<Triple> b = Stream.builder();
066
067        of("RepositoryRoot", "Resource", "Container").forEach(x ->
068            b.accept(create(subject(), type.asNode(), createURI(REPOSITORY_NAMESPACE + x))));
069
070        // offer all these accumulated triples
071        return b.build();
072    }
073}