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 java.io.ByteArrayOutputStream;
022import java.io.IOException;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.Path;
026import javax.ws.rs.Produces;
027
028import org.apache.commons.lang.StringUtils;
029import org.nuxeo.apidoc.api.NuxeoArtifact;
030import org.nuxeo.apidoc.api.OperationInfo;
031import org.nuxeo.ecm.automation.AutomationService;
032import org.nuxeo.ecm.automation.OperationDocumentation;
033import org.nuxeo.ecm.automation.OperationDocumentation.Param;
034import org.nuxeo.ecm.automation.OperationException;
035import org.nuxeo.ecm.automation.jaxrs.io.JsonWriter;
036import org.nuxeo.ecm.core.api.NuxeoException;
037import org.nuxeo.ecm.webengine.model.Template;
038import org.nuxeo.ecm.webengine.model.WebObject;
039import org.nuxeo.runtime.api.Framework;
040
041@WebObject(type = "operation")
042public class OperationWO extends NuxeoArtifactWebObject {
043
044    @Override
045    @GET
046    @Produces("text/html")
047    @Path("introspection")
048    public Object doGet() {
049        return getView("view").arg("operation", getTargetComponentInfo());
050    }
051
052    public OperationInfo getTargetComponentInfo() {
053        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getOperation(nxArtifactId);
054    }
055
056    @Override
057    public NuxeoArtifact getNxArtifact() {
058        return getTargetComponentInfo();
059    }
060
061    protected String[] getInputs(OperationInfo op) {
062        String[] signature = op.getSignature();
063        if (signature == null || signature.length == 0) {
064            return new String[0];
065        }
066        String[] result = new String[signature.length / 2];
067        for (int i = 0, k = 0; i < signature.length; i += 2, k++) {
068            result[k] = signature[i];
069        }
070        return result;
071    }
072
073    protected String[] getOutputs(OperationInfo op) {
074        String[] signature = op.getSignature();
075        if (signature == null || signature.length == 0) {
076            return new String[0];
077        }
078        String[] result = new String[signature.length / 2];
079        for (int i = 1, k = 0; i < signature.length; i += 2, k++) {
080            result[k] = signature[i];
081        }
082        return result;
083    }
084
085    public String getInputsAsString(OperationInfo op) {
086        String[] result = getInputs(op);
087        if (result == null || result.length == 0) {
088            return "void";
089        }
090        return StringUtils.join(result, ", ");
091    }
092
093    public String getOutputsAsString(OperationInfo op) {
094        String[] result = getOutputs(op);
095        if (result == null || result.length == 0) {
096            return "void";
097        }
098        return StringUtils.join(result, ", ");
099    }
100
101    public String getParamDefaultValue(Param param) {
102        if (param.values != null && param.values.length > 0) {
103            return StringUtils.join(param.values, ", ");
104        }
105        return "";
106    }
107
108    @GET
109    @Produces("text/html")
110    @Override
111    public Object doViewDefault() {
112        Template t = (Template) super.doViewDefault();
113        try {
114            OperationDocumentation opeDoc = Framework.getService(AutomationService.class)
115                                                     .getOperation(nxArtifactId)
116                                                     .getDocumentation();
117
118            ByteArrayOutputStream baos = new ByteArrayOutputStream();
119            JsonWriter.writeOperation(baos, opeDoc, true);
120            t.arg("json", baos.toString());
121        } catch (OperationException | IOException e) {
122            throw new NuxeoException(e);
123        }
124        return t;
125    }
126
127    @Override
128    public String getSearchCriterion() {
129        return "'" + super.getSearchCriterion() + "' Operation";
130    }
131}