001/*
002 * (C) Copyright 2014-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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.ecm.platform.routing.core.io;
022
023import java.io.Serializable;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.commons.lang3.StringUtils;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
030import org.nuxeo.ecm.platform.routing.core.impl.GraphNode;
031
032/**
033 * @since 7.2
034 */
035public class TaskCompletionRequest {
036
037    private String comment;
038
039    private Map<String, Serializable> variables;
040
041    private boolean badJsonFormat;
042
043    public TaskCompletionRequest(String comment, Map<String, Serializable> variables, boolean badJsonFormat) {
044        super();
045        this.comment = comment;
046        this.variables = variables;
047        this.badJsonFormat = badJsonFormat;
048    }
049
050    public String getComment() {
051        if (StringUtils.isNotBlank(comment)) {
052            return comment;
053        }
054
055        // otherwise get the comment from the variables, if any,
056        // so that it will be logged by the audit
057        return (String) variables.get(GraphNode.NODE_VARIABLE_COMMENT);
058    }
059
060    public Map<String, Object> getDataMap() {
061        Map<String, Object> data = new HashMap<>();
062        if (getVariables() != null) {
063            data.put(Constants.VAR_WORKFLOW, getVariables());
064            data.put(Constants.VAR_WORKFLOW_NODE, getVariables());
065        }
066        if (badJsonFormat) {
067            data.put(DocumentRoutingConstants._MAP_VAR_FORMAT_JSON, badJsonFormat);
068        }
069        if (StringUtils.isNotBlank(getComment())) {
070            data.put(GraphNode.NODE_VARIABLE_COMMENT, getComment());
071        }
072        return data;
073    }
074
075    public Map<String, Serializable> getVariables() {
076        return variables;
077    }
078
079}