Search in sources :

Example 11 with JSEvent

use of com.servoy.j2db.scripting.JSEvent in project servoy-client by Servoy.

the class EventExecutor method executeEvent.

public Object executeEvent(WebComponent component, String eventType, int eventId, Object[] eventArgs) {
    Scriptable scope = null;
    Function f = null;
    Object[] newargs = eventArgs != null ? Arrays.copyOf(eventArgs, eventArgs.length) : null;
    if (eventId > 0) {
        ScriptMethod scriptMethod = formController.getApplication().getFlattenedSolution().getScriptMethod(eventId);
        if (scriptMethod != null) {
            if (scriptMethod.getParent() instanceof Form) {
                FormScope formScope = formController.getFormScope();
                f = formScope.getFunctionByName(scriptMethod.getName());
                if (f != null && f != Scriptable.NOT_FOUND) {
                    scope = formScope;
                }
            } else // is it a global method
            if (scriptMethod.getParent() instanceof Solution) {
                scope = formController.getApplication().getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
                if (scope != null) {
                    f = ((GlobalScope) scope).getFunctionByName(scriptMethod.getName());
                }
            } else // very like a foundset/entity method
            {
                Scriptable foundsetScope = null;
                if (component instanceof WebFormComponent) {
                    IRecord rec = ((WebFormComponent) component).getDataAdapterList().getRecord();
                    if (rec != null) {
                        foundsetScope = (Scriptable) rec.getParentFoundSet();
                    }
                }
                if (foundsetScope == null)
                    foundsetScope = (Scriptable) formController.getFormModel();
                if (foundsetScope != null) {
                    // TODO ViewFoundSets should be come a scriptable if they have foundset methods..
                    scope = foundsetScope;
                    Object scopeMethod = scope.getPrototype().get(scriptMethod.getName(), scope);
                    if (scopeMethod instanceof Function)
                        f = (Function) scopeMethod;
                }
            }
            if (f == null) {
                Debug.error(// $NON-NLS-1$ //$NON-NLS-2$
                "No function found for " + scriptMethod + " when trying to execute the event " + eventType + '(' + eventId + ") of component: " + component, // $NON-NLS-1$
                new RuntimeException());
                return null;
            }
        } else {
            Debug.warn("Couldn't find the ScriptMethod for event: " + eventType + " with event id: " + eventId + " to execute for component " + component);
        }
    }
    // $NON-NLS-1$
    if (formController.isInFindMode() && !Utils.getAsBoolean(f.get("_AllowToRunInFind_", f)))
        return null;
    if (newargs != null) {
        for (int i = 0; i < newargs.length; i++) {
            if (newargs[i] instanceof JSONObject && "event".equals(((JSONObject) newargs[i]).optString("type"))) {
                JSONObject json = (JSONObject) newargs[i];
                JSEvent event = new JSEvent();
                JSEventType.fillJSEvent(event, json, component, formController);
                event.setType(getEventType(eventType));
                event.setName(RepositoryHelper.getDisplayName(eventType, BaseComponent.class));
                newargs[i] = event;
            } else {
                // try to convert the received arguments
                WebObjectFunctionDefinition propertyDesc = component.getSpecification().getHandler(eventType);
                List<PropertyDescription> parameters = propertyDesc.getParameters();
                if (i < parameters.size()) {
                    PropertyDescription parameterPropertyDescription = parameters.get(i);
                    ValueReference<Boolean> returnValueAdjustedIncommingValueForIndex = new ValueReference<Boolean>(Boolean.FALSE);
                    newargs[i] = NGConversions.INSTANCE.convertSabloComponentToRhinoValue(JSONUtils.fromJSON(null, newargs[i], parameterPropertyDescription, new BrowserConverterContext(component, PushToServerEnum.allow), returnValueAdjustedIncommingValueForIndex), parameterPropertyDescription, component, scope);
                }
            // TODO? if in propertyDesc.getAsPropertyDescription().getConfig() we have  "type":"${dataproviderType}" and parameterPropertyDescription.getType() is Object
            // then get the type from the dataprovider and try to convert the json to that type instead of simply object
            }
        }
    }
    if (component instanceof WebFormComponent) {
        IPersist persist = ((WebFormComponent) component).getFormElement().getPersistIfAvailable();
        if (persist instanceof AbstractBase) {
            List<Object> instanceMethodArguments = ((AbstractBase) persist).getFlattenedMethodArguments(eventType);
            if (instanceMethodArguments != null && instanceMethodArguments.size() > 0) {
                // create entries for the instanceMethodArguments if they are more then callback arguments
                if (instanceMethodArguments.size() > newargs.length) {
                    newargs = Utils.arrayJoin(newargs, new Object[instanceMethodArguments.size() - newargs.length]);
                }
                // use instanceMethodArguments if not null, else just use the callback argument
                for (int i = 0; i < instanceMethodArguments.size(); i++) {
                    Object value = instanceMethodArguments.get(i);
                    if (value != null && value != JSONObject.NULL) {
                        newargs[i] = Utils.parseJSExpression(value);
                    }
                }
            }
        }
    }
    try {
        formController.getApplication().updateLastAccessed();
        return formController.getApplication().getScriptEngine().executeFunction(f, scope, scope, newargs, false, false);
    } catch (Exception ex) {
        formController.getApplication().reportJSError(ex.getMessage(), ex);
        return null;
    }
}
Also used : GlobalScope(com.servoy.j2db.scripting.GlobalScope) BaseComponent(com.servoy.j2db.persistence.BaseComponent) Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) FormScope(com.servoy.j2db.scripting.FormScope) Function(org.mozilla.javascript.Function) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) Solution(com.servoy.j2db.persistence.Solution) ValueReference(org.sablo.util.ValueReference) JSEvent(com.servoy.j2db.scripting.JSEvent) IJSEvent(com.servoy.base.scripting.api.IJSEvent) IRecord(com.servoy.j2db.dataprocessing.IRecord) AbstractBase(com.servoy.j2db.persistence.AbstractBase) Scriptable(org.mozilla.javascript.Scriptable) JSONException(org.json.JSONException) PropertyDescription(org.sablo.specification.PropertyDescription) JSONObject(org.json.JSONObject) IPersist(com.servoy.j2db.persistence.IPersist) JSONObject(org.json.JSONObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Aggregations

JSEvent (com.servoy.j2db.scripting.JSEvent)11 JSONObject (org.json.JSONObject)4 IComponent (com.servoy.j2db.ui.IComponent)3 IJSEvent (com.servoy.base.scripting.api.IJSEvent)2 FormController (com.servoy.j2db.FormController)2 Form (com.servoy.j2db.persistence.Form)2 IPersist (com.servoy.j2db.persistence.IPersist)2 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)2 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)2 ServoyException (com.servoy.j2db.util.ServoyException)2 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 Scriptable (org.mozilla.javascript.Scriptable)2 PropertyDescription (org.sablo.specification.PropertyDescription)2 FoundSetListWrapper (com.servoy.j2db.dataprocessing.FoundSetListWrapper)1 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)1 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)1 IRecord (com.servoy.j2db.dataprocessing.IRecord)1 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)1 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)1