001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation;
013
014import org.nuxeo.common.xmap.annotation.XNode;
015import org.nuxeo.common.xmap.annotation.XNodeList;
016import org.nuxeo.common.xmap.annotation.XObject;
017import org.nuxeo.ecm.automation.core.Constants;
018import org.nuxeo.ecm.automation.core.OperationChainContribution;
019import org.nuxeo.ecm.platform.forms.layout.api.WidgetDefinition;
020
021import java.io.Serializable;
022import java.util.Arrays;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 * @author <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
027 */
028public class OperationDocumentation implements Comparable<OperationDocumentation>, Serializable {
029
030    private static final long serialVersionUID = 1L;
031
032    public String id;
033
034    /**
035     * @since 7.1
036     */
037    public String[] aliases;
038
039    /**
040     * an array of size multiple of 2. Each pair in the array is the input and output type of a method.
041     */
042    public String[] signature;
043
044    public String category;
045
046    public String label;
047
048    public String requires;
049
050    public String since;
051
052    /**
053     * @since 5.9.1
054     */
055    public String deprecatedSince;
056
057    /**
058     * @since 5.9.1
059     */
060    public boolean addToStudio;
061
062    /**
063     * @since 5.9.1
064     */
065    public String implementationClass;
066
067    public String description;
068
069    public Param[] params;
070
071    public WidgetDefinition[] widgetDefinitions;
072
073    /**
074     * The operations listing in case of a chain.
075     */
076    public OperationChainContribution.Operation[] operations;
077
078    // optional URL indicating the relative path (relative to the automation
079    // service home)
080    // of the page where the operation is exposed
081    public String url;
082
083    public OperationDocumentation(String id) {
084        this.id = id;
085        url = id;
086    }
087
088    @XObject("param")
089    public static class Param implements Serializable, Comparable<Param> {
090        private static final long serialVersionUID = 1L;
091
092        @XNode("@name")
093        public String name;
094
095        @XNode("@description")
096        public String description;
097
098        @XNode("@type")
099        public String type; // the data type
100
101        // is this useful (?)
102        public String widget; // the widget type
103
104        // is this useful (?)
105        @XNodeList(value = "value", type = String[].class, componentType = String.class)
106        public String[] values; // the default values
107
108        // is this useful (?)
109        @XNode("@order")
110        public int order;
111
112        // is this useful (?)
113        @XNode("@required")
114        public boolean isRequired;
115
116        public Param() {
117        }
118
119        public String getName() {
120            return name;
121        }
122
123        /**
124         * @since 5.7.3
125         */
126        public String getDescription() {
127            return description;
128        }
129
130        public String getType() {
131            return type;
132        }
133
134        public String getWidget() {
135            return widget;
136        }
137
138        public String[] getValues() {
139            return values;
140        }
141
142        public boolean isRequired() {
143            return isRequired;
144        }
145
146        public int getOrder() {
147            return order;
148        }
149
150        @Override
151        public String toString() {
152            return name + " [" + type + "] " + (isRequired ? "required" : "optional");
153        }
154
155        @Override
156        public int compareTo(Param o) {
157            if (order != 0 && o.order != 0) {
158                if (order < o.order) {
159                    return -1;
160                } else if (order > o.order) {
161                    return 1;
162                }
163            }
164            if (isRequired && !o.isRequired) {
165                return -1;
166            }
167            if (o.isRequired && !isRequired) {
168                return 1;
169            }
170            return name.compareTo(o.name);
171        }
172    }
173
174    @Override
175    public int compareTo(OperationDocumentation o) {
176        String s1 = label == null ? id : label;
177        String s2 = o.label == null ? o.id : o.label;
178        return s1.compareTo(s2);
179    }
180
181    public String getDescription() {
182        return description;
183    }
184
185    /**
186     * @since 5.9.1
187     */
188    public String getSince() {
189        return since;
190    }
191
192    /**
193     * @since 5.9.1
194     */
195    public String getDeprecatedSince() {
196        return deprecatedSince;
197    }
198
199    /**
200     * @since 5.9.1
201     */
202    public boolean isAddToStudio() {
203        return addToStudio;
204    }
205
206    /**
207     * @since 5.9.1
208     */
209    public String getImplementationClass() {
210        return implementationClass;
211    }
212
213    /**
214     * @since 5.9.4
215     */
216    public boolean isChain() {
217        return (id != null && id.startsWith(Constants.CHAIN_ID_PREFIX)) || Constants.CAT_CHAIN.equals(category);
218    }
219
220    public String[] getSignature() {
221        return signature;
222    }
223
224    public String getCategory() {
225        return category;
226    }
227
228    public String getId() {
229        return id;
230    }
231
232    public String getUrl() {
233        return url;
234    }
235
236    public String getLabel() {
237        return label;
238    }
239
240    public String getRequires() {
241        return requires;
242    }
243
244    public Param[] getParams() {
245        return params;
246    }
247
248    public String[] getAliases() {
249        return aliases;
250    }
251
252    public void setAliases(String[] aliases) {
253        this.aliases = aliases;
254    }
255
256    public OperationChainContribution.Operation[] getOperations() {
257        return operations;
258    }
259
260    @Override
261    public String toString() {
262        return category + " > " + label + " [" + id + ": " + Arrays.asList(signature) + "] (" + params + ")\n"
263                + description;
264    }
265}