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