Search in sources :

Example 1 with JSSignature

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

the class FoundSet method sort.

/**
 * Sorts the foundset based on the given record comparator function.
 * Tries to preserve selection based on primary key. If first record is selected or cannot select old record it will select first record after sort.
 * The comparator function is called to compare
 * two records, that are passed as arguments, and
 * it will return -1/0/1 if the first record is less/equal/greater
 * then the second record.
 *
 * The function based sorting does not work with printing.
 * It is just a temporary in-memory sort.
 *
 * NOTE: starting with 7.2 release this function doesn't save the data anymore
 *
 * @sample
 * %%prefix%%foundset.sort(mySortFunction);
 *
 * function mySortFunction(r1, r2)
 * {
 *	var o = 0;
 *	if(r1.id < r2.id)
 *	{
 *		o = -1;
 *	}
 *	else if(r1.id > r2.id)
 *	{
 *		o = 1;
 *	}
 *	return o;
 * }
 *
 * @param recordComparisonFunction record comparator function
 */
@JSFunction
@JSSignature(arguments = { Function.class })
public void sort(Object recordComparisonFunction) {
    if (recordComparisonFunction instanceof Function) {
        final Function func = (Function) recordComparisonFunction;
        final IExecutingEnviroment scriptEngine = fsm.getApplication().getScriptEngine();
        final Scriptable recordComparatorScope = func.getParentScope();
        sort(new Comparator<Object[]>() {

            public int compare(Object[] o1, Object[] o2) {
                try {
                    Object compareResult = scriptEngine.executeFunction(func, recordComparatorScope, recordComparatorScope, new Object[] { getRecord(o1), getRecord(o2) }, false, true);
                    double cmp = Utils.getAsDouble(compareResult, true);
                    return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
                } catch (Exception ex) {
                    Debug.error(ex);
                }
                return 0;
            }
        });
    }
}
Also used : BaseFunction(org.mozilla.javascript.BaseFunction) JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSSignature(com.servoy.j2db.scripting.annotations.JSSignature) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 2 with JSSignature

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

the class ViewFoundSet method sort.

/**
 * Sorts the foundset based on the given record comparator function.
 * Tries to preserve selection based on primary key. If first record is selected or cannot select old record it will select first record after sort.
 * The comparator function is called to compare
 * two records, that are passed as arguments, and
 * it will return -1/0/1 if the first record is less/equal/greater
 * then the second record.
 *
 * The function based sorting does not work with printing.
 * It is just a temporary in-memory sort.
 *
 * NOTE: starting with 7.2 release this function doesn't save the data anymore
 *
 * @sample
 * %%prefix%%foundset.sort(mySortFunction);
 *
 * function mySortFunction(r1, r2)
 * {
 *	var o = 0;
 *	if(r1.id < r2.id)
 *	{
 *		o = -1;
 *	}
 *	else if(r1.id > r2.id)
 *	{
 *		o = 1;
 *	}
 *	return o;
 * }
 *
 * @param recordComparisonFunction record comparator function
 */
@JSFunction
@JSSignature(arguments = { Function.class })
public void sort(Object recordComparisonFunction) {
    if (recordComparisonFunction instanceof Function) {
        final Function func = (Function) recordComparisonFunction;
        final IExecutingEnviroment scriptEngine = manager.getApplication().getScriptEngine();
        final Scriptable recordComparatorScope = func.getParentScope();
        sort(new Comparator<Object[]>() {

            public int compare(Object[] o1, Object[] o2) {
                try {
                    Object compareResult = scriptEngine.executeFunction(func, recordComparatorScope, recordComparatorScope, new Object[] { getRecord(o1), getRecord(o2) }, false, true);
                    double cmp = Utils.getAsDouble(compareResult, true);
                    return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
                } catch (Exception ex) {
                    Debug.error(ex);
                }
                return 0;
            }
        });
    }
}
Also used : JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) IConstantsObject(com.servoy.j2db.scripting.IConstantsObject) Scriptable(org.mozilla.javascript.Scriptable) RemoteException(java.rmi.RemoteException) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSSignature(com.servoy.j2db.scripting.annotations.JSSignature) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)2 JSSignature (com.servoy.j2db.scripting.annotations.JSSignature)2 ServoyException (com.servoy.j2db.util.ServoyException)2 RemoteException (java.rmi.RemoteException)2 Function (org.mozilla.javascript.Function)2 Scriptable (org.mozilla.javascript.Scriptable)2 JSFunction (org.mozilla.javascript.annotations.JSFunction)2 ApplicationException (com.servoy.j2db.ApplicationException)1 IConstantsObject (com.servoy.j2db.scripting.IConstantsObject)1 BaseFunction (org.mozilla.javascript.BaseFunction)1