-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchConversion.java
78 lines (54 loc) · 2.02 KB
/
SearchConversion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package c.a.i.search;
import com.google.api.client.util.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class SearchConversion {
private static final Logger LOG = LoggerFactory.getLogger(SearchConversion.class);
private static final String ES_DATE_FMT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
private static final TimeZone ES_DATE_TMZ = TimeZone.getTimeZone("UTC");
public static Boolean getBoolean(Object input) {
return input == null || Data.isNull(input) ? null : (Boolean) input;
}
public static String getString(Object input) {
return input == null || Data.isNull(input) ? null : (String) input;
}
public static Integer getInteger(Object input) {
return input == null || Data.isNull(input)
? null
: BigDecimal.class.isAssignableFrom(input.getClass())
? ((BigDecimal) input).intValueExact()
: (Integer) input;
}
public static String formatSearchDate(Date input) {
if (input == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(ES_DATE_FMT);
format.setTimeZone(ES_DATE_TMZ);
return format.format(input);
}
public static Date parseSearchDate(Object input) {
if (input == null || Data.isNull(input)) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat(ES_DATE_FMT);
format.setTimeZone(ES_DATE_TMZ);
try {
if (input instanceof Date) {
return (Date) input;
}
if (input instanceof String) {
return format.parse((String) input);
}
LOG.error("ES-PARSE-DATE-FAILED-UNKWN {}", input);
} catch (ParseException e) {
LOG.error("ES-PARSE-DATE-FAILED {}", input);
}
return null;
}
}