Search in sources :

Example 16 with UserSessionSource

use of com.haulmont.cuba.core.global.UserSessionSource in project cuba by cuba-platform.

the class PropertyWrapper method valueOf.

protected Object valueOf(Object newValue) throws Converter.ConversionException {
    if (newValue == null) {
        return null;
    }
    final Range range = propertyPath.getRange();
    if (range == null) {
        return newValue;
    } else {
        final Object obj;
        if (range.isDatatype()) {
            Datatype<Object> datatype = range.asDatatype();
            if (newValue instanceof String) {
                try {
                    newValue = Strings.emptyToNull((String) newValue);
                    UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
                    obj = datatype.parse((String) newValue, sessionSource.getLocale());
                } catch (ParseException e) {
                    throw new Converter.ConversionException(e);
                }
            } else {
                if (newValue.getClass().equals(datatype.getJavaClass())) {
                    return newValue;
                } else {
                    Datatype newValueDatatype = Datatypes.getNN(newValue.getClass());
                    // noinspection unchecked
                    String str = newValueDatatype.format(newValue);
                    try {
                        obj = datatype.parse(str);
                    } catch (ParseException e) {
                        throw new Converter.ConversionException(e);
                    }
                }
            }
        } else {
            obj = newValue;
        }
        return obj;
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) Converter(com.vaadin.data.util.converter.Converter) ParseException(java.text.ParseException) Range(com.haulmont.chile.core.model.Range) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Aggregations

UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)16 Datatype (com.haulmont.chile.core.datatypes.Datatype)5 ParseException (java.text.ParseException)5 ValidationException (com.haulmont.cuba.gui.components.ValidationException)4 Date (java.util.Date)3 Instance (com.haulmont.chile.core.model.Instance)2 Range (com.haulmont.chile.core.model.Range)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 BigDecimal (java.math.BigDecimal)2 MetaProperty (com.haulmont.chile.core.annotations.MetaProperty)1 TimeZoneAwareDatatype (com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Executions (com.haulmont.cuba.core.app.execution.Executions)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Messages (com.haulmont.cuba.core.global.Messages)1 TimeSource (com.haulmont.cuba.core.global.TimeSource)1 TreeModelAdapter (com.haulmont.cuba.desktop.gui.data.TreeModelAdapter)1 AbstractAction (com.haulmont.cuba.gui.components.AbstractAction)1 Action (com.haulmont.cuba.gui.components.Action)1