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 static java.util.stream.Stream.of;
019import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
020import static com.hp.hpl.jena.graph.NodeFactory.createURI;
021import static com.hp.hpl.jena.graph.Triple.create;
022import static com.hp.hpl.jena.vocabulary.RDF.type;
023import static com.hp.hpl.jena.vocabulary.RDFS.Class;
024import static com.hp.hpl.jena.vocabulary.RDFS.label;
025import static com.hp.hpl.jena.vocabulary.RDFS.subClassOf;
026import static org.fcrepo.kernel.modeshape.rdf.impl.mappings.ItemDefinitionToTriples.getResource;
027import static org.fcrepo.kernel.modeshape.utils.StreamUtils.iteratorToStream;
028import static org.fcrepo.kernel.modeshape.utils.UncheckedFunction.uncheck;
029import static org.slf4j.LoggerFactory.getLogger;
030
031import java.util.Arrays;
032import java.util.stream.Stream;
033import java.util.function.Function;
034import java.util.function.Predicate;
035
036import com.hp.hpl.jena.graph.Node;
037import com.hp.hpl.jena.graph.Triple;
038
039import org.fcrepo.kernel.modeshape.rdf.impl.mappings.NodeDefinitionToTriples;
040import org.fcrepo.kernel.modeshape.rdf.impl.mappings.PropertyDefinitionToTriples;
041import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
042
043import org.slf4j.Logger;
044
045import javax.jcr.RepositoryException;
046import javax.jcr.nodetype.ItemDefinition;
047import javax.jcr.nodetype.NodeType;
048import javax.jcr.nodetype.NodeTypeManager;
049
050/**
051 * Assemble {@link com.hp.hpl.jena.graph.Triple}s derived from the {@link NodeType}s in a repository.
052 *
053 * @author cbeer
054 */
055@Deprecated
056public class NodeTypeRdfContext extends DefaultRdfStream {
057
058    private static final Logger LOGGER = getLogger(NodeTypeRdfContext.class);
059
060    private static final Predicate<ItemDefinition> isWildcardResidualDefinition = x -> x.getName().equals("*");
061
062    /**
063     * Convert the NodeTypeManager to an RDF stream, including both primary and
064     * mixin node types.
065     *
066     * @param nodeTypeManager the node type manager
067     * @throws RepositoryException if repository exception occurred
068     */
069    public NodeTypeRdfContext(final NodeTypeManager nodeTypeManager) throws RepositoryException {
070        super(createURI("fedora:info/"));
071        concat(getNodeTypes(nodeTypeManager).flatMap(getTriplesFromNodeType));
072    }
073
074    /**
075     * Convert a stream of NodeTypes to an RDF stream, including both primary and
076     * mixin node types.
077     *
078     * @param nodeTypes the node types
079     */
080    public NodeTypeRdfContext(final Stream<NodeType> nodeTypes) {
081        super(createURI("fedora:info/"));
082        concat(nodeTypes.flatMap(getTriplesFromNodeType));
083    }
084
085    /**
086     * Convert a single NodeType to an RDF stream, including both primary and
087     * mixin node types.
088     *
089     * @param nodeType the node type
090     */
091    public NodeTypeRdfContext(final NodeType nodeType) {
092        super(createURI("fedora:info/"));
093        concat(getTriplesFromNodeType.apply(nodeType));
094    }
095
096    @SuppressWarnings("unchecked")
097    private static Stream<NodeType> getNodeTypes(final NodeTypeManager manager) throws RepositoryException {
098         return Stream.concat(
099                iteratorToStream(manager.getPrimaryNodeTypes()),
100                iteratorToStream(manager.getMixinNodeTypes()));
101    }
102
103    private static Function<NodeType, Stream<Triple>> getTriplesFromNodeType = uncheck(nodeType -> {
104        final Node nodeTypeResource = getResource(nodeType).asNode();
105        final String nodeTypeName = nodeType.getName();
106
107        return Stream.concat(
108            Stream.concat(
109                Arrays.stream(nodeType.getDeclaredSupertypes())
110                    .map(uncheck((final NodeType x) -> {
111                        final Node supertypeNode = getResource(x).asNode();
112                        LOGGER.trace("Adding triple for nodeType: {} with subclass: {}", nodeTypeName,
113                            supertypeNode.getURI());
114                        return create(nodeTypeResource, subClassOf.asNode(), supertypeNode);
115                    })),
116
117                Arrays.stream(nodeType.getDeclaredChildNodeDefinitions())
118                    .filter(isWildcardResidualDefinition.negate())
119                    .flatMap((new NodeDefinitionToTriples(nodeTypeResource))::apply)),
120            Stream.concat(
121                Arrays.stream(nodeType.getDeclaredPropertyDefinitions())
122                    .filter(isWildcardResidualDefinition.negate())
123                    .flatMap((new PropertyDefinitionToTriples(nodeTypeResource))::apply),
124
125                of(create(nodeTypeResource, type.asNode(), Class.asNode()),
126                   create(nodeTypeResource, label.asNode(), createLiteral(nodeTypeName)))));
127    });
128}