use of de.metas.ui.web.window.datatypes.DateRangeValue in project metasfresh-webui-api by metasfresh.
the class ADProcessParametersRepository method createProcessInfoParameter.
private static ProcessInfoParameter createProcessInfoParameter(final IDocumentFieldView field) {
final String parameterName = field.getFieldName();
final Object fieldValue = field.getValue();
final Object parameter;
final String info;
final Object parameterTo;
final String infoTo;
if (fieldValue instanceof LookupValue) {
final LookupValue lookupValue = (LookupValue) fieldValue;
parameter = lookupValue.getId();
info = lookupValue.getDisplayName();
parameterTo = null;
infoTo = null;
} else if (fieldValue instanceof DateRangeValue) {
final DateRangeValue dateRange = (DateRangeValue) fieldValue;
parameter = dateRange.getFrom();
info = parameter == null ? null : parameter.toString();
parameterTo = dateRange.getTo();
infoTo = parameterTo == null ? null : parameterTo.toString();
} else if (fieldValue instanceof Password) {
final Password password = Password.cast(fieldValue);
parameter = password.getAsString();
info = Password.OBFUSCATE_STRING;
parameterTo = null;
infoTo = null;
} else {
parameter = fieldValue;
info = fieldValue == null ? null : fieldValue.toString();
parameterTo = null;
infoTo = null;
}
return new ProcessInfoParameter(parameterName, parameter, parameterTo, info, infoTo);
}
use of de.metas.ui.web.window.datatypes.DateRangeValue 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