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.api.FedoraTypes.FEDORA_LASTMODIFIED;
021import static org.fcrepo.kernel.modeshape.FedoraJcrConstants.JCR_LASTMODIFIED;
022import static org.fcrepo.kernel.modeshape.FedoraResourceImpl.fixDatesIfNecessary;
023import static org.fcrepo.kernel.modeshape.identifiers.NodeResourceConverter.nodeToResource;
024import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
025import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isInternalProperty;
026import static org.fcrepo.kernel.modeshape.utils.StreamUtils.iteratorToStream;
027import static org.fcrepo.kernel.modeshape.utils.UncheckedPredicate.uncheck;
028import static org.slf4j.LoggerFactory.getLogger;
029
030import java.util.stream.Stream;
031
032import javax.jcr.Node;
033import javax.jcr.RepositoryException;
034
035import com.google.common.base.Converter;
036import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
037import org.fcrepo.kernel.api.models.FedoraResource;
038import org.fcrepo.kernel.modeshape.rdf.impl.mappings.PropertyToTriple;
039
040import org.slf4j.Logger;
041
042import com.hp.hpl.jena.graph.Triple;
043import com.hp.hpl.jena.rdf.model.Resource;
044
045/**
046 * {@link NodeRdfContext} for RDF that derives from JCR properties on a Resource
047 *
048 * @author ajs6f
049 * @since Oct 10, 2013
050 */
051public class PropertiesRdfContext extends NodeRdfContext {
052
053    private static final Logger LOGGER = getLogger(PropertiesRdfContext.class);
054
055    /**
056     * Default constructor.
057     *
058     * @param resource the resource
059     * @param idTranslator the id translator
060     * @throws RepositoryException if repository exception occurred
061     */
062
063    public PropertiesRdfContext(final FedoraResource resource,
064                                final IdentifierConverter<Resource, FedoraResource> idTranslator)
065        throws RepositoryException {
066        super(resource, idTranslator);
067        concat(triplesFromProperties(resource, nodeToResource(translator()),
068                new PropertyToTriple(getJcrNode(resource).getSession(), translator())));
069    }
070
071    @SuppressWarnings("unchecked")
072    private static Stream<Triple> triplesFromProperties(final FedoraResource n,
073                                                        final Converter<Node, Resource> translator,
074                                                        final PropertyToTriple propertyToTriple)
075            throws RepositoryException {
076        LOGGER.trace("Creating triples for node: {}", n);
077        return iteratorToStream(getJcrNode(n).getProperties())
078            .filter(isInternalProperty.negate().or(uncheck(prop ->
079                prop.getName().equals(JCR_LASTMODIFIED) && !n.hasProperty(FEDORA_LASTMODIFIED))))
080            .flatMap(propertyToTriple).map(fixDatesIfNecessary(n, translator));
081    }
082
083}