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 @Override 053 public Topology getTopology(Map<String, String> options) { 054 return Topology.builder() 055 .addComputation(SetSystemPropertyComputation::new, 056 Arrays.asList(INPUT_1 + ":" + ACTION_NAME, // 057 OUTPUT_1 + ":" + STATUS_STREAM)) 058 .build(); 059 } 060 061 public static class SetSystemPropertyComputation extends AbstractBulkComputation { 062 063 private static final Logger log = LogManager.getLogger(SetSystemPropertyComputation.class); 064 065 public SetSystemPropertyComputation() { 066 super(ACTION_NAME); 067 } 068 069 @Override 070 protected void compute(CoreSession session, List<String> ids, Map<String, Serializable> properties) { 071 Collection<DocumentRef> refs = ids.stream().map(IdRef::new).collect(Collectors.toList()); 072 for (DocumentRef ref : refs) { 073 for (Entry<String, Serializable> entry : properties.entrySet()) { 074 try { 075 session.setDocumentSystemProp(ref, entry.getKey(), entry.getValue()); 076 } catch (NuxeoException e) { 077 // TODO send to error stream 078 log.warn("Cannot set system property: " + entry.getKey() + " on: " + ref.toString(), e); 079 } 080 } 081 } 082 try { 083 session.save(); 084 } catch (PropertyException e) { 085 // TODO send to error stream 086 log.warn("Cannot save session", e); 087 } 088 } 089 } 090}