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.services;
019
020import static java.util.stream.Stream.empty;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.stream.Stream;
025
026import javax.inject.Inject;
027
028import org.fcrepo.kernel.api.Transaction;
029import org.fcrepo.kernel.api.models.FedoraResource;
030import org.fcrepo.kernel.api.rdf.LdpTriplePreferences;
031import org.fcrepo.kernel.api.services.ContainmentTriplesService;
032import org.fcrepo.kernel.api.services.ManagedPropertiesService;
033import org.fcrepo.kernel.api.services.MembershipService;
034import org.fcrepo.kernel.api.services.ReferenceService;
035import org.fcrepo.kernel.api.services.ResourceTripleService;
036
037import org.apache.jena.graph.Triple;
038import org.springframework.beans.factory.annotation.Autowired;
039import org.springframework.beans.factory.annotation.Qualifier;
040import org.springframework.stereotype.Component;
041
042/**
043 * Implementation of the ResourceTripleService
044 * @author whikloj
045 * @since 6.0.0
046 */
047@Component
048public class ResourceTripleServiceImpl implements ResourceTripleService {
049
050    @Inject
051    private ManagedPropertiesService managedPropertiesService;
052
053    @Inject
054    private ContainmentTriplesService containmentTriplesService;
055
056    @Autowired
057    @Qualifier("referenceService")
058    private ReferenceService referenceService;
059
060    @Inject
061    private MembershipService membershipService;
062
063    @Override
064    public Stream<Triple> getResourceTriples(final Transaction tx, final FedoraResource resource,
065                                             final LdpTriplePreferences preferences, final int limit) {
066        final List<Stream<Triple>> streams = new ArrayList<>();
067
068        // Provide user RDF if we didn't ask for omit=ldp:PreferMinimalContainer.
069        if (preferences.displayUserRdf()) {
070            streams.add(resource.getTriples());
071        }
072        // Provide server-managed triples if we didn't ask for omit=fedora:ServerManaged or
073        // omit=ldp:PreferMinimalContainer
074        if (preferences.displayServerManaged()) {
075            streams.add(this.managedPropertiesService.get(resource));
076        }
077        // containment triples about this resource, return by default. Containment is server managed so also
078        // check for that prefer tag.
079        if (preferences.displayContainment()) {
080            if (limit == -1) {
081                streams.add(this.containmentTriplesService.get(tx, resource));
082            } else {
083                streams.add(this.containmentTriplesService.get(tx, resource).limit(limit));
084            }
085        }
086
087        // LDP container membership triples for this resource, returned by default. Membership is server managed so
088        // also check that tag.
089        if (preferences.displayMembership()) {
090            streams.add(membershipService.getMembership(tx, resource.getFedoraId()));
091        }
092
093        // Include inbound references to this object, NOT returned by default.
094        if (preferences.displayReferences()) {
095            streams.add(referenceService.getInboundReferences(tx, resource));
096        }
097
098        return streams.stream().reduce(empty(), Stream::concat);
099    }
100
101}