Search in sources :

Example 21 with LookupValue

use of de.metas.ui.web.window.datatypes.LookupValue in project metasfresh-webui-api by metasfresh.

the class HuTraceQueryCreator method updateVhuSourceIdFromParameter.

private static HUTraceEventQuery updateVhuSourceIdFromParameter(@NonNull final HUTraceEventQuery query, @NonNull final DocumentFilterParam parameter) {
    errorIfQueryValueGreaterThanZero("VhuSourceId", query.getVhuSourceId(), query);
    final LookupValue value = (LookupValue) parameter.getValue();
    return query.withVhuSourceId(value.getIdAsInt());
}
Also used : LookupValue(de.metas.ui.web.window.datatypes.LookupValue) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue)

Example 22 with LookupValue

use of de.metas.ui.web.window.datatypes.LookupValue in project metasfresh-webui-api by metasfresh.

the class HuTraceQueryCreator method updateVhuStatusFromParameter.

private static HUTraceEventQuery updateVhuStatusFromParameter(@NonNull final HUTraceEventQuery query, @NonNull final DocumentFilterParam parameter) {
    errorIfQueryValueNotEmpty("VhuStatus", query.getVhuStatus(), query);
    final LookupValue value = (LookupValue) parameter.getValue();
    return query.withVhuStatus(value.getIdAsString());
}
Also used : LookupValue(de.metas.ui.web.window.datatypes.LookupValue) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue)

Example 23 with LookupValue

use of de.metas.ui.web.window.datatypes.LookupValue in project metasfresh-webui-api by metasfresh.

the class SqlDocumentsRepository method convertValueToPO.

public static Object convertValueToPO(final Object value, final String columnName, final DocumentFieldWidgetType widgetType, final Class<?> targetClass) {
    final Class<?> valueClass = JSONNullValue.isNull(value) ? null : value.getClass();
    if (valueClass != null && targetClass.isAssignableFrom(valueClass)) {
        return value;
    } else if (int.class.equals(targetClass) || Integer.class.equals(targetClass)) {
        if (JSONNullValue.isNull(value)) {
            return null;
        } else if (LookupValue.class.isAssignableFrom(valueClass)) {
            return ((LookupValue) value).getIdAsInt();
        } else if (Number.class.isAssignableFrom(valueClass)) {
            return ((Number) value).intValue();
        } else if (String.class.equals(valueClass)) {
            return Integer.parseInt((String) value);
        } else if (Map.class.isAssignableFrom(valueClass)) {
            @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
            final IntegerLookupValue lookupValue = JSONLookupValue.integerLookupValueFromJsonMap(map);
            return lookupValue == null ? null : lookupValue.getIdAsInt();
        }
    } else if (String.class.equals(targetClass)) {
        if (JSONNullValue.isNull(value)) {
            return null;
        } else if (LookupValue.class.isAssignableFrom(valueClass)) {
            return ((LookupValue) value).getIdAsString();
        } else if (Map.class.isAssignableFrom(valueClass)) {
            @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
            final StringLookupValue lookupValue = JSONLookupValue.stringLookupValueFromJsonMap(map);
            return lookupValue == null ? null : lookupValue.getIdAsString();
        } else if (Password.class.isAssignableFrom(valueClass)) {
            final Password password = (Password) value;
            return password.getAsString();
        }
    } else if (Timestamp.class.equals(targetClass)) {
        if (JSONNullValue.isNull(value)) {
            return null;
        } else if (java.util.Date.class.isAssignableFrom(valueClass)) {
            return new Timestamp(((java.util.Date) value).getTime());
        } else if (value instanceof String) {
            final java.util.Date valueDate = JSONDate.fromJson(value.toString(), widgetType);
            return TimeUtil.asTimestamp(valueDate);
        }
    } else if (Boolean.class.equals(targetClass) || boolean.class.equals(targetClass)) {
        if (JSONNullValue.isNull(value)) {
            return false;
        } else if (String.class.equals(valueClass)) {
            return DisplayType.toBoolean(value);
        } else if (StringLookupValue.class.isAssignableFrom(valueClass)) {
            // Corner case: e.g. Posted column which is a List but the PO is handling it as boolean
            final StringLookupValue stringLookupValue = (StringLookupValue) value;
            final Boolean valueBoolean = DisplayType.toBoolean(stringLookupValue.getIdAsString(), null);
            if (valueBoolean != null) {
                return valueBoolean;
            }
        }
    }
    // Better return the original value and let the PO fail.
    return value;
// throw new AdempiereException("Cannot convert value '" + value + "' from " + valueClass + " to " + targetClass
// + "\n ColumnName: " + columnName
// + "\n PO: " + po);
}
Also used : StringLookupValue(de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) Timestamp(java.sql.Timestamp) JSONDate(de.metas.ui.web.window.datatypes.json.JSONDate) Map(java.util.Map) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) StringLookupValue(de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue) JSONLookupValue(de.metas.ui.web.window.datatypes.json.JSONLookupValue) LookupValue(de.metas.ui.web.window.datatypes.LookupValue) Password(de.metas.ui.web.window.datatypes.Password)

Example 24 with LookupValue

use of de.metas.ui.web.window.datatypes.LookupValue in project metasfresh-webui-api by metasfresh.

the class FullyCachedLookupDataSource method findEntities.

@Override
public LookupValuesList findEntities(final Evaluatee ctx, final String filter, final int firstRow, final int pageLength) {
    final LookupValuesList partition = getLookupValuesList(ctx);
    if (partition.isEmpty()) {
        return partition;
    }
    final Predicate<LookupValue> filterPredicate = LookupValueFilterPredicates.of(filter);
    if (filterPredicate == LookupValueFilterPredicates.MATCH_ALL) {
        return partition.offsetAndLimit(firstRow, pageLength);
    }
    return partition.filter(filterPredicate, firstRow, pageLength);
}
Also used : LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) LookupValue(de.metas.ui.web.window.datatypes.LookupValue)

Example 25 with LookupValue

use of de.metas.ui.web.window.datatypes.LookupValue in project metasfresh-webui-api by metasfresh.

the class DocumentFieldDescriptor method convertToValueClass.

/**
 * Converts given value to target class.
 *
 * @param fieldName field name, needed only for logging purposes
 * @param value value to be converted
 * @param widgetType widget type (optional)
 * @param targetType target type
 * @param lookupDataSource optional Lookup data source, if needed
 * @return converted value
 */
public static <// 
T> // 
T convertToValueClass(// 
final String fieldName, // 
final Object value, // 
final DocumentFieldWidgetType widgetType, // 
final Class<T> targetType, // 
final LookupValueByIdSupplier lookupDataSource) {
    if (value == null) {
        return null;
    }
    final Class<?> fromType = value.getClass();
    try {
        // Corner case: we need to convert Timestamp(which extends Date) to strict Date because else all value changed comparing methods will fail
        if (java.util.Date.class.equals(targetType) && Timestamp.class.equals(fromType)) {
            @SuppressWarnings("unchecked") final T valueConv = (T) JSONDate.fromTimestamp((Timestamp) value);
            return valueConv;
        }
        if (targetType.isAssignableFrom(fromType)) {
            if (!targetType.equals(fromType)) {
                logger.warn("Possible optimization issue: target type is assignable from source type, but they are not the same class." + "\n In future we will disallow this case, so please check and fix it." + "\n Field name: " + fieldName + "\n Target type: " + targetType + "\n Source type: " + fromType + "\n Value: " + value + "\n LookupDataSource: " + lookupDataSource);
            }
            @SuppressWarnings("unchecked") final T valueConv = (T) value;
            return valueConv;
        }
        if (String.class == targetType) {
            if (Map.class.isAssignableFrom(fromType)) {
            // this is not allowed for consistency. let it fail.
            } else // For any other case, blindly convert it to string
            {
                @SuppressWarnings("unchecked") final T valueConv = (T) value.toString();
                return valueConv;
            }
        } else if (java.util.Date.class == targetType) {
            if (value instanceof String) {
                @SuppressWarnings("unchecked") final T valueConv = (T) JSONDate.fromJson((String) value, widgetType);
                return valueConv;
            }
        } else if (Integer.class == targetType || int.class == targetType) {
            if (value instanceof String) {
                final String valueStr = (String) value;
                if (valueStr.isEmpty()) {
                    return null;
                }
                final BigDecimal valueBD = new BigDecimal(valueStr);
                @SuppressWarnings("unchecked") final T valueConv = (T) (Integer) valueBD.intValueExact();
                return valueConv;
            } else if (value instanceof Number) {
                @SuppressWarnings("unchecked") final T valueConv = (T) (Integer) ((Number) value).intValue();
                return valueConv;
            } else if (value instanceof LookupValue) {
                @SuppressWarnings("unchecked") final T valueConv = (T) (Integer) ((LookupValue) value).getIdAsInt();
                return valueConv;
            } else if (value instanceof Map) {
                @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
                final IntegerLookupValue lookupValue = JSONLookupValue.integerLookupValueFromJsonMap(map);
                @SuppressWarnings("unchecked") final T valueConv = (T) (Integer) lookupValue.getIdAsInt();
                return valueConv;
            }
        } else if (BigDecimal.class == targetType) {
            if (String.class == fromType) {
                final String valueStr = (String) value;
                @SuppressWarnings("unchecked") final T valueConv = (T) (valueStr.isEmpty() ? BigDecimal.ZERO : new BigDecimal(valueStr));
                return valueConv;
            } else if (Integer.class == fromType || int.class == fromType) {
                final int valueInt = (int) value;
                @SuppressWarnings("unchecked") final T valueConv = (T) BigDecimal.valueOf(valueInt);
                return valueConv;
            }
        } else if (Boolean.class == targetType) {
            final Object valueToConv;
            if (value instanceof StringLookupValue) {
                // If String lookup value then consider only the Key.
                // usage example 1: the Posted column which can be Y, N and some other error codes.
                // In this case we want to convert the "Y" to "true".
                // usage example 2: some column which is List and the reference is "_YesNo".
                valueToConv = ((StringLookupValue) value).getIdAsString();
            } else {
                valueToConv = value;
            }
            @SuppressWarnings("unchecked") final T valueConv = (T) DisplayType.toBoolean(valueToConv, Boolean.FALSE);
            return valueConv;
        } else if (IntegerLookupValue.class == targetType) {
            if (Map.class.isAssignableFrom(fromType)) {
                @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
                final IntegerLookupValue lookupValue = JSONLookupValue.integerLookupValueFromJsonMap(map);
                if (Check.isEmpty(lookupValue.getDisplayName(), true) && lookupDataSource != null) {
                    // corner case: the frontend sent a lookup value like '{ 1234567 : "" }'
                    // => we need to resolve the name against the lookup
                    // see https://github.com/metasfresh/metasfresh-webui/issues/230
                    final LookupValue lookupValueResolved = lookupDataSource.findById(lookupValue.getId());
                    return convertToValueClass(fieldName, lookupValueResolved, widgetType, targetType, /* lookupDataSource */
                    null);
                } else {
                    @SuppressWarnings("unchecked") final T valueConv = (T) lookupValue;
                    return valueConv;
                }
            } else if (Number.class.isAssignableFrom(fromType)) {
                final int valueInt = ((Number) value).intValue();
                if (lookupDataSource != null) {
                    final LookupValue valueLookup = lookupDataSource.findById(valueInt);
                    final T valueConv = convertToValueClass(fieldName, valueLookup, widgetType, targetType, /* lookupDataSource */
                    null);
                    // TODO: what if valueConv was not found?
                    return valueConv;
                }
            } else if (String.class == fromType) {
                final String valueStr = (String) value;
                if (valueStr.isEmpty()) {
                    return null;
                }
                if (lookupDataSource != null) {
                    final LookupValue valueLookup = lookupDataSource.findById(valueStr);
                    final T valueConv = convertToValueClass(fieldName, valueLookup, widgetType, targetType, /* lookupDataSource */
                    null);
                    // TODO: what if valueConv was not found?
                    return valueConv;
                }
            } else if (StringLookupValue.class == fromType) {
                // TODO: implement https://github.com/metasfresh/metasfresh-webui-api/issues/417
                final StringLookupValue stringLookupValue = (StringLookupValue) value;
                @SuppressWarnings("unchecked") final T valueConv = (T) IntegerLookupValue.of(stringLookupValue);
                return valueConv;
            }
        } else if (StringLookupValue.class == targetType) {
            if (Map.class.isAssignableFrom(fromType)) {
                @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
                final StringLookupValue lookupValue = JSONLookupValue.stringLookupValueFromJsonMap(map);
                if (Check.isEmpty(lookupValue.getDisplayName(), true) && lookupDataSource != null) {
                    // corner case: the frontend sent a lookup value like '{ "someKey" : "" }'
                    // => we need to resolve the name against the lookup
                    // see https://github.com/metasfresh/metasfresh-webui/issues/230
                    final LookupValue lookupValueResolved = lookupDataSource.findById(lookupValue.getId());
                    return convertToValueClass(fieldName, lookupValueResolved, widgetType, targetType, /* lookupDataSource */
                    null);
                } else {
                    @SuppressWarnings("unchecked") final T valueConv = (T) lookupValue;
                    return valueConv;
                }
            } else if (String.class == fromType) {
                final String valueStr = (String) value;
                if (valueStr.isEmpty()) {
                    return null;
                }
                if (lookupDataSource != null) {
                    final LookupValue valueLookup = lookupDataSource.findById(valueStr);
                    final T valueConv = convertToValueClass(fieldName, valueLookup, widgetType, targetType, /* lookupDataSource */
                    null);
                    // TODO: what if valueConv was not found?
                    return valueConv;
                }
            } else if (IntegerLookupValue.class == fromType) {
                final IntegerLookupValue lookupValueInt = (IntegerLookupValue) value;
                @SuppressWarnings("unchecked") final T valueConv = (T) StringLookupValue.of(lookupValueInt.getIdAsString(), lookupValueInt.getDisplayName());
                return valueConv;
            }
        } else if (LookupValuesList.class == targetType) {
            if (Map.class.isAssignableFrom(fromType)) {
                @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) value;
                @SuppressWarnings("unchecked") final T valueConv = (T) JSONLookupValuesList.lookupValuesListFromJsonMap(map);
                return valueConv;
            }
        } else if (DateRangeValue.class == targetType) {
            if (Map.class.isAssignableFrom(fromType)) {
                @SuppressWarnings("unchecked") final Map<String, String> map = (Map<String, String>) value;
                final DateRangeValue dateRange = JSONRange.dateRangeFromJSONMap(map);
                @SuppressWarnings("unchecked") final T valueConv = (T) dateRange;
                return valueConv;
            }
        } else if (Password.class == targetType) {
            final Password password = Password.ofNullableString(value.toString());
            @SuppressWarnings("unchecked") final T valueConv = (T) password;
            return valueConv;
        }
    } catch (final Exception e) {
        throw new AdempiereException("Failed converting " + fieldName + "'s value '" + value + "' (" + fromType + ") to " + targetType + "\n LookupDataSource: " + // 
        lookupDataSource + "\n Widget type: " + widgetType + "\n Reason: " + e.getLocalizedMessage(), e);
    }
    // consider empty strings as null objects
    if (value instanceof String || value.toString().isEmpty()) {
        return null;
    }
    throw new AdempiereException("Cannot convert " + fieldName + "'s value '" + value + "' (" + fromType + ") to " + targetType + "\n LookupDataSource: " + // 
    lookupDataSource);
}
Also used : DateRangeValue(de.metas.ui.web.window.datatypes.DateRangeValue) StringLookupValue(de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue) ITranslatableString(de.metas.i18n.ITranslatableString) ImmutableTranslatableString(de.metas.i18n.ImmutableTranslatableString) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) Timestamp(java.sql.Timestamp) JSONDate(de.metas.ui.web.window.datatypes.json.JSONDate) BigDecimal(java.math.BigDecimal) AdempiereException(org.adempiere.exceptions.AdempiereException) AdempiereException(org.adempiere.exceptions.AdempiereException) LookupValuesList(de.metas.ui.web.window.datatypes.LookupValuesList) JSONLookupValuesList(de.metas.ui.web.window.datatypes.json.JSONLookupValuesList) Map(java.util.Map) IntegerLookupValue(de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue) JSONLookupValue(de.metas.ui.web.window.datatypes.json.JSONLookupValue) LookupValue(de.metas.ui.web.window.datatypes.LookupValue) StringLookupValue(de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue) Password(de.metas.ui.web.window.datatypes.Password)

Aggregations

LookupValue (de.metas.ui.web.window.datatypes.LookupValue)31 IntegerLookupValue (de.metas.ui.web.window.datatypes.LookupValue.IntegerLookupValue)25 LookupValuesList (de.metas.ui.web.window.datatypes.LookupValuesList)7 JSONLookupValue (de.metas.ui.web.window.datatypes.json.JSONLookupValue)6 ITranslatableString (de.metas.i18n.ITranslatableString)5 AdempiereException (org.adempiere.exceptions.AdempiereException)4 Map (java.util.Map)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 IHUOrderBL (de.metas.handlingunits.order.api.IHUOrderBL)2 NumberTranslatableString (de.metas.i18n.NumberTranslatableString)2 BoilerPlateContext (de.metas.letters.model.MADBoilerPlate.BoilerPlateContext)2 ProcessInfoParameter (de.metas.process.ProcessInfoParameter)2 QuickInput (de.metas.ui.web.quickinput.QuickInput)2 DocumentPath (de.metas.ui.web.window.datatypes.DocumentPath)2 StringLookupValue (de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue)2 Password (de.metas.ui.web.window.datatypes.Password)2 JSONDate (de.metas.ui.web.window.datatypes.json.JSONDate)2 JSONDocumentPath (de.metas.ui.web.window.datatypes.json.JSONDocumentPath)2 JSONLookupValuesList (de.metas.ui.web.window.datatypes.json.JSONLookupValuesList)2