001/* 002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Funsho David 018 */ 019 020package org.nuxeo.ecm.core.action; 021 022import static org.nuxeo.ecm.core.bulk.BulkServiceImpl.STATUS_STREAM; 023import static org.nuxeo.lib.stream.computation.AbstractComputation.INPUT_1; 024import static org.nuxeo.lib.stream.computation.AbstractComputation.OUTPUT_1; 025 026import java.io.Serializable; 027import java.util.Arrays; 028import java.util.List; 029import java.util.Map; 030 031import org.apache.logging.log4j.LogManager; 032import org.apache.logging.log4j.Logger; 033import org.nuxeo.ecm.core.api.AbstractSession; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentExistsException; 036import org.nuxeo.ecm.core.api.DocumentNotFoundException; 037import org.nuxeo.ecm.core.bulk.action.computation.AbstractBulkComputation; 038import org.nuxeo.ecm.core.model.Document; 039import org.nuxeo.ecm.core.model.Session; 040import org.nuxeo.lib.stream.computation.Topology; 041import org.nuxeo.runtime.stream.StreamProcessorTopology; 042 043/** 044 * Bulk Action to delete documents directly at storage level. This might cause inconsistencies if using SQL-based 045 * storages because of parent-children constraints. 046 * 047 * @since 11.1 048 */ 049public class DeletionAction implements StreamProcessorTopology { 050 051 private static final Logger log = LogManager.getLogger(DeletionAction.class); 052 053 public static final String ACTION_NAME = "deletion"; 054 055 public static final String ACTION_FULL_NAME = "bulk/" + ACTION_NAME; 056 057 @Override 058 public Topology getTopology(Map<String, String> options) { 059 return Topology.builder() 060 .addComputation(DeletionComputation::new, 061 Arrays.asList(INPUT_1 + ":" + ACTION_FULL_NAME, OUTPUT_1 + ":" + STATUS_STREAM)) 062 .build(); 063 } 064 065 public static class DeletionComputation extends AbstractBulkComputation { 066 067 public DeletionComputation() { 068 super(ACTION_FULL_NAME); 069 } 070 071 @Override 072 protected void compute(CoreSession session, List<String> ids, Map<String, Serializable> properties) { 073 Session internalSession = ((AbstractSession) session).getSession(); 074 for (String id : ids) { 075 try { 076 Document doc = internalSession.getDocumentByUUID(id); 077 doc.removeSingleton(); 078 } catch (DocumentNotFoundException e) { 079 // Document is already deleted 080 } catch (DocumentExistsException e) { 081 log.debug("Cannot delete {}: {}", id, e.getMessage()); 082 } 083 } 084 } 085 } 086}