001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.persistence.ocfl.impl;
007
008import static org.fcrepo.kernel.api.operations.ResourceOperationType.UPDATE_HEADERS;
009
010import org.fcrepo.kernel.api.operations.ResourceOperation;
011import org.fcrepo.kernel.api.operations.UpdateNonRdfSourceHeadersOperation;
012import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
013import org.fcrepo.persistence.ocfl.api.FedoraToOcflObjectIndex;
014import org.fcrepo.storage.ocfl.OcflObjectSession;
015import org.slf4j.Logger;
016import org.slf4j.LoggerFactory;
017
018/**
019 * This class implements the persistence of the headers for an RDFSource to bring in new system managed properties
020 *
021 * @author mikejritter
022 * @since 6.0.0
023 */
024public class UpdateNonRdfSourceHeadersPersister extends AbstractRdfSourcePersister {
025
026    private static final Logger log = LoggerFactory.getLogger(UpdateRdfSourcePersister.class);
027
028    /**
029     * Constructor
030     *
031     * @param fedoraOcflIndex the FedoraToOcflObjectIndex
032     */
033    public UpdateNonRdfSourceHeadersPersister(final FedoraToOcflObjectIndex fedoraOcflIndex) {
034        super(UpdateNonRdfSourceHeadersOperation.class, UPDATE_HEADERS, fedoraOcflIndex);
035    }
036
037    @Override
038    public void persist(final OcflPersistentStorageSession session, final ResourceOperation operation)
039        throws PersistentStorageException {
040        final var resourceId = operation.getResourceId();
041        log.debug("persisting {} headers to {}", resourceId, session);
042
043        final var fedoraOcflMapping = getMapping(operation.getTransaction(), resourceId);
044        final var ocflId = fedoraOcflMapping.getOcflObjectId();
045        final OcflObjectSession objSession = session.findOrCreateSession(ocflId);
046
047        // unlike with normal updates we don't want to clear the digests/content-size, just the server managed headers
048        // in the event the server managed mode is strict, all values will be null
049        final var headers = new ResourceHeadersAdapter(objSession.readHeaders(resourceId.getResourceId()));
050
051        final var updateHeadersOp = (UpdateNonRdfSourceHeadersOperation) operation;
052        final var createdDate = updateHeadersOp.getCreatedDate();
053        final var lastModifiedDate = updateHeadersOp.getLastModifiedDate();
054        final var createdBy = updateHeadersOp.getCreatedBy();
055        final var lastModifiedBy = updateHeadersOp.getLastModifiedBy();
056        final var mimetype = updateHeadersOp.getMimeType();
057        final var filename = updateHeadersOp.getFilename();
058        if (createdDate != null) {
059            headers.setCreatedDate(createdDate);
060        }
061        if (lastModifiedDate != null) {
062            headers.setLastModifiedDate(lastModifiedDate);
063        }
064        if (createdBy != null) {
065            headers.setCreatedBy(createdBy);
066        }
067        if (lastModifiedBy != null) {
068            headers.setLastModifiedBy(lastModifiedBy);
069        }
070        if (mimetype != null) {
071            headers.setMimeType(mimetype);
072        }
073        if (filename != null) {
074            headers.setFilename(filename);
075        }
076
077        objSession.writeHeaders(headers.asStorageHeaders());
078    }
079
080}