Search in sources :

Example 1 with TemporalPropertyValueConverter

use of org.springframework.data.elasticsearch.core.convert.TemporalPropertyValueConverter in project spring-data-elasticsearch by spring-projects.

the class SimpleElasticsearchPersistentProperty method initPropertyValueConverter.

/**
 * Initializes the property converter for this {@link PersistentProperty}, if any.
 */
private void initPropertyValueConverter() {
    initPropertyValueConverterFromAnnotation();
    if (hasPropertyValueConverter()) {
        return;
    }
    Class<?> actualType = getActualTypeOrNull();
    if (actualType == null) {
        return;
    }
    Field field = findAnnotation(Field.class);
    if (field == null) {
        return;
    }
    switch(field.type()) {
        case Date:
        case Date_Nanos:
            {
                List<ElasticsearchDateConverter> dateConverters = getDateConverters(field, actualType);
                if (dateConverters.isEmpty()) {
                    LOGGER.warn(String.format("No date formatters configured for property '%s'.", getName()));
                    return;
                }
                if (TemporalAccessor.class.isAssignableFrom(actualType)) {
                    propertyValueConverter = new TemporalPropertyValueConverter(this, dateConverters);
                } else if (Date.class.isAssignableFrom(actualType)) {
                    propertyValueConverter = new DatePropertyValueConverter(this, dateConverters);
                } else {
                    LOGGER.warn(String.format("Unsupported type '%s' for date property '%s'.", actualType, getName()));
                }
                break;
            }
        case Date_Range:
            {
                if (!Range.class.isAssignableFrom(actualType)) {
                    return;
                }
                List<ElasticsearchDateConverter> dateConverters = getDateConverters(field, actualType);
                if (dateConverters.isEmpty()) {
                    LOGGER.warn(String.format("No date formatters configured for property '%s'.", getName()));
                    return;
                }
                Class<?> genericType = getTypeInformation().getTypeArguments().get(0).getType();
                if (TemporalAccessor.class.isAssignableFrom(genericType)) {
                    propertyValueConverter = new TemporalRangePropertyValueConverter(this, dateConverters);
                } else if (Date.class.isAssignableFrom(genericType)) {
                    propertyValueConverter = new DateRangePropertyValueConverter(this, dateConverters);
                } else {
                    LOGGER.warn(String.format("Unsupported generic type '{%s' for date range property '%s'.", genericType, getName()));
                }
                break;
            }
        case Integer_Range:
        case Float_Range:
        case Long_Range:
        case Double_Range:
            {
                if (!Range.class.isAssignableFrom(actualType)) {
                    return;
                }
                Class<?> genericType = getTypeInformation().getTypeArguments().get(0).getType();
                if ((field.type() == FieldType.Integer_Range && !Integer.class.isAssignableFrom(genericType)) || (field.type() == FieldType.Float_Range && !Float.class.isAssignableFrom(genericType)) || (field.type() == FieldType.Long_Range && !Long.class.isAssignableFrom(genericType)) || (field.type() == FieldType.Double_Range && !Double.class.isAssignableFrom(genericType))) {
                    LOGGER.warn(String.format("Unsupported generic type '%s' for range field type '%s' of property '%s'.", genericType, field.type(), getName()));
                    return;
                }
                propertyValueConverter = new NumberRangePropertyValueConverter(this);
                break;
            }
        case Ip_Range:
            {
            // TODO currently unsupported, needs a library like https://seancfoley.github.io/IPAddress/
            }
        default:
            break;
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalRangePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.TemporalRangePropertyValueConverter) DatePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.DatePropertyValueConverter) Field(org.springframework.data.elasticsearch.annotations.Field) GeoPointField(org.springframework.data.elasticsearch.annotations.GeoPointField) MultiField(org.springframework.data.elasticsearch.annotations.MultiField) GeoShapeField(org.springframework.data.elasticsearch.annotations.GeoShapeField) JoinField(org.springframework.data.elasticsearch.core.join.JoinField) TemporalPropertyValueConverter(org.springframework.data.elasticsearch.core.convert.TemporalPropertyValueConverter) DateRangePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter) NumberRangePropertyValueConverter(org.springframework.data.elasticsearch.core.convert.NumberRangePropertyValueConverter) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TemporalAccessor (java.time.temporal.TemporalAccessor)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Field (org.springframework.data.elasticsearch.annotations.Field)1 GeoPointField (org.springframework.data.elasticsearch.annotations.GeoPointField)1 GeoShapeField (org.springframework.data.elasticsearch.annotations.GeoShapeField)1 MultiField (org.springframework.data.elasticsearch.annotations.MultiField)1 DatePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.DatePropertyValueConverter)1 DateRangePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.DateRangePropertyValueConverter)1 NumberRangePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.NumberRangePropertyValueConverter)1 TemporalPropertyValueConverter (org.springframework.data.elasticsearch.core.convert.TemporalPropertyValueConverter)1 TemporalRangePropertyValueConverter (org.springframework.data.elasticsearch.core.convert.TemporalRangePropertyValueConverter)1 JoinField (org.springframework.data.elasticsearch.core.join.JoinField)1