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.impl.rdf.impl; 017 018import com.google.common.base.Function; 019import com.google.common.collect.Iterators; 020import com.hp.hpl.jena.graph.Triple; 021import com.hp.hpl.jena.rdf.model.Resource; 022import org.fcrepo.kernel.models.NonRdfSourceDescription; 023import org.fcrepo.kernel.models.FedoraResource; 024import org.fcrepo.kernel.identifiers.IdentifierConverter; 025import org.fcrepo.kernel.utils.iterators.RdfStream; 026import org.slf4j.Logger; 027 028import javax.jcr.RepositoryException; 029 030import java.util.Iterator; 031 032import static com.hp.hpl.jena.graph.Triple.create; 033import static org.fcrepo.kernel.RdfLexicon.CONTAINS; 034import static org.slf4j.LoggerFactory.getLogger; 035 036/** 037 * @author cabeer 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."); 058 concat(childrenContext()); 059 } 060 } 061 062 063 private Iterator<Triple> childrenContext() { 064 065 final Iterator<FedoraResource> niceChildren = resource().getChildren(); 066 067 return Iterators.concat(Iterators.transform(niceChildren, child2triples())); 068 } 069 070 private Function<FedoraResource, Iterator<Triple>> child2triples() { 071 return new Function<FedoraResource, Iterator<Triple>>() { 072 073 @Override 074 public Iterator<Triple> apply(final FedoraResource child) { 075 076 final com.hp.hpl.jena.graph.Node childSubject; 077 078 if (child instanceof NonRdfSourceDescription) { 079 childSubject = translator().reverse() 080 .convert(((NonRdfSourceDescription) child).getDescribedResource()) 081 .asNode(); 082 } else { 083 childSubject = translator().reverse().convert(child).asNode(); 084 } 085 LOGGER.trace("Creating triples for child node: {}", child); 086 final RdfStream childStream = new RdfStream(); 087 childStream.concat(create(subject(), CONTAINS.asNode(), childSubject)); 088 return childStream; 089 } 090 }; 091 } 092 093}