001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.impl.services;
007
008import org.apache.jena.graph.Node;
009import org.apache.jena.graph.Triple;
010import org.fcrepo.kernel.api.ContainmentIndex;
011import org.fcrepo.kernel.api.Transaction;
012import org.fcrepo.kernel.api.models.FedoraResource;
013import org.fcrepo.kernel.api.services.ContainmentTriplesService;
014import org.springframework.beans.factory.annotation.Autowired;
015import org.springframework.beans.factory.annotation.Qualifier;
016import org.springframework.stereotype.Component;
017
018import java.util.stream.Stream;
019
020import static org.apache.jena.graph.NodeFactory.createURI;
021import static org.fcrepo.kernel.api.RdfLexicon.CONTAINS;
022
023/**
024 * Containment Triples service.
025 * @author whikloj
026 * @since 6.0.0
027 */
028@Component
029public class ContainmentTriplesServiceImpl implements ContainmentTriplesService {
030
031    @Autowired
032    @Qualifier("containmentIndex")
033    private ContainmentIndex containmentIndex;
034
035    @Override
036    public Stream<Triple> get(final Transaction tx, final FedoraResource resource) {
037        final var fedoraId = resource.getFedoraId();
038        final var nodeUri = fedoraId.isMemento() ? fedoraId.getBaseId() : fedoraId.getFullId();
039        final Node currentNode = createURI(nodeUri);
040        return containmentIndex.getContains(tx, fedoraId).map(c ->
041                new Triple(currentNode, CONTAINS.asNode(), createURI(c)));
042    }
043
044}