001/*
002 * (C) Copyright 2015 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 *     Thierry Delprat <tdelprat@nuxeo.com>
018 *     Vladimir Pasquier <vpasquier@nuxeo.com>
019 */
020package org.nuxeo.automation.scripting.internals;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import jdk.nashorn.api.scripting.ScriptObjectMirror;
029
030import org.nuxeo.ecm.automation.AutomationService;
031import org.nuxeo.ecm.automation.core.scripting.DocumentWrapper;
032import org.nuxeo.ecm.automation.core.util.DataModelProperties;
033import org.nuxeo.ecm.automation.core.util.Properties;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentModelList;
037import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Class injected/published in Nashorn engine to execute automation service.
042 *
043 * @since 7.2
044 */
045public class AutomationMapper {
046
047    protected final CoreSession session;
048
049    public final ScriptOperationContext ctx;
050
051    public AutomationMapper(CoreSession session, ScriptOperationContext operationContext) {
052        this.session = session;
053        ctx = operationContext;
054    }
055
056    public Object executeOperation(String opId, Object input, ScriptObjectMirror parameters) throws Exception {
057        AutomationService automationService = Framework.getService(AutomationService.class);
058        unwrapContext(ctx, input);
059        Map<String, Object> params = unwrapParameters(parameters);
060        Object output = automationService.run(ctx, opId, params);
061        return wrapContextAndOutput(output);
062    }
063
064    public void unwrapContext(ScriptOperationContext ctx, Object inputOutput) {
065        if (inputOutput instanceof ScriptObjectMirror) {
066            ctx.setInput(extractProperties((ScriptObjectMirror) inputOutput));
067        } else if (inputOutput instanceof DocumentWrapper) {
068            ctx.setInput(((DocumentWrapper) inputOutput).getDoc());
069        } else if (inputOutput instanceof List<?>) {
070            DocumentModelList docs = new DocumentModelListImpl();
071            List<?> l = (List<?>) inputOutput;
072            for (Object item : l) {
073                if (item instanceof DocumentWrapper) {
074                    docs.add(((DocumentWrapper) item).getDoc());
075                }
076            }
077            if (docs.size() == l.size() && docs.size() > 0) {
078                ctx.setInput(docs);
079            }
080        } else {
081            ctx.setInput(inputOutput);
082        }
083        for (String entryId : ctx.keySet()) {
084            Object entry = ctx.get(entryId);
085            if (entry instanceof DocumentWrapper) {
086                ctx.put(entryId, ((DocumentWrapper) entry).getDoc());
087            } else if (ctx.get(entryId) instanceof List<?>) {
088                DocumentModelList docs = new DocumentModelListImpl();
089                List<?> l = (List<?>) entry;
090                for (Object item : l) {
091                    if (item instanceof DocumentWrapper) {
092                        docs.add(((DocumentWrapper) item).getDoc());
093                    }
094                }
095                if (docs.size() == l.size() && docs.size() > 0) {
096                    ctx.put(entryId, docs);
097                }
098            }
099        }
100    }
101
102    protected Properties extractProperties(ScriptObjectMirror parameters) {
103        DataModelProperties props = new DataModelProperties();
104        Map<String, Object> data = MarshalingHelper.unwrapMap(parameters);
105        for (String k : data.keySet()) {
106            props.getMap().put(k, (Serializable) data.get(k));
107        }
108        return props;
109    }
110
111    protected Object wrapContextAndOutput(Object output) {
112        for (String entryId : ctx.keySet()) {
113            Object entry = ctx.get(entryId);
114            if (entry instanceof DocumentModel) {
115                ctx.put(entryId, new DocumentWrapper(ctx.getCoreSession(), (DocumentModel) entry));
116            }
117            if (entry instanceof DocumentModelList) {
118                List<DocumentWrapper> docs = new ArrayList<>();
119                for (DocumentModel doc : (DocumentModelList) entry) {
120                    docs.add(new DocumentWrapper(ctx.getCoreSession(), doc));
121                }
122                ctx.put(entryId, docs);
123            }
124        }
125        if (output instanceof DocumentModel) {
126            return new DocumentWrapper(ctx.getCoreSession(), (DocumentModel) output);
127        } else if (output instanceof DocumentModelList) {
128            List<DocumentWrapper> docs = new ArrayList<>();
129            for (DocumentModel doc : (DocumentModelList) output) {
130                docs.add(new DocumentWrapper(ctx.getCoreSession(), doc));
131            }
132            return docs;
133        }
134        return output;
135    }
136
137    protected Map<String, Object> unwrapParameters(ScriptObjectMirror parameters) {
138        Map<String, Object> params = new HashMap<String, Object>();
139        for (String k : parameters.keySet()) {
140            Object value = parameters.get(k);
141            if (value instanceof ScriptObjectMirror) {
142                ScriptObjectMirror jso = (ScriptObjectMirror) value;
143                if (jso.isArray()) {
144                    params.put(k, MarshalingHelper.unwrap(jso));
145                } else {
146                    params.put(k, extractProperties(jso));
147                }
148            } else if (value instanceof DocumentWrapper) {
149                params.put(k, ((DocumentWrapper) value).getDoc());
150            } else if (value instanceof List<?>) {
151                DocumentModelList docs = new DocumentModelListImpl();
152                List<?> l = (List<?>) value;
153                for (Object item : l) {
154                    if (item instanceof DocumentWrapper) {
155                        docs.add(((DocumentWrapper) item).getDoc());
156                    }
157                }
158                if (docs.size() == l.size() && docs.size() > 0) {
159                    params.put(k, docs);
160                }
161            } else {
162                params.put(k, value);
163            }
164        }
165        return params;
166    }
167
168}