Search in sources :

Example 16 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet 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 17 with FoundSet

use of com.servoy.j2db.dataprocessing.FoundSet 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)

Example 18 with FoundSet

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

the class MediaURLStreamHandler method getBlobLoaderMedia.

public static byte[] getBlobLoaderMedia(IServiceProvider application, String urlQueryPart) throws IOException {
    // cannot work without a solution
    if (application.getSolution() == null)
        return null;
    String datasource = null;
    String serverName = null;
    String tableName = null;
    try {
        if (urlQueryPart == null)
            return null;
        String dataProvider = null;
        List<String> pks = new SafeArrayList<String>();
        // $NON-NLS-1$
        StringTokenizer tk = new StringTokenizer(urlQueryPart, "?&");
        while (tk.hasMoreTokens()) {
            String token = tk.nextToken();
            if (// $NON-NLS-1$
            token.startsWith("global=")) {
                // $NON-NLS-1$
                String globalName = token.substring("global=".length());
                Object obj = application.getScriptEngine().getScopesScope().get(null, globalName);
                if (obj instanceof byte[]) {
                    return (byte[]) obj;
                }
                if (obj instanceof String) {
                    // TODO check can we always just convert to the default encoding of this machine (server if web)
                    return ((String) obj).getBytes();
                }
                return null;
            }
            if (// $NON-NLS-1$
            token.startsWith("servername=")) {
                // $NON-NLS-1$
                serverName = token.substring("servername=".length());
            } else if (// $NON-NLS-1$
            token.startsWith("tablename=")) {
                // $NON-NLS-1$
                tableName = token.substring("tablename=".length());
            } else if (// $NON-NLS-1$
            token.startsWith("datasource=")) {
                // $NON-NLS-1$
                datasource = token.substring("datasource=".length());
            } else if (// $NON-NLS-1$
            token.startsWith("dataprovider=")) {
                // $NON-NLS-1$
                dataProvider = token.substring("dataprovider=".length());
            } else if (// $NON-NLS-1$
            token.startsWith("rowid")) {
                // get id
                int index = Utils.getAsInteger(token.substring(5, 6));
                if (index > 0) {
                    // get value after 'rowidX='
                    pks.add(index - 1, token.substring(7));
                } else {
                    // get value after 'rowid='
                    pks.add(0, token.substring(6));
                }
            }
        }
        if (datasource == null && serverName != null && tableName != null) {
            datasource = DataSourceUtils.createDBTableDataSource(serverName, tableName);
        }
        if (application.getFoundSetManager().getTable(datasource) == null) {
            // $NON-NLS-1$
            throw new IOException(Messages.getString("servoy.exception.serverAndTableNotFound", DataSourceUtils.getDBServernameTablename(datasource)));
        }
        FoundSet fs = (FoundSet) application.getFoundSetManager().getNewFoundSet(datasource);
        // use mutable list here, elements are overwritten with Column.getAsRightType equivalent
        List<Object[]> rows = new ArrayList<Object[]>(1);
        rows.add(pks.toArray());
        if (!fs.loadExternalPKList(new BufferedDataSet(null, null, rows))) {
            return null;
        }
        IRecordInternal rec = fs.getRecord(0);
        if (rec == null) {
            return null;
        }
        Object blob_value = rec.getValue(dataProvider);
        if (blob_value instanceof byte[]) {
            return (byte[]) blob_value;
        }
        if (blob_value instanceof String) {
            // TODO check can we always just convert to the default encoding of this machine (server if web)
            return ((String) blob_value).getBytes();
        }
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
    return null;
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) FoundSet(com.servoy.j2db.dataprocessing.FoundSet) IOException(java.io.IOException) IOException(java.io.IOException) StringTokenizer(java.util.StringTokenizer) BufferedDataSet(com.servoy.j2db.dataprocessing.BufferedDataSet)

Example 19 with FoundSet

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

the class FormManager method ifRootFormInFind.

private boolean ifRootFormInFind(FormController fc) {
    boolean result = false;
    if (fc.isFormVisible() && fc.isInFindMode()) {
        IFoundSetInternal foundSet = fc.getFoundSet();
        if (foundSet instanceof RelatedFoundSet) {
            // see if any parent foundset is in find mode; if not, then add it as root
            // form in find mode
            List<IRecordInternal> parentRecords = ((RelatedFoundSet) foundSet).getParents();
            boolean hasParentsInFindMode = false;
            for (IRecordInternal record : parentRecords) {
                IFoundSetInternal parentFoundSet = record.getParentFoundSet();
                if (parentFoundSet instanceof FoundSet && ((FoundSet) parentFoundSet).isInFindMode()) {
                    hasParentsInFindMode = true;
                    break;
                }
            }
            if (!hasParentsInFindMode) {
                result = true;
            }
        } else {
            result = true;
        }
    }
    return result;
}
Also used : IRecordInternal(com.servoy.j2db.dataprocessing.IRecordInternal) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) IFoundSetInternal(com.servoy.j2db.dataprocessing.IFoundSetInternal) RelatedFoundSet(com.servoy.j2db.dataprocessing.RelatedFoundSet) FoundSet(com.servoy.j2db.dataprocessing.FoundSet)

Aggregations

FoundSet (com.servoy.j2db.dataprocessing.FoundSet)19 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)7 IFoundSet (com.servoy.j2db.dataprocessing.IFoundSet)6 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)6 ISwingFoundSet (com.servoy.j2db.dataprocessing.ISwingFoundSet)6 ArrayList (java.util.ArrayList)5 Scriptable (org.mozilla.javascript.Scriptable)5 IJSFoundSet (com.servoy.base.scripting.api.IJSFoundSet)4 RelatedFoundSet (com.servoy.j2db.dataprocessing.RelatedFoundSet)4 SortColumn (com.servoy.j2db.dataprocessing.SortColumn)3 ViewFoundSet (com.servoy.j2db.dataprocessing.ViewFoundSet)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)3 ServoyException (com.servoy.j2db.util.ServoyException)3 Function (org.mozilla.javascript.Function)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 JSFunction (org.mozilla.javascript.annotations.JSFunction)3 FormController (com.servoy.j2db.FormController)2 FormManager (com.servoy.j2db.FormManager)2 FoundSetManager (com.servoy.j2db.dataprocessing.FoundSetManager)2