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.modeshape.utils.StreamUtils.iteratorToStream;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import java.util.stream.Stream;
023
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.hp.hpl.jena.graph.Triple;
033import com.hp.hpl.jena.rdf.model.Resource;
034
035/**
036 * {@link NodeRdfContext} for RDF that derives from JCR properties on a Resource
037 *
038 * @author ajs6f
039 * @since Oct 10, 2013
040 */
041public class PropertiesRdfContext extends NodeRdfContext {
042
043    private static final Logger LOGGER = getLogger(PropertiesRdfContext.class);
044
045    /**
046     * Default constructor.
047     *
048     * @param resource the resource
049     * @param idTranslator the id translator
050     * @throws RepositoryException if repository exception occurred
051     */
052
053    public PropertiesRdfContext(final FedoraResource resource,
054                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
055        throws RepositoryException {
056        super(resource, idTranslator);
057        concat(triplesFromProperties(resource,
058                new PropertyToTriple(resource.getNode().getSession(), translator())));
059    }
060
061    @SuppressWarnings("unchecked")
062    private static Stream<Triple> triplesFromProperties(final FedoraResource n, final PropertyToTriple propertyToTriple)
063            throws RepositoryException {
064        LOGGER.trace("Creating triples for node: {}", n);
065        return iteratorToStream(n.getNode().getProperties())
066            .filter(isInternalProperty.negate())
067            .flatMap(propertyToTriple);
068    }
069}