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.modeshape.rdf.impl;
019
020import static java.util.stream.Stream.of;
021import static org.apache.jena.graph.NodeFactory.createURI;
022import static org.apache.jena.graph.Triple.create;
023import static org.apache.jena.vocabulary.RDF.type;
024import static org.fcrepo.kernel.api.FedoraTypes.FEDORA_TIME_MAP;
025import static org.fcrepo.kernel.api.RdfLexicon.REPOSITORY_NAMESPACE;
026import static org.fcrepo.kernel.api.RdfLexicon.VERSIONING_TIMEMAP_TYPE;
027import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isInternalType;
028
029import org.apache.jena.rdf.model.Resource;
030
031import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
032import org.fcrepo.kernel.api.models.FedoraResource;
033
034import java.net.URI;
035import java.util.List;
036import java.util.stream.Collectors;
037
038/**
039 * @author cabeer
040 * @author ajs6f
041 * @since 10/1/14
042 */
043public class TypeRdfContext extends NodeRdfContext {
044
045    /**
046     * Full URI for internal fedora:TimeMap type.
047     */
048    private static URI fedoraTimemap = URI.create(FEDORA_TIME_MAP.replace("fedora:", REPOSITORY_NAMESPACE));
049
050    /**
051     * Default constructor.
052     *
053     * @param resource the resource
054     * @param idTranslator the id translator
055     */
056    public TypeRdfContext(final FedoraResource resource,
057                          final IdentifierConverter<Resource, FedoraResource> idTranslator) {
058        super(resource, idTranslator);
059
060        final List<URI> typeStream =
061                resource.getTypes().stream().filter(isInternalType.negate()).collect(Collectors.toList());
062
063        concat(typeStream.stream().map(uri -> create(subject(), type.asNode(), createURI(uri.toString()))));
064
065        // If has rdf:type fedora:TimeMap and no memento:TimeMap, add a memento:TimeMap type.
066        // https://jira.duraspace.org/browse/FCREPO-3006
067        final boolean hasTimeMap = typeStream.stream().anyMatch(uri -> uri.equals(fedoraTimemap)) &&
068                typeStream.stream().noneMatch(uri -> uri.equals(URI.create(VERSIONING_TIMEMAP_TYPE)));
069        if (hasTimeMap) {
070            concat(of(create(subject(), type.asNode(), createURI(VERSIONING_TIMEMAP_TYPE))));
071        }
072    }
073}