use of com.servoy.j2db.server.ngclient.WebFormComponent in project servoy-client by Servoy.
the class FormComponentPropertyType method toRhinoValue.
@Override
public Object toRhinoValue(Object webComponentValue, PropertyDescription pd, IWebObjectContext componentOrService, Scriptable startScriptable) {
Scriptable newObject = DefaultScope.newObject(startScriptable);
WebFormComponent webFormComponent = (WebFormComponent) componentOrService;
FlattenedSolution fs = webFormComponent.getDataConverterContext().getSolution();
FormComponentCache cache = null;
if (webComponentValue instanceof FormComponentSabloValue) {
cache = ((FormComponentSabloValue) webComponentValue).getCache();
} else {
Form form = getForm(webComponentValue, fs);
if (form == null)
return null;
// TODO return here a NativeScriptable object that understand the full hiearchy?
cache = FormElementHelper.INSTANCE.getFormComponentCache(webFormComponent.getFormElement(), pd, (JSONObject) webComponentValue, form, fs);
}
IWebFormUI formUI = webFormComponent.findParent(IWebFormUI.class);
String prefix = FormElementHelper.getStartElementName(webFormComponent.getFormElement(), pd);
for (FormElement fe : cache.getFormComponentElements()) {
String name = fe.getName();
if (name != null && !name.startsWith(FormElement.SVY_NAME_PREFIX)) {
RuntimeWebComponent webComponent = formUI.getRuntimeWebComponent(fe.getRawName());
if (webComponent != null) {
newObject.put(name.substring(prefix.length()), newObject, webComponent);
}
}
}
return newObject;
}
use of com.servoy.j2db.server.ngclient.WebFormComponent in project servoy-client by Servoy.
the class FormPropertyType method toJSON.
@Override
public JSONWriter toJSON(JSONWriter writer, String key, Object sabloValue, PropertyDescription pd, DataConversion clientConversion, IBrowserConverterContext dataConverterContext) throws JSONException {
if (key != null) {
writer.key(key);
}
String formName = null;
if (sabloValue instanceof String) {
formName = (String) sabloValue;
if (dataConverterContext != null && dataConverterContext.getWebObject() instanceof IContextProvider) {
FlattenedSolution flattenedSolution = ((IContextProvider) dataConverterContext.getWebObject()).getDataConverterContext().getApplication().getFlattenedSolution();
Form form = flattenedSolution.getForm(formName);
// form name
if (form == null) {
form = (Form) flattenedSolution.searchPersist((String) sabloValue);
}
if (form != null) {
formName = form.getName();
}
}
} else if (sabloValue instanceof CharSequence) {
formName = ((CharSequence) sabloValue).toString();
} else if (sabloValue instanceof Form) {
formName = ((Form) sabloValue).getName();
} else if (sabloValue instanceof FormController) {
formName = ((FormController) sabloValue).getName();
} else if (sabloValue instanceof FormScope) {
formName = ((FormScope) sabloValue).getFormController().getName();
} else {
formName = null;
Debug.error("Cannot handle unknown value for Form type: " + sabloValue);
}
writer.value(formName);
if (CurrentWindow.get() instanceof INGClientWindow) {
// if this is a web component that triggered it, register to only allow this form for that component
if (dataConverterContext != null && dataConverterContext.getWebObject() instanceof WebFormComponent)
((INGClientWindow) CurrentWindow.get()).registerAllowedForm(formName, ((WebFormComponent) dataConverterContext.getWebObject()).getFormElement());
else
// else register it for null then this form is allowed globally (a form in dialog of popup)
((INGClientWindow) CurrentWindow.get()).registerAllowedForm(formName, null);
}
return writer;
}
use of com.servoy.j2db.server.ngclient.WebFormComponent in project servoy-client by Servoy.
the class FormatTypeSabloValue method getSabloValue.
protected ComponentFormat getSabloValue(String formatValue, String dataproviderId, Object valuelistId, String foundsetId, IWebObjectContext webObjectCntxt) {
INGApplication application = ((WebFormComponent) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getApplication();
IDataProviderLookup dataProviderLookup = null;
// IMPORTANT: here we use the for: configs in .spec file
//
// if you have for: [valuelist, dataprovider] then 2 things can happen:
// - valuelist if it has both real and display values - forces the type; it is either TEXT (custom vl., global method vl.) or the 'display' column type in case it's a DB valuelist
// - valuelist if not real/display but only one kind of values: here it is required in docs in the spec file that the valuelist property also defines "for": dataprovider if format
// defines both "for" valuelist and dataprovider => valuelist doesn't force the type and then the dataprovider will decide the type
//
// if you have just for: dataprovider the the dataprovider property determines the type
// if you have just for: valuelist (TODO) - this is currently not properly supported - as here we should get the type always from the VL (for both display and real values) - as we don't have a dataprovider to fall back on
isValuelistFormatSet = false;
if (valuelistId != null) {
// if we have a "for" valuelist, see if this valuelist forces the format type due to display values (when they are separate from real values)
// otherwise it will do nothing and loop/fallback to the other if clause below which checks the "for" dataprovider
ValueList valuelistPersist = ValueListTypeSabloValue.getValuelistPersist(valuelistId, application);
if (valuelistPersist != null) {
IDataProvider dataProvider = null;
ITable table;
try {
if (valuelistPersist.getRelationName() != null) {
Relation[] relations = application.getFlattenedSolution().getRelationSequence(valuelistPersist.getRelationName());
table = application.getFlattenedSolution().getTable(relations[relations.length - 1].getForeignDataSource());
} else {
table = application.getFlattenedSolution().getTable(valuelistPersist.getDataSource());
}
if (table != null) {
// if the format is for a table valuelist - the type to be used is the one of the dp chosen as 'display' in the valuelist
String dp = null;
// if show == real then we can use show anyway cause there is only one value for both real and display; if show != real we care about show
int showDataProviders = valuelistPersist.getShowDataProviders();
if ((showDataProviders & 1) != 0) {
dp = valuelistPersist.getDataProviderID1();
}
if ((showDataProviders & 2) != 0) {
// display value is a concat of multiple columns, so a string; not even sure if format property makes sense, but it is for a String then
if (dp != null)
return ComponentFormat.getComponentFormat(formatValue, IColumnTypes.TEXT, application);
dp = valuelistPersist.getDataProviderID2();
}
if ((showDataProviders & 4) != 0) {
// display value is a concat of multiple columns, so a string; not even sure if format property makes sense, but it is for a String then
if (dp != null)
return ComponentFormat.getComponentFormat(formatValue, IColumnTypes.TEXT, application);
dp = valuelistPersist.getDataProviderID3();
}
if (dp != null) {
dataProvider = application.getFlattenedSolution().getDataProviderForTable(table, dp);
}
isValuelistFormatSet = true;
return ComponentFormat.getComponentFormat(formatValue, dataProvider, application, true);
} else if (valuelistPersist.getValueListType() == IValueListConstants.CUSTOM_VALUES) {
IValueList realValuelist = null;
ValueListTypeSabloValue valuelistSabloValue = (ValueListTypeSabloValue) FoundsetLinkedTypeSabloValue.unwrapIfNeeded(webObjectContext.getProperty(propertyDependencies.valueListPropertyName));
if (valuelistSabloValue != null) {
// take it from property, may not be the shared instance in case setvaluelistitems on component was used
realValuelist = valuelistSabloValue.getValueList();
}
if (realValuelist == null) {
realValuelist = com.servoy.j2db.component.ComponentFactory.getRealValueList(application, valuelistPersist, true, Types.OTHER, ComponentFormat.getComponentFormat(formatValue, dataproviderId, null, application, true).parsedFormat, null, true);
}
if (realValuelist.hasRealValues()) {
// if custom vl has both real and display values, the display values are TEXT (format is for those)
// of if it has displayValueType set, use that
isValuelistFormatSet = true;
int realValueDisplayType = realValuelist.getValueList().getDisplayValueType();
return ComponentFormat.getComponentFormat(formatValue, realValueDisplayType != 0 ? realValueDisplayType : IColumnTypes.TEXT, application);
}
} else if (valuelistPersist.getValueListType() == IValueListConstants.GLOBAL_METHOD_VALUES) {
PropertyDescription vlPD = webObjectCntxt.getPropertyDescription(propertyDependencies.valueListPropertyName);
Object vlPDConfig = null;
if (vlPD != null) {
vlPDConfig = vlPD.getConfig();
if (vlPDConfig instanceof FoundsetLinkedConfig)
vlPDConfig = ((FoundsetLinkedConfig) vlPDConfig).getWrappedConfig();
}
boolean lazyLoad = valuelistPersist.getLazyLoading() && vlPDConfig instanceof ValueListConfig && ((ValueListConfig) vlPDConfig).getLazyLoading();
if (!lazyLoad) {
IValueList realValuelist = com.servoy.j2db.component.ComponentFactory.getRealValueList(application, valuelistPersist, true, Types.OTHER, null, null, true);
if (realValuelist instanceof GlobalMethodValueList) {
((GlobalMethodValueList) realValuelist).fill(null, "", null);
if (realValuelist.hasRealValues() || realValuelist.getSize() == 0 || (realValuelist.getSize() == 1 && valuelistPersist.getAddEmptyValue() == IValueListConstants.EMPTY_VALUE_ALWAYS)) {
// if global method vl has both real and display values, the display values are TEXT (format is for those)
// of if it has displayValueType set, use that
isValuelistFormatSet = true;
int realValueDisplayType = realValuelist.getValueList().getDisplayValueType();
return ComponentFormat.getComponentFormat(formatValue, realValueDisplayType != 0 ? realValueDisplayType : IColumnTypes.TEXT, application);
}
}
}
}
} catch (Exception ex) {
Debug.error(ex);
}
}
// here - we want to fall back to the dataprovider if available in for: [ ..., dataprovider] if valuelist didn't force a certain display type on the format
}
if (dataproviderId != null && foundsetId != null) {
ITable table = null;
Form form = ((IContextProvider) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm();
// always assume now that the the properties has the foundset property name.
FoundsetTypeSabloValue runtimeValOfFoundset = (FoundsetTypeSabloValue) webObjectCntxt.getUnderlyingWebObject().getProperty(this.propertyDependencies.foundsetPropertyName);
if (runtimeValOfFoundset != null && runtimeValOfFoundset.getFoundset() != null && runtimeValOfFoundset.getFoundset().getDataSource().equals(foundsetId)) {
table = runtimeValOfFoundset.getFoundset().getTable();
}
if (table == null)
table = FoundsetTypeSabloValue.getTableBasedOfFoundsetPropertyFromFoundsetIdentifier(foundsetId, application, form);
if (table != null) {
dataProviderLookup = new FormAndTableDataProviderLookup(application.getFlattenedSolution(), form, table);
}
// else it will be searched for in form's context and table as below
}
if (dataProviderLookup == null) {
WebObjectSpecification spec = ((WebFormComponent) webObjectCntxt.getUnderlyingWebObject()).getParent().getSpecification();
if (spec != null) {
Collection<PropertyDescription> formComponentProperties = spec.getProperties(FormComponentPropertyType.INSTANCE);
if (formComponentProperties != null) {
for (PropertyDescription property : formComponentProperties) {
if (property.getConfig() instanceof ComponentTypeConfig && ((ComponentTypeConfig) property.getConfig()).forFoundset != null) {
FoundsetTypeSabloValue runtimeValOfFoundset = (FoundsetTypeSabloValue) ((WebFormComponent) webObjectCntxt.getUnderlyingWebObject()).getParent().getProperty(((ComponentTypeConfig) property.getConfig()).forFoundset);
ITable table = null;
Form form = ((IContextProvider) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm();
if (runtimeValOfFoundset.getFoundset() != null)
table = runtimeValOfFoundset.getFoundset().getTable();
if (table == null)
table = FoundsetTypeSabloValue.getTableBasedOfFoundsetPropertyFromFoundsetIdentifier(runtimeValOfFoundset.getFoundsetSelector(), application, form);
if (table != null) {
dataProviderLookup = new FormAndTableDataProviderLookup(application.getFlattenedSolution(), form, table);
}
break;
}
}
}
}
}
if (dataProviderLookup == null && application != null)
dataProviderLookup = application.getFlattenedSolution().getDataproviderLookup(application.getFoundSetManager(), ((IContextProvider) webObjectCntxt.getUnderlyingWebObject()).getDataConverterContext().getForm().getForm());
ComponentFormat format = ComponentFormat.getComponentFormat(formatValue, dataproviderId, dataProviderLookup, application, true);
return format;
}
use of com.servoy.j2db.server.ngclient.WebFormComponent in project servoy-client by Servoy.
the class JSEventType method fillJSEvent.
/**
* @param event Event that needs to be filled
* @param jsonObject The json data for that has the event data.
* @param webObject The webObject element (WEbFormComponent or WebFormUI)
* @param controller Optional the controller object if the caller knows this already
* @return
*/
@SuppressWarnings("nls")
public static void fillJSEvent(JSBaseEvent event, JSONObject jsonObject, BaseWebObject webObject, IWebFormController controller) {
event.setType(jsonObject.optString("eventType"));
String formName = controller != null ? controller.getName() : "";
String elementName = "";
if (webObject instanceof WebFormComponent) {
elementName = ((WebFormComponent) webObject).getFormElement().getRawName();
if (elementName == null)
elementName = "";
if (formName.isEmpty()) {
BaseWebObject parentWebObject = ((WebFormComponent) webObject).getParent();
while (parentWebObject != null && !(parentWebObject instanceof WebFormUI)) {
parentWebObject = ((WebFormComponent) parentWebObject).getParent();
}
if (parentWebObject instanceof WebFormUI) {
formName = parentWebObject.getName();
}
}
}
if (formName.isEmpty()) {
formName = jsonObject.optString("formName");
}
if (formName.isEmpty() && webObject instanceof WebFormUI) {
// executeInlineScript with an event in params tells a DAL to execute it and it gives a formui as context to the fromJSON conversion
// for JSEventType problem is that it can give any formName from the client (the component/service can give anything there as an arg); or, if the function
// (sent to client before through "function" type) that will be used to execute the script is a global/scope function
// then formName can be null and in that case the WebFormUI will be the main form or the window (not the most nested one)
// so this is just a fallback and we do give priority to the form name determined on client through $window.createJSEvent(...) an put into
// "jsonObject" (see if above) - to target the correct form - closest one to event
formName = webObject.getName();
}
if (elementName.isEmpty()) {
elementName = jsonObject.optString("elementName");
}
if (!formName.isEmpty())
event.setFormName(formName);
if (!elementName.isEmpty())
event.setElementName(elementName);
if (!formName.isEmpty()) {
INGApplication application = ((IContextProvider) webObject).getDataConverterContext().getApplication();
IWebFormController formController = controller != null ? controller : application.getFormManager().getForm(formName);
if (formController != null) {
FormScope formScope = formController.getFormScope();
if (formScope != null) {
ElementScope elementsScope = (ElementScope) formScope.get("elements", null);
if (elementsScope != null) {
Object scriptableElement = !elementName.isEmpty() ? elementsScope.get(elementName, null) : null;
if (scriptableElement != null && scriptableElement != Scriptable.NOT_FOUND) {
event.setSource(scriptableElement);
} else if (webObject instanceof WebFormComponent) {
// quickly create a scriptable wrapper around the component so that the source can be set to a value that we expect.
FormElement fe = ((WebFormComponent) webObject).getFormElement();
RuntimeWebComponent runtimeComponent = new RuntimeWebComponent((WebFormComponent) webObject, webObject.getSpecification());
if (fe.isLegacy() || ((fe.getForm().getView() == IForm.LIST_VIEW || fe.getForm().getView() == FormController.LOCKED_LIST_VIEW || fe.getForm().getView() == FormController.TABLE_VIEW || fe.getForm().getView() == FormController.LOCKED_TABLE_VIEW) && fe.getTypeName().startsWith("svy-"))) {
// add legacy behavior
runtimeComponent.setPrototype(new RuntimeLegacyComponent((WebFormComponent) webObject));
}
event.setSource(runtimeComponent);
}
}
}
}
}
try {
if (jsonObject.has("x"))
event.setLocation(new Point(jsonObject.optInt("x"), jsonObject.optInt("y")));
if (jsonObject.has("modifiers"))
event.setModifiers(jsonObject.optInt("modifiers"));
if (jsonObject.has("data"))
event.setData(jsonObject.opt("data"));
if (jsonObject.has("timestamp"))
event.setTimestamp(new Timestamp(jsonObject.getLong("timestamp")));
else
event.setTimestamp(new Date());
} catch (Exception e) {
Debug.error("error setting event properties from " + jsonObject + ", for component: " + elementName + " on form " + formName, e);
}
}
use of com.servoy.j2db.server.ngclient.WebFormComponent 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;
}
}
Aggregations