001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017package org.nuxeo.apidoc.browse;
018
019import javax.ws.rs.GET;
020import javax.ws.rs.Path;
021import javax.ws.rs.Produces;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.apidoc.api.NuxeoArtifact;
025import org.nuxeo.apidoc.api.OperationInfo;
026import org.nuxeo.ecm.automation.OperationDocumentation.Param;
027import org.nuxeo.ecm.automation.OperationException;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.webengine.model.WebObject;
030
031@WebObject(type = "operation")
032public class OperationWO extends NuxeoArtifactWebObject {
033
034    @Override
035    @GET
036    @Produces("text/html")
037    @Path("introspection")
038    public Object doGet() {
039        return getView("view").arg("operation", getTargetComponentInfo());
040    }
041
042    public OperationInfo getTargetComponentInfo() {
043        return getSnapshotManager().getSnapshot(getDistributionId(), ctx.getCoreSession()).getOperation(nxArtifactId);
044    }
045
046    @Override
047    public NuxeoArtifact getNxArtifact() {
048        return getTargetComponentInfo();
049    }
050
051    protected String[] getInputs(OperationInfo op) {
052        String[] signature = op.getSignature();
053        if (signature == null || signature.length == 0) {
054            return new String[0];
055        }
056        String[] result = new String[signature.length / 2];
057        for (int i = 0, k = 0; i < signature.length; i += 2, k++) {
058            result[k] = signature[i];
059        }
060        return result;
061    }
062
063    protected String[] getOutputs(OperationInfo op) {
064        String[] signature = op.getSignature();
065        if (signature == null || signature.length == 0) {
066            return new String[0];
067        }
068        String[] result = new String[signature.length / 2];
069        for (int i = 1, k = 0; i < signature.length; i += 2, k++) {
070            result[k] = signature[i];
071        }
072        return result;
073    }
074
075    public String getInputsAsString(OperationInfo op) {
076        String[] result = getInputs(op);
077        if (result == null || result.length == 0) {
078            return "void";
079        }
080        return StringUtils.join(result, ", ");
081    }
082
083    public String getOutputsAsString(OperationInfo op) {
084        String[] result = getOutputs(op);
085        if (result == null || result.length == 0) {
086            return "void";
087        }
088        return StringUtils.join(result, ", ");
089    }
090
091    public String getParamDefaultValue(Param param) {
092        if (param.values != null && param.values.length > 0) {
093            return StringUtils.join(param.values, ", ");
094        }
095        return "";
096    }
097
098}