KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > search > DateSearchControl


1 package info.magnolia.cms.gui.controlx.search;
2
3 import info.magnolia.cms.gui.query.DateSearchQueryParameter;
4 import info.magnolia.cms.gui.query.SearchQueryExpression;
5 import info.magnolia.cms.util.AlertUtil;
6 import info.magnolia.context.MgnlContext;
7
8 import java.text.DateFormat JavaDoc;
9 import java.text.ParseException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.util.Date JavaDoc;
12
13 import org.apache.commons.lang.time.FastDateFormat;
14
15
16 /**
17  * Special Date Search Control
18  * @author philipp
19  */

20 public class DateSearchControl extends SearchControl {
21
22     /**
23      *
24      */

25     public static final String JavaDoc RENDER_TYPE = "dateSearchControl";
26
27     /**
28      *
29      */

30     public DateSearchControl() {
31         this.setRenderType(RENDER_TYPE);
32     }
33
34     /**
35      * @param definition
36      * @param value
37      * @param condition
38      */

39     public DateSearchControl(SearchControlDefinition definition, String JavaDoc value, String JavaDoc condition) {
40         super(definition, value, condition);
41         this.setRenderType(RENDER_TYPE);
42     }
43
44     /**
45      * create the date query expression
46      */

47     public SearchQueryExpression getExpression() {
48         Date JavaDoc date = null;
49         if (this.getConstraint().equals(DateSearchQueryParameter.TODAY)) {
50             date = new Date JavaDoc();
51         }
52         else {
53             String JavaDoc value = getValue();
54             try {
55                 DateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
56                 date = format.parse(value);
57             }
58             catch (ParseException JavaDoc e) {
59                 try {
60                     DateFormat JavaDoc format = DateFormat.getDateInstance(FastDateFormat.SHORT, MgnlContext.getLocale());
61                     date = format.parse(value);
62                 }
63                 catch (ParseException JavaDoc e1) {
64                     try {
65                         DateFormat JavaDoc format = DateFormat.getDateInstance(FastDateFormat.SHORT, MgnlContext.getLocale());
66                         date = format.parse(value);
67                     }
68                     catch (ParseException JavaDoc e2) {
69                         AlertUtil.setMessage("The date is not properly formated [" + value + "] ");
70                     }
71                 }
72             }
73         }
74
75         return new DateSearchQueryParameter(this.getDefinition().getColumn(), date, this.getConstraint());
76     }
77 }
78
Popular Tags