001/*
002 * (C) Copyright 2011 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 *     Florent Guillaume
018 */
019package org.nuxeo.apidoc.introspection;
020
021import java.util.Arrays;
022import java.util.List;
023import org.nuxeo.apidoc.api.BaseNuxeoArtifact;
024import org.nuxeo.apidoc.api.OperationInfo;
025import org.nuxeo.ecm.automation.OperationDocumentation;
026import org.nuxeo.ecm.automation.OperationDocumentation.Param;
027
028import com.fasterxml.jackson.annotation.JsonIgnore;
029import com.fasterxml.jackson.annotation.JsonIgnoreType;
030import com.fasterxml.jackson.annotation.JsonProperty;
031
032/**
033 * DTO for an {@link OperationInfo}, used for the runtime implementation.
034 */
035@JsonIgnoreType
036public class OperationInfoImpl extends BaseNuxeoArtifact implements OperationInfo {
037
038    protected final String name;
039
040    protected final String version;
041
042    protected final String[] aliases;
043
044    protected final String operationClass;
045
046    protected final String contributingComponent;
047
048    protected final String description;
049
050    protected final String[] signature;
051
052    protected final String category;
053
054    protected final String url;
055
056    protected final String label;
057
058    protected final String requires;
059
060    protected final String since;
061
062    protected final List<Param> params;
063
064    public OperationInfoImpl(@JsonProperty("name") String name,
065            @JsonProperty("version") String version,
066            @JsonProperty("aliases") String[] aliases,
067            @JsonProperty("description") String description,
068            @JsonProperty("operationClass") String operationClass,
069            @JsonProperty("contributingComponent") String contributingComponent,
070            @JsonProperty("signature") String[] signature,
071            @JsonProperty("category") String category,
072            @JsonProperty("url") String url,
073            @JsonProperty("label") String label,
074            @JsonProperty("requires") String requires,
075            @JsonProperty("since") String since,
076            @JsonProperty("params") List<Param> params) {
077
078        this.name = name;
079        this.version = version;
080        this.aliases = aliases;
081        this.description = description;
082        this.operationClass = operationClass;
083        if (contributingComponent == null || contributingComponent.isEmpty()) {
084            this.contributingComponent = OperationInfo.BUILT_IN;
085        } else {
086            String[] parts = contributingComponent.split(":");
087            if (parts.length > 1) {
088                this.contributingComponent = parts[1];
089            } else {
090                this.contributingComponent = contributingComponent;
091            }
092        }
093        this.signature = signature;
094        this.category = category;
095        this.url = url;
096        this.label = label;
097        this.requires = requires;
098        this.since = since;
099        this.params = params;
100    }
101
102    public OperationInfoImpl(OperationDocumentation op, String version, String operationClass,
103            String contributingComponent) {
104        this(op.getId(), version, op.getAliases(), op.getDescription(), operationClass, contributingComponent, op.getSignature(), op.getCategory(),
105                op.getUrl(), op.getLabel(), op.getRequires(), op.getSince(),
106                Arrays.asList(op.getParams()));
107    }
108
109    @Override
110    public String getName() {
111        return name;
112    }
113
114    @Override
115    @JsonIgnore
116    public String getId() {
117        return ARTIFACT_PREFIX + name;
118    }
119
120    @Override
121    public String[] getAliases() {
122        return aliases;
123    }
124
125    @Override
126    public String getDescription() {
127        return description;
128    }
129
130    @Override
131    public String[] getSignature() {
132        return signature;
133    }
134
135    @Override
136    public String getCategory() {
137        return category;
138    }
139
140    @Override
141    public String getUrl() {
142        return url;
143    }
144
145    @Override
146    public String getLabel() {
147        return label;
148    }
149
150    @Override
151    public String getRequires() {
152        return requires;
153    }
154
155    @Override
156    public String getSince() {
157        return since;
158    }
159
160    @Override
161    public List<Param> getParams() {
162        return params;
163    }
164
165    @Override
166    public String getVersion() {
167        return version;
168    }
169
170    @Override
171    @JsonIgnore
172    public String getArtifactType() {
173        return TYPE_NAME;
174    }
175
176    @Override
177    @JsonIgnore
178    public String getHierarchyPath() {
179        return "/";
180    }
181
182    @Override
183    public int compareTo(OperationInfo o) {
184        String s1 = getLabel() == null ? getId() : getLabel();
185        String s2 = o.getLabel() == null ? o.getId() : o.getLabel();
186        return s1.compareTo(s2);
187    }
188
189    @Override
190    public String getOperationClass() {
191        return operationClass;
192    }
193
194    @Override
195    public String getContributingComponent() {
196        return contributingComponent;
197    }
198
199}