Search in sources :

Example 1 with ViewFoundSet

use of com.servoy.j2db.dataprocessing.ViewFoundSet in project servoy-client by Servoy.

the class BasicFormController method executeFunction.

/**
 * call a scriptMethod (== function)
 *
 * @param cmd be the the id from the method or the name
 * @param methodKey
 */
public Object executeFunction(String cmd, Object[] args, boolean saveData, Object src, boolean focusEvent, String methodKey, boolean allowFoundsetMethods, boolean executeWhenFieldValidationFailed, boolean throwException) throws Exception {
    Object function = null;
    Scriptable scope = formScope;
    String name = cmd;
    int id = Utils.getAsInteger(cmd);
    if (id > 0) {
        name = formScope.getFunctionName(new Integer(id));
    }
    Pair<String, String> nameScope = ScopesUtils.getVariableScope(name);
    boolean global = nameScope != null && nameScope.getLeft() != null;
    if (id <= 0 && global) {
        name = nameScope.getRight();
    } else {
        function = formScope.getFunctionByName(name);
    }
    if (// TODO foundset methods for ViewFoundSet?
    allowFoundsetMethods && !global && function == null && formModel instanceof FoundSet) {
        // try foundset method
        ScriptMethod scriptMethod = application.getFlattenedSolution().getScriptMethod(id);
        if (scriptMethod != null) {
            name = scriptMethod.getName();
        }
        if (name != null) {
            scope = (FoundSet) formModel;
            function = scope.getPrototype().get(name, scope);
        }
    }
    if (function == null || function == Scriptable.NOT_FOUND) {
        GlobalScope globalScope = null;
        if (id > 0) {
            ScriptMethod scriptMethod = application.getFlattenedSolution().getScriptMethod(id);
            if (scriptMethod != null) {
                globalScope = application.getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
            }
        } else if (nameScope != null) {
            globalScope = application.getScriptEngine().getScopesScope().getGlobalScope(nameScope.getLeft());
        }
        if (globalScope != null) {
            scope = globalScope;
            if (id > 0) {
                name = globalScope.getFunctionName(new Integer(id));
            }
            function = globalScope.getFunctionByName(name);
        }
    }
    Function f;
    if (function instanceof Function) /* else null or UniqueTag.NOT_FOUND */
    {
        f = (Function) function;
    } else {
        if (cmd != null) {
            if (throwException) {
                throw new IllegalArgumentException("Could not find function '" + cmd + "' for form " + getName());
            } else {
                application.reportJSError("Could not find function '" + cmd + "' for form " + getName() + " , invoked by Object:", src);
            }
            return null;
        }
        // sometimes executeFunction is called with cmd=null just to trigger field validation, see BaseEventExecutor.fireEventCommand()
        f = null;
    }
    if (throwException) {
        return executeFunction(// $NON-NLS-1$
        f, // $NON-NLS-1$
        args, // $NON-NLS-1$
        scope, // $NON-NLS-1$
        scope, // $NON-NLS-1$
        saveData, // $NON-NLS-1$
        src, // $NON-NLS-1$
        f == null || !Utils.getAsBoolean(f.get("_AllowToRunInFind_", f)), focusEvent, methodKey, executeWhenFieldValidationFailed, false, true);
    }
    try {
        return executeFunction(// $NON-NLS-1$
        f, // $NON-NLS-1$
        args, // $NON-NLS-1$
        scope, // $NON-NLS-1$
        scope, // $NON-NLS-1$
        saveData, // $NON-NLS-1$
        src, // $NON-NLS-1$
        f == null || !Utils.getAsBoolean(f.get("_AllowToRunInFind_", f)), focusEvent, methodKey, executeWhenFieldValidationFailed, false, false);
    } catch (ApplicationException ex) {
        application.reportError(ex.getMessage(), null);
    } catch (Exception ex) {
        // this.requestFocus();
        // $NON-NLS-1$ //$NON-NLS-2$
        application.reportError(application.getI18NMessage("servoy.formPanel.error.executingMethod", new Object[] { getName() + "." + name }), ex);
    }
    return null;
}
Also used : GlobalScope(com.servoy.j2db.scripting.GlobalScope) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) ViewFoundSet(com.servoy.j2db.dataprocessing.ViewFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 2 with ViewFoundSet

use of com.servoy.j2db.dataprocessing.ViewFoundSet in project servoy-client by Servoy.

the class BasicFormController method executeFormMethod.

public Object executeFormMethod(TypedProperty<Integer> methodProperty, Object[] args, Boolean testFindMode, boolean saveData, boolean allowFoundsetMethods) {
    Object ret = null;
    Integer id = ((Integer) form.getProperty(methodProperty.getPropertyName()));
    if (id.intValue() > 0 && formScope != null) {
        FormExecutionState formExecutionState = null;
        if (getFormUI() instanceof ISupportFormExecutionState) {
            formExecutionState = ((ISupportFormExecutionState) getFormUI()).formMethodExecution();
        }
        String sName = null;
        try {
            Object function = null;
            Scriptable scope = formScope;
            // try form method
            sName = formScope.getFunctionName(id);
            if (sName != null) {
                function = formScope.getFunctionByName(sName);
            }
            if (!(function instanceof Function)) {
                // try global method
                ScriptMethod scriptMethod = application.getFlattenedSolution().getScriptMethod(id.intValue());
                if (scriptMethod != null) {
                    GlobalScope globalScope = application.getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
                    if (globalScope != null) {
                        scope = globalScope;
                        sName = globalScope.getFunctionName(id);
                        if (sName != null) {
                            function = globalScope.getFunctionByName(sName);
                        }
                    }
                    if (// TODO foundset methods for ViewFoundSet?
                    allowFoundsetMethods && !(function instanceof Function) && formModel instanceof FoundSet) {
                        scope = (FoundSet) formModel;
                        function = scope.getPrototype().get(scriptMethod.getName(), scope);
                    }
                }
            }
            if (function instanceof Function) {
                if (testFindMode == null) {
                    // $NON-NLS-1$
                    testFindMode = Boolean.valueOf(!Utils.getAsBoolean(((Function) function).get("_AllowToRunInFind_", (Function) function)));
                }
                ret = executeFunction((Function) function, Utils.arrayMerge(args, Utils.parseJSExpressions(form.getFlattenedMethodArguments(methodProperty.getPropertyName()))), scope, scope, saveData, null, testFindMode.booleanValue(), false, methodProperty.getPropertyName(), false, true, false);
            }
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.executeMethod", new Object[] { sName }), ex);
        } finally {
            if (formExecutionState != null && getFormUI() instanceof ISupportFormExecutionState) {
                ((ISupportFormExecutionState) getFormUI()).formMethodExecuted(formExecutionState);
            }
        }
    }
    return ret;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) GlobalScope(com.servoy.j2db.scripting.GlobalScope) IJSFoundSet(com.servoy.base.scripting.api.IJSFoundSet) IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) ViewFoundSet(com.servoy.j2db.dataprocessing.ViewFoundSet) ISwingFoundSet(com.servoy.j2db.dataprocessing.ISwingFoundSet) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) Scriptable(org.mozilla.javascript.Scriptable) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException)

Aggregations

IJSFoundSet (com.servoy.base.scripting.api.IJSFoundSet)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)2 IFoundSet (com.servoy.j2db.dataprocessing.IFoundSet)2 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)2 RelatedFoundSet (com.servoy.j2db.dataprocessing.RelatedFoundSet)2 ViewFoundSet (com.servoy.j2db.dataprocessing.ViewFoundSet)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)2 GlobalScope (com.servoy.j2db.scripting.GlobalScope)2 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)2 ServoyException (com.servoy.j2db.util.ServoyException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (org.mozilla.javascript.Function)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 Scriptable (org.mozilla.javascript.Scriptable)2 JSFunction (org.mozilla.javascript.annotations.JSFunction)2