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 com.hp.hpl.jena.graph.Triple;
021import com.hp.hpl.jena.rdf.model.Resource;
022
023import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
024import org.fcrepo.kernel.api.models.FedoraResource;
025
026import javax.jcr.Node;
027import javax.jcr.RepositoryException;
028
029import java.util.function.Predicate;
030import java.util.stream.Stream;
031
032import static com.hp.hpl.jena.vocabulary.RDF.type;
033import static org.fcrepo.kernel.api.RdfLexicon.isManagedNamespace;
034import static org.fcrepo.kernel.api.RequiredRdfContext.PROPERTIES;
035import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeConverter;
036import static org.fcrepo.kernel.modeshape.rdf.ManagedRdf.isManagedTriple;
037import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
038import static org.fcrepo.kernel.modeshape.utils.StreamUtils.iteratorToStream;
039
040/**
041 * @author cabeer
042 * @author ajs6f
043 * @since 10/9/14
044 */
045public class HashRdfContext extends NodeRdfContext {
046
047    private static final Predicate<Triple> IS_MANAGED_TYPE = t -> t.getPredicate().equals(type.asNode()) &&
048            isManagedNamespace.test(t.getObject().getNameSpace());
049
050    private static final Predicate<Triple> IS_MANAGED_TRIPLE = IS_MANAGED_TYPE.or(isManagedTriple);
051
052    /**
053     * Default constructor.
054     *
055     * @param resource the resource
056     * @param idTranslator the id translator
057     * @throws javax.jcr.RepositoryException if repository exception occurred
058     */
059    public HashRdfContext(final FedoraResource resource,
060                          final IdentifierConverter<Resource, FedoraResource> idTranslator)
061            throws RepositoryException {
062        super(resource, idTranslator);
063
064        concat(getNodeStream(resource)
065                .flatMap(n -> nodeConverter.convert(n).getTriples(idTranslator, PROPERTIES))
066                .filter(IS_MANAGED_TRIPLE.negate()));
067    }
068
069    @SuppressWarnings("unchecked")
070    private static Stream<Node> getNodeStream(final FedoraResource resource) throws RepositoryException {
071        final Node node = getJcrNode(resource);
072        if (node.hasNode("#")) {
073            return iteratorToStream(node.getNode("#").getNodes());
074        }
075        return Stream.empty();
076    }
077}