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