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