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 * An {@link NodeRdfContext} 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 final Logger LOGGER = getLogger(VersionsRdfContext.class);
063
064    /**
065     * Ordinary constructor.
066     *
067     * @param resource
068     * @param idTranslator
069     * @throws RepositoryException
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 =
088        new Function<Version, Iterator<Triple>>() {
089
090            @Override
091            public Iterator<Triple> apply(final Version version) {
092
093                try {
094                    /* Discard jcr:rootVersion */
095                    if (version.getName().equals(versionHistory.getRootVersion().getName())) {
096                        LOGGER.trace("Skipped root version from triples");
097                        return new RdfStream();
098                    }
099
100                    final Node frozenNode = version.getFrozenNode();
101                    final com.hp.hpl.jena.graph.Node versionSubject
102                            = nodeToResource(idTranslator).convert(frozenNode).asNode();
103
104                    final RdfStream results = new RdfStream();
105
106                    results.concat(create(subject, HAS_VERSION.asNode(),
107                            versionSubject));
108
109                    for (final String label : versionHistory
110                            .getVersionLabels(version)) {
111                        results.concat(create(versionSubject, HAS_VERSION_LABEL
112                                .asNode(), createLiteral(label)));
113                    }
114                    results.concat(create(versionSubject, CREATED_DATE.asNode(),
115                            createTypedLiteral(version.getCreated()).asNode()));
116
117                    return results;
118
119                } catch (final RepositoryException e) {
120                    throw propagate(e);
121                }
122            }
123
124        };
125
126
127}