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.google.common.collect.Iterators; 019import com.hp.hpl.jena.graph.Triple; 020import com.hp.hpl.jena.rdf.model.Resource; 021import org.fcrepo.kernel.api.models.NonRdfSourceDescription; 022import org.fcrepo.kernel.api.models.FedoraResource; 023import org.fcrepo.kernel.api.identifiers.IdentifierConverter; 024import org.slf4j.Logger; 025 026import javax.jcr.RepositoryException; 027 028import java.util.Iterator; 029import java.util.function.Function; 030 031import static com.hp.hpl.jena.graph.Triple.create; 032import static org.fcrepo.kernel.api.RdfLexicon.CONTAINS; 033import static org.slf4j.LoggerFactory.getLogger; 034 035/** 036 * @author cabeer 037 * @author ajs6f 038 * @since 9/16/14 039 */ 040public class ChildrenRdfContext extends NodeRdfContext { 041 042 private static final Logger LOGGER = getLogger(ChildrenRdfContext.class); 043 044 /** 045 * Default constructor. 046 * 047 * @param resource the resource 048 * @param idTranslator the idTranslator 049 * @throws javax.jcr.RepositoryException if repository exception occurred 050 */ 051 public ChildrenRdfContext(final FedoraResource resource, 052 final IdentifierConverter<Resource, FedoraResource> idTranslator) 053 throws RepositoryException { 054 super(resource, idTranslator); 055 056 if (resource.getNode().hasNodes()) { 057 LOGGER.trace("Found children of this resource: {}", resource.getPath()); 058 final Iterator<FedoraResource> niceChildren = resource().getChildren(); 059 concat(Iterators.transform(niceChildren, child2triple::apply)); 060 } 061 } 062 063 private final Function<FedoraResource, Triple> child2triple = child -> { 064 065 final com.hp.hpl.jena.graph.Node childSubject = child instanceof NonRdfSourceDescription ? 066 uriFor(((NonRdfSourceDescription) child).getDescribedResource()) : uriFor(child); 067 068 LOGGER.trace("Creating triple for child node: {}", child); 069 return create(subject(), CONTAINS.asNode(), childSubject); 070 }; 071}