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