use of de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue 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);
}
use of de.metas.ui.web.window.datatypes.LookupValue.StringLookupValue 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);
}
Aggregations