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 package info.magnolia.cms.gui.controlx.search;
35
36 import info.magnolia.cms.gui.query.DateSearchQueryParameter;
37 import info.magnolia.cms.gui.query.SearchQueryExpression;
38 import info.magnolia.cms.util.AlertUtil;
39 import info.magnolia.context.MgnlContext;
40
41 import java.text.DateFormat;
42 import java.text.ParseException;
43 import java.text.SimpleDateFormat;
44 import java.util.Date;
45
46 import org.apache.commons.lang.time.FastDateFormat;
47
48
49
50
51
52
53 public class DateSearchControl extends SearchControl {
54
55
56
57
58 public static final String RENDER_TYPE = "dateSearchControl";
59
60
61
62
63 public DateSearchControl() {
64 this.setRenderType(RENDER_TYPE);
65 }
66
67
68
69
70
71
72 public DateSearchControl(SearchControlDefinition definition, String value, String condition) {
73 super(definition, value, condition);
74 this.setRenderType(RENDER_TYPE);
75 }
76
77
78
79
80 @Override
81 public SearchQueryExpression getExpression() {
82 Date date = null;
83 if (this.getConstraint().equals(DateSearchQueryParameter.TODAY)) {
84 date = new Date();
85 }
86 else {
87 String value = getValue();
88 try {
89 DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
90 date = format.parse(value);
91 }
92 catch (ParseException e) {
93 try {
94 DateFormat format = DateFormat.getDateInstance(FastDateFormat.SHORT, MgnlContext.getLocale());
95 date = format.parse(value);
96 }
97 catch (ParseException e1) {
98 try {
99 DateFormat format = DateFormat.getDateInstance(FastDateFormat.SHORT, MgnlContext.getLocale());
100 date = format.parse(value);
101 }
102 catch (ParseException e2) {
103 AlertUtil.setMessage("The date is not properly formated [" + value + "] ");
104 }
105 }
106 }
107 }
108
109 return new DateSearchQueryParameter(this.getDefinition().getColumn(), date, this.getConstraint());
110 }
111 }