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.datatypes.xsd.XSDDatatype.XSDint; 032import static com.hp.hpl.jena.graph.Triple.create; 033import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral; 034import static org.fcrepo.kernel.api.RdfLexicon.CONTAINS; 035import static org.fcrepo.kernel.api.RdfLexicon.HAS_CHILD_COUNT; 036import static org.slf4j.LoggerFactory.getLogger; 037 038/** 039 * @author cabeer 040 * @author ajs6f 041 * @since 9/16/14 042 */ 043public class ChildrenRdfContext extends NodeRdfContext { 044 045 private static final Logger LOGGER = getLogger(ChildrenRdfContext.class); 046 047 /** 048 * Default constructor. 049 * 050 * @param resource the resource 051 * @param idTranslator the idTranslator 052 * @throws javax.jcr.RepositoryException if repository exception occurred 053 */ 054 public ChildrenRdfContext(final FedoraResource resource, 055 final IdentifierConverter<Resource, FedoraResource> idTranslator) 056 throws RepositoryException { 057 super(resource, idTranslator); 058 059 if (resource.getNode().hasNodes()) { 060 LOGGER.trace("Found children of this resource: {}", resource.getPath()); 061 062 // Count the number of children 063 final Iterator<FedoraResource> childrenCounter = resource().getChildren(); 064 concat(createNumChildrenTriple(Iterators.size(childrenCounter))); 065 066 final Iterator<FedoraResource> niceChildren = resource().getChildren(); 067 concat(Iterators.transform(niceChildren, child2triple::apply)); 068 069 } else { 070 concat(createNumChildrenTriple(0)); 071 } 072 073 074 } 075 076 private Triple createNumChildrenTriple(final int numChildren) { 077 return create(subject(), 078 HAS_CHILD_COUNT.asNode(), 079 createTypedLiteral(Integer.toString(numChildren), XSDint).asNode()); 080 } 081 082 private final Function<FedoraResource, Triple> child2triple = child -> { 083 084 final com.hp.hpl.jena.graph.Node childSubject = child instanceof NonRdfSourceDescription ? 085 uriFor(((NonRdfSourceDescription) child).getDescribedResource()) : uriFor(child); 086 087 LOGGER.trace("Creating triple for child node: {}", child); 088 return create(subject(), CONTAINS.asNode(), childSubject); 089 }; 090}