KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.controlx.search;
14
15 import info.magnolia.cms.gui.query.DateSearchQueryParameter;
16 import info.magnolia.cms.gui.query.SearchQueryExpression;
17 import info.magnolia.cms.gui.query.SearchQueryOperator;
18 import info.magnolia.cms.gui.query.StringSearchQueryParameter;
19
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.NumberFormat JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.TimeZone JavaDoc;
25
26 import javax.swing.text.NumberFormatter JavaDoc;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.commons.lang.time.DateFormatUtils;
30
31
32 /**
33  * @author Sameer Charles
34  * $Id: QueryBuilder.java 7713 2006-12-07 17:57:17Z philipp $
35  * package private helper class Implement this class if you need any further operations in
36  * future <b>NOTE</b> : its a very simple in-order binary traversal, order of operation is not preserved
37  */

38 class QueryBuilder {
39
40     /**
41      * statement
42      */

43     private StringBuffer JavaDoc statement = new StringBuffer JavaDoc();
44
45     /**
46      * search model using which query will be created
47      */

48     private RepositorySearchListModel model;
49
50     /**
51      * package private
52      * @param model
53      */

54     protected QueryBuilder(RepositorySearchListModel model) {
55         this.model = model;
56         this.build(this.model.getQuery().getRootExpression());
57         this.addSelect();
58     }
59
60     /**
61      * get SQL statement based on SearchQuery
62      * @return SQL statement
63      */

64     protected String JavaDoc getSQLStatement() {
65         return this.statement.toString();
66     }
67
68     /**
69      * prepend select statement
70      */

71     private void addSelect() {
72         StringBuffer JavaDoc select = new StringBuffer JavaDoc("select * from ");
73         select.append(this.model.getNodeType());
74         if (StringUtils.isNotEmpty(this.model.getSearchPath())) {
75             this.statement.append(" jcr:path like '");
76             this.statement.append(this.model.getSearchPath());
77             this.statement.append("%'");
78         }
79         if (this.statement.length() > 0) {
80             select.append(" where");
81         }
82         this.statement.insert(0, select.toString());
83     }
84
85     /**
86      * add orgering
87      */

88     /*
89      * private void addOrder() { if (StringUtils.isNotEmpty(this.model.getGroupBy()) &&
90      * StringUtils.isNotEmpty(this.model.getSortBy())) { statement.append(" order by ");
91      * statement.append(this.model.getGroupBy()); statement.append(" "); statement.append(this.model.getGroupByOrder());
92      * statement.append(", "); statement.append(this.model.getSortBy()); statement.append(" ");
93      * statement.append(this.model.getSortByOrder()); } else if (StringUtils.isNotEmpty(this.model.getGroupBy()) &&
94      * StringUtils.isEmpty(this.model.getSortBy())) { statement.append(" order by ");
95      * statement.append(this.model.getGroupBy()); statement.append(" "); statement.append(this.model.getGroupByOrder()); }
96      * else if (StringUtils.isEmpty(this.model.getGroupBy()) && StringUtils.isNotEmpty(this.model.getSortBy())) {
97      * statement.append(" order by "); statement.append(this.model.getSortBy()); statement.append(" ");
98      * statement.append(this.model.getSortByOrder()); } }
99      */

100
101     /**
102      * NOTE : its a very simple in-order binary traversal, order of operation is not preserved
103      * @param expression
104      */

105     private void build(SearchQueryExpression expression) {
106         if (expression == null) {
107             return;
108         }
109         this.build(expression.getLeft());
110         this.statement.append(" ");
111         this.statement.append(toJCRExpression(expression));
112         this.build(expression.getRight());
113     }
114
115     /**
116      * Make a jcr expression out of the expression
117      * @param expression
118      * @return the expression as string
119      */

120     private String JavaDoc toJCRExpression(SearchQueryExpression expression) {
121         if (expression instanceof SearchQueryOperator) {
122             // operator is 1:1 usable in jcr
123
return StringUtils.defaultString(((SearchQueryOperator) expression).getOperator());
124         }
125         else if (expression instanceof StringSearchQueryParameter) {
126             return toStringJCRExpression((StringSearchQueryParameter) expression);
127         }
128         else if (expression instanceof DateSearchQueryParameter) {
129             return getDateJCRExpression((DateSearchQueryParameter) expression);
130         }
131         return StringUtils.EMPTY;
132     }
133
134     /**
135      * Make a jcr expression out of the expression
136      * @param param
137      * @return the expression as a string
138      */

139     private String JavaDoc getDateJCRExpression(DateSearchQueryParameter param) {
140         Date JavaDoc date = param.getValue();
141         if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.TODAY)) {
142             date = new Date JavaDoc();
143         }
144
145         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
146         buffer.append(param.getName());
147         if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.BEFORE)) {
148             buffer.append(" <= ");
149         }
150         else if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.AFTER)) {
151             buffer.append(" >= ");
152         }
153         else if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.IS)) {
154             buffer.append(" = ");
155         }
156         
157         buffer.append("TIMESTAMP '");
158         buffer.append(DateFormatUtils.format(date, "yyyy-MM-dd"));
159         buffer.append("T00:00:00.000");
160         
161         TimeZone JavaDoc timezone = TimeZone.getDefault();
162         int milis = Math.abs(timezone.getRawOffset());
163         if(milis == 0){
164             buffer.append("Z");
165         }
166         else{
167             if(timezone.getRawOffset() > 0){
168                 buffer.append("+");
169             }
170             else{
171                 buffer.append("-");
172             }
173
174             int hours = milis / (1000 * 60 * 60);
175             int minutes = (milis - hours * 1000 * 60 * 60) / (1000 * 60);
176             DecimalFormat JavaDoc format = new DecimalFormat JavaDoc("00");
177             buffer.append(format.format(hours)).append(":").append(format.format(minutes));
178         }
179         buffer.append("'");
180         return buffer.toString();
181     }
182
183     /**
184      * @param param
185      * @return jcr search expression
186      */

187     private String JavaDoc toStringJCRExpression(StringSearchQueryParameter param) {
188         if (param.getConstraint().equals(StringSearchQueryParameter.CONTAINS)) {
189             return "contains(" + param.getName() + ",'" + param.getValue() + "*')";
190         }
191
192         else if (param.getConstraint().equals(StringSearchQueryParameter.CONTAINS_NOT)) {
193             return "not contains(" + param.getName() + ",'*" + param.getValue() + "*')";
194         }
195
196         else if (param.getConstraint().equals(StringSearchQueryParameter.ENDS)) {
197             return "contains(" + param.getName() + ",'*" + param.getValue() + "')";
198         }
199
200         else if (param.getConstraint().equals(StringSearchQueryParameter.STARTS)) {
201             return "contains(" + param.getName() + ",'" + param.getValue() + "*')";
202         }
203
204         else if (param.getConstraint().equals(StringSearchQueryParameter.IS)) {
205             return param.getName() + " = '" + param.getValue() + "'";
206         }
207
208         else if (param.getConstraint().equals(StringSearchQueryParameter.IS_NOT)) {
209             return param.getName() + " <> '" + param.getValue() + "'";
210         }
211         else{
212             return param.getName() + " " + param.getConstraint() + " '" + param.getValue() + "'";
213         }
214     }
215
216 }
217
Popular Tags