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