001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.rdf.impl;
019
020import static org.fcrepo.kernel.modeshape.FedoraResourceImpl.fixDatesIfNecessary;
021import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeToResource;
022import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
023import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isInternalProperty;
024import static org.fcrepo.kernel.modeshape.utils.StreamUtils.iteratorToStream;
025import static org.slf4j.LoggerFactory.getLogger;
026
027import java.util.stream.Stream;
028
029import javax.jcr.Node;
030import javax.jcr.RepositoryException;
031
032import com.google.common.base.Converter;
033import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
034import org.fcrepo.kernel.api.models.FedoraResource;
035import org.fcrepo.kernel.modeshape.rdf.impl.mappings.PropertyToTriple;
036
037import org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils;
038import org.slf4j.Logger;
039
040import org.apache.jena.graph.Triple;
041import org.apache.jena.rdf.model.Resource;
042
043/**
044 * {@link NodeRdfContext} for RDF that derives from JCR properties on a Resource
045 *
046 * @author ajs6f
047 * @since Oct 10, 2013
048 */
049public class PropertiesRdfContext extends NodeRdfContext {
050
051    private static final Logger LOGGER = getLogger(PropertiesRdfContext.class);
052
053    /**
054     * Default constructor.
055     *
056     * @param resource the resource
057     * @param idTranslator the id translator
058     * @throws RepositoryException if repository exception occurred
059     */
060
061    public PropertiesRdfContext(final FedoraResource resource,
062                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
063        throws RepositoryException {
064        super(resource, idTranslator);
065        concat(triplesFromProperties(resource, nodeToResource(translator()),
066                new PropertyToTriple(getJcrNode(resource).getSession(), translator())));
067    }
068
069    @SuppressWarnings("unchecked")
070    private static Stream<Triple> triplesFromProperties(final FedoraResource n,
071                                                        final Converter<Node, Resource> translator,
072                                                        final PropertyToTriple propertyToTriple)
073            throws RepositoryException {
074        LOGGER.trace("Creating triples for node: {}", n);
075        return iteratorToStream(getJcrNode(n).getProperties())
076            .filter(isInternalProperty.negate().or(new FedoraTypesUtils.IsExposedJCRPropertyPredicate(n)))
077            .flatMap(propertyToTriple).map(fixDatesIfNecessary(n, translator));
078    }
079
080}