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 org.apache.jena.datatypes.xsd.XSDDatatype.XSDlong;
021import static org.apache.jena.datatypes.xsd.impl.XSDDateTimeType.XSDdateTime;
022import static org.apache.jena.graph.NodeFactory.createLiteral;
023import static org.apache.jena.graph.NodeFactory.createURI;
024import static org.apache.jena.vocabulary.RDF.type;
025import static org.fcrepo.kernel.api.RdfLexicon.CREATED_BY;
026import static org.fcrepo.kernel.api.RdfLexicon.CREATED_DATE;
027import static org.fcrepo.kernel.api.RdfLexicon.HAS_MESSAGE_DIGEST;
028import static org.fcrepo.kernel.api.RdfLexicon.HAS_MIME_TYPE;
029import static org.fcrepo.kernel.api.RdfLexicon.HAS_ORIGINAL_NAME;
030import static org.fcrepo.kernel.api.RdfLexicon.HAS_SIZE;
031import static org.fcrepo.kernel.api.RdfLexicon.LAST_MODIFIED_BY;
032import static org.fcrepo.kernel.api.RdfLexicon.LAST_MODIFIED_DATE;
033
034import java.util.ArrayList;
035import java.util.List;
036import java.util.stream.Stream;
037
038import org.apache.jena.graph.Triple;
039import org.fcrepo.kernel.api.models.Binary;
040import org.fcrepo.kernel.api.models.FedoraResource;
041import org.fcrepo.kernel.api.models.NonRdfSourceDescription;
042import org.fcrepo.kernel.api.models.TimeMap;
043import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
044import org.fcrepo.kernel.api.services.ManagedPropertiesService;
045import org.springframework.stereotype.Component;
046
047/**
048 * Retrieve the managed properties as triples
049 *
050 * @author dbernstein
051 * @since 2020-01-07
052 */
053@Component
054public class ManagedPropertiesServiceImpl implements ManagedPropertiesService {
055
056    @Override
057    public Stream<Triple> get(final FedoraResource resource) {
058        final List<Triple> triples = new ArrayList<>();
059        final var subject = createURI(resolveId(resource.getDescribedResource()));
060        triples.add(Triple.create(subject, CREATED_DATE.asNode(),
061                createLiteral(resource.getCreatedDate().toString(), XSDdateTime)));
062        triples.add(Triple.create(subject, LAST_MODIFIED_DATE.asNode(),
063                createLiteral(resource.getLastModifiedDate().toString(), XSDdateTime)));
064        if (resource.getCreatedBy() != null) {
065            triples.add(Triple.create(subject, CREATED_BY.asNode(), createLiteral(resource.getCreatedBy())));
066        }
067        if (resource.getLastModifiedBy() != null) {
068            triples.add(Triple.create(subject, LAST_MODIFIED_BY.asNode(), createLiteral(resource.getLastModifiedBy())));
069        }
070
071        resource.getDescribedResource().getSystemTypes(true).forEach(triple -> {
072            triples.add(Triple.create(subject, type.asNode(), createURI(triple.toString())));
073        });
074        if (resource instanceof NonRdfSourceDescription) {
075            final Binary binary = (Binary) resource.getDescribedResource();
076
077            triples.add(Triple.create(subject, HAS_SIZE.asNode(),
078                    createLiteral(String.valueOf(binary.getContentSize()), XSDlong)));
079            if (binary.getFilename() != null) {
080                triples.add(Triple.create(subject, HAS_ORIGINAL_NAME.asNode(), createLiteral(binary.getFilename())));
081            }
082            if (binary.getMimeType() != null) {
083                triples.add(Triple.create(subject, HAS_MIME_TYPE.asNode(), createLiteral(binary.getMimeType())));
084            }
085            if (binary.getContentDigests() != null) {
086                for (var digest : binary.getContentDigests()) {
087                    triples.add(Triple.create(subject, HAS_MESSAGE_DIGEST.asNode(),
088                            createURI(digest.toString())));
089                }
090
091            }
092        }
093
094        return new DefaultRdfStream(subject, triples.stream());
095    }
096
097    private String resolveId(final FedoraResource resource) {
098        if (resource instanceof TimeMap) {
099            return resource.getFedoraId().getFullId();
100        }
101        return resource.getId();
102    }
103
104}