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