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