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 java.util.stream.Stream.of;
021import static org.apache.jena.graph.NodeFactory.createLiteral;
022import static org.apache.jena.graph.Triple.create;
023import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
024import static org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral;
025import static org.fcrepo.kernel.api.FedoraTypes.FCR_VERSIONS;
026import static org.fcrepo.kernel.api.RdfLexicon.CREATED_DATE;
027import static org.fcrepo.kernel.api.RdfLexicon.HAS_VERSION;
028import static org.fcrepo.kernel.api.RdfLexicon.HAS_VERSION_LABEL;
029import static org.slf4j.LoggerFactory.getLogger;
030
031import java.util.Calendar;
032import java.util.stream.Stream;
033
034import org.apache.jena.rdf.model.Resource;
035import org.modeshape.common.text.UrlEncoder;
036
037import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
038import org.fcrepo.kernel.api.models.FedoraResource;
039import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
040
041import org.apache.jena.graph.Node;
042import org.apache.jena.graph.Triple;
043
044import org.slf4j.Logger;
045
046
047/**
048 * {@link org.fcrepo.kernel.api.RdfStream} that supplies {@link Triple}s concerning
049 * the versions of a selected {@link Node}.
050 *
051 * @author ajs6f
052 * @since Oct 15, 2013
053 */
054public class VersionsRdfContext extends DefaultRdfStream {
055
056    private final FedoraResource resource;
057
058    private static final Logger LOGGER = getLogger(VersionsRdfContext.class);
059
060    private static final UrlEncoder URL_ENCODER = new UrlEncoder();
061
062    /**
063     * Ordinary constructor.
064     *
065     * @param resource the resource
066     * @param idTranslator the id translator
067     */
068    public VersionsRdfContext(final FedoraResource resource,
069                              final IdentifierConverter<Resource, FedoraResource> idTranslator) {
070        super(idTranslator.reverse().convert(resource).asNode());
071        this.resource = resource;
072        concat(versionTriples());
073    }
074
075    @SuppressWarnings("unchecked")
076    private Stream<Triple> versionTriples() {
077        return resource.getVersions()
078            .flatMap(version -> {
079                final Node versionSubject = createProperty(topic() + "/" + FCR_VERSIONS + "/" +
080                        urlEncode(version.getIdentifier())).asNode();
081
082                final Calendar cal = new Calendar.Builder().setInstant(version.getCreated().toEpochMilli()).build();
083                return of(
084                        create(topic(), HAS_VERSION.asNode(), versionSubject),
085                        create(versionSubject, HAS_VERSION_LABEL.asNode(), createLiteral(version.getIdentifier())),
086                        create(versionSubject, CREATED_DATE.asNode(), createTypedLiteral(cal).asNode()));
087            });
088    }
089
090    private String urlEncode(final String string) {
091        return URL_ENCODER.encode(string).replaceAll("[+]", "%20");
092    }
093}