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.FedoraBinary;
030import org.fcrepo.kernel.api.models.FedoraResource;
031import org.fcrepo.kernel.modeshape.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;
038import com.hp.hpl.jena.rdf.model.Resource;
039
040/**
041 * {@link NodeRdfContext} for RDF that derives from JCR properties on a Resource
042 *
043 * @author ajs6f
044 * @since Oct 10, 2013
045 */
046public class PropertiesRdfContext extends NodeRdfContext {
047
048    private final PropertyToTriple property2triple;
049
050    private static final Logger LOGGER = getLogger(PropertiesRdfContext.class);
051
052    private static final Predicate<Property> IS_NOT_UUID = uncheck(p -> !p.getName().equals("jcr:uuid"));
053
054    /**
055     * Default constructor.
056     *
057     * @param resource the resource
058     * @param idTranslator the id translator
059     * @throws RepositoryException if repository exception occurred
060     */
061
062    public PropertiesRdfContext(final FedoraResource resource,
063                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
064        throws RepositoryException {
065        super(resource, idTranslator);
066        property2triple = new PropertyToTriple(resource.getNode().getSession(), idTranslator);
067        concat(triplesFromProperties(resource()));
068    }
069
070    private Iterator<Triple> triplesFromProperties(final FedoraResource n)
071        throws RepositoryException {
072        LOGGER.trace("Creating triples for node: {}", n);
073
074        final Iterator<Property> allProperties;
075        if (n instanceof FedoraBinary) {
076            final FedoraResource description = ((FedoraBinary)n).getDescription();
077            allProperties = Iterators.concat(n.getNode().getProperties(), description.getNode().getProperties());
078        } else {
079            allProperties = n.getNode().getProperties();
080        }
081
082        final UnmodifiableIterator<Property> properties =
083                Iterators.filter(allProperties, isInternalProperty.negate().and(IS_NOT_UUID)::test);
084        return Iterators.concat(Iterators.transform(properties, property2triple));
085
086    }
087
088}