001/*
002 * Copyright 2019 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.fcrepo.migration.handlers.ocfl;
018
019import edu.wisc.library.ocfl.api.MutableOcflRepository;
020import org.fcrepo.storage.ocfl.OcflObjectSession;
021import org.fcrepo.storage.ocfl.OcflObjectSessionFactory;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import java.nio.file.Path;
026import java.util.UUID;
027
028/**
029 * Factory for PlainOcflObjectSessions
030 *
031 * @author pwinckles
032 */
033public class PlainOcflObjectSessionFactory implements OcflObjectSessionFactory {
034
035    private static final Logger LOG = LoggerFactory.getLogger(PlainOcflObjectSessionFactory.class);
036
037    private final MutableOcflRepository ocflRepo;
038    private final Path stagingRoot;
039    private final String defaultVersionMessage;
040    private final String defaultVersionUserName;
041    private final String defaultVersionUserAddress;
042    private final boolean disableChecksumValidation;
043
044    private boolean closed = false;
045
046    /**
047     * @param ocflRepo the OCFL client
048     * @param stagingRoot the root staging directory
049     * @param defaultVersionMessage OCFL version message
050     * @param defaultVersionUserName OCFL version user
051     * @param defaultVersionUserAddress OCFL version user address
052     * @param disableChecksumValidation whether to verify fedora3 checksums or not
053     */
054    public PlainOcflObjectSessionFactory(final MutableOcflRepository ocflRepo,
055                                         final Path stagingRoot,
056                                         final String defaultVersionMessage,
057                                         final String defaultVersionUserName,
058                                         final String defaultVersionUserAddress,
059                                         final boolean disableChecksumValidation) {
060        this.ocflRepo = ocflRepo;
061        this.stagingRoot = stagingRoot;
062        this.defaultVersionMessage = defaultVersionMessage;
063        this.defaultVersionUserName = defaultVersionUserName;
064        this.defaultVersionUserAddress = defaultVersionUserAddress;
065        this.disableChecksumValidation = disableChecksumValidation;
066    }
067
068    @Override
069    public OcflObjectSession newSession(final String ocflObjectId) {
070        enforceOpen();
071
072        final var sessionId = UUID.randomUUID().toString();
073        final var session = new PlainOcflObjectSession(
074                sessionId,
075                ocflRepo,
076                ocflObjectId,
077                stagingRoot.resolve(sessionId),
078                disableChecksumValidation
079        );
080
081        session.versionAuthor(defaultVersionUserName, defaultVersionUserAddress);
082        session.versionMessage(defaultVersionMessage);
083        return session;
084    }
085
086    @Override
087    public void close() {
088        if (!closed) {
089            closed = true;
090            ocflRepo.close();
091        }
092    }
093
094    @Override
095    public void useUnsafeWrite(final boolean useUnsafeWrite) {
096        // not supported
097    }
098
099    private void enforceOpen() {
100        if (closed) {
101            throw new IllegalStateException("The session factory is closed!");
102        }
103    }
104
105}