001/*
002 * (C) Copyright 2013 Nuxeo SA (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 *     bstefanescu
018 *     vpasquier
019 */
020package org.nuxeo.ecm.automation;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.commons.logging.LogFactory;
030
031
032/**
033 * Describes an operation chain execution.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class OperationChain implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    protected final String id;
042
043    // (via REST for example)
044    protected final List<OperationParameters> operations = new ArrayList<>();
045
046    protected final Map<String, Object> chainParameters = new HashMap<>();
047
048    protected String description;
049
050    /**
051     * @since 7.1
052     */
053    protected String[] aliases;
054
055    protected boolean isPublic; // whether this chain is visible to clients
056
057    public OperationChain(String id) {
058        this(id, Collections.emptyList());
059    }
060
061    public OperationChain(String id, List<OperationParameters> operations) {
062        this(id, operations, Collections.emptyMap());
063    }
064
065    public OperationChain(String id, List<OperationParameters> operations, Map<String, Object> chainParameters) {
066        this.id = id;
067        this.operations.addAll(operations);
068        this.chainParameters.putAll(chainParameters);
069    }
070
071    public String getId() {
072        return id;
073    }
074
075    /**
076     * @since 7.1
077     */
078    public String[] getAliases() {
079        return aliases;
080    }
081
082    public String getDescription() {
083        return description;
084    }
085
086    public void setDescription(String description) {
087        this.description = description;
088    }
089
090    /**
091     * @since 7.1
092     */
093    public void setAliases(String[] aliases) {
094        this.aliases = aliases;
095    }
096
097    public boolean isPublic() {
098        return isPublic;
099    }
100
101    public void setPublic(boolean isPublic) {
102        this.isPublic = isPublic;
103    }
104
105    public List<OperationParameters> getOperations() {
106        return operations;
107    }
108
109    public void add(OperationParameters op) {
110        operations.add(op);
111    }
112
113    public OperationParameters add(String operationId) {
114        OperationParameters op = new OperationParameters(operationId);
115        operations.add(op);
116        return op;
117    }
118
119    /**
120     * @since 5.7.2 Adding chain parameters
121     */
122    public void addChainParameters(Map<String, ?> chainParameter) {
123        if (chainParameter == null) {
124            LogFactory.getLog(OperationChain.class).warn("null parameters given to " + id, new Throwable("stack trace"));
125            return;
126        }
127        chainParameters.putAll(chainParameter);
128    }
129
130    /**
131     * @since 5.7.2 Getting chain parameters
132     */
133    public Map<String, ?> getChainParameters() {
134        return chainParameters;
135    }
136
137    @Override
138    public int hashCode() {
139        final int prime = 31;
140        int result = 1;
141        result = prime * result + id.hashCode();
142        result = prime * result + chainParameters.hashCode();
143        result = prime * result +  operations.hashCode();
144        return result;
145    }
146
147    @Override
148    public boolean equals(Object obj) {
149        if (this == obj) {
150            return true;
151        }
152        if (obj == null) {
153            return false;
154        }
155        if (!(obj instanceof OperationChain)) {
156            return false;
157        }
158        OperationChain other = (OperationChain) obj;
159        if (!id.equals(other.id)) {
160            return false;
161        }
162        if (!chainParameters.equals(other.chainParameters)) {
163            return false;
164        }
165        if (!operations.equals(other.operations)) {
166            return false;
167        }
168        return true;
169    }
170
171    @Override
172    public String toString() {
173        return "OperationChain [id="+id+"]";
174    }
175}