001/* 002 * (C) Copyright 2018 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.bulk.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.HashSet; 029import java.util.List; 030import java.util.Map; 031import java.util.Set; 032 033import org.nuxeo.ecm.core.api.CoreSession; 034import org.nuxeo.ecm.core.api.DocumentRef; 035import org.nuxeo.ecm.core.api.IdRef; 036import org.nuxeo.ecm.core.api.IterableQueryResult; 037import org.nuxeo.ecm.core.bulk.action.computation.AbstractBulkComputation; 038import org.nuxeo.ecm.core.query.sql.NXQL; 039import org.nuxeo.lib.stream.computation.Topology; 040import org.nuxeo.runtime.stream.StreamProcessorTopology; 041 042/** 043 * Removes documents which are proxies and whose id is contained in the given ids list. 044 * 045 * @since 10.3 046 */ 047public class RemoveProxyAction implements StreamProcessorTopology { 048 049 public static final String ACTION_NAME = "removeProxy"; 050 051 public static final String ACTION_FULL_NAME = "bulk/" + ACTION_NAME; 052 053 public static final String QUERY_TEMPLATE = "SELECT ecm:uuid FROM Document WHERE ecm:isProxy=1 AND ecm:uuid IN ('%s')"; 054 055 @Override 056 public Topology getTopology(Map<String, String> options) { 057 return Topology.builder() 058 .addComputation(RemoveProxyComputation::new, 059 Arrays.asList(INPUT_1 + ":" + ACTION_FULL_NAME, // 060 OUTPUT_1 + ":" + STATUS_STREAM)) 061 .build(); 062 } 063 064 public static class RemoveProxyComputation extends AbstractBulkComputation { 065 066 public RemoveProxyComputation() { 067 super(ACTION_FULL_NAME); 068 } 069 070 @Override 071 protected void compute(CoreSession session, List<String> ids, Map<String, Serializable> properties) { 072 String query = String.format(QUERY_TEMPLATE, String.join("', '", ids)); 073 Set<DocumentRef> proxies = new HashSet<>(); 074 try (IterableQueryResult res = session.queryAndFetch(query, NXQL.NXQL)) { 075 for (Map<String, Serializable> map : res) { 076 proxies.add(new IdRef((String) map.get(NXQL.ECM_UUID))); 077 } 078 } 079 session.removeDocuments(proxies.toArray(new DocumentRef[0])); 080 session.save(); 081 } 082 } 083}