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.List;
029import java.util.Map;
030import java.util.Map.Entry;
031
032import org.apache.logging.log4j.LogManager;
033import org.apache.logging.log4j.Logger;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.PropertyException;
037import org.nuxeo.ecm.core.api.VersioningOption;
038import org.nuxeo.ecm.core.api.versioning.VersioningService;
039import org.nuxeo.ecm.core.bulk.action.computation.AbstractBulkComputation;
040import org.nuxeo.ecm.core.bulk.message.BulkCommand;
041import org.nuxeo.lib.stream.computation.Topology;
042import org.nuxeo.runtime.stream.StreamProcessorTopology;
043
044/**
045 * @since 10.2
046 */
047public class SetPropertiesAction implements StreamProcessorTopology {
048
049    public static final String ACTION_NAME = "setProperties";
050
051    // duplicated from NXAuditEventsService.DISABLE_AUDIT_LOGGER
052    public static final String PARAM_DISABLE_AUDIT = "disableAuditLogger";
053
054    public static final String PARAM_VERSIONING_OPTION = VersioningService.VERSIONING_OPTION;
055
056    @Override
057    public Topology getTopology(Map<String, String> options) {
058        return Topology.builder()
059                       .addComputation(SetPropertyComputation::new,
060                               Arrays.asList(INPUT_1 + ":" + ACTION_NAME, //
061                                       OUTPUT_1 + ":" + STATUS_STREAM))
062                       .build();
063    }
064
065    public static class SetPropertyComputation extends AbstractBulkComputation {
066
067        private static final Logger log = LogManager.getLogger(SetPropertyComputation.class);
068
069        protected VersioningOption versioningOption;
070
071        protected boolean disableAudit;
072
073        public SetPropertyComputation() {
074            super(ACTION_NAME);
075        }
076
077        @Override
078        public void startBucket(String bucketKey) {
079            BulkCommand command = getCurrentCommand();
080            Serializable auditParam = command.getParam(PARAM_DISABLE_AUDIT);
081            disableAudit = auditParam != null && Boolean.parseBoolean(auditParam.toString());
082            Serializable versioningParam = command.getParam(PARAM_VERSIONING_OPTION);
083            versioningOption = VersioningOption.NONE.toString().equals(versioningParam) ? VersioningOption.NONE : null;
084        }
085
086        @Override
087        protected void compute(CoreSession session, List<String> ids, Map<String, Serializable> properties) {
088            for (DocumentModel doc : loadDocuments(session, ids)) {
089                if (disableAudit) {
090                    doc.putContextData(PARAM_DISABLE_AUDIT, Boolean.TRUE);
091                }
092                if (versioningOption != null) {
093                    doc.putContextData(VersioningService.VERSIONING_OPTION, versioningOption);
094                }
095                // update properties
096                for (Entry<String, Serializable> es : properties.entrySet()) {
097                    if (!PARAM_DISABLE_AUDIT.equals(es.getKey()) && !PARAM_VERSIONING_OPTION.equals(es.getKey())) {
098                        try {
099                            doc.setPropertyValue(es.getKey(), es.getValue());
100                        } catch (PropertyException e) {
101                            // TODO send to error stream
102                            log.warn("Cannot write property: {} of document: {}", es.getKey(), doc.getId(), e);
103                        }
104                    }
105                }
106                try {
107                    session.saveDocument(doc);
108                } catch (PropertyException e) {
109                    // TODO send to error stream
110                    log.warn("Cannot save document: {}", doc.getId(), e);
111                }
112            }
113        }
114    }
115
116}