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 * Describes an operation chain execution.
033 *
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class OperationChain implements Serializable {
037
038    private static final long serialVersionUID = 1L;
039
040    protected final String id;
041
042    // (via REST for example)
043    protected final transient List<OperationParameters> operations = new ArrayList<>();
044
045    protected final transient Map<String, Object> chainParameters = new HashMap<>();
046
047    protected String description;
048
049    /**
050     * @since 7.1
051     */
052    protected String[] aliases;
053
054    protected boolean isPublic; // whether this chain is visible to clients
055
056    public OperationChain(String id) {
057        this(id, Collections.emptyList());
058    }
059
060    public OperationChain(String id, List<OperationParameters> operations) {
061        this(id, operations, Collections.emptyMap());
062    }
063
064    public OperationChain(String id, List<OperationParameters> operations, Map<String, Object> chainParameters) {
065        this.id = id;
066        this.operations.addAll(operations);
067        this.chainParameters.putAll(chainParameters);
068    }
069
070    public String getId() {
071        return id;
072    }
073
074    /**
075     * @since 7.1
076     */
077    public String[] getAliases() {
078        return aliases;
079    }
080
081    public String getDescription() {
082        return description;
083    }
084
085    public void setDescription(String description) {
086        this.description = description;
087    }
088
089    /**
090     * @since 7.1
091     */
092    public void setAliases(String[] aliases) {
093        this.aliases = aliases;
094    }
095
096    public boolean isPublic() {
097        return isPublic;
098    }
099
100    public void setPublic(boolean isPublic) {
101        this.isPublic = isPublic;
102    }
103
104    public List<OperationParameters> getOperations() {
105        return operations;
106    }
107
108    public void add(OperationParameters op) {
109        operations.add(op);
110    }
111
112    public OperationParameters add(String operationId) {
113        OperationParameters op = new OperationParameters(operationId);
114        operations.add(op);
115        return op;
116    }
117
118    /**
119     * @since 5.7.2 Adding chain parameters
120     */
121    public void addChainParameters(Map<String, Object> chainParameter) {
122        if (chainParameter == null) {
123            LogFactory.getLog(OperationChain.class).warn("null parameters given to " + id,
124                    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, Object> 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        return operations.equals(other.operations);
166    }
167
168    @Override
169    public String toString() {
170        return "OperationChain [id=" + id + "]";
171    }
172}