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.time.Instant.now;
021import static org.apache.jena.graph.NodeFactory.createLiteral;
022import static org.apache.jena.graph.NodeFactory.createURI;
023import static org.apache.jena.graph.Triple.create;
024import static org.apache.jena.rdf.model.ResourceFactory.createTypedLiteral;
025import static org.apache.jena.vocabulary.RDF.type;
026import static org.fcrepo.kernel.api.RdfLexicon.FIXITY_TYPE;
027import static org.fcrepo.kernel.api.RdfLexicon.HAS_MESSAGE_DIGEST;
028import static org.fcrepo.kernel.api.RdfLexicon.HAS_MESSAGE_DIGEST_ALGORITHM;
029import static org.fcrepo.kernel.api.RdfLexicon.HAS_SIZE;
030import static org.fcrepo.kernel.api.RdfLexicon.EVENT_OUTCOME_INFORMATION;
031import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_RESULT;
032import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_STATE;
033import static org.fcrepo.kernel.modeshape.utils.UncheckedFunction.uncheck;
034
035import java.net.URI;
036import java.util.ArrayList;
037import java.util.List;
038import java.util.stream.StreamSupport;
039
040import org.apache.jena.rdf.model.Resource;
041import org.fcrepo.kernel.api.models.FedoraResource;
042import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
043import org.fcrepo.kernel.api.utils.FixityResult;
044import org.apache.jena.graph.Triple;
045
046/**
047 * An {@link org.fcrepo.kernel.api.RdfStream} containing information about the fixity of a
048 * {@link org.fcrepo.kernel.api.models.FedoraBinary}.
049 *
050 * @author ajs6f
051 * @since Oct 15, 2013
052 */
053public class FixityRdfContext extends NodeRdfContext {
054
055    /**
056     * Ordinary constructor.
057     *
058     * @param resource the resource
059     * @param idTranslator the id translator
060     * @param blobs the blobs
061     * @param digest the digest uri
062     * @param size the size
063     */
064    public FixityRdfContext(final FedoraResource resource,
065                            final IdentifierConverter<Resource, FedoraResource> idTranslator,
066                            final Iterable<FixityResult> blobs,
067                            final URI digest,
068                            final long size) {
069        super(resource, idTranslator);
070
071        concat(StreamSupport.stream(blobs.spliterator(), false).flatMap(uncheck(blob -> {
072            final org.apache.jena.graph.Node resultSubject =
073                    createURI(subject().getURI() + "#fixity/" + now().toEpochMilli());
074            final List<Triple> b = new ArrayList<>();
075
076            b.add(create(subject(), HAS_FIXITY_RESULT.asNode(), resultSubject));
077            b.add(create(resultSubject, type.asNode(), FIXITY_TYPE.asNode()));
078            b.add(create(resultSubject, type.asNode(), EVENT_OUTCOME_INFORMATION.asNode()));
079
080            blob.getStatus(size, digest).stream().map(state -> createLiteral(state.toString()))
081                    .map(state -> create(resultSubject, HAS_FIXITY_STATE.asNode(), state)).forEach(b::add);
082
083            b.add(create(resultSubject, HAS_MESSAGE_DIGEST_ALGORITHM.asNode(),
084                    createTypedLiteral(blob.getUsedAlgorithm()).asNode()));
085            b.add(create(resultSubject, HAS_MESSAGE_DIGEST.asNode(), createURI(blob.getComputedChecksum().toString())));
086            b.add(create(resultSubject, HAS_SIZE.asNode(), createTypedLiteral(blob.getComputedSize()).asNode()));
087
088            return b.stream();
089        })));
090    }
091
092}