Search in sources :

Example 21 with FormScope

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

the class WebFormUI method initElementScope.

private ElementScope initElementScope(IFormController controller) {
    FormScope formScope = controller.getFormScope();
    ElementScope elementsScope = new ElementScope(formScope);
    // $NON-NLS-1$
    formScope.putWithoutFireChange("elements", elementsScope);
    return elementsScope;
}
Also used : ElementScope(com.servoy.j2db.scripting.ElementScope) FormScope(com.servoy.j2db.scripting.FormScope)

Example 22 with FormScope

use of com.servoy.j2db.scripting.FormScope 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)

Example 23 with FormScope

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

the class RhinoConversion method defaultFromRhino.

/**
 * Default conversion used to convert from Rhino property types that do not explicitly implement component <-> Rhino conversions. <BR/><BR/>
 * Values of types that don't implement the sablo <-> rhino conversions are by default accessible directly.
 */
@SuppressWarnings("unchecked")
public static // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object defaultFromRhino(// PropertyDescription / IWebObjectContext ... can be made available here if needed
Object propertyValue, // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object oldValue) {
    // convert simple values to json values
    if (isUndefinedOrNotFound(propertyValue)) {
        return null;
    }
    if (propertyValue instanceof NativeDate) {
        return ((NativeDate) propertyValue).unwrap();
    }
    if (propertyValue instanceof NativeObject) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> oldMap = (oldValue instanceof Map) ? (Map<String, Object>) oldValue : null;
        NativeObject no = (NativeObject) propertyValue;
        Object[] ids = no.getIds();
        for (Object id2 : ids) {
            Object value = null;
            if (id2 instanceof String) {
                value = no.get((String) id2, no);
            } else if (id2 instanceof Symbol) {
                value = no.get((Symbol) id2, no);
            } else if (id2 instanceof Number) {
                value = no.get(((Number) id2).intValue(), no);
            }
            String id = String.valueOf(id2);
            map.put(id, defaultFromRhino(value, oldMap != null ? oldMap.get(id) : null));
        }
        return map;
    }
    if (propertyValue instanceof NativeArray) {
        List<Object> list = new ArrayList<Object>();
        List<Object> oldList = (oldValue instanceof List) ? (List<Object>) oldValue : null;
        final NativeArray no = (NativeArray) propertyValue;
        final long naLength = no.getLength();
        for (int id = 0; id < naLength; id++) {
            list.add(defaultFromRhino(no.get(id, no), oldList != null && oldList.size() > id ? oldList.get(id) : null));
        }
        return list;
    }
    if (propertyValue instanceof FormScope && ((FormScope) propertyValue).getFormController() != null)
        return ((FormScope) propertyValue).getFormController().getName();
    if (propertyValue instanceof IFormController)
        return ((IFormController) propertyValue).getName();
    if (propertyValue instanceof JSDataSet) {
        return ((JSDataSet) propertyValue).getDataSet();
    }
    if (propertyValue instanceof RhinoMapOrArrayWrapper) {
        return ((RhinoMapOrArrayWrapper) propertyValue).getWrappedValue();
    }
    if (propertyValue instanceof CharSequence) {
        return propertyValue.toString();
    }
    return propertyValue;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) HashMap(java.util.HashMap) Symbol(org.mozilla.javascript.Symbol) ArrayList(java.util.ArrayList) JSDataSet(com.servoy.j2db.dataprocessing.JSDataSet) FormScope(com.servoy.j2db.scripting.FormScope) NativeObject(org.mozilla.javascript.NativeObject) NativeDate(org.mozilla.javascript.NativeDate) NativeObject(org.mozilla.javascript.NativeObject) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ArrayList(java.util.ArrayList) List(java.util.List) IFormController(com.servoy.j2db.IFormController) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

FormScope (com.servoy.j2db.scripting.FormScope)23 IFormController (com.servoy.j2db.IFormController)6 Form (com.servoy.j2db.persistence.Form)5 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)4 IRecord (com.servoy.j2db.dataprocessing.IRecord)3 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)3 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)3 GlobalScope (com.servoy.j2db.scripting.GlobalScope)3 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)3 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)3 ServoyException (com.servoy.j2db.util.ServoyException)3 JSONException (org.json.JSONException)3 Scriptable (org.mozilla.javascript.Scriptable)3 ApplicationException (com.servoy.j2db.ApplicationException)2 IServiceProvider (com.servoy.j2db.IServiceProvider)2 FindState (com.servoy.j2db.dataprocessing.FindState)2 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)2 IPersist (com.servoy.j2db.persistence.IPersist)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2