20
20
import java .util .regex .Pattern ;
21
21
import java .util .stream .Collectors ;
22
22
23
+ import org .springframework .core .convert .ConversionService ;
23
24
import org .springframework .core .convert .support .GenericConversionService ;
24
25
import org .springframework .data .elasticsearch .core .convert .DateTimeConverters ;
25
26
import org .springframework .data .elasticsearch .repository .query .ElasticsearchStringQuery ;
26
27
import org .springframework .data .repository .query .ParameterAccessor ;
28
+ import org .springframework .util .Assert ;
27
29
import org .springframework .util .ClassUtils ;
28
30
import org .springframework .util .NumberUtils ;
29
31
34
36
final public class StringQueryUtil {
35
37
36
38
private static final Pattern PARAMETER_PLACEHOLDER = Pattern .compile ("\\ ?(\\ d+)" );
37
- private static final GenericConversionService conversionService = new GenericConversionService ();
38
-
39
- {
40
- if (!conversionService .canConvert (java .util .Date .class , String .class )) {
41
- conversionService .addConverter (DateTimeConverters .JavaDateConverter .INSTANCE );
42
- }
43
- if (ClassUtils .isPresent ("org.joda.time.DateTimeZone" , ElasticsearchStringQuery .class .getClassLoader ())) {
44
- if (!conversionService .canConvert (org .joda .time .ReadableInstant .class , String .class )) {
45
- conversionService .addConverter (DateTimeConverters .JodaDateTimeConverter .INSTANCE );
46
- }
47
- if (!conversionService .canConvert (org .joda .time .LocalDateTime .class , String .class )) {
48
- conversionService .addConverter (DateTimeConverters .JodaLocalDateTimeConverter .INSTANCE );
49
- }
50
- }
51
- }
52
-
53
- private StringQueryUtil () {}
54
-
55
- public static String replacePlaceholders (String input , ParameterAccessor accessor ) {
39
+
40
+ private final ConversionService conversionService ;
41
+ private final GenericConversionService genericConversionService ;
42
+
43
+ public StringQueryUtil (ConversionService conversionService ) {
44
+
45
+ Assert .notNull (conversionService , "conversionService must not be null" );
46
+
47
+ this .conversionService = conversionService ;
48
+ genericConversionService = setupGenericConversionService ();
49
+ }
50
+
51
+ private GenericConversionService setupGenericConversionService () {
52
+
53
+ GenericConversionService genericConversionService = new GenericConversionService ();
54
+
55
+ if (!genericConversionService .canConvert (java .util .Date .class , String .class )) {
56
+ genericConversionService .addConverter (DateTimeConverters .JavaDateConverter .INSTANCE );
57
+ }
58
+
59
+ if (ClassUtils .isPresent ("org.joda.time.DateTimeZone" , ElasticsearchStringQuery .class .getClassLoader ())) {
60
+ if (!genericConversionService .canConvert (org .joda .time .ReadableInstant .class , String .class )) {
61
+ genericConversionService .addConverter (DateTimeConverters .JodaDateTimeConverter .INSTANCE );
62
+ }
63
+ if (!genericConversionService .canConvert (org .joda .time .LocalDateTime .class , String .class )) {
64
+ genericConversionService .addConverter (DateTimeConverters .JodaLocalDateTimeConverter .INSTANCE );
65
+ }
66
+ }
67
+ return genericConversionService ;
68
+ }
69
+
70
+ public String replacePlaceholders (String input , ParameterAccessor accessor ) {
56
71
57
72
Matcher matcher = PARAMETER_PLACEHOLDER .matcher (input );
58
73
String result = input ;
@@ -65,7 +80,7 @@ public static String replacePlaceholders(String input, ParameterAccessor accesso
65
80
return result ;
66
81
}
67
82
68
- private static String getParameterWithIndex (ParameterAccessor accessor , int index ) {
83
+ private String getParameterWithIndex (ParameterAccessor accessor , int index ) {
69
84
70
85
Object parameter = accessor .getBindableValue (index );
71
86
String parameterValue = "null" ;
@@ -80,7 +95,7 @@ private static String getParameterWithIndex(ParameterAccessor accessor, int inde
80
95
81
96
}
82
97
83
- private static String convert (Object parameter ) {
98
+ private String convert (Object parameter ) {
84
99
if (Collection .class .isAssignableFrom (parameter .getClass ())) {
85
100
Collection <?> collectionParam = (Collection <?>) parameter ;
86
101
StringBuilder sb = new StringBuilder ("[" );
@@ -101,6 +116,13 @@ private static String convert(Object parameter) {
101
116
if (converted != null ) {
102
117
parameterValue = converted ;
103
118
}
119
+ } else if (genericConversionService .canConvert (parameter .getClass (), String .class )) {
120
+ String converted = genericConversionService .convert (parameter , String .class );
121
+
122
+ if (converted != null ) {
123
+ parameterValue = converted ;
124
+
125
+ }
104
126
} else {
105
127
parameterValue = parameter .toString ();
106
128
}
0 commit comments