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 com.hp.hpl.jena.graph.Triple.create;
021import static com.hp.hpl.jena.vocabulary.RDF.type;
022import static org.fcrepo.kernel.api.FedoraTypes.FEDORA_CONTAINER;
023import static org.fcrepo.kernel.api.RdfLexicon.BASIC_CONTAINER;
024import static org.fcrepo.kernel.api.RdfLexicon.CONTAINER;
025import static org.fcrepo.kernel.api.RdfLexicon.NON_RDF_SOURCE;
026import static org.fcrepo.kernel.api.RdfLexicon.RDF_SOURCE;
027
028import java.util.stream.Stream;
029import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
030import org.fcrepo.kernel.api.models.Container;
031import org.fcrepo.kernel.api.models.FedoraBinary;
032import org.fcrepo.kernel.api.models.FedoraResource;
033
034import com.hp.hpl.jena.graph.Triple;
035import com.hp.hpl.jena.rdf.model.Resource;
036
037/**
038 * @author cabeer
039 * @since 9/16/14
040 */
041public class LdpRdfContext extends NodeRdfContext {
042
043
044    /**
045     * Default constructor.
046     *
047     * @param resource the resource
048     * @param idTranslator the id translator
049     */
050    public LdpRdfContext(final FedoraResource resource,
051                         final IdentifierConverter<Resource, FedoraResource> idTranslator) {
052        super(resource, idTranslator);
053
054        final Stream.Builder<Triple> builder = Stream.builder();
055
056        if (resource instanceof FedoraBinary) {
057            builder.accept(nonRdfSourceContext());
058        } else {
059            builder.accept(typeContext());
060        }
061
062        if (resource instanceof Container) {
063            builder.accept(containerContext());
064
065            if (!resource.hasType(FEDORA_CONTAINER)) {
066                builder.accept(defaultContainerContext());
067            }
068        }
069        concat(builder.build());
070    }
071
072    private Triple typeContext() {
073        return create(subject(), type.asNode(), RDF_SOURCE.asNode());
074    }
075
076    private Triple containerContext() {
077        return create(subject(), type.asNode(), CONTAINER.asNode());
078    }
079
080    private Triple defaultContainerContext() {
081        return create(subject(), type.asNode(), BASIC_CONTAINER.asNode());
082    }
083
084    private Triple nonRdfSourceContext() {
085        return create(subject(), type.asNode(), NON_RDF_SOURCE.asNode());
086    }
087}