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.browse;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.Produces;
024
025import org.apache.commons.lang.StringUtils;
026import org.nuxeo.apidoc.api.NuxeoArtifact;
027import org.nuxeo.apidoc.api.OperationInfo;
028import org.nuxeo.ecm.automation.OperationDocumentation.Param;
029import org.nuxeo.ecm.automation.OperationException;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.webengine.model.WebObject;
032
033@WebObject(type = "operation")
034public class OperationWO extends NuxeoArtifactWebObject {
035
036    @Override
037    @GET
038    @Produces("text/html")
039    @Path("introspection")
040    public Object doGet() {
041        return getView("view").arg("operation", getTargetComponentInfo());
042    }
043
044    public OperationInfo getTargetComponentInfo() {
045        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getOperation(nxArtifactId);
046    }
047
048    @Override
049    public NuxeoArtifact getNxArtifact() {
050        return getTargetComponentInfo();
051    }
052
053    protected String[] getInputs(OperationInfo op) {
054        String[] signature = op.getSignature();
055        if (signature == null || signature.length == 0) {
056            return new String[0];
057        }
058        String[] result = new String[signature.length / 2];
059        for (int i = 0, k = 0; i < signature.length; i += 2, k++) {
060            result[k] = signature[i];
061        }
062        return result;
063    }
064
065    protected String[] getOutputs(OperationInfo op) {
066        String[] signature = op.getSignature();
067        if (signature == null || signature.length == 0) {
068            return new String[0];
069        }
070        String[] result = new String[signature.length / 2];
071        for (int i = 1, k = 0; i < signature.length; i += 2, k++) {
072            result[k] = signature[i];
073        }
074        return result;
075    }
076
077    public String getInputsAsString(OperationInfo op) {
078        String[] result = getInputs(op);
079        if (result == null || result.length == 0) {
080            return "void";
081        }
082        return StringUtils.join(result, ", ");
083    }
084
085    public String getOutputsAsString(OperationInfo op) {
086        String[] result = getOutputs(op);
087        if (result == null || result.length == 0) {
088            return "void";
089        }
090        return StringUtils.join(result, ", ");
091    }
092
093    public String getParamDefaultValue(Param param) {
094        if (param.values != null && param.values.length > 0) {
095            return StringUtils.join(param.values, ", ");
096        }
097        return "";
098    }
099
100    @Override
101    public String getSearchCriterion() {
102        return "'" + super.getSearchCriterion() + "' Operation";
103    }
104}