|
30 | 30 | import org.springframework.data.elasticsearch.annotations.GeoShapeField; |
31 | 31 | import org.springframework.data.elasticsearch.annotations.MultiField; |
32 | 32 | import org.springframework.data.elasticsearch.core.completion.Completion; |
33 | | -import org.springframework.data.elasticsearch.core.convert.ConversionException; |
| 33 | +import org.springframework.data.elasticsearch.core.convert.DatePersistentPropertyConverter; |
34 | 34 | import org.springframework.data.elasticsearch.core.convert.ElasticsearchDateConverter; |
| 35 | +import org.springframework.data.elasticsearch.core.convert.TemporalPersistentPropertyConverter; |
35 | 36 | import org.springframework.data.elasticsearch.core.geo.GeoJson; |
36 | 37 | import org.springframework.data.elasticsearch.core.geo.GeoPoint; |
37 | 38 | import org.springframework.data.elasticsearch.core.join.JoinField; |
38 | 39 | import org.springframework.data.elasticsearch.core.query.SeqNoPrimaryTerm; |
39 | 40 | import org.springframework.data.mapping.Association; |
40 | 41 | import org.springframework.data.mapping.MappingException; |
41 | 42 | import org.springframework.data.mapping.PersistentEntity; |
| 43 | +import org.springframework.data.mapping.PersistentProperty; |
42 | 44 | import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty; |
43 | 45 | import org.springframework.data.mapping.model.FieldNamingStrategy; |
44 | 46 | import org.springframework.data.mapping.model.Property; |
@@ -92,7 +94,7 @@ public SimpleElasticsearchPersistentProperty(Property property, |
92 | 94 | throw new MappingException("@Field annotation must not be used on a @MultiField property."); |
93 | 95 | } |
94 | 96 |
|
95 | | - initDateConverter(); |
| 97 | + initPropertyConverter(); |
96 | 98 |
|
97 | 99 | storeNullValue = isField && getRequiredAnnotation(Field.class).storeNullValue(); |
98 | 100 | } |
@@ -128,102 +130,82 @@ protected boolean hasExplicitFieldName() { |
128 | 130 | } |
129 | 131 |
|
130 | 132 | /** |
131 | | - * Initializes an {@link ElasticsearchPersistentPropertyConverter} if this property is annotated as a Field with type |
132 | | - * {@link FieldType#Date}, has a {@link DateFormat} set and if the type of the property is one of the Java8 temporal |
133 | | - * classes or java.util.Date. |
| 133 | + * Initializes the property converter for this {@link PersistentProperty}, if any. |
134 | 134 | */ |
135 | | - private void initDateConverter() { |
136 | | - Field field = findAnnotation(Field.class); |
| 135 | + private void initPropertyConverter() { |
137 | 136 |
|
138 | 137 | Class<?> actualType = getActualTypeOrNull(); |
139 | | - |
140 | 138 | if (actualType == null) { |
141 | 139 | return; |
142 | 140 | } |
143 | 141 |
|
144 | | - boolean isTemporalAccessor = TemporalAccessor.class.isAssignableFrom(actualType); |
145 | | - boolean isDate = Date.class.isAssignableFrom(actualType); |
| 142 | + Field field = findAnnotation(Field.class); |
| 143 | + if (field == null || field.type() == null) { |
| 144 | + return; |
| 145 | + } |
146 | 146 |
|
147 | | - if (field != null && (field.type() == FieldType.Date || field.type() == FieldType.Date_Nanos) |
148 | | - && (isTemporalAccessor || isDate)) { |
| 147 | + switch (field.type()) { |
| 148 | + case Date: |
| 149 | + case Date_Nanos: |
| 150 | + List<ElasticsearchDateConverter> dateConverters = getDateConverters(field, actualType); |
149 | 151 |
|
150 | | - DateFormat[] dateFormats = field.format(); |
151 | | - String[] dateFormatPatterns = field.pattern(); |
| 152 | + if (dateConverters.isEmpty()) { |
| 153 | + LOGGER.warn("No date formatters configured for property '{}'.", getName()); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + if (TemporalAccessor.class.isAssignableFrom(actualType)) { |
| 158 | + propertyConverter = new TemporalPersistentPropertyConverter(this, dateConverters); |
| 159 | + } else if (Date.class.isAssignableFrom(actualType)) { |
| 160 | + propertyConverter = new DatePersistentPropertyConverter(this, dateConverters); |
| 161 | + } else { |
| 162 | + LOGGER.warn("Unsupported type '{}' for date property '{}'.", actualType, getName()); |
| 163 | + } |
| 164 | + break; |
| 165 | + default: |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
152 | 169 |
|
153 | | - String property = getOwner().getType().getSimpleName() + "." + getName(); |
| 170 | + private List<ElasticsearchDateConverter> getDateConverters(Field field, Class<?> actualType) { |
154 | 171 |
|
155 | | - if (dateFormats.length == 0 && dateFormatPatterns.length == 0) { |
156 | | - LOGGER.warn( |
157 | | - "Property '{}' has @Field type '{}' but has no built-in format or custom date pattern defined. Make sure you have a converter registered for type {}.", |
158 | | - property, field.type().name(), actualType.getSimpleName()); |
159 | | - return; |
160 | | - } |
| 172 | + DateFormat[] dateFormats = field.format(); |
| 173 | + String[] dateFormatPatterns = field.pattern(); |
| 174 | + List<ElasticsearchDateConverter> converters = new ArrayList<>(); |
161 | 175 |
|
162 | | - List<ElasticsearchDateConverter> converters = new ArrayList<>(); |
163 | | - |
164 | | - // register converters for built-in formats |
165 | | - for (DateFormat dateFormat : dateFormats) { |
166 | | - switch (dateFormat) { |
167 | | - case none: |
168 | | - case custom: |
169 | | - break; |
170 | | - case weekyear: |
171 | | - case weekyear_week: |
172 | | - case weekyear_week_day: |
173 | | - LOGGER.warn("No default converter available for '{}' and date format '{}'. Use a custom converter instead.", |
174 | | - actualType.getName(), dateFormat.name()); |
175 | | - break; |
176 | | - default: |
177 | | - converters.add(ElasticsearchDateConverter.of(dateFormat)); |
178 | | - break; |
179 | | - } |
180 | | - } |
| 176 | + if (dateFormats.length == 0 && dateFormatPatterns.length == 0) { |
| 177 | + LOGGER.warn( |
| 178 | + "Property '{}' has @Field type '{}' but has no built-in format or custom date pattern defined. Make sure you have a converter registered for type {}.", |
| 179 | + getName(), field.type().name(), actualType.getSimpleName()); |
| 180 | + return converters; |
| 181 | + } |
181 | 182 |
|
182 | | - // register converters for custom formats |
183 | | - for (String dateFormatPattern : dateFormatPatterns) { |
184 | | - if (!StringUtils.hasText(dateFormatPattern)) { |
185 | | - throw new MappingException(String.format("Date pattern of property '%s' must not be empty", property)); |
186 | | - } |
187 | | - converters.add(ElasticsearchDateConverter.of(dateFormatPattern)); |
| 183 | + // register converters for built-in formats |
| 184 | + for (DateFormat dateFormat : dateFormats) { |
| 185 | + switch (dateFormat) { |
| 186 | + case none: |
| 187 | + case custom: |
| 188 | + break; |
| 189 | + case weekyear: |
| 190 | + case weekyear_week: |
| 191 | + case weekyear_week_day: |
| 192 | + LOGGER.warn("No default converter available for '{}' and date format '{}'. Use a custom converter instead.", |
| 193 | + actualType.getName(), dateFormat.name()); |
| 194 | + break; |
| 195 | + default: |
| 196 | + converters.add(ElasticsearchDateConverter.of(dateFormat)); |
| 197 | + break; |
188 | 198 | } |
| 199 | + } |
189 | 200 |
|
190 | | - if (!converters.isEmpty()) { |
191 | | - propertyConverter = new ElasticsearchPersistentPropertyConverter() { |
192 | | - final List<ElasticsearchDateConverter> dateConverters = converters; |
193 | | - |
194 | | - @SuppressWarnings("unchecked") |
195 | | - @Override |
196 | | - public Object read(String s) { |
197 | | - for (ElasticsearchDateConverter dateConverter : dateConverters) { |
198 | | - try { |
199 | | - if (isTemporalAccessor) { |
200 | | - return dateConverter.parse(s, (Class<? extends TemporalAccessor>) actualType); |
201 | | - } else { // must be date |
202 | | - return dateConverter.parse(s); |
203 | | - } |
204 | | - } catch (Exception e) { |
205 | | - LOGGER.trace(e.getMessage(), e); |
206 | | - } |
207 | | - } |
208 | | - |
209 | | - throw new ConversionException(String |
210 | | - .format("Unable to parse date value '%s' of property '%s' with configured converters", s, property)); |
211 | | - } |
212 | | - |
213 | | - @Override |
214 | | - public String write(Object property) { |
215 | | - ElasticsearchDateConverter dateConverter = dateConverters.get(0); |
216 | | - if (isTemporalAccessor && TemporalAccessor.class.isAssignableFrom(property.getClass())) { |
217 | | - return dateConverter.format((TemporalAccessor) property); |
218 | | - } else if (isDate && Date.class.isAssignableFrom(property.getClass())) { |
219 | | - return dateConverter.format((Date) property); |
220 | | - } else { |
221 | | - return property.toString(); |
222 | | - } |
223 | | - } |
224 | | - }; |
| 201 | + for (String dateFormatPattern : dateFormatPatterns) { |
| 202 | + if (!StringUtils.hasText(dateFormatPattern)) { |
| 203 | + throw new MappingException(String.format("Date pattern of property '%s' must not be empty", getName())); |
225 | 204 | } |
| 205 | + converters.add(ElasticsearchDateConverter.of(dateFormatPattern)); |
226 | 206 | } |
| 207 | + |
| 208 | + return converters; |
227 | 209 | } |
228 | 210 |
|
229 | 211 | @SuppressWarnings("ConstantConditions") |
@@ -303,4 +285,5 @@ public boolean isJoinFieldProperty() { |
303 | 285 | public boolean isCompletionProperty() { |
304 | 286 | return getActualType() == Completion.class; |
305 | 287 | } |
| 288 | + |
306 | 289 | } |
0 commit comments