Search in sources :

Example 1 with DBValueList

use of com.servoy.j2db.dataprocessing.DBValueList in project servoy-client by Servoy.

the class ValueListTypeSabloValue method getDisplayValue.

/**
 * @param newJSONValue
 */
private void getDisplayValue(JSONObject newJSONValue) {
    this.handledIDForResponse = Long.valueOf(newJSONValue.getLong(ID_KEY));
    Object realValue = newJSONValue.opt(DISPLAYVALUE);
    displayValue = realValue;
    int realValueIndex = valueList.realValueIndexOf(realValue);
    if (realValueIndex != -1) {
        try {
            displayValue = valueList.getElementAt(realValueIndex);
        } catch (Exception ex) {
            Debug.error(ex);
        }
    } else {
        if (valueList instanceof DBValueList) {
            try {
                LookupValueList lookup = new LookupValueList(valueList.getValueList(), dataAdapterListToUse.getApplication(), ComponentFactory.getFallbackValueList(dataAdapterListToUse.getApplication(), dataproviderID, format != null ? format.uiType : 0, format != null ? format.parsedFormat : null, valueList.getValueList()), format != null && format.parsedFormat != null ? format.parsedFormat.getDisplayFormat() : null, dataAdapterListToUse.getRecord());
                if (lookup.realValueIndexOf(realValue) != -1) {
                    displayValue = lookup.getElementAt(lookup.realValueIndexOf(realValue));
                }
                lookup.deregister();
            } catch (Exception e) {
                Debug.error(e);
            }
        }
    }
    changeMonitor.markFullyChanged(true);
}
Also used : DBValueList(com.servoy.j2db.dataprocessing.DBValueList) JSONObject(org.json.JSONObject) LookupValueList(com.servoy.j2db.dataprocessing.LookupValueList) JSONException(org.json.JSONException) ServoyException(com.servoy.j2db.util.ServoyException)

Example 2 with DBValueList

use of com.servoy.j2db.dataprocessing.DBValueList 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

DBValueList (com.servoy.j2db.dataprocessing.DBValueList)2 LookupValueList (com.servoy.j2db.dataprocessing.LookupValueList)2 JSONException (org.json.JSONException)2 FormAndTableDataProviderLookup (com.servoy.j2db.FormAndTableDataProviderLookup)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ServoyException (com.servoy.j2db.util.ServoyException)1 ParseException (java.text.ParseException)1 JSONObject (org.json.JSONObject)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