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