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 QUERY_TEMPLATE = "SELECT ecm:uuid FROM Document WHERE ecm:isProxy=1 AND ecm:uuid IN ('%s')";
052
053    @Override
054    public Topology getTopology(Map<String, String> options) {
055        return Topology.builder()
056                       .addComputation(RemoveProxyComputation::new,
057                               Arrays.asList(INPUT_1 + ":" + ACTION_NAME, //
058                                       OUTPUT_1 + ":" + STATUS_STREAM))
059                       .build();
060    }
061
062    public static class RemoveProxyComputation extends AbstractBulkComputation {
063
064        public RemoveProxyComputation() {
065            super(ACTION_NAME);
066        }
067
068        @Override
069        protected void compute(CoreSession session, List<String> ids, Map<String, Serializable> properties) {
070            String query = String.format(QUERY_TEMPLATE, String.join("', '", ids));
071            Set<DocumentRef> proxies = new HashSet<>();
072            try (IterableQueryResult res = session.queryAndFetch(query, NXQL.NXQL)) {
073                for (Map<String, Serializable> map : res) {
074                    proxies.add(new IdRef((String) map.get(NXQL.ECM_UUID)));
075                }
076            }
077            session.removeDocuments(proxies.toArray(new DocumentRef[0]));
078            session.save();
079        }
080    }
081}