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.mappings;
017
018import com.hp.hpl.jena.graph.Node;
019import com.hp.hpl.jena.graph.Triple;
020import org.slf4j.Logger;
021
022import java.util.stream.Stream;
023
024import javax.jcr.RepositoryException;
025import javax.jcr.nodetype.NodeDefinition;
026import javax.jcr.nodetype.NodeType;
027
028import static java.util.stream.Stream.concat;
029import static java.util.stream.Stream.of;
030import static com.google.common.base.Throwables.propagate;
031import static com.hp.hpl.jena.graph.Triple.create;
032import static com.hp.hpl.jena.vocabulary.RDFS.range;
033import static org.slf4j.LoggerFactory.getLogger;
034
035/**
036 * Utility for moving Node Definitions into RDFS triples
037 * @author cbeer
038 */
039public class NodeDefinitionToTriples extends ItemDefinitionToTriples<NodeDefinition> {
040
041    private static final Logger LOGGER = getLogger(NodeDefinitionToTriples.class);
042
043    /**
044     * Translate ItemDefinitions into triples. The definitions will hang off
045     * the provided RDF Node
046     * @param domain the domain
047     */
048    public NodeDefinitionToTriples(final Node domain) {
049        super(domain);
050    }
051
052    @Override
053    public Stream<Triple> apply(final NodeDefinition input) {
054
055        try {
056
057            final Node propertyDefinitionNode = getResource(input).asNode();
058
059            final NodeType[] requiredPrimaryTypes = input.getRequiredPrimaryTypes();
060
061            if (requiredPrimaryTypes.length > 1) {
062                // TODO we can express this as an OWL unionOf. But should we?
063                LOGGER.trace(
064                        "Skipping RDFS:range for {} with multiple primary types",
065                        propertyDefinitionNode.getName());
066            } else if (requiredPrimaryTypes.length == 1) {
067                LOGGER.trace("Adding RDFS:range for {} with primary types {}",
068                             input.getName(),
069                             requiredPrimaryTypes[0].getName());
070                return concat(
071                        of(create(propertyDefinitionNode, range.asNode(),
072                                getResource(requiredPrimaryTypes[0]).asNode())),
073                        super.apply(input));
074            } else {
075                LOGGER.trace("Skipping RDFS:range for {} with no required primary types");
076            }
077            return super.apply(input);
078
079        } catch (final RepositoryException e) {
080            throw propagate(e);
081        }
082
083    }
084}