Search in sources :

Example 1 with UserSessionSource

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

the class FilterConditionUtils method formatParamValue.

public static String formatParamValue(Param param, Object value) {
    // noinspection unchecked
    Datatype datatype = Datatypes.get(param.getJavaClass());
    MetaProperty property = param.getProperty();
    if (property != null) {
        TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
        if (tt == TemporalType.DATE) {
            datatype = Datatypes.getNN(java.sql.Date.class);
        }
    }
    if (datatype != null) {
        UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.class);
        return datatype.format(value, userSessionSource.getLocale());
    }
    return value.toString();
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Datatype(com.haulmont.chile.core.datatypes.Datatype) TemporalType(javax.persistence.TemporalType)

Example 2 with UserSessionSource

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

the class DoubleValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Double> datatype = Datatypes.getNN(Double.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            Double num = datatype.parse((String) value, sessionSource.getLocale());
            result = checkDoubleOnPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = (value instanceof Double && checkDoubleOnPositive((Double) value)) || (value instanceof BigDecimal && checkBigDecimalOnPositive((BigDecimal) value));
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException) BigDecimal(java.math.BigDecimal)

Example 3 with UserSessionSource

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

the class IntegerValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Integer> datatype = Datatypes.getNN(Integer.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            Integer num = datatype.parse((String) value, sessionSource.getLocale());
            result = checkIntegerOnPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Integer && checkIntegerOnPositive((Integer) value);
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException)

Example 4 with UserSessionSource

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

the class LongValidator method validate.

@Override
public void validate(Object value) throws ValidationException {
    boolean result;
    if (value instanceof String) {
        try {
            Datatype<Long> datatype = Datatypes.getNN(Long.class);
            UserSessionSource sessionSource = AppBeans.get(UserSessionSource.NAME);
            Long num = datatype.parse((String) value, sessionSource.getLocale());
            result = checkPositive(num);
        } catch (ParseException e) {
            result = false;
        }
    } else {
        result = value instanceof Long && checkPositive((Long) value);
    }
    if (!result) {
        String msg = message != null ? messages.getTools().loadString(messagesPack, message) : "Invalid value '%s'";
        throw new ValidationException(String.format(msg, value));
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) ValidationException(com.haulmont.cuba.gui.components.ValidationException) ParseException(java.text.ParseException)

Example 5 with UserSessionSource

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

the class AggregatableDelegate method doAggregation.

protected Map<AggregationInfo, String> doAggregation(Collection<K> itemIds, AggregationInfo[] aggregationInfos) {
    final Map<AggregationInfo, String> aggregationResults = new HashMap<>();
    for (AggregationInfo aggregationInfo : aggregationInfos) {
        final Object value = doPropertyAggregation(aggregationInfo, itemIds);
        String formattedValue;
        if (aggregationInfo.getFormatter() != null) {
            // noinspection unchecked
            formattedValue = aggregationInfo.getFormatter().format(value);
        } else {
            MetaPropertyPath propertyPath = aggregationInfo.getPropertyPath();
            final Range range = propertyPath.getRange();
            if (range.isDatatype()) {
                if (aggregationInfo.getType() != AggregationInfo.Type.COUNT) {
                    Class resultClass;
                    if (aggregationInfo.getStrategy() == null) {
                        Class rangeJavaClass = propertyPath.getRangeJavaClass();
                        Aggregation aggregation = Aggregations.get(rangeJavaClass);
                        resultClass = aggregation.getResultClass();
                    } else {
                        resultClass = aggregationInfo.getStrategy().getResultClass();
                    }
                    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                    Locale locale = userSessionSource.getLocale();
                    formattedValue = Datatypes.getNN(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            } else {
                if (aggregationInfo.getStrategy() != null) {
                    Class resultClass = aggregationInfo.getStrategy().getResultClass();
                    UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
                    Locale locale = userSessionSource.getLocale();
                    formattedValue = Datatypes.getNN(resultClass).format(value, locale);
                } else {
                    formattedValue = value.toString();
                }
            }
        }
        aggregationResults.put(aggregationInfo, formattedValue);
    }
    return aggregationResults;
}
Also used : Aggregation(com.haulmont.cuba.gui.data.aggregation.Aggregation) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) AggregationInfo(com.haulmont.cuba.gui.components.AggregationInfo) Range(com.haulmont.chile.core.model.Range)

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