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;
}
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;
}
});
}
}
Aggregations