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