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.DeleteResourceService;
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 * This class mediates delete operations between the kernel and persistent storage layers
039 *
040 * @author dbernstein
041 */
042@Component
043public class DeleteResourceServiceImpl extends AbstractDeleteResourceService implements DeleteResourceService {
044
045    private final static Logger log = LoggerFactory.getLogger(DeleteResourceService.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.getContains(tx, resource.getFedoraId());
053    }
054
055    @Override
056    protected void doAction(final Transaction tx, final PersistentStorageSession pSession,
057                            final FedoraId fedoraId, final String userPrincipal)
058            throws PersistentStorageException {
059        log.debug("starting delete of {}", fedoraId.getFullId());
060        final ResourceOperation deleteOp = deleteResourceFactory.deleteBuilder(tx, fedoraId)
061                .userPrincipal(userPrincipal)
062                .build();
063
064        lockArchivalGroupResource(tx, pSession, fedoraId);
065        tx.lockResource(fedoraId);
066
067        pSession.persist(deleteOp);
068        membershipService.resourceDeleted(tx, fedoraId);
069        containmentIndex.removeResource(tx, fedoraId);
070        referenceService.deleteAllReferences(tx, fedoraId);
071        searchIndex.removeFromIndex(tx, fedoraId);
072        recordEvent(tx, fedoraId, deleteOp);
073        log.debug("deleted {}", fedoraId.getFullId());
074    }
075
076}