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.operations;
019
020import static org.fcrepo.kernel.api.utils.RelaxedPropertiesHelper.checkTripleForDisallowed;
021import static org.fcrepo.kernel.api.utils.RelaxedPropertiesHelper.getCreatedBy;
022import static org.fcrepo.kernel.api.utils.RelaxedPropertiesHelper.getCreatedDate;
023import static org.fcrepo.kernel.api.utils.RelaxedPropertiesHelper.getModifiedBy;
024import static org.fcrepo.kernel.api.utils.RelaxedPropertiesHelper.getModifiedDate;
025
026import java.time.Instant;
027
028import org.fcrepo.config.ServerManagedPropsMode;
029import org.fcrepo.kernel.api.RdfStream;
030import org.fcrepo.kernel.api.identifiers.FedoraId;
031import org.fcrepo.kernel.api.operations.RdfSourceOperationBuilder;
032import org.fcrepo.kernel.api.rdf.DefaultRdfStream;
033
034import org.apache.jena.rdf.model.Model;
035
036/**
037 * Abstract builder for interacting with an Rdf Source Operation Builder
038 * @author bseeger
039 */
040public abstract class AbstractRdfSourceOperationBuilder implements RdfSourceOperationBuilder {
041
042    /**
043     * Holds the stream of user's triples.
044     */
045    protected RdfStream tripleStream;
046
047    /**
048     * String of the resource ID.
049     */
050    protected final FedoraId resourceId;
051
052    /**
053     * The interaction model of this resource, null in case of update.
054     */
055    protected final String interactionModel;
056
057    /**
058     * Principal of the user performing the operation
059     */
060    protected String userPrincipal;
061
062    protected String lastModifiedBy;
063
064    protected String createdBy;
065
066    protected Instant lastModifiedDate;
067
068    protected Instant createdDate;
069
070    protected ServerManagedPropsMode serverManagedPropsMode;
071
072    protected AbstractRdfSourceOperationBuilder(final FedoraId rescId,
073                                                final String model,
074                                                final ServerManagedPropsMode serverManagedPropsMode) {
075        resourceId = rescId;
076        interactionModel = model;
077        this.serverManagedPropsMode = serverManagedPropsMode;
078    }
079
080    @Override
081    public RdfSourceOperationBuilder userPrincipal(final String userPrincipal) {
082        this.userPrincipal = userPrincipal;
083        return this;
084    }
085
086    @Override
087    public RdfSourceOperationBuilder triples(final RdfStream triples) {
088        if (this.serverManagedPropsMode.equals(ServerManagedPropsMode.RELAXED)) {
089            // Filter out server managed properties, they should only matter to the relaxedProperties method.
090            this.tripleStream = new DefaultRdfStream(triples.topic(), triples.filter(t -> {
091                try {
092                    checkTripleForDisallowed(t);
093                } catch (final Exception e) {
094                    return false;
095                }
096                return true;
097            }));
098        } else {
099            this.tripleStream = triples;
100        }
101        return this;
102    }
103
104    @Override
105    public RdfSourceOperationBuilder relaxedProperties(final Model model) {
106        // Has no affect if the server is not in relaxed mode
107        if (model != null && serverManagedPropsMode == ServerManagedPropsMode.RELAXED) {
108            final var resc = model.getResource(resourceId.getResourceId());
109
110            final var createdDateVal = getCreatedDate(resc);
111            if (createdDateVal != null) {
112                this.createdDate = createdDateVal.toInstant();
113            }
114            final var createdByVal = getCreatedBy(resc);
115            if (createdByVal != null) {
116                this.createdBy = createdByVal;
117            }
118            final var modifiedDate = getModifiedDate(resc);
119            if (modifiedDate != null) {
120                this.lastModifiedDate = modifiedDate.toInstant();
121            }
122            final var modifiedBy = getModifiedBy(resc);
123            if (modifiedBy != null) {
124                this.lastModifiedBy = modifiedBy;
125            }
126        }
127
128        return this;
129    }
130}