Search in sources :

Example 6 with RhinoMapOrArrayWrapper

use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.

the class NGCustomJSONArrayType method toSabloComponentValue.

@Override
public Object toSabloComponentValue(final Object rhinoValue, Object previousComponentValue, PropertyDescription pd, final IWebObjectContext componentOrService) {
    if (rhinoValue == null || RhinoConversion.isUndefinedOrNotFound(rhinoValue))
        return null;
    final ChangeAwareList<SabloT, SabloWT> previousSpecialArray = (ChangeAwareList<SabloT, SabloWT>) previousComponentValue;
    if (rhinoValue instanceof RhinoMapOrArrayWrapper) {
        return ((RhinoMapOrArrayWrapper) rhinoValue).getWrappedValue();
    } else {
        // if it's some kind of array
        // we always make a new copy to simplify code; so previous Rhino reference in js code should no longer be used after this conversion
        List<SabloT> rhinoArrayCopy = null;
        PropertyDescription elementPD = getCustomJSONTypeDefinition();
        if (rhinoValue instanceof NativeArray) {
            rhinoArrayCopy = new ArrayList<SabloT>();
            NativeArray nativeArray = (NativeArray) rhinoValue;
            for (Object element : nativeArray) {
                rhinoArrayCopy.add((SabloT) NGConversions.INSTANCE.convertRhinoToSabloComponentValue(element, null, elementPD, componentOrService));
            }
        } else if (rhinoValue instanceof NativeJavaArray) {
            rhinoArrayCopy = new ArrayList<SabloT>();
            NativeJavaArray nativeJavaArray = (NativeJavaArray) rhinoValue;
            int length = ((Integer) nativeJavaArray.get("length", nativeJavaArray)).intValue();
            for (int i = 0; i < length; i++) {
                rhinoArrayCopy.add((SabloT) NGConversions.INSTANCE.convertRhinoToSabloComponentValue(nativeJavaArray.get(i, nativeJavaArray), null, elementPD, componentOrService));
            }
        } else
            Debug.warn(// $NON-NLS-1$
            "Cannot convert value assigned from solution scripting into array property type; class: " + rhinoValue.getClass() + ", new value = " + rhinoValue + // $NON-NLS-1$ //$NON-NLS-2$
            "; property = " + pd.getName() + "; component name = " + // $NON-NLS-1$
            componentOrService.getUnderlyingWebObject().getName());
        if (rhinoArrayCopy != null) {
            return wrap(rhinoArrayCopy, previousSpecialArray, pd, new WrappingContext(componentOrService.getUnderlyingWebObject(), pd.getName()));
        }
    }
    // or should we return null or throw exception here? incompatible thing was assigned
    return previousComponentValue;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) WrappingContext(org.sablo.specification.property.WrappingContext) ArrayList(java.util.ArrayList) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) ChangeAwareList(org.sablo.specification.property.ChangeAwareList) PropertyDescription(org.sablo.specification.PropertyDescription) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 7 with RhinoMapOrArrayWrapper

use of com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper in project servoy-client by Servoy.

the class NGCustomJSONObjectType method toRhinoValue.

@Override
public Object toRhinoValue(Map<String, SabloT> webComponentValue, PropertyDescription pd, IWebObjectContext componentOrService, Scriptable startScriptable) {
    if (webComponentValue != null) {
        CustomObjectContext<SabloT, SabloWT> ext = ((ChangeAwareMap<SabloT, SabloWT>) webComponentValue).getOrCreateComponentOrServiceExtension();
        RhinoMapOrArrayWrapper rhinoValue = new RhinoMapOrArrayWrapper(webComponentValue, ext, pd, startScriptable);
        return rhinoValue;
    }
    return null;
}
Also used : RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) ChangeAwareMap(org.sablo.specification.property.ChangeAwareMap)

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