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 org.fcrepo.kernel.identifiers.IdentifierConverter;
028import org.fcrepo.kernel.impl.rdf.impl.mappings.PropertyToTriple;
029import org.fcrepo.kernel.models.FedoraBinary;
030import org.fcrepo.kernel.models.FedoraResource;
031
032import org.slf4j.Logger;
033
034import com.google.common.collect.Iterators;
035import com.google.common.collect.UnmodifiableIterator;
036import com.hp.hpl.jena.graph.Triple;
037import com.hp.hpl.jena.rdf.model.Resource;
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 the resource
055     * @param idTranslator the id translator
056     * @throws RepositoryException if repository exception occurred
057     */
058
059    public PropertiesRdfContext(final FedoraResource resource,
060                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
061        throws RepositoryException {
062        super(resource, idTranslator);
063        property2triple = new PropertyToTriple(resource.getNode().getSession(), idTranslator);
064        concat(triplesFromProperties(resource()));
065    }
066
067    private Iterator<Triple> triplesFromProperties(final FedoraResource n)
068        throws RepositoryException {
069        LOGGER.trace("Creating triples for node: {}", n);
070
071        final Iterator<Property> allProperties;
072        if (n instanceof FedoraBinary) {
073            final FedoraResource description = ((FedoraBinary)n).getDescription();
074            allProperties = Iterators.concat(n.getNode().getProperties(), description.getNode().getProperties());
075        } else {
076            allProperties = n.getNode().getProperties();
077        }
078
079        final UnmodifiableIterator<Property> properties =
080                Iterators.filter(allProperties, not(isInternalProperty));
081        return Iterators.concat(Iterators.transform(properties, property2triple));
082
083    }
084
085}