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;
}
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;
}
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;
}
Aggregations