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.impl.models;
019
020import org.apache.jena.graph.Node;
021import org.apache.jena.graph.Triple;
022import org.fcrepo.kernel.api.RdfStream;
023import org.fcrepo.kernel.api.Transaction;
024import org.fcrepo.kernel.api.exception.PathNotFoundException;
025import org.fcrepo.kernel.api.exception.PathNotFoundRuntimeException;
026import org.fcrepo.kernel.api.identifiers.FedoraId;
027import org.fcrepo.kernel.api.models.FedoraResource;
028import org.fcrepo.kernel.api.models.NonRdfSourceDescription;
029import org.fcrepo.kernel.api.models.ResourceFactory;
030import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
031import org.fcrepo.persistence.api.PersistentStorageSessionManager;
032
033import java.net.URI;
034import java.util.List;
035import java.util.stream.Stream;
036
037import static org.apache.jena.graph.NodeFactory.createURI;
038import static org.fcrepo.kernel.api.RdfLexicon.RDF_SOURCE;
039
040/**
041 * Implementation of a non-rdf source description
042 *
043 * @author bbpennel
044 */
045public class NonRdfSourceDescriptionImpl extends FedoraResourceImpl implements NonRdfSourceDescription {
046
047    private static final URI RDF_SOURCE_URI = URI.create(RDF_SOURCE.getURI());
048
049    /**
050     * Construct a description resource
051     *
052     * @param fedoraID internal identifier
053     * @param transaction transaction
054     * @param pSessionManager session manager
055     * @param resourceFactory resource factory
056     */
057    public NonRdfSourceDescriptionImpl(final FedoraId fedoraID,
058            final Transaction transaction,
059            final PersistentStorageSessionManager pSessionManager,
060            final ResourceFactory resourceFactory) {
061        super(fedoraID, transaction, pSessionManager, resourceFactory);
062    }
063
064    @Override
065    public String getId() {
066        return getFedoraId().getResourceId();
067    }
068
069    @Override
070    public FedoraResource getDescribedResource() {
071        // Get a FedoraId for the binary
072        final FedoraId describedId = FedoraId.create(this.getFedoraId().getBaseId());
073        try {
074            return this.resourceFactory.getResource(transaction, describedId);
075        } catch (final PathNotFoundException e) {
076            throw new PathNotFoundRuntimeException(e.getMessage(), e);
077        }
078    }
079
080    @Override
081    public List<URI> getSystemTypes(final boolean forRdf) {
082        var types = resolveSystemTypes(forRdf);
083
084        if (types == null) {
085            types = super.getSystemTypes(forRdf);
086            // NonRdfSource gets the ldp:Resource and adds ldp:RDFSource types.
087            types.add(RDF_SOURCE_URI);
088        }
089
090        return types;
091    }
092
093    @Override
094    public RdfStream getTriples() {
095        // Remap the subject to the described resource
096        final Node describedID = createURI(this.getDescribedResource().getId());
097        final Stream<Triple> triples = super.getTriples().map(t ->
098                new Triple(describedID, t.getPredicate(), t.getObject()));
099        return new DefaultRdfStream(describedID, triples);
100    }
101}