Search in sources :

Example 1 with IExecutingEnviroment

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

the class NGClient method createScriptEngine.

@Override
protected IExecutingEnviroment createScriptEngine() {
    IExecutingEnviroment scriptEngine = super.createScriptEngine();
    WebObjectSpecification[] serviceSpecifications = WebServiceSpecProvider.getSpecProviderState().getAllWebComponentSpecifications();
    PluginScope scope = (PluginScope) scriptEngine.getSolutionScope().get("plugins", scriptEngine.getSolutionScope());
    scope.setLocked(false);
    for (WebObjectSpecification serviceSpecification : serviceSpecifications) {
        if (serviceSpecification.getApiFunctions().size() != 0 || serviceSpecification.getAllPropertiesNames().size() != 0) {
            scope.put(ClientService.convertToJSName(serviceSpecification.getName()), scope, new WebServiceScriptable(this, serviceSpecification, scriptEngine.getSolutionScope()));
        }
    }
    scope.setLocked(true);
    return scriptEngine;
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) PluginScope(com.servoy.j2db.scripting.PluginScope)

Example 2 with IExecutingEnviroment

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

the class BasicFormController method executeFunction.

@SuppressWarnings("nls")
protected Object executeFunction(Function f, Object[] args, Scriptable scope, Scriptable thisObject, boolean saveData, Object src, boolean testFindMode, boolean focusEvent, String methodKey, boolean executeWhenFieldValidationFailed, boolean useFormAsEventSourceEventually, boolean throwException) throws Exception {
    if (// only run certain methods in find
    !(testFindMode && isInFindMode())) {
        // this is a semi saveData , we do NOT want the record go out of edit(!) and is updated in db
        if (saveData) {
            application.getFoundSetManager().getEditRecordList().prepareForSave(false);
        }
        if (f != null) {
            if (!executeWhenFieldValidationFailed && Boolean.TRUE.equals(application.getRuntimeProperties().get(IServiceProvider.RT_LASTFIELDVALIDATIONFAILED_FLAG))) {
                if (Debug.tracing()) {
                    Debug.trace("Function not executed because a field is marked invalid");
                }
                return null;
            }
            FormAndComponent formAndComponent = getJSApplicationNames(src, f, useFormAsEventSourceEventually);
            try {
                currentFormExecutingFunctionCount.incrementAndGet();
                Object[] newArgs = args;
                if (formAndComponent != null) {
                    // for use of deprecated aplication.getMethodTriggerElementName() and aplication.getMethodTriggerFormName()
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().pushLastNames(formAndComponent);
                    }
                    if (methodKey != null) {
                        // add form event if needed
                        MethodTemplate methodTemplate = MethodTemplate.getTemplate(null, methodKey);
                        if (methodTemplate != null) {
                            MethodArgument[] methodArguments = methodTemplate.getArguments();
                            for (int i = 0; methodArguments != null && i < methodArguments.length; i++) {
                                if (methodArguments[i].getType() == ArgumentType.JSEvent) {
                                    // method template declares an event argument
                                    if (args == null || args.length <= i || args[i] == null) {
                                        // no event argument there yet, insert a form event
                                        JSEvent event = getJSEvent(formAndComponent.src, methodKey);
                                        if (args == null || args.length <= i) {
                                            newArgs = new Object[i + 1];
                                            if (args != null) {
                                                System.arraycopy(args, 0, newArgs, 0, args.length);
                                            }
                                        }
                                        newArgs[i] = event;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                if (newArgs != null && newArgs.length > 0) {
                    for (Object newArg : newArgs) {
                        if (newArg instanceof JSEvent) {
                            JSEvent event = (JSEvent) newArg;
                            if (formScope != null && event.getSource() instanceof IComponent && ((IComponent) event.getSource()).getName() != null) {
                                Object elementScope = formScope.get("elements");
                                if (elementScope instanceof Scriptable) {
                                    Object elementSrc = ((Scriptable) elementScope).get(((IComponent) event.getSource()).getName(), (Scriptable) elementScope);
                                    if (elementSrc != null) {
                                        if (elementSrc instanceof ITwoNativeJavaObject) {
                                            Object scriptable = event.getSource();
                                            if (scriptable instanceof IScriptableProvider) {
                                                scriptable = ((IScriptableProvider) scriptable).getScriptObject();
                                            }
                                            ((ITwoNativeJavaObject) elementSrc).setRealObject(scriptable);
                                        }
                                        event.setSource(elementSrc);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                return application.getScriptEngine().executeFunction(f, scope, thisObject, newArgs, focusEvent, throwException);
            } finally {
                currentFormExecutingFunctionCount.decrementAndGet();
                if (formAndComponent != null) {
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().popLastStackNames(formAndComponent);
                    }
                }
                // after a script clear the unchanged records so that no records keep hanging around.
                if (!focusEvent && !"onRecordEditStopMethodID".equals(methodKey) && !"onRenderMethodID".equals(methodKey) && application.getFoundSetManager() != null) {
                    application.getFoundSetManager().getEditRecordList().removeUnChangedRecords(false, false);
                }
            }
        }
    }
    return null;
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) JSEvent(com.servoy.j2db.scripting.JSEvent) MethodArgument(com.servoy.j2db.persistence.MethodArgument) IComponent(com.servoy.j2db.ui.IComponent) MethodTemplate(com.servoy.j2db.persistence.MethodTemplate) Scriptable(org.mozilla.javascript.Scriptable) FormAndComponent(com.servoy.j2db.scripting.JSApplication.FormAndComponent) ScriptEngine(com.servoy.j2db.scripting.ScriptEngine) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider)

Example 3 with IExecutingEnviroment

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

the class BasicFormController method initForJSUsage.

// if the js init is not done , do!
public synchronized JSForm initForJSUsage() {
    if (formScope == null) {
        if (destroyed) {
            Debug.error("Calling initForJSUsage on a destroyed form: " + this, new RuntimeException("Calling initForJSUsage on a destroyed form: " + this));
            return null;
        }
        Context.enter();
        try {
            IExecutingEnviroment se = application.getScriptEngine();
            if (se != null) {
                SolutionScope solScope = application.getScriptEngine().getSolutionScope();
                // $NON-NLS-1$
                initForJSUsage((CreationalPrototype) solScope.get("forms", solScope));
            } else {
                // saw this happen on MAC debug smart client - edit the value in a combobox (change it) then directly click on the close window button;
                // shutDown executed first (windowClosing) - setting solution to null, then the combo item state change (generated by focus lost) event was triggered
                // $NON-NLS-1$
                Debug.log("Trying to initForJSUsage with null script engine - probably a method was about to be invoked post-shutdown", null);
            }
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.initScriptEngine"), ex);
        } finally {
            Context.exit();
        }
    }
    return scriptableForm;
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) SolutionScope(com.servoy.j2db.scripting.SolutionScope)

Example 4 with IExecutingEnviroment

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

the class DataAdapterList method destroy.

/**
 */
public void destroy() {
    if (currentRecord != null) {
        // With prototype you can still get global foundsets
        // setRecord(new PrototypeState(currentRecord.getParentFoundSet()), true);
        setRecord(null, false);
    }
    if (formController != null && !formController.isDestroyed() && formController.getFormScope() != null) {
        formController.getFormScope().getModificationSubject().removeModificationListener(this);
    }
    IExecutingEnviroment er = application.getScriptEngine();
    if (er != null) {
        SolutionScope ss = er.getSolutionScope();
        if (ss != null) {
            ScopesScope gs = ss.getScopesScope();
            if (gs != null) {
                gs.getModificationSubject().removeModificationListener(this);
            }
        }
    }
    if (servoyAwareBeans != null) {
        for (IServoyAwareBean b : servoyAwareBeans) {
            try {
                if (b instanceof IDestroyable) {
                    ((IDestroyable) b).destroy();
                }
            } catch (RuntimeException e) {
                // never make the app break on faulty beans
                Debug.error(e);
            }
        }
    }
    servoyAwareBeans = null;
    if (relatedDataAdapters != null) {
        for (IDisplayRelatedData drd : relatedDataAdapters) {
            drd.destroy();
        }
    }
    relatedDataAdapters = null;
    if (dataDisplays != null) {
        for (IDisplayData dd : dataDisplays) {
            if (dd instanceof IDestroyable) {
                ((IDestroyable) dd).destroy();
            }
        }
    }
    dataDisplays = null;
    if (dataAdapters != null) {
        for (IDataAdapter da : dataAdapters.values()) {
            if (da instanceof IDestroyable) {
                ((IDestroyable) da).destroy();
            }
        }
    }
    dataAdapters = null;
    currentDisplay = null;
    visible = false;
    destroyed = true;
    if (currentRecord != null) {
        // $NON-NLS-1$
        Debug.error("After destroy there is still a current record in DataAdapterList of formcontroller: " + formController, new RuntimeException());
        currentRecord.removeModificationListener(this);
    }
}
Also used : IServoyAwareBean(com.servoy.j2db.dataui.IServoyAwareBean) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) IDestroyable(com.servoy.j2db.util.IDestroyable) ScopesScope(com.servoy.j2db.scripting.ScopesScope) SolutionScope(com.servoy.j2db.scripting.SolutionScope)

Example 5 with IExecutingEnviroment

use of com.servoy.j2db.scripting.IExecutingEnviroment 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)

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