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.adapters;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.apidoc.api.OperationInfo;
026import org.nuxeo.common.utils.Path;
027import org.nuxeo.ecm.automation.OperationDocumentation.Param;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.PathRef;
031import org.nuxeo.ecm.core.api.PropertyException;
032
033/**
034 * Adapter from a Nuxeo document to the {@link OperationInfo} interface.
035 */
036public class OperationInfoDocAdapter extends BaseNuxeoArtifactDocAdapter implements OperationInfo {
037
038    protected OperationInfoDocAdapter(DocumentModel doc) {
039        super(doc);
040    }
041
042    @Override
043    public String getArtifactType() {
044        return TYPE_NAME;
045    }
046
047    // artifact id with type-specific prefix
048    @Override
049    public String getId() {
050        return ARTIFACT_PREFIX + getName();
051    }
052
053    @Override
054    public String getName() {
055        return safeGet(PROP_NAME);
056    }
057
058    @SuppressWarnings("unchecked")
059    @Override
060    public String[] getAliases() {
061        try {
062            return ((List<String>) doc.getPropertyValue(PROP_ALIASES)).toArray(new String[0]);
063        } catch (PropertyException e) {
064            log.error("Unable to get signature field", e);
065        }
066        return null;
067    }
068
069    @Override
070    public String getVersion() {
071        return safeGet(PROP_VERSION);
072    }
073
074    @Override
075    public String getDescription() {
076        return safeGet(PROP_DESCRIPTION);
077    }
078
079    @SuppressWarnings("unchecked")
080    @Override
081    public String[] getSignature() {
082        try {
083            return ((List<String>) doc.getPropertyValue(PROP_SIGNATURE)).toArray(new String[0]);
084        } catch (PropertyException e) {
085            log.error("Unable to get signature field", e);
086        }
087        return null;
088    }
089
090    @Override
091    public String getCategory() {
092        return safeGet(PROP_CATEGORY);
093    }
094
095    @Override
096    public String getUrl() {
097        return safeGet(PROP_URL);
098    }
099
100    @Override
101    public String getLabel() {
102        return safeGet(PROP_LABEL);
103    }
104
105    @Override
106    public String getRequires() {
107        return safeGet(PROP_REQUIRES);
108    }
109
110    @Override
111    public String getSince() {
112        return safeGet(PROP_SINCE);
113    }
114
115    @SuppressWarnings("unchecked")
116    @Override
117    public List<Param> getParams() {
118        List<Map<String, Serializable>> maps = (List<Map<String, Serializable>>) doc.getPropertyValue(PROP_PARAMS);
119        List<Param> params = new ArrayList<Param>();
120        if (maps != null) {
121            for (Map<String, Serializable> map : maps) {
122                Param p = new Param();
123                p.name = (String) map.get(PROP_PARAM_NAME);
124                p.type = (String) map.get(PROP_PARAM_TYPE);
125                p.widget = (String) map.get(PROP_PARAM_WIDGET);
126                p.values = ((List<String>) map.get(PROP_PARAM_VALUES)).toArray(new String[0]);
127                Long order = (Long) map.get(PROP_PARAM_ORDER);
128                p.order = order == null ? 0 : order.intValue();
129                Boolean required = (Boolean) map.get(PROP_PARAM_REQUIRED);
130                p.isRequired = required == null ? false : required.booleanValue();
131                params.add(p);
132            }
133        }
134        return params;
135    }
136
137    @Override
138    public int compareTo(OperationInfo o) {
139        String s1 = getLabel() == null ? getId() : getLabel();
140        String s2 = o.getLabel() == null ? o.getId() : o.getLabel();
141        return s1.compareTo(s2);
142    }
143
144    /**
145     * Creates an actual document from the {@link OperationInfo}.
146     */
147    public static OperationInfo create(OperationInfo oi, CoreSession session, String containerPath) {
148        String name = computeDocumentName(oi.getId());
149        String targetPath = new Path(containerPath).append(name).toString();
150        boolean exists = session.exists(new PathRef(targetPath));
151        DocumentModel doc;
152        if (exists) {
153            doc = session.getDocument(new PathRef(targetPath));
154        } else {
155            doc = session.createDocumentModel(TYPE_NAME);
156            doc.setPathInfo(containerPath, name);
157        }
158        doc.setPropertyValue("dc:title", oi.getName());
159        doc.setPropertyValue(PROP_NAME, oi.getName());
160        doc.setPropertyValue(PROP_ALIASES, oi.getAliases());
161        doc.setPropertyValue(PROP_VERSION, oi.getVersion());
162        doc.setPropertyValue(PROP_DESCRIPTION, oi.getDescription());
163        doc.setPropertyValue(PROP_SIGNATURE, oi.getSignature());
164        doc.setPropertyValue(PROP_CATEGORY, oi.getCategory());
165        doc.setPropertyValue(PROP_URL, oi.getUrl());
166        doc.setPropertyValue(PROP_LABEL, oi.getLabel());
167        doc.setPropertyValue(PROP_REQUIRES, oi.getRequires());
168        doc.setPropertyValue(PROP_SINCE, oi.getSince());
169        doc.setPropertyValue(PROP_OP_CLASS, oi.getOperationClass());
170        doc.setPropertyValue(PROP_CONTRIBUTING_COMPONENT, oi.getContributingComponent());
171        List<Map<String, Serializable>> params = new ArrayList<Map<String, Serializable>>();
172        for (Param p : oi.getParams()) {
173            Map<String, Serializable> map = new HashMap<String, Serializable>();
174            map.put(PROP_PARAM_NAME, p.getName());
175            map.put(PROP_PARAM_TYPE, p.getType());
176            map.put(PROP_PARAM_WIDGET, p.getWidget());
177            map.put(PROP_PARAM_VALUES, p.getValues());
178            map.put(PROP_PARAM_REQUIRED, Boolean.valueOf(p.isRequired()));
179            map.put(PROP_PARAM_ORDER, Long.valueOf(p.getOrder()));
180            params.add(map);
181        }
182        doc.setPropertyValue(PROP_PARAMS, (Serializable) params);
183        if (exists) {
184            doc = session.saveDocument(doc);
185        } else {
186            doc = session.createDocument(doc);
187        }
188        return new OperationInfoDocAdapter(doc);
189    }
190
191    @Override
192    public String getOperationClass() {
193        return safeGet(PROP_OP_CLASS);
194    }
195
196    @Override
197    public String getContributingComponent() {
198        return safeGet(PROP_CONTRIBUTING_COMPONENT);
199    }
200
201}