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