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.api;
019
020import static com.google.common.collect.ImmutableSet.of;
021import static org.apache.jena.rdf.model.ResourceFactory.createProperty;
022import static org.apache.jena.rdf.model.ResourceFactory.createResource;
023import static org.apache.jena.vocabulary.RDF.type;
024
025import java.util.Set;
026import java.util.function.Predicate;
027import java.util.stream.Collectors;
028
029import org.apache.jena.graph.Triple;
030import org.apache.jena.rdf.model.Property;
031import org.apache.jena.rdf.model.Resource;
032
033import com.google.common.collect.ImmutableSet;
034
035/**
036 * A lexicon of the RDF properties that the fcrepo kernel (or close-to-core modules) use
037 *
038 * @author ajs6f
039 */
040public final class RdfLexicon {
041
042    /**
043     * Repository namespace "fedora"
044    **/
045    public static final String REPOSITORY_NAMESPACE = "http://fedora.info/definitions/v4/repository#";
046
047    public static final String REPOSITORY_WEBAC_NAMESPACE = "http://fedora.info/definitions/v4/webac#";
048
049    public static final String FCREPO_API_NAMESPACE = "http://fedora.info/definitions/fcrepo#";
050
051    public static final String ACTIVITY_STREAMS_NAMESPACE = "https://www.w3.org/ns/activitystreams#";
052
053    public static final String EBUCORE_NAMESPACE = "http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#";
054
055    public static final String OA_NAMESPACE = "http://www.w3.org/ns/oa#";
056
057    public static final String PROV_NAMESPACE = "http://www.w3.org/ns/prov#";
058
059    public static final String PREMIS_NAMESPACE = "http://www.loc.gov/premis/rdf/v1#";
060
061    public static final String MEMENTO_NAMESPACE = "http://mementoweb.org/ns#";
062
063    public static final String RDF_NAMESPACE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
064
065
066    /**
067     * Namespace for the W3C WebAC vocabulary.
068     */
069    public static final String WEBAC_NAMESPACE_VALUE = "http://www.w3.org/ns/auth/acl#";
070
071    /**
072     * Linked Data Platform namespace.
073     */
074    public static final String LDP_NAMESPACE = "http://www.w3.org/ns/ldp#";
075
076    /**
077     * Is this namespace one that the repository manages?
078     */
079    public static final Predicate<String> isManagedNamespace = p -> p.equals(REPOSITORY_NAMESPACE) ||
080            p.equals(LDP_NAMESPACE) || p.equals(MEMENTO_NAMESPACE);
081
082    /**
083     * Tests if the triple has a predicate of rdf:type and an object with a managed namespace.
084     * @see RdfLexicon#isManagedNamespace
085     */
086    public static final Predicate<Triple> restrictedType = s -> s.getPredicate().equals(type.asNode()) &&
087            (s.getObject().isURI() && isManagedNamespace.test(s.getObject().getNameSpace()));
088
089    // FIXITY
090    public static final Resource PREMIS_FIXITY =
091            createResource(PREMIS_NAMESPACE + "Fixity");
092    public static final Resource PREMIS_EVENT_OUTCOME_DETAIL =
093            createResource(PREMIS_NAMESPACE + "EventOutcomeDetail");
094
095    public static final Property HAS_MESSAGE_DIGEST =
096            createProperty(PREMIS_NAMESPACE + "hasMessageDigest");
097
098    public static final Property HAS_SIZE =
099        createProperty(PREMIS_NAMESPACE + "hasSize");
100    public static final Property HAS_FIXITY_RESULT =
101        createProperty(PREMIS_NAMESPACE + "hasFixity");
102
103    private static final Set<Property> fixityProperties = of(
104            HAS_FIXITY_RESULT, HAS_MESSAGE_DIGEST);
105
106    public static final Property HAS_FIXITY_STATE =
107            createProperty(PREMIS_NAMESPACE + "hasEventOutcome");
108
109    public static final Property WRITABLE =
110            createProperty(REPOSITORY_NAMESPACE + "writable");
111
112    public static final String FEDORA_NON_RDF_SOURCE_DESCRIPTION_URI = REPOSITORY_NAMESPACE +
113            "NonRdfSourceDescription";
114
115    // Server managed properties
116    public static final Property CREATED_DATE =
117            createProperty(REPOSITORY_NAMESPACE + "created");
118    public static final Property CREATED_BY =
119            createProperty(REPOSITORY_NAMESPACE + "createdBy");
120    public static final Property LAST_MODIFIED_DATE =
121            createProperty(REPOSITORY_NAMESPACE + "lastModified");
122    public static final Property LAST_MODIFIED_BY =
123            createProperty(REPOSITORY_NAMESPACE + "lastModifiedBy");
124
125    public static final Resource FEDORA_CONTAINER =
126            createResource(REPOSITORY_NAMESPACE + "Container");
127    public static final Resource FEDORA_BINARY =
128            createResource(REPOSITORY_NAMESPACE + "Binary");
129    public static final Resource FEDORA_RESOURCE =
130            createResource(REPOSITORY_NAMESPACE + "Resource");
131    public static final Resource FEDORA_PAIR_TREE =
132            createResource(REPOSITORY_NAMESPACE + "Pairtree");
133    public static final Resource ARCHIVAL_GROUP =
134            createResource(REPOSITORY_NAMESPACE + "ArchivalGroup");
135
136    // Linked Data Platform
137    public static final Property PAGE =
138        createProperty(LDP_NAMESPACE + "Page");
139    public static final Resource CONTAINER =
140            createResource(LDP_NAMESPACE + "Container");
141    public static final Resource BASIC_CONTAINER =
142            createResource(LDP_NAMESPACE + "BasicContainer");
143    public static final Resource DIRECT_CONTAINER =
144            createResource(LDP_NAMESPACE + "DirectContainer");
145    public static final Resource INDIRECT_CONTAINER =
146            createResource(LDP_NAMESPACE + "IndirectContainer");
147    public static final Property MEMBERSHIP_RESOURCE =
148            createProperty(LDP_NAMESPACE + "membershipResource");
149    public static final Property HAS_MEMBER_RELATION =
150            createProperty(LDP_NAMESPACE + "hasMemberRelation");
151    public static final Property INSERTED_CONTENT_RELATION =
152            createProperty(LDP_NAMESPACE + "insertedContentRelation");
153    public static final Property IS_MEMBER_OF_RELATION =
154            createProperty(LDP_NAMESPACE + "isMemberOfRelation");
155    public static final Property CONTAINS =
156        createProperty(LDP_NAMESPACE + "contains");
157    public static final Property LDP_MEMBER =
158            createProperty(LDP_NAMESPACE + "member");
159    public static final Resource RESOURCE =
160            createResource(LDP_NAMESPACE + "Resource");
161    public static final Resource RDF_SOURCE =
162            createResource(LDP_NAMESPACE + "RDFSource");
163    public static final Resource NON_RDF_SOURCE =
164        createResource(LDP_NAMESPACE + "NonRDFSource");
165    public static final Property CONSTRAINED_BY =
166            createProperty(LDP_NAMESPACE + "constrainedBy");
167    public static final Property MEMBER_SUBJECT =
168            createProperty(LDP_NAMESPACE + "MemberSubject");
169
170    private static final Set<Property> ldpManagedProperties = of(CONTAINS);
171
172    // REPOSITORY INFORMATION
173    public static final Property HAS_TRANSACTION_SERVICE =
174            createProperty(REPOSITORY_NAMESPACE + "hasTransactionProvider");
175    public static final Resource REPOSITORY_ROOT =
176            createResource(REPOSITORY_NAMESPACE + "RepositoryRoot");
177
178    // OTHER SERVICES
179    public static final Property HAS_FIXITY_SERVICE =
180            createProperty(REPOSITORY_NAMESPACE + "hasFixityService");
181
182    public static final Property HAS_MIME_TYPE =
183            createProperty(EBUCORE_NAMESPACE + "hasMimeType");
184    public static final Property HAS_ORIGINAL_NAME =
185            createProperty(EBUCORE_NAMESPACE + "filename");
186
187    // EXTERNAL CONTENT
188    public static final Property EXTERNAL_CONTENT = createProperty(FCREPO_API_NAMESPACE + "ExternalContent");
189
190    // RDF EXTRACTION
191    public static final Property INBOUND_REFERENCES = createProperty(FCREPO_API_NAMESPACE + "PreferInboundReferences");
192    public static final Property PREFER_SERVER_MANAGED = createProperty(FCREPO_API_NAMESPACE + "ServerManaged");
193    public static final Property PREFER_MINIMAL_CONTAINER = createProperty(LDP_NAMESPACE +
194            "PreferMinimalContainer");
195    public static final Property PREFER_CONTAINMENT = createProperty(LDP_NAMESPACE + "PreferContainment");
196    public static final Property PREFER_MEMBERSHIP = createProperty(LDP_NAMESPACE + "PreferMembership");
197
198
199    public static final Property EMBED_CONTAINED = createProperty(OA_NAMESPACE + "PreferContainedDescriptions");
200
201
202    // WEBAC
203    public static final String WEBAC_ACCESS_CONTROL_VALUE = WEBAC_NAMESPACE_VALUE + "accessControl";
204
205    public static final String WEBAC_ACCESS_TO = WEBAC_NAMESPACE_VALUE + "accessTo";
206
207    public static final String WEBAC_ACCESS_TO_CLASS = WEBAC_NAMESPACE_VALUE + "accessToClass";
208
209    public static final Property WEBAC_ACCESS_TO_PROPERTY = createProperty(WEBAC_ACCESS_TO);
210
211    public static final String FEDORA_WEBAC_ACL_URI = REPOSITORY_WEBAC_NAMESPACE + "Acl";
212
213    // Properties which are managed by the server but are not from managed namespaces
214    private static final Set<Property> serverManagedProperties;
215    static {
216        final ImmutableSet.Builder<Property> b = ImmutableSet.builder();
217        b.addAll(fixityProperties).addAll(ldpManagedProperties);
218        serverManagedProperties = b.build();
219    }
220
221    private static final Predicate<Property> hasFedoraNamespace =
222        p -> !p.isAnon() && p.getNameSpace().startsWith(REPOSITORY_NAMESPACE);
223
224    private static Predicate<Property> hasMementoNamespace =
225        p -> !p.isAnon() && p.getNameSpace().startsWith(MEMENTO_NAMESPACE);
226
227    // Set of relaxable server managed properties
228    private static final Set<Property> serverManagedRelaxableProperties = Set.of(
229            CREATED_DATE, CREATED_BY, LAST_MODIFIED_DATE, LAST_MODIFIED_BY
230    );
231
232    /**
233     * Detects whether an RDF property is managed by the repository.
234     */
235    public static final Predicate<Property> isManagedPredicate =
236            hasFedoraNamespace.or(hasMementoNamespace).or(serverManagedProperties::contains);
237
238    /**
239     * Tests if a property is relaxable (if the server is in relaxed mode).
240     */
241    public static final Predicate<Property> isRelaxablePredicate = serverManagedRelaxableProperties::contains;
242
243    // VERSIONING
244    /**
245     * Memento TimeMap type.
246     */
247    public static final String VERSIONING_TIMEMAP_TYPE = MEMENTO_NAMESPACE + "TimeMap";
248    public static final Resource VERSIONING_TIMEMAP = createResource(VERSIONING_TIMEMAP_TYPE);
249
250    /**
251     * Memento TimeGate type.
252     */
253    public static final String VERSIONING_TIMEGATE_TYPE = MEMENTO_NAMESPACE + "TimeGate";
254
255    /**
256     * Type for memento objects.
257     */
258    public static final String MEMENTO_TYPE = MEMENTO_NAMESPACE + "Memento";
259
260    /**
261     * This is an internal RDF type for versionable resources, this may be replaced by a Memento type.
262     */
263    public static final Resource VERSIONED_RESOURCE =
264        createResource(MEMENTO_NAMESPACE + "OriginalResource");
265
266    public static final Property MEMENTO_ORIGINAL_RESOURCE =
267        createProperty(MEMENTO_NAMESPACE + "original");
268
269    /*
270     * Interaction Models.
271     */
272    public static final Set<Resource> INTERACTION_MODEL_RESOURCES = of(
273            BASIC_CONTAINER, INDIRECT_CONTAINER, DIRECT_CONTAINER, NON_RDF_SOURCE);
274
275    /**
276     * String set of valid interaction models with full LDP URI.
277     */
278    public static final Set<String> INTERACTION_MODELS_FULL = INTERACTION_MODEL_RESOURCES.stream().map(Resource::getURI)
279            .collect(Collectors.toSet());
280
281    /**
282     * This defines what we assume if you don't specify.
283     */
284    public static final Resource DEFAULT_INTERACTION_MODEL = BASIC_CONTAINER;
285
286    private RdfLexicon() {
287        // This constructor left intentionally blank.
288    }
289}