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