Search in sources :

Example 1 with RhinoMapOrArrayWrapper

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;
}
Also used : IWrappingContext(org.sablo.specification.property.IWrappingContext) WrappingContext(org.sablo.specification.property.WrappingContext) HashMap(java.util.HashMap) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) NativeObject(org.mozilla.javascript.NativeObject) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) NativeObject(org.mozilla.javascript.NativeObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IChildWebObject(com.servoy.j2db.persistence.IChildWebObject) Map(java.util.Map) HashMap(java.util.HashMap) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap)

Example 2 with RhinoMapOrArrayWrapper

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();
}
Also used : Context(org.mozilla.javascript.Context) PropertyDescription(org.sablo.specification.PropertyDescription) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) PropertyDescriptionBuilder(org.sablo.specification.PropertyDescriptionBuilder) NGCustomJSONObjectType(com.servoy.j2db.server.ngclient.property.types.NGCustomJSONObjectType) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) ScriptableObject(org.mozilla.javascript.ScriptableObject) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) TopLevel(org.mozilla.javascript.TopLevel) Before(org.junit.Before)

Example 3 with RhinoMapOrArrayWrapper

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) FormScope(com.servoy.j2db.scripting.FormScope) Test(org.junit.Test)

Example 4 with RhinoMapOrArrayWrapper

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) FormScope(com.servoy.j2db.scripting.FormScope) Test(org.junit.Test)

Example 5 with RhinoMapOrArrayWrapper

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();
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) NativeArray(org.mozilla.javascript.NativeArray) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IdScriptableObject(org.mozilla.javascript.IdScriptableObject) NativeObject(org.mozilla.javascript.NativeObject) Map(java.util.Map) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

RhinoMapOrArrayWrapper (com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper)7 Context (org.mozilla.javascript.Context)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 FormScope (com.servoy.j2db.scripting.FormScope)2 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)2 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)2 Map (java.util.Map)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 NativeArray (org.mozilla.javascript.NativeArray)2 NativeObject (org.mozilla.javascript.NativeObject)2 PropertyDescription (org.sablo.specification.PropertyDescription)2 ChangeAwareMap (org.sablo.specification.property.ChangeAwareMap)2 WrappingContext (org.sablo.specification.property.WrappingContext)2 IChildWebObject (com.servoy.j2db.persistence.IChildWebObject)1 NGCustomJSONObjectType (com.servoy.j2db.server.ngclient.property.types.NGCustomJSONObjectType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1 IdScriptableObject (org.mozilla.javascript.IdScriptableObject)1