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 javax.inject.Inject;
023
024import java.util.ArrayList;
025import java.util.List;
026import java.util.stream.Stream;
027
028import org.apache.jena.graph.Triple;
029import org.fcrepo.kernel.api.Transaction;
030import org.fcrepo.kernel.api.models.FedoraResource;
031import org.fcrepo.kernel.api.rdf.LdpTriplePreferences;
032import org.fcrepo.kernel.api.services.ContainmentTriplesService;
033import org.fcrepo.kernel.api.services.ManagedPropertiesService;
034import org.fcrepo.kernel.api.services.MembershipService;
035import org.fcrepo.kernel.api.services.ReferenceService;
036import org.fcrepo.kernel.api.services.ResourceTripleService;
037import org.springframework.beans.factory.annotation.Autowired;
038import org.springframework.beans.factory.annotation.Qualifier;
039import org.springframework.stereotype.Component;
040
041/**
042 * Implementation of the ResourceTripleService
043 * @author whikloj
044 * @since 6.0.0
045 */
046@Component
047public class ResourceTripleServiceImpl implements ResourceTripleService {
048
049    @Inject
050    private ManagedPropertiesService managedPropertiesService;
051
052    @Inject
053    private ContainmentTriplesService containmentTriplesService;
054
055    @Autowired
056    @Qualifier("referenceService")
057    private ReferenceService referenceService;
058
059    @Inject
060    private MembershipService membershipService;
061
062    @Override
063    public Stream<Triple> getResourceTriples(final Transaction tx, final FedoraResource resource,
064                                             final LdpTriplePreferences preferences, final int limit) {
065        final List<Stream<Triple>> streams = new ArrayList<>();
066
067        if (!preferences.preferNoUserRdf()) {
068            // provide user RDF only if we didn't receive an omit=ldp:PreferMinimalContainer
069            streams.add(resource.getTriples());
070        }
071        if (preferences.getMinimal()) {
072            if (preferences.prefersServerManaged())  {
073                streams.add(this.managedPropertiesService.get(resource));
074                //TODO Implement minimal return preference (https://jira.lyrasis.org/browse/FCREPO-3334)
075                //streams.add(getTriples(resource, MINIMAL));
076            }
077        } else {
078
079            // Additional server-managed triples about this resource
080            if (preferences.prefersServerManaged()) {
081                streams.add(this.managedPropertiesService.get(resource));
082            }
083
084            // containment triples about this resource
085            if (preferences.prefersContainment()) {
086                if (limit == -1) {
087                    streams.add(this.containmentTriplesService.get(tx, resource));
088                } else {
089                    streams.add(this.containmentTriplesService.get(tx, resource).limit(limit));
090                }
091            }
092
093            // LDP container membership triples for this resource
094            if (preferences.prefersMembership()) {
095                streams.add(membershipService.getMembership(tx.getId(), resource.getFedoraId()));
096            }
097
098            // Include inbound references to this object
099            if (preferences.prefersReferences()) {
100                streams.add(referenceService.getInboundReferences(tx.getId(), resource));
101            }
102
103        }
104
105        return streams.stream().reduce(empty(), Stream::concat);
106    }
107
108}