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;
017
018import com.google.common.base.Function;
019import com.google.common.collect.ImmutableList;
020import com.google.common.collect.Iterators;
021import com.hp.hpl.jena.graph.Triple;
022import com.hp.hpl.jena.rdf.model.Resource;
023
024import org.fcrepo.kernel.models.FedoraResource;
025import org.fcrepo.kernel.identifiers.IdentifierConverter;
026
027import javax.jcr.Node;
028import javax.jcr.RepositoryException;
029
030import java.util.Iterator;
031
032import static org.fcrepo.kernel.impl.identifiers.NodeResourceConverter.nodeConverter;
033
034/**
035 * @author cabeer
036 * @since 10/9/14
037 */
038public class HashRdfContext extends NodeRdfContext {
039
040
041    /**
042     * Default constructor.
043     *
044     * @param resource the resource
045     * @param idTranslator the id translator
046     * @throws javax.jcr.RepositoryException if repository exception occurred
047     */
048    public HashRdfContext(final FedoraResource resource,
049                          final IdentifierConverter<Resource, FedoraResource> idTranslator)
050            throws RepositoryException {
051        super(resource, idTranslator);
052
053        final Node node = resource().getNode();
054        if (node.hasNode("#")) {
055            final Iterator<Node> hashChildrenNodes = node.getNode("#").getNodes();
056            concat(Iterators.concat(Iterators.transform(hashChildrenNodes,
057                    new Function<Node, Iterator<Triple>>() {
058                        @Override
059                        public Iterator<Triple> apply(final Node input) {
060                            final FedoraResource resource = nodeConverter.convert(input);
061
062                            return resource.getTriples(idTranslator, ImmutableList.of(TypeRdfContext.class,
063                                    PropertiesRdfContext.class,
064                                    BlankNodeRdfContext.class));
065                        }
066                    })));
067        }
068    }
069}