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.mappings; 017 018import com.google.common.base.Function; 019import com.hp.hpl.jena.graph.Node; 020import com.hp.hpl.jena.graph.Triple; 021import com.hp.hpl.jena.rdf.model.Resource; 022import org.fcrepo.kernel.utils.iterators.RdfStream; 023import org.modeshape.jcr.api.Namespaced; 024import org.slf4j.Logger; 025 026import javax.jcr.RepositoryException; 027import javax.jcr.nodetype.ItemDefinition; 028import javax.jcr.nodetype.NodeType; 029import java.util.Iterator; 030 031import static com.google.common.base.Throwables.propagate; 032import static com.hp.hpl.jena.graph.NodeFactory.createLiteral; 033import static com.hp.hpl.jena.graph.Triple.create; 034import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty; 035import static com.hp.hpl.jena.vocabulary.RDF.Property; 036import static com.hp.hpl.jena.vocabulary.RDF.type; 037import static com.hp.hpl.jena.vocabulary.RDFS.domain; 038import static com.hp.hpl.jena.vocabulary.RDFS.label; 039import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace; 040import static org.slf4j.LoggerFactory.getLogger; 041 042 043/** 044 * Utility for moving generic Item Definitions into RDFS triples 045 * @author cbeer 046 * @author ajs6f 047 * 048 * @since Oct 2013 049 * 050 * @param <T> the property of T 051 */ 052public class ItemDefinitionToTriples<T extends ItemDefinition> implements Function<T, Iterator<Triple>> { 053 054 private static final Logger LOGGER = getLogger(ItemDefinitionToTriples.class); 055 056 private Node context; 057 058 /** 059 * Translate ItemDefinitions into triples. The definitions will hang off 060 * the provided RDF Node 061 * @param context the context 062 */ 063 public ItemDefinitionToTriples(final Node context) { 064 this.context = context; 065 } 066 067 @Override 068 public Iterator<Triple> apply(final T input) { 069 070 try { 071 final Node propertyDefinitionNode = getResource(input).asNode(); 072 073 LOGGER.trace("Adding triples for nodeType: {} with child nodes: {}", 074 context.getURI(), 075 propertyDefinitionNode.getURI()); 076 077 return new RdfStream( 078 create(propertyDefinitionNode, type.asNode(), Property.asNode()), 079 create(propertyDefinitionNode, domain.asNode(), context), 080 create(propertyDefinitionNode, label.asNode(), createLiteral(input.getName()))); 081 } catch (final RepositoryException e) { 082 throw propagate(e); 083 } 084 } 085 086 /** 087 * Get a RDF {@link Resource} for a {@link Namespaced} JCR object. 088 * {@link Namespaced} is a Modeshape API type which is implemented by types 089 * that fulfill the JCR interfaces that represent definitions. 090 * 091 * @param namespacedObject the namespace object 092 * @return a resource for the given Namespaced JCR object 093 * @throws javax.jcr.RepositoryException if repository exception occurred 094 */ 095 public static Resource getResource(final Namespaced namespacedObject) 096 throws RepositoryException { 097 // TODO find a better way to create an explicitly-namespaced resource 098 // if Jena offers one, since this isn't actually a Property 099 LOGGER.trace("Creating RDF resource for {}:{}", 100 namespacedObject.getNamespaceURI(), 101 namespacedObject.getLocalName()); 102 return createProperty( 103 getRDFNamespaceForJcrNamespace(namespacedObject 104 .getNamespaceURI()), namespacedObject.getLocalName()) 105 .asResource(); 106 } 107 108 /** 109 * Get a RDF {@link Resource} for a {@link NodeType} JCR object. 110 * {@link Namespaced} is a Modeshape API type which is implemented by types 111 * that fulfill the JCR interfaces that represent definitions. 112 * 113 * @param nodeType the node type 114 * @return a Resource for the given NodeType 115 * @throws javax.jcr.RepositoryException if repository exception occurred 116 */ 117 public static Resource getResource(final NodeType nodeType) throws RepositoryException { 118 return getResource((Namespaced) nodeType); 119 } 120 121 /** 122 * Get a RDF {@link Resource} for a {@link ItemDefinition} JCR object. 123 * {@link Namespaced} is a Modeshape API type which is implemented by types 124 * that fulfill the JCR interfaces that represent definitions. 125 * 126 * @param itemDefinition the given item definition 127 * @return a resource for the given ItemDefinition 128 * @throws javax.jcr.RepositoryException if repository exception occurred 129 */ 130 public static Resource getResource(final ItemDefinition itemDefinition) throws RepositoryException { 131 return getResource((Namespaced) itemDefinition); 132 } 133}