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.Collection;
029import java.util.List;
030import java.util.Map;
031import java.util.Map.Entry;
032import java.util.stream.Collectors;
033
034import org.apache.logging.log4j.LogManager;
035import org.apache.logging.log4j.Logger;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentRef;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.core.api.PropertyException;
041import org.nuxeo.ecm.core.bulk.action.computation.AbstractBulkComputation;
042import org.nuxeo.lib.stream.computation.Topology;
043import org.nuxeo.runtime.stream.StreamProcessorTopology;
044
045/**
046 * @since 10.3
047 */
048public class SetSystemPropertiesAction implements StreamProcessorTopology {
049
050    public static final String ACTION_NAME = "setSystemProperties";
051
052    public static final String ACTION_FULL_NAME = "bulk/" + ACTION_NAME;
053
054    @Override
055    public Topology getTopology(Map<String, String> options) {
056        return Topology.builder()
057                       .addComputation(SetSystemPropertyComputation::new,
058                               Arrays.asList(INPUT_1 + ":" + ACTION_FULL_NAME, //
059                                       OUTPUT_1 + ":" + STATUS_STREAM))
060                       .build();
061    }
062
063    public static class SetSystemPropertyComputation extends AbstractBulkComputation {
064
065        private static final Logger log = LogManager.getLogger(SetSystemPropertyComputation.class);
066
067        public SetSystemPropertyComputation() {
068            super(ACTION_FULL_NAME);
069        }
070
071        @Override
072        protected void compute(CoreSession session, List<String> ids, Map<String, Serializable> properties) {
073            Collection<DocumentRef> refs = ids.stream().map(IdRef::new).collect(Collectors.toList());
074            for (DocumentRef ref : refs) {
075                for (Entry<String, Serializable> entry : properties.entrySet()) {
076                    try {
077                        session.setDocumentSystemProp(ref, entry.getKey(), entry.getValue());
078                    } catch (NuxeoException e) {
079                        // TODO send to error stream
080                        log.warn("Cannot set system property: " + entry.getKey() + " on: " + ref.toString(), e);
081                    }
082                }
083            }
084            try {
085                session.save();
086            } catch (PropertyException e) {
087                // TODO send to error stream
088                log.warn("Cannot save session", e);
089            }
090        }
091    }
092}