use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.
the class NGCustomJSONObjectType method toSabloComponentValue.
@Override
public Map<String, SabloT> toSabloComponentValue(final Object rhinoValue, final Map<String, SabloT> previousComponentValue, PropertyDescription pd, final IWebObjectContext webObjectContext) {
if (rhinoValue == null || RhinoConversion.isUndefinedOrNotFound(rhinoValue))
return null;
if (rhinoValue instanceof RhinoMapOrArrayWrapper) {
return (Map<String, SabloT>) ((RhinoMapOrArrayWrapper) rhinoValue).getWrappedValue();
} else {
final ChangeAwareMap<SabloT, SabloWT> previousSpecialMap = (ChangeAwareMap<SabloT, SabloWT>) previousComponentValue;
// if it's some kind of object, convert it (in depth, iterate over children)
if (rhinoValue instanceof NativeObject) {
Map<String, SabloT> rhinoObjectCopy = new HashMap<>();
NativeObject rhinoNativeObject = (NativeObject) rhinoValue;
CustomObjectContext<SabloT, SabloWT> customObjectContext = createComponentOrServiceExtension(webObjectContext);
Object[] keys = rhinoNativeObject.getIds();
Object value;
String keyAsString;
// perform the rhino-to-sablo conversions
for (Object key : keys) {
if (key instanceof String) {
keyAsString = (String) key;
value = rhinoNativeObject.get(keyAsString, rhinoNativeObject);
} else if (key instanceof Number) {
keyAsString = String.valueOf(((Number) key).intValue());
value = rhinoNativeObject.get(((Number) key).intValue(), rhinoNativeObject);
} else
throw new RuntimeException("JS Object key must be either String or Number.");
rhinoObjectCopy.put(keyAsString, (SabloT) NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, null, getCustomJSONTypeDefinition().getProperty(keyAsString), customObjectContext));
}
// create the new change-aware-map based on the converted sub-properties
ChangeAwareMap<SabloT, SabloWT> retVal = wrapAndKeepRhinoPrototype(rhinoObjectCopy, rhinoNativeObject.getPrototype(), previousSpecialMap, pd, new WrappingContext(webObjectContext.getUnderlyingWebObject(), pd.getName()), customObjectContext);
// after it is returned it and it's sub-properties will at some point get "attached" (ISmartPropertyValue)
return retVal;
} else
Debug.warn("Cannot convert value assigned from solution scripting into custom object property type; new value = " + rhinoValue + "; property = " + pd.getName() + "; component name = " + webObjectContext.getUnderlyingWebObject().getName());
}
// or should we return null or throw exception here? incompatible thing was assigned
return previousComponentValue;
}
use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.
the class RhinoMapWrapperTest method setUp.
@Before
public void setUp() {
Context cx = Context.enter();
TopLevel toplevelScope = new ImporterTopLevel(cx);
PropertyDescription pd = new PropertyDescriptionBuilder().withName("myCustomObjectProp").withType(new NGCustomJSONObjectType("myCustomObjectType", null)).build();
PropertyDescription copd = new PropertyDescriptionBuilder().withName("myCustomObjectType").withType(pd.getType()).withProperty("someInt", new PropertyDescriptionBuilder().withName("someInt").withType(IntPropertyType.INSTANCE).build()).build();
((NGCustomJSONObjectType) pd.getType()).setCustomJSONDefinition(copd);
wrappedMapValue = new HashMap<String, Object>();
wrappedMapValue.put("someInt", 111);
w = new RhinoMapOrArrayWrapper(wrappedMapValue, null, pd, toplevelScope);
Context.exit();
}
use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.
the class RuntimeWebComponentTest method arrayPropDirectAccess.
@Test
public void arrayPropDirectAccess() throws Exception {
IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("testForm");
Assert.assertNotNull(form);
FormScope formScope = form.getFormScope();
Context cx = Context.enter();
try {
// CHECK INITIAL DEFAULT VALUE FROM SPEC
RhinoMapOrArrayWrapper stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
Assert.assertArrayEquals(new String[] { "a", "b", "c" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
// same check as above but directly inside Rhino
Boolean testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 3) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\"][i] } ) ", "Evaluation Test Script", 1, null);
Assert.assertTrue(testResult.booleanValue());
// ADD ELEMENT TO EXISTING VALUE
cx.evaluateString(formScope, "elements.testComponent.getStringArray().push(\"d\") ", "Evaluation Test Script", 1, null);
// CHECK CHANGED VALUE
stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
Assert.assertArrayEquals(new String[] { "a", "b", "c", "d" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
// same check as above but directly inside Rhino
testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 4) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\", \"d\"][i] } ) ", "Evaluation Test Script", 1, null);
Assert.assertTrue(testResult.booleanValue());
// ASSIGN DIFFERENT ARRAY BY REF TO PROPERTY
cx.evaluateString(formScope, "elements.testComponent.stringArray = ['1', '2', '3']", "Evaluation Test Script", 1, null);
// CHECK NEW VALUE
stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
Assert.assertArrayEquals(new String[] { "1", "2", "3" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
// same check as above but directly inside Rhino
testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 3) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"1\", \"2\", \"3\"][i] } ) ", "Evaluation Test Script", 1, null);
Assert.assertTrue(testResult.booleanValue());
} finally {
Context.exit();
}
}
use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.
the class RuntimeWebComponentTest method arrayPropAccessThroughGetterAndSetter.
@Test
public void arrayPropAccessThroughGetterAndSetter() throws Exception {
IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("testForm");
Assert.assertNotNull(form);
FormScope formScope = form.getFormScope();
Context cx = Context.enter();
try {
// CHECK INITIAL DEFAULT VALUE FROM SPEC
RhinoMapOrArrayWrapper stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, // this used to fail with an exception when RuntimeLegacyComponent gave null scope in getter code
null);
Assert.assertArrayEquals(new String[] { "a", "b", "c" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
// same check as above but directly inside Rhino
Boolean testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 3) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\"][i] } ) ", "Evaluation Test Script", 1, null);
Assert.assertTrue(testResult.booleanValue());
// ADD ELEMENT TO EXISTING VALUE
cx.evaluateString(formScope, "elements.testComponent.getStringArray().push(\"d\") ", "Evaluation Test Script", 1, null);
// CHECK CHANGED VALUE
stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, null);
Assert.assertArrayEquals(new String[] { "a", "b", "c", "d" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
// same check as above but directly inside Rhino
testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 4) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\", \"d\"][i] } ) ", "Evaluation Test Script", 1, null);
Assert.assertTrue(testResult.booleanValue());
// ASSIGN DIFFERENT ARRAY BY REF TO PROPERTY
cx.evaluateString(formScope, "elements.testComponent.setStringArray(['1', '2', '3'])", "Evaluation Test Script", 1, null);
// CHECK NEW VALUE
stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, null);
Assert.assertArrayEquals(new String[] { "1", "2", "3" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
// same check as above but directly inside Rhino
testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 3) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"1\", \"2\", \"3\"][i] } ) ", "Evaluation Test Script", 1, null);
Assert.assertTrue(testResult.booleanValue());
} finally {
Context.exit();
}
}
use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.
the class ServoyApiObject method copyObject.
/**
* Can be used to deep copy a custom value.
*
* @sample
* var eventSourceCopy = servoyApi.copyObject(eventSource);
*
* @param value the value to be copied
* @return a copy of the value object, the same as constructing the object in javascript from scratch
*/
@JSFunction
public IdScriptableObject copyObject(Object value) {
if (value instanceof NativeObject) {
NativeObject nativeObject = new NativeObject();
Object[] ids = ((NativeObject) value).getIds();
for (Object id : ids) {
Object objectValue = ((NativeObject) value).get(id.toString(), (NativeObject) value);
if (objectValue instanceof RhinoMapOrArrayWrapper || objectValue instanceof NativeObject || objectValue instanceof NativeArray) {
objectValue = copyObject(objectValue);
}
nativeObject.put(id.toString(), nativeObject, objectValue);
}
return nativeObject;
}
if (value instanceof NativeArray) {
NativeArray arr = (NativeArray) value;
Object[] values = new Object[arr.size()];
for (int i = 0; i < arr.size(); i++) {
Object objectValue = arr.get(i);
if (objectValue instanceof RhinoMapOrArrayWrapper || objectValue instanceof NativeObject || objectValue instanceof NativeArray) {
objectValue = copyObject(objectValue);
}
values[i] = objectValue;
}
return new NativeArray(values);
}
if (value instanceof RhinoMapOrArrayWrapper) {
if (((RhinoMapOrArrayWrapper) value).getWrappedValue() instanceof Map) {
NativeObject nativeObject = new NativeObject();
Object[] ids = ((RhinoMapOrArrayWrapper) value).getIds();
for (Object id : ids) {
Object objectValue = ((RhinoMapOrArrayWrapper) value).get(id.toString(), (RhinoMapOrArrayWrapper) value);
if (objectValue instanceof RhinoMapOrArrayWrapper || objectValue instanceof NativeObject || objectValue instanceof NativeArray) {
objectValue = copyObject(objectValue);
}
nativeObject.put(id.toString(), nativeObject, objectValue);
}
return nativeObject;
} else {
Object[] ids = ((RhinoMapOrArrayWrapper) value).getIds();
Object[] values = new Object[ids.length];
for (int i = 0; i < ids.length; i++) {
Object objectValue = ((RhinoMapOrArrayWrapper) value).get(i, (RhinoMapOrArrayWrapper) value);
if (objectValue instanceof RhinoMapOrArrayWrapper || objectValue instanceof NativeObject || objectValue instanceof NativeArray) {
objectValue = copyObject(objectValue);
}
values[i] = objectValue;
}
NativeArray nativeArray = new NativeArray(values);
return nativeArray;
}
}
Debug.error("cannot return object: " + value + " as NativeObject");
return new NativeObject();
}
Aggregations