use of com.servoy.j2db.scripting.GlobalScope 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;
}
use of com.servoy.j2db.scripting.GlobalScope 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;
}
use of com.servoy.j2db.scripting.GlobalScope in project servoy-client by Servoy.
the class ServoyFunctionPropertyType method nativeFunctionToJSON.
private void nativeFunctionToJSON(NativeFunction function, Map<String, Object> map) throws Exception {
String functionName = function.getFunctionName();
Scriptable parentScope = function.getParentScope();
while (parentScope != null && !(parentScope instanceof ScriptVariableScope)) {
parentScope = parentScope.getParentScope();
}
if (parentScope instanceof FormScope && ((FormScope) parentScope).getFormController() != null) {
String formName = ((FormScope) parentScope).getFormController().getName();
map.put("script", SecuritySupport.encrypt(Settings.getInstance(), "forms." + formName + "." + functionName + "()"));
map.put("formname", formName);
} else if (parentScope instanceof GlobalScope) {
map.put("script", SecuritySupport.encrypt(Settings.getInstance(), "scopes." + ((GlobalScope) parentScope).getScopeName() + "." + functionName + "()"));
}
}
use of com.servoy.j2db.scripting.GlobalScope in project servoy-client by Servoy.
the class EventExecutor method executeEvent.
public Object executeEvent(WebComponent component, String eventType, int eventId, Object[] eventArgs) {
Scriptable scope = null;
Function f = null;
Object[] newargs = eventArgs != null ? Arrays.copyOf(eventArgs, eventArgs.length) : null;
if (eventId > 0) {
ScriptMethod scriptMethod = formController.getApplication().getFlattenedSolution().getScriptMethod(eventId);
if (scriptMethod != null) {
if (scriptMethod.getParent() instanceof Form) {
FormScope formScope = formController.getFormScope();
f = formScope.getFunctionByName(scriptMethod.getName());
if (f != null && f != Scriptable.NOT_FOUND) {
scope = formScope;
}
} else // is it a global method
if (scriptMethod.getParent() instanceof Solution) {
scope = formController.getApplication().getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
if (scope != null) {
f = ((GlobalScope) scope).getFunctionByName(scriptMethod.getName());
}
} else // very like a foundset/entity method
{
Scriptable foundsetScope = null;
if (component instanceof WebFormComponent) {
IRecord rec = ((WebFormComponent) component).getDataAdapterList().getRecord();
if (rec != null) {
foundsetScope = (Scriptable) rec.getParentFoundSet();
}
}
if (foundsetScope == null)
foundsetScope = (Scriptable) formController.getFormModel();
if (foundsetScope != null) {
// TODO ViewFoundSets should be come a scriptable if they have foundset methods..
scope = foundsetScope;
Object scopeMethod = scope.getPrototype().get(scriptMethod.getName(), scope);
if (scopeMethod instanceof Function)
f = (Function) scopeMethod;
}
}
if (f == null) {
Debug.error(// $NON-NLS-1$ //$NON-NLS-2$
"No function found for " + scriptMethod + " when trying to execute the event " + eventType + '(' + eventId + ") of component: " + component, // $NON-NLS-1$
new RuntimeException());
return null;
}
} else {
Debug.warn("Couldn't find the ScriptMethod for event: " + eventType + " with event id: " + eventId + " to execute for component " + component);
}
}
// $NON-NLS-1$
if (formController.isInFindMode() && !Utils.getAsBoolean(f.get("_AllowToRunInFind_", f)))
return null;
if (newargs != null) {
for (int i = 0; i < newargs.length; i++) {
if (newargs[i] instanceof JSONObject && "event".equals(((JSONObject) newargs[i]).optString("type"))) {
JSONObject json = (JSONObject) newargs[i];
JSEvent event = new JSEvent();
JSEventType.fillJSEvent(event, json, component, formController);
event.setType(getEventType(eventType));
event.setName(RepositoryHelper.getDisplayName(eventType, BaseComponent.class));
newargs[i] = event;
} else {
// try to convert the received arguments
WebObjectFunctionDefinition propertyDesc = component.getSpecification().getHandler(eventType);
List<PropertyDescription> parameters = propertyDesc.getParameters();
if (i < parameters.size()) {
PropertyDescription parameterPropertyDescription = parameters.get(i);
ValueReference<Boolean> returnValueAdjustedIncommingValueForIndex = new ValueReference<Boolean>(Boolean.FALSE);
newargs[i] = NGConversions.INSTANCE.convertSabloComponentToRhinoValue(JSONUtils.fromJSON(null, newargs[i], parameterPropertyDescription, new BrowserConverterContext(component, PushToServerEnum.allow), returnValueAdjustedIncommingValueForIndex), parameterPropertyDescription, component, scope);
}
// TODO? if in propertyDesc.getAsPropertyDescription().getConfig() we have "type":"${dataproviderType}" and parameterPropertyDescription.getType() is Object
// then get the type from the dataprovider and try to convert the json to that type instead of simply object
}
}
}
if (component instanceof WebFormComponent) {
IPersist persist = ((WebFormComponent) component).getFormElement().getPersistIfAvailable();
if (persist instanceof AbstractBase) {
List<Object> instanceMethodArguments = ((AbstractBase) persist).getFlattenedMethodArguments(eventType);
if (instanceMethodArguments != null && instanceMethodArguments.size() > 0) {
// create entries for the instanceMethodArguments if they are more then callback arguments
if (instanceMethodArguments.size() > newargs.length) {
newargs = Utils.arrayJoin(newargs, new Object[instanceMethodArguments.size() - newargs.length]);
}
// use instanceMethodArguments if not null, else just use the callback argument
for (int i = 0; i < instanceMethodArguments.size(); i++) {
Object value = instanceMethodArguments.get(i);
if (value != null && value != JSONObject.NULL) {
newargs[i] = Utils.parseJSExpression(value);
}
}
}
}
}
try {
formController.getApplication().updateLastAccessed();
return formController.getApplication().getScriptEngine().executeFunction(f, scope, scope, newargs, false, false);
} catch (Exception ex) {
formController.getApplication().reportJSError(ex.getMessage(), ex);
return null;
}
}
use of com.servoy.j2db.scripting.GlobalScope in project servoy-client by Servoy.
the class DataAdapterList method isGlobalDataprovider.
protected final boolean isGlobalDataprovider(String dataprovider) {
if (dataprovider == null)
return false;
ScopesScope ss = formController.getApplication().getScriptEngine().getScopesScope();
Pair<String, String> scope = ScopesUtils.getVariableScope(dataprovider);
if (scope.getLeft() != null) {
GlobalScope gs = ss.getGlobalScope(scope.getLeft());
return gs != null && gs.has(scope.getRight(), gs);
}
return false;
}
Aggregations