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 static com.google.common.base.Predicates.not;
019import static org.fcrepo.kernel.impl.utils.FedoraTypesUtils.isInternalProperty;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.util.Iterator;
023
024import javax.jcr.Property;
025import javax.jcr.RepositoryException;
026
027import com.hp.hpl.jena.rdf.model.Resource;
028
029import org.fcrepo.kernel.models.FedoraResource;
030import org.fcrepo.kernel.identifiers.IdentifierConverter;
031import org.fcrepo.kernel.impl.rdf.impl.mappings.PropertyToTriple;
032
033import org.slf4j.Logger;
034
035import com.google.common.collect.Iterators;
036import com.google.common.collect.UnmodifiableIterator;
037import com.hp.hpl.jena.graph.Triple;
038
039/**
040 * {@link NodeRdfContext} for RDF that derives from JCR properties on a Resource
041 *
042 * @author ajs6f
043 * @since Oct 10, 2013
044 */
045public class PropertiesRdfContext extends NodeRdfContext {
046
047    private final PropertyToTriple property2triple;
048
049    private static final Logger LOGGER = getLogger(PropertiesRdfContext.class);
050
051    /**
052     * Default constructor.
053     *
054     * @param resource
055     * @throws RepositoryException
056     */
057
058    public PropertiesRdfContext(final FedoraResource resource,
059                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
060        throws RepositoryException {
061        super(resource, idTranslator);
062        property2triple = new PropertyToTriple(resource.getNode().getSession(), idTranslator);
063        concat(triplesFromProperties(resource()));
064    }
065
066    private Iterator<Triple> triplesFromProperties(final FedoraResource n)
067        throws RepositoryException {
068        LOGGER.trace("Creating triples for node: {}", n);
069        final Iterator<Property> allProperties = n.getNode().getProperties();
070        final UnmodifiableIterator<Property> properties =
071            Iterators.filter(allProperties, not(isInternalProperty));
072
073        return Iterators.concat(Iterators.transform(properties, property2triple));
074
075    }
076
077}