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.impl.rdf.impl;
017
018import static com.google.common.base.Throwables.propagate;
019import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
020import static com.hp.hpl.jena.graph.Triple.create;
021import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral;
022import static org.fcrepo.kernel.RdfLexicon.CREATED_DATE;
023import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
024import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION_LABEL;
025import static org.fcrepo.kernel.impl.identifiers.NodeResourceConverter.nodeToResource;
026import static org.slf4j.LoggerFactory.getLogger;
027
028import java.util.Iterator;
029
030import javax.jcr.Node;
031import javax.jcr.RepositoryException;
032import javax.jcr.version.Version;
033import javax.jcr.version.VersionHistory;
034import com.hp.hpl.jena.rdf.model.Resource;
035
036import org.fcrepo.kernel.models.FedoraResource;
037import org.fcrepo.kernel.identifiers.IdentifierConverter;
038import org.fcrepo.kernel.utils.iterators.RdfStream;
039
040import com.google.common.base.Function;
041import com.google.common.collect.Iterators;
042import com.hp.hpl.jena.graph.Triple;
043
044import org.slf4j.Logger;
045
046
047/**
048 * {@link 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 RdfStream {
055
056    private final VersionHistory versionHistory;
057
058    private final IdentifierConverter<Resource, FedoraResource> idTranslator;
059
060    private final com.hp.hpl.jena.graph.Node subject;
061
062    private static final Logger LOGGER = getLogger(VersionsRdfContext.class);
063
064    /**
065     * Ordinary constructor.
066     *
067     * @param resource the resource
068     * @param idTranslator the id translator
069     * @throws RepositoryException if repository exception occurred
070     */
071    public VersionsRdfContext(final FedoraResource resource,
072                              final IdentifierConverter<Resource, FedoraResource> idTranslator)
073        throws RepositoryException {
074        super();
075        this.idTranslator = idTranslator;
076        this.subject = idTranslator.reverse().convert(resource).asNode();
077        versionHistory = resource.getVersionHistory();
078
079        concat(versionTriples());
080    }
081
082    private Iterator<Triple> versionTriples() throws RepositoryException {
083        final Iterator<Version> allVersions = versionHistory.getAllVersions();
084        return Iterators.concat(Iterators.transform(allVersions, version2triples));
085    }
086
087    private final Function<Version, Iterator<Triple>> version2triples = new Version2Triples();
088
089    private class Version2Triples implements Function<Version, Iterator<Triple>> {
090
091        @Override
092        public Iterator<Triple> apply(final Version version) {
093
094            try {
095                    /* Discard jcr:rootVersion */
096                if (version.getName().equals(versionHistory.getRootVersion().getName())) {
097                    LOGGER.trace("Skipped root version from triples");
098                    return new RdfStream();
099                }
100
101                final Node frozenNode = version.getFrozenNode();
102                final com.hp.hpl.jena.graph.Node versionSubject
103                        = nodeToResource(idTranslator).convert(frozenNode).asNode();
104
105                final RdfStream results = new RdfStream();
106
107                results.concat(create(subject, HAS_VERSION.asNode(),
108                        versionSubject));
109
110                for (final String label : versionHistory
111                        .getVersionLabels(version)) {
112                    results.concat(create(versionSubject, HAS_VERSION_LABEL
113                            .asNode(), createLiteral(label)));
114                }
115                results.concat(create(versionSubject, CREATED_DATE.asNode(),
116                        createTypedLiteral(version.getCreated()).asNode()));
117
118                return results;
119
120            } catch (final RepositoryException e) {
121                throw propagate(e);
122            }
123        }
124    }
125
126
127}