001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.rdf.impl.mappings;
019
020import com.hp.hpl.jena.graph.Node;
021import com.hp.hpl.jena.graph.Triple;
022import org.slf4j.Logger;
023
024import java.util.stream.Stream;
025
026import javax.jcr.RepositoryException;
027import javax.jcr.nodetype.NodeDefinition;
028import javax.jcr.nodetype.NodeType;
029
030import static java.util.stream.Stream.concat;
031import static java.util.stream.Stream.of;
032import static com.google.common.base.Throwables.propagate;
033import static com.hp.hpl.jena.graph.Triple.create;
034import static com.hp.hpl.jena.vocabulary.RDFS.range;
035import static org.slf4j.LoggerFactory.getLogger;
036
037/**
038 * Utility for moving Node Definitions into RDFS triples
039 * @author cbeer
040 */
041public class NodeDefinitionToTriples extends ItemDefinitionToTriples<NodeDefinition> {
042
043    private static final Logger LOGGER = getLogger(NodeDefinitionToTriples.class);
044
045    /**
046     * Translate ItemDefinitions into triples. The definitions will hang off
047     * the provided RDF Node
048     * @param domain the domain
049     */
050    public NodeDefinitionToTriples(final Node domain) {
051        super(domain);
052    }
053
054    @Override
055    public Stream<Triple> apply(final NodeDefinition input) {
056
057        try {
058
059            final Node propertyDefinitionNode = getResource(input).asNode();
060
061            final NodeType[] requiredPrimaryTypes = input.getRequiredPrimaryTypes();
062
063            if (requiredPrimaryTypes.length > 1) {
064                // TODO we can express this as an OWL unionOf. But should we?
065                LOGGER.trace(
066                        "Skipping RDFS:range for {} with multiple primary types",
067                        propertyDefinitionNode.getName());
068            } else if (requiredPrimaryTypes.length == 1) {
069                LOGGER.trace("Adding RDFS:range for {} with primary types {}",
070                             input.getName(),
071                             requiredPrimaryTypes[0].getName());
072                return concat(
073                        of(create(propertyDefinitionNode, range.asNode(),
074                                getResource(requiredPrimaryTypes[0]).asNode())),
075                        super.apply(input));
076            } else {
077                LOGGER.trace("Skipping RDFS:range for {} with no required primary types");
078            }
079            return super.apply(input);
080
081        } catch (final RepositoryException e) {
082            throw propagate(e);
083        }
084
085    }
086}