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.kernel.impl.services;
007
008import com.google.common.annotations.VisibleForTesting;
009
010import org.fcrepo.kernel.api.RdfLexicon;
011import org.fcrepo.kernel.api.Transaction;
012import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
013import org.fcrepo.kernel.api.identifiers.FedoraId;
014import org.fcrepo.kernel.api.operations.VersionResourceOperationFactory;
015import org.fcrepo.kernel.api.services.VersionService;
016import org.fcrepo.persistence.api.PersistentStorageSessionManager;
017import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
018import org.springframework.stereotype.Component;
019
020import javax.inject.Inject;
021
022/**
023 * Implementation of {@link VersionService}
024 *
025 * @author dbernstein
026 */
027@Component
028public class VersionServiceImpl extends AbstractService implements VersionService {
029
030    @Inject
031    private PersistentStorageSessionManager psManager;
032
033    @Inject
034    private VersionResourceOperationFactory versionOperationFactory;
035
036    @Override
037    public void createVersion(final Transaction transaction, final FedoraId fedoraId, final String userPrincipal) {
038        final var session = psManager.getSession(transaction);
039        final var operation = versionOperationFactory.createBuilder(transaction, fedoraId)
040                .userPrincipal(userPrincipal)
041                .build();
042
043        lockArchivalGroupResource(transaction, session, fedoraId);
044        final var headers = session.getHeaders(fedoraId, null);
045        if (RdfLexicon.FEDORA_NON_RDF_SOURCE_DESCRIPTION_URI.equals(headers.getInteractionModel())) {
046            transaction.lockResource(fedoraId.asBaseId());
047        }
048        if (RdfLexicon.NON_RDF_SOURCE.toString().equals(headers.getInteractionModel())) {
049            transaction.lockResource(fedoraId.asDescription());
050        }
051        transaction.lockResource(fedoraId);
052
053        try {
054            session.persist(operation);
055            recordEvent(transaction, fedoraId, operation);
056        } catch (final PersistentStorageException e) {
057            throw new RepositoryRuntimeException(String.format("Failed to create new version of %s",
058                    fedoraId.getResourceId()), e);
059        }
060    }
061
062    @VisibleForTesting
063    void setPsManager(final PersistentStorageSessionManager psManager) {
064        this.psManager = psManager;
065    }
066
067    @VisibleForTesting
068    void setVersionOperationFactory(final VersionResourceOperationFactory versionOperationFactory) {
069        this.versionOperationFactory = versionOperationFactory;
070    }
071
072}