001/**
002 * Copyright 2014 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 static com.google.common.base.Predicates.not;
019import static com.google.common.base.Throwables.propagate;
020import static com.google.common.collect.ImmutableList.copyOf;
021import static com.google.common.collect.Iterators.forArray;
022import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
023import static com.hp.hpl.jena.graph.Triple.create;
024import static com.hp.hpl.jena.vocabulary.RDF.type;
025import static com.hp.hpl.jena.vocabulary.RDFS.Class;
026import static com.hp.hpl.jena.vocabulary.RDFS.label;
027import static com.hp.hpl.jena.vocabulary.RDFS.subClassOf;
028import static org.fcrepo.kernel.impl.rdf.impl.mappings.ItemDefinitionToTriples.getResource;
029import static org.slf4j.LoggerFactory.getLogger;
030
031import com.google.common.base.Function;
032import com.google.common.base.Predicate;
033import com.google.common.collect.Collections2;
034import com.google.common.collect.Iterators;
035import com.hp.hpl.jena.graph.Node;
036import com.hp.hpl.jena.graph.Triple;
037import org.fcrepo.kernel.impl.rdf.impl.mappings.NodeDefinitionToTriples;
038import org.fcrepo.kernel.impl.rdf.impl.mappings.PropertyDefinitionToTriples;
039import org.fcrepo.kernel.utils.iterators.NodeTypeIterator;
040import org.fcrepo.kernel.utils.iterators.RdfStream;
041import org.slf4j.Logger;
042
043import javax.jcr.RepositoryException;
044import javax.jcr.nodetype.ItemDefinition;
045import javax.jcr.nodetype.NodeType;
046import javax.jcr.nodetype.NodeTypeManager;
047
048/**
049 * Assemble {@link Triple}s derived from the {@link NodeType}s in a repository.
050 *
051 * @author cbeer
052 */
053public class NodeTypeRdfContext extends RdfStream {
054
055    private static final Logger LOGGER = getLogger(NodeTypeRdfContext.class);
056
057    private static final Predicate<ItemDefinition> isWildcardResidualDefinition =
058        new Predicate<ItemDefinition>() {
059
060            @Override
061            public boolean apply(final ItemDefinition input) {
062                return input.getName().equals("*");
063            }
064        };
065
066    /**
067     * Convert the NodeTypeManager to an RDF stream, including both primary and
068     * mixin node types.
069     *
070     * @param nodeTypeManager
071     * @throws RepositoryException
072     */
073    public NodeTypeRdfContext(final NodeTypeManager nodeTypeManager)
074        throws RepositoryException {
075        super();
076
077        concat(new NodeTypeRdfContext(new NodeTypeIterator(nodeTypeManager
078                .getPrimaryNodeTypes())));
079        concat(new NodeTypeRdfContext(new NodeTypeIterator(nodeTypeManager
080                .getMixinNodeTypes())));
081
082    }
083
084    /**
085     * Convert a NodeType iterator into an RDF stream
086     *
087     * @param nodeTypeIterator
088     * @throws RepositoryException
089     */
090    public NodeTypeRdfContext(final Iterable<NodeType> nodeTypeIterator)
091        throws RepositoryException {
092        super();
093
094        for (final NodeType t : nodeTypeIterator) {
095            concat(new NodeTypeRdfContext(t));
096        }
097    }
098
099    /**
100     * Convert a NodeType into an RDF stream by capturing the supertypes, node
101     * definitions, and property definitions of the type as RDFS triples.
102     *
103     * @param nodeType
104     * @throws RepositoryException
105     */
106    public NodeTypeRdfContext(final NodeType nodeType)
107        throws RepositoryException {
108        super();
109
110        final Node nodeTypeResource = getResource(nodeType).asNode();
111        final String nodeTypeName = nodeType.getName();
112
113        LOGGER.trace("Adding triples for nodeType: {} with URI: {}",
114                nodeTypeName, nodeTypeResource.getURI());
115
116        concat(Collections2.transform(copyOf(nodeType.getDeclaredSupertypes()),
117
118                new Function<NodeType, Triple>() {
119
120                    @Override
121                    public Triple apply(final NodeType input) {
122                        final Node supertypeNode;
123                        try {
124                            supertypeNode = getResource(input).asNode();
125                            LOGGER.trace(
126                                    "Adding triple for nodeType: {} with subclass: {}",
127                                    nodeTypeName, supertypeNode.getURI());
128                            return create(nodeTypeResource,
129                                    subClassOf.asNode(), supertypeNode);
130
131                        } catch (final RepositoryException e) {
132                            throw propagate(e);
133                        }
134                    }
135                }));
136
137        concat(Iterators.concat(
138            Iterators.transform(Iterators.filter(
139                forArray(nodeType.getDeclaredChildNodeDefinitions()),
140                not(isWildcardResidualDefinition)),
141                new NodeDefinitionToTriples(nodeTypeResource))));
142
143        concat(Iterators.concat(
144            Iterators.transform(Iterators.filter(
145                forArray(nodeType.getDeclaredPropertyDefinitions()),
146                not(isWildcardResidualDefinition)),
147                new PropertyDefinitionToTriples(nodeTypeResource))));
148
149        concat(create(nodeTypeResource, type.asNode(), Class.asNode()), create(
150                nodeTypeResource, label.asNode(), createLiteral(nodeTypeName)));
151    }
152
153
154}