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.services;
019
020import com.google.common.annotations.VisibleForTesting;
021import org.fcrepo.kernel.api.Transaction;
022import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
023import org.fcrepo.kernel.api.identifiers.FedoraId;
024import org.fcrepo.kernel.api.operations.VersionResourceOperationFactory;
025import org.fcrepo.kernel.api.services.VersionService;
026import org.fcrepo.persistence.api.PersistentStorageSessionManager;
027import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
028import org.springframework.stereotype.Component;
029
030import javax.inject.Inject;
031
032/**
033 * Implementation of {@link VersionService}
034 *
035 * @author dbernstein
036 */
037@Component
038public class VersionServiceImpl extends AbstractService implements VersionService {
039
040    @Inject
041    private PersistentStorageSessionManager psManager;
042
043    @Inject
044    private VersionResourceOperationFactory versionOperationFactory;
045
046    @Override
047    public void createVersion(final Transaction transaction, final FedoraId fedoraId, final String userPrincipal) {
048        final var session = psManager.getSession(transaction.getId());
049        final var operation = versionOperationFactory.createBuilder(fedoraId)
050                .userPrincipal(userPrincipal)
051                .build();
052
053        try {
054            session.persist(operation);
055            recordEvent(transaction.getId(), fedoraId, operation);
056        } catch (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}