001/*
002 * (C) Copyright 2015-2016 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.HashMap;
024import java.util.Map;
025import java.util.Map.Entry;
026
027import org.nuxeo.ecm.automation.AutomationService;
028import org.nuxeo.ecm.automation.core.util.DataModelProperties;
029import org.nuxeo.ecm.automation.core.util.Properties;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.runtime.api.Framework;
032
033import jdk.nashorn.api.scripting.ScriptObjectMirror;
034
035/**
036 * Class injected/published in Nashorn engine to execute automation service.
037 *
038 * @since 7.2
039 */
040public class AutomationMapper {
041
042    protected final CoreSession session;
043
044    public final ScriptOperationContext ctx;
045
046    public AutomationMapper(CoreSession session, ScriptOperationContext operationContext) {
047        this.session = session;
048        ctx = operationContext;
049    }
050
051    public Object executeOperation(String opId, Object input, ScriptObjectMirror parameters) throws Exception {
052        AutomationService automationService = Framework.getService(AutomationService.class);
053        unwrapContext(ctx, input);
054        Map<String, Object> params = unwrapParameters(parameters);
055        Object output = automationService.run(ctx, opId, params);
056        return wrapContextAndOutput(output);
057    }
058
059    public void unwrapContext(ScriptOperationContext ctx, Object inputOutput) {
060        Object result = unwrap(inputOutput);
061        ctx.setInput(result);
062        ctx.replaceAll((key, value) -> WrapperHelper.unwrap(value));
063    }
064
065    protected Map<String, Object> unwrapParameters(ScriptObjectMirror parameters) {
066        Map<String, Object> params = new HashMap<>();
067        for (String key : parameters.keySet()) {
068            params.put(key, unwrap(parameters.get(key)));
069        }
070        return params;
071    }
072
073    private Object unwrap(Object inputOutput) {
074        Object result = WrapperHelper.unwrap(inputOutput);
075        if (result instanceof Map<?, ?>) {
076            result = computeProperties((Map<?, ?>) result);
077        }
078        return result;
079    }
080
081    protected Object wrapContextAndOutput(Object output) {
082        ctx.replaceAll((key, value) -> WrapperHelper.wrap(value, ctx.getCoreSession()));
083        return WrapperHelper.wrap(output, ctx.getCoreSession());
084    }
085
086    protected Properties computeProperties(Map<?, ?> result) {
087        DataModelProperties props = new DataModelProperties();
088        for (Entry<?, ?> entry : result.entrySet()) {
089            props.getMap().put(entry.getKey().toString(), (Serializable) entry.getValue());
090        }
091        return props;
092    }
093
094}