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 org.fcrepo.kernel.api.Transaction;
021import org.fcrepo.kernel.api.TransactionUtils;
022import org.fcrepo.kernel.api.identifiers.FedoraId;
023import org.fcrepo.kernel.api.models.FedoraResource;
024import org.fcrepo.kernel.api.operations.DeleteResourceOperationFactory;
025import org.fcrepo.kernel.api.operations.ResourceOperation;
026import org.fcrepo.kernel.api.services.PurgeResourceService;
027import org.fcrepo.persistence.api.PersistentStorageSession;
028import org.fcrepo.persistence.api.exceptions.PersistentStorageException;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import org.springframework.stereotype.Component;
032
033import javax.inject.Inject;
034import java.util.stream.Stream;
035
036/**
037 * Implementation of purge resource service.
038 * @author whikloj
039 * @since 6.0.0
040 */
041@Component
042public class PurgeResourceServiceImpl extends AbstractDeleteResourceService implements PurgeResourceService {
043
044    private final static Logger log = LoggerFactory.getLogger(PurgeResourceServiceImpl.class);
045
046    @Inject
047    private DeleteResourceOperationFactory deleteResourceFactory;
048
049    @Override
050    protected Stream<String> getContained(final Transaction tx, final FedoraResource resource) {
051        return containmentIndex.getContainsDeleted(TransactionUtils.openTxId(tx), resource.getFedoraId());
052    }
053
054    @Override
055    protected void doAction(final Transaction tx, final PersistentStorageSession pSession, final FedoraId resourceId,
056                  final String userPrincipal) throws PersistentStorageException {
057        log.debug("starting purge of {}", resourceId.getFullId());
058        final ResourceOperation purgeOp = deleteResourceFactory.purgeBuilder(resourceId)
059                .userPrincipal(userPrincipal)
060                .build();
061
062        lockArchivalGroupResource(tx, pSession, resourceId);
063        tx.lockResource(resourceId);
064
065        pSession.persist(purgeOp);
066        containmentIndex.purgeResource(tx.getId(), resourceId);
067        recordEvent(tx.getId(), resourceId, purgeOp);
068        log.debug("purged {}", resourceId.getFullId());
069    }
070
071}