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; 020import org.fcrepo.kernel.api.models.NonRdfSourceDescription; 021import org.fcrepo.kernel.api.models.FedoraResource; 022import org.fcrepo.kernel.api.identifiers.IdentifierConverter; 023import org.slf4j.Logger; 024 025import javax.jcr.RepositoryException; 026 027import static java.util.stream.Stream.of; 028import static com.hp.hpl.jena.datatypes.xsd.XSDDatatype.XSDlong; 029import static com.hp.hpl.jena.graph.Triple.create; 030import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral; 031import static org.fcrepo.kernel.api.RdfLexicon.CONTAINS; 032import static org.fcrepo.kernel.api.RdfLexicon.HAS_CHILD_COUNT; 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 059 // Count the number of children 060 concat(of(createNumChildrenTriple(resource().getChildren().count()))); 061 concat(resource().getChildren().peek(child -> LOGGER.trace("Creating triple for child node: {}", child)) 062 .map(child -> create(subject(), CONTAINS.asNode(), child instanceof NonRdfSourceDescription ? 063 uriFor(((NonRdfSourceDescription) child).getDescribedResource()) : uriFor(child)))); 064 } else { 065 concat(of(createNumChildrenTriple(0))); 066 } 067 068 } 069 070 private Triple createNumChildrenTriple(final long numChildren) { 071 return create(subject(), 072 HAS_CHILD_COUNT.asNode(), 073 createTypedLiteral(Long.toString(numChildren), XSDlong).asNode()); 074 } 075 076}