Search in sources :

Example 11 with IExecutingEnviroment

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

the class FoundSetManager method executeFoundsetTriggerInternal.

private Object executeFoundsetTriggerInternal(ITable table, Object[] args, TypedProperty<Integer> property, TriggerExecutionMode executionMode, boolean throwException, Scriptable foundsetScope) throws ServoyException {
    IExecutingEnviroment scriptEngine = getApplication().getScriptEngine();
    List<TriggerFunction> functions = getTriggerFunctions(table, property, foundsetScope);
    if (executionMode == ReturnFirst && functions.size() > 1) {
        Debug.warn("Multiple event handlers found for event " + property.getPropertyName() + ", only executing one");
    }
    for (TriggerFunction function : functions) {
        try {
            Object returnValue = scriptEngine.executeFunction(function.getFunction(), function.getScope(), function.getScope(), arrayMerge(args, parseJSExpressions(function.getTableNode().getFlattenedMethodArguments(property.getPropertyName()))), false, throwException);
            if (executionMode == ReturnFirst || (executionMode == BreakOnFalse && Boolean.FALSE.equals(returnValue))) {
                // return first value or break on false return, do not execute remaining triggers.
                return returnValue;
            }
        } catch (JavaScriptException e) {
            // update or insert method threw exception.
            throw new DataException(ServoyException.RECORD_VALIDATION_FAILED, e.getValue(), e).setContext(this.toString());
        } catch (EcmaError e) {
            throw new ApplicationException(ServoyException.SAVE_FAILED, e).setContext(this.toString());
        } catch (Exception e) {
            Debug.error(e);
            throw new ServoyException(ServoyException.SAVE_FAILED, new Object[] { e.getMessage() }).setContext(this.toString());
        }
    }
    return Boolean.TRUE;
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) EcmaError(org.mozilla.javascript.EcmaError) ApplicationException(com.servoy.j2db.ApplicationException) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) MarshallException(org.jabsorb.serializer.MarshallException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) ServoyException(com.servoy.j2db.util.ServoyException) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 12 with IExecutingEnviroment

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

the class JSDataSet method js_sort.

/**
 * Sort the dataset using the function as comparator.
 * The comparator function is called to compare two rows, that are passed as arguments, and
 * it will return -1/0/1 if the first row is less/equal/greater then the second row.
 *
 * NOTE: starting with 7.2 release, when called on datasource(foundset) dataset, this function doesn't save the data anymore
 *
 * @sample
 * //sort using comparator
 * dataset.sort(mySortFunction);
 *
 * function mySortFunction(r1, r2)
 * {
 *	var o = 0;
 *	if(r1[0] < r2[0])
 *	{
 *		o = -1;
 *	}
 *	else if(r1[0] > r2[0])
 *	{
 *		o = 1;
 *	}
 *	return o;
 * }
 *
 * @param comparator comparator function
 */
public void js_sort(final Function comparator) {
    if (set != null && comparator != null) {
        final IExecutingEnviroment scriptEngine = application.getScriptEngine();
        final Scriptable rowComparatorScope = comparator.getParentScope();
        set.sort(new Comparator<Object[]>() {

            public int compare(Object[] o1, Object[] o2) {
                try {
                    Object[] param1 = o1;
                    Object[] param2 = o2;
                    if (// o1 and o2 are pks, get the raw data to pass to rowComparator
                    set instanceof FoundsetDataSet) {
                        IFoundSetInternal foundset = ((FoundsetDataSet) set).getFoundSet();
                        if (foundset instanceof FoundSet) {
                            param1 = ((FoundSet) foundset).getRecord(o1).getRawData().getRawColumnData();
                            param2 = ((FoundSet) foundset).getRecord(o2).getRawData().getRawColumnData();
                            if (((FoundsetDataSet) set).autoAddedPkAsFirstCol_Offset == 1) {
                                // hide servoy internal pk column when pknames is null
                                Object[] res = new Object[param1.length - 1];
                                System.arraycopy(param1, 1, res, 0, res.length);
                                param1 = res;
                                res = new Object[param2.length - 1];
                                System.arraycopy(param2, 1, res, 0, res.length);
                                param2 = res;
                            }
                        }
                    }
                    Object compareResult = scriptEngine.executeFunction(comparator, rowComparatorScope, rowComparatorScope, new Object[] { param1, param2 }, false, true);
                    return Utils.getAsInteger(compareResult, true);
                } catch (Exception ex) {
                    Debug.error(ex);
                }
                return 0;
            }
        });
    }
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException)

Aggregations

IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)12 ServoyException (com.servoy.j2db.util.ServoyException)5 Scriptable (org.mozilla.javascript.Scriptable)5 RepositoryException (com.servoy.j2db.persistence.RepositoryException)4 SolutionScope (com.servoy.j2db.scripting.SolutionScope)3 RemoteException (java.rmi.RemoteException)3 Function (org.mozilla.javascript.Function)3 ApplicationException (com.servoy.j2db.ApplicationException)2 JSSignature (com.servoy.j2db.scripting.annotations.JSSignature)2 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 JSFunction (org.mozilla.javascript.annotations.JSFunction)2 FlattenedSolution (com.servoy.j2db.FlattenedSolution)1 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)1 MethodArgument (com.servoy.j2db.persistence.MethodArgument)1 MethodTemplate (com.servoy.j2db.persistence.MethodTemplate)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1 Solution (com.servoy.j2db.persistence.Solution)1 GlobalScope (com.servoy.j2db.scripting.GlobalScope)1 IConstantsObject (com.servoy.j2db.scripting.IConstantsObject)1