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