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 public SearchQueryExpression getExpression() {
81 Date date = null;
82 if (this.getConstraint().equals(DateSearchQueryParameter.TODAY)) {
83 date = new Date();
84 }
85 else {
86 String value = getValue();
87 try {
88 DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
89 date = format.parse(value);
90 }
91 catch (ParseException e) {
92 try {
93 DateFormat format = DateFormat.getDateInstance(FastDateFormat.SHORT, MgnlContext.getLocale());
94 date = format.parse(value);
95 }
96 catch (ParseException e1) {
97 try {
98 DateFormat format = DateFormat.getDateInstance(FastDateFormat.SHORT, MgnlContext.getLocale());
99 date = format.parse(value);
100 }
101 catch (ParseException e2) {
102 AlertUtil.setMessage("The date is not properly formated [" + value + "] ");
103 }
104 }
105 }
106 }
107
108 return new DateSearchQueryParameter(this.getDefinition().getColumn(), date, this.getConstraint());
109 }
110 }