Search in sources :

Example 1 with WrappingContext

use of org.sablo.specification.property.WrappingContext 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 WrappingContext

use of org.sablo.specification.property.WrappingContext in project servoy-client by Servoy.

the class DatasetPropertyType method toJSON.

@Override
public JSONWriter toJSON(JSONWriter writer, String key, IDataSet value, PropertyDescription propertyDescription, DataConversion clientConversion, IBrowserConverterContext dataConverterContext) throws JSONException {
    JSONUtils.addKeyIfPresent(writer, key);
    if (value == null) {
        return writer.value(null);
    }
    writer.array();
    if (value.getColumnCount() > 0) {
        DatasetConfig datasetConfig = (DatasetConfig) propertyDescription.getConfig();
        String[] columnNames = value.getColumnNames();
        if (datasetConfig.isIncludeColumnNames() && columnNames != null) {
            writer.array();
            for (String columnName : columnNames) {
                writer.value(columnName);
            }
            writer.endArray();
        }
        for (int i = 0; i < value.getRowCount(); i++) {
            writer.array();
            Object[] row = value.getRow(i);
            PropertyDescription pd;
            for (int j = 0; j < row.length; j++) {
                pd = datasetConfig.getColumnType(columnNames[j]);
                if (pd != null) {
                    Object v;
                    if (pd.getType() instanceof IWrapperType<?, ?>) {
                        IWrappingContext c = (dataConverterContext instanceof IWrappingContext ? (IWrappingContext) dataConverterContext : new WrappingContext(dataConverterContext.getWebObject(), pd.getName()));
                        v = ((IWrapperType<Object, ?>) pd.getType()).wrap(row[j], null, pd, c);
                    } else {
                        v = row[j];
                    }
                    FullValueToJSONConverter.INSTANCE.toJSONValue(writer, null, v, pd, clientConversion, null);
                } else {
                    writer.value(row[j]);
                }
            }
            writer.endArray();
        }
    }
    writer.endArray();
    return writer;
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) IWrappingContext(org.sablo.specification.property.IWrappingContext) WrappingContext(org.sablo.specification.property.WrappingContext) IWrappingContext(org.sablo.specification.property.IWrappingContext) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IWrapperType(org.sablo.specification.property.IWrapperType)

Example 3 with WrappingContext

use of org.sablo.specification.property.WrappingContext 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)

Aggregations

ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)3 JSONObject (org.json.JSONObject)3 WrappingContext (org.sablo.specification.property.WrappingContext)3 RhinoMapOrArrayWrapper (com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper)2 ScriptableObject (org.mozilla.javascript.ScriptableObject)2 PropertyDescription (org.sablo.specification.PropertyDescription)2 IWrappingContext (org.sablo.specification.property.IWrappingContext)2 IChildWebObject (com.servoy.j2db.persistence.IChildWebObject)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NativeArray (org.mozilla.javascript.NativeArray)1 NativeJavaArray (org.mozilla.javascript.NativeJavaArray)1 NativeObject (org.mozilla.javascript.NativeObject)1 ChangeAwareList (org.sablo.specification.property.ChangeAwareList)1 ChangeAwareMap (org.sablo.specification.property.ChangeAwareMap)1 IWrapperType (org.sablo.specification.property.IWrapperType)1