Search in sources :

Example 1 with JSONStringWithConversions

use of org.sablo.websocket.utils.JSONUtils.JSONStringWithConversions in project servoy-client by Servoy.

the class DataproviderTypeSabloValue method getValueForToJSON.

protected Object getValueForToJSON(Object uiValue, IBrowserConverterContext dataConverterContext) throws JSONException {
    Object jsonValueRepresentation;
    boolean valuelistDisplayValue = false;
    int valuelistDisplayType = 0;
    if (shouldResolveFromValuelistWithName != null) {
        ValueListTypeSabloValue valuelistSabloValue = (ValueListTypeSabloValue) FoundsetLinkedTypeSabloValue.unwrapIfNeeded(webObjectContext.getProperty(shouldResolveFromValuelistWithName));
        if (valuelistSabloValue != null && valuelistSabloValue.getValueList() != null) {
            valuelistDisplayValue = true;
            valuelistDisplayType = valuelistSabloValue.getValueList().getValueList().getDisplayValueType();
            if (valuelistSabloValue.getValueList().realValueIndexOf(uiValue) != -1) {
                try {
                    // TODO don't we have to apply the UI converter's toObject here as well in the unlikely case of a valuelist + UI converter? and also
                    // when searching we should then use the fromObject(uiValue) rather then uiValue directly I think
                    uiValue = valuelistSabloValue.getValueList().getElementAt(valuelistSabloValue.getValueList().realValueIndexOf(uiValue));
                } catch (Exception ex) {
                    Debug.error(ex);
                }
            } else if (valuelistSabloValue.getValueList() instanceof DBValueList) {
                try {
                    LookupValueList lookup = new LookupValueList(valuelistSabloValue.getValueList().getValueList(), dataAdapterList.getApplication(), ComponentFactory.getFallbackValueList(dataAdapterList.getApplication(), null, Types.OTHER, null, valuelistSabloValue.getValueList().getValueList()), null, dataAdapterList.getRecord());
                    if (lookup.realValueIndexOf(uiValue) != -1) {
                        // TODO don't we have to apply the UI converter's toObject here as well in the unlikely case of a valuelist + UI converter? and also
                        // when searching we should then use the fromObject(uiValue) rather then uiValue directly I think
                        uiValue = lookup.getElementAt(lookup.realValueIndexOf(uiValue));
                    }
                    lookup.deregister();
                } catch (Exception ex) {
                    Debug.error(ex);
                }
            }
        }
    }
    if (findMode) {
        // in UI show only strings in find mode (just like SC/WC do); if they are something else like real dates/numbers which could happen
        // from scripting, then show string representation
        jsonValueRepresentation = uiValue instanceof String ? uiValue : (uiValue != null ? String.valueOf(uiValue) : "");
    } else if (typeOfDP != null && !valuelistDisplayValue) {
        Object value = uiValue;
        // merged this change from SC, DisplaysAdapter
        if (value == null && com.servoy.j2db.dataprocessing.DataAdapterList.isCountOrAvgOrSumAggregateDataProvider(dataProviderID, new FormAndTableDataProviderLookup(servoyDataConverterContext.getApplication().getFlattenedSolution(), servoyDataConverterContext.getForm().getForm(), dataAdapterList.getRecord() != null ? dataAdapterList.getRecord().getParentFoundSet().getTable() : null)))
            value = Integer.valueOf(0);
        // that 'true' is a workaround for allowing directly a value instead of object or array
        EmbeddableJSONWriter ejw = new EmbeddableJSONWriter(true);
        DataConversion jsonDataConversion = new DataConversion();
        FullValueToJSONConverter.INSTANCE.toJSONValue(ejw, null, value, typeOfDP, jsonDataConversion, dataConverterContext);
        if (jsonDataConversion.getConversions().size() == 0)
            jsonDataConversion = null;
        String str = ejw.toJSONString();
        if (str == null || str.trim().length() == 0) {
            Debug.error("A dataprovider that is not able to send itself to client... (" + typeOfDP + ", " + uiValue + ")");
            str = "null";
        }
        jsonValueRepresentation = new JSONStringWithConversions(str, jsonDataConversion);
    } else if (valuelistDisplayValue && (valuelistDisplayType == IColumnTypes.DATETIME || valuelistDisplayType == IColumnTypes.INTEGER || valuelistDisplayType == IColumnTypes.NUMBER || valuelistDisplayType == IColumnTypes.MEDIA) && !(uiValue instanceof String)) {
        // that 'true' is a workaround for allowing directly a value instead of object or array
        EmbeddableJSONWriter ejw = new EmbeddableJSONWriter(true);
        DataConversion jsonDataConversion = new DataConversion();
        if (valuelistDisplayType == IColumnTypes.DATETIME) {
            NGDatePropertyType.NG_INSTANCE.toJSON(ejw, null, (Date) uiValue, null, jsonDataConversion, dataConverterContext);
        } else if (valuelistDisplayType == IColumnTypes.INTEGER) {
            IntPropertyType.INSTANCE.toJSON(ejw, null, (Integer) uiValue, null, jsonDataConversion, dataConverterContext);
        } else if (valuelistDisplayType == IColumnTypes.NUMBER) {
            DoublePropertyType.INSTANCE.toJSON(ejw, null, (Number) uiValue, null, jsonDataConversion, dataConverterContext);
        } else if (valuelistDisplayType == IColumnTypes.MEDIA) {
            MediaDataproviderPropertyType.INSTANCE.toJSON(ejw, null, uiValue, null, jsonDataConversion, dataConverterContext);
        }
        if (jsonDataConversion.getConversions().size() == 0)
            jsonDataConversion = null;
        String str = ejw.toJSONString();
        if (str == null || str.trim().length() == 0) {
            Debug.error("A dataprovider with resolveValuelist that is not able to send itself to client... (" + valuelistDisplayType + ", " + uiValue + ")");
            str = "null";
        }
        jsonValueRepresentation = new JSONStringWithConversions(str, jsonDataConversion);
    } else {
        jsonValueRepresentation = uiValue;
    }
    return jsonValueRepresentation;
}
Also used : DBValueList(com.servoy.j2db.dataprocessing.DBValueList) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) IJSONStringWithConversions(org.sablo.websocket.utils.JSONUtils.IJSONStringWithConversions) JSONStringWithConversions(org.sablo.websocket.utils.JSONUtils.JSONStringWithConversions) DataConversion(org.sablo.websocket.utils.DataConversion) EmbeddableJSONWriter(org.sablo.websocket.utils.JSONUtils.EmbeddableJSONWriter) FormAndTableDataProviderLookup(com.servoy.j2db.FormAndTableDataProviderLookup) LookupValueList(com.servoy.j2db.dataprocessing.LookupValueList)

Aggregations

FormAndTableDataProviderLookup (com.servoy.j2db.FormAndTableDataProviderLookup)1 DBValueList (com.servoy.j2db.dataprocessing.DBValueList)1 LookupValueList (com.servoy.j2db.dataprocessing.LookupValueList)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ParseException (java.text.ParseException)1 JSONException (org.json.JSONException)1 DataConversion (org.sablo.websocket.utils.DataConversion)1 EmbeddableJSONWriter (org.sablo.websocket.utils.JSONUtils.EmbeddableJSONWriter)1 IJSONStringWithConversions (org.sablo.websocket.utils.JSONUtils.IJSONStringWithConversions)1 JSONStringWithConversions (org.sablo.websocket.utils.JSONUtils.JSONStringWithConversions)1