16
16
package org .springframework .data .elasticsearch .core .mapping ;
17
17
18
18
import java .time .temporal .TemporalAccessor ;
19
+ import java .util .ArrayList ;
19
20
import java .util .Arrays ;
20
21
import java .util .Date ;
21
22
import java .util .List ;
@@ -154,51 +155,79 @@ private void initDateConverter() {
154
155
155
156
if (field != null && (field .type () == FieldType .Date || field .type () == FieldType .Date_Nanos )
156
157
&& (isTemporalAccessor || isDate )) {
157
- DateFormat dateFormat = field .format ();
158
+ DateFormat [] dateFormats = field .format ();
158
159
159
160
String property = getOwner ().getType ().getSimpleName () + "." + getName ();
160
161
161
- if (dateFormat == DateFormat . none ) {
162
+ if (dateFormats . length == 0 ) {
162
163
LOGGER .warn (
163
164
String .format ("No DateFormat defined for property %s. Make sure you have a Converter registered for %s" ,
164
165
property , actualType .getSimpleName ()));
165
166
return ;
166
167
}
167
168
168
- ElasticsearchDateConverter converter = null ;
169
+ List < ElasticsearchDateConverter > converters = new ArrayList <>() ;
169
170
170
- if ( dateFormat == DateFormat . custom ) {
171
- String pattern = field . pattern () ;
171
+ for ( int i = 0 ; i < dateFormats . length ; i ++ ) {
172
+ DateFormat dateFormat = dateFormats [ i ] ;
172
173
173
- if (!StringUtils .hasLength (pattern )) {
174
- throw new MappingException (
175
- String .format ("Property %s is annotated with FieldType.%s and a custom format but has no pattern defined" ,
176
- property , field .type ().name ()));
177
- }
174
+ if (dateFormat == DateFormat .custom ) {
175
+ String pattern = field .pattern ();
176
+
177
+ if (!StringUtils .hasLength (pattern )) {
178
+ throw new MappingException (String .format (
179
+ "Property %s is annotated with FieldType.%s and a custom format but has no pattern defined" , property ,
180
+ field .type ().name ()));
181
+ }
178
182
179
- converter = ElasticsearchDateConverter .of (pattern );
180
- } else {
181
-
182
- switch (dateFormat ) {
183
- case weekyear :
184
- case weekyear_week :
185
- case weekyear_week_day :
186
- LOGGER .warn ("no Converter available for " + actualType .getName () + " and date format " + dateFormat .name ()
187
- + ". Use a custom converter instead" );
188
- break ;
189
- default :
190
- converter = ElasticsearchDateConverter .of (dateFormat );
191
- break ;
183
+ converters .add (ElasticsearchDateConverter .of (pattern ));
184
+ } else {
185
+
186
+ switch (dateFormat ) {
187
+ case weekyear :
188
+ case weekyear_week :
189
+ case weekyear_week_day :
190
+ LOGGER .warn ("no Converter available for " + actualType .getName () + " and date format " + dateFormat .name ()
191
+ + ". Use a custom converter instead" );
192
+ break ;
193
+ default :
194
+ converters .add (ElasticsearchDateConverter .of (dateFormat ));
195
+ break ;
196
+ }
192
197
}
198
+
193
199
}
194
200
195
- if (converter != null ) {
196
- ElasticsearchDateConverter finalConverter = converter ;
201
+ if (!converters .isEmpty ()) {
197
202
propertyConverter = new ElasticsearchPersistentPropertyConverter () {
198
- final ElasticsearchDateConverter dateConverter = finalConverter ;
203
+ final List <ElasticsearchDateConverter > dateConverters = converters ;
204
+
205
+ @ SuppressWarnings ("unchecked" )
206
+ @ Override
207
+ public Object read (String s ) {
208
+ if (isTemporalAccessor ) {
209
+ for (ElasticsearchDateConverter dateConverter : dateConverters ) {
210
+ try {
211
+ return dateConverter .parse (s , (Class <? extends TemporalAccessor >) actualType );
212
+ } catch (Exception e ) {
213
+ }
214
+ }
215
+ } else { // must be date
216
+ for (ElasticsearchDateConverter dateConverter : dateConverters ) {
217
+ try {
218
+ return dateConverter .parse (s );
219
+ } catch (Exception e ) {
220
+ }
221
+ }
222
+ }
223
+
224
+ throw new RuntimeException (String
225
+ .format ("Unable to parse date value '%s' of property '%s' with configured converters" , s , property ));
226
+ }
199
227
200
228
@ Override
201
229
public String write (Object property ) {
230
+ ElasticsearchDateConverter dateConverter = dateConverters .get (0 );
202
231
if (isTemporalAccessor && TemporalAccessor .class .isAssignableFrom (property .getClass ())) {
203
232
return dateConverter .format ((TemporalAccessor ) property );
204
233
} else if (isDate && Date .class .isAssignableFrom (property .getClass ())) {
@@ -207,16 +236,6 @@ public String write(Object property) {
207
236
return property .toString ();
208
237
}
209
238
}
210
-
211
- @ SuppressWarnings ("unchecked" )
212
- @ Override
213
- public Object read (String s ) {
214
- if (isTemporalAccessor ) {
215
- return dateConverter .parse (s , (Class <? extends TemporalAccessor >) actualType );
216
- } else { // must be date
217
- return dateConverter .parse (s );
218
- }
219
- }
220
239
};
221
240
}
222
241
}
0 commit comments