View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.utils;
18  
19  
20  /**
21   * Created by The eXo Platform SAS
22   * Author : Hoa Pham
23   * hoa.pham@exoplatform.com
24   * Oct 7, 2008
25   */
26  
27  /*
28   * This abstract class help developer can easy create jcr query statement base on query criteria.
29   * This class will have 2 implementation for Sql-based query and XPath-based query
30   * */
31  public abstract class AbstractQueryBuilder {
32  
33    /** The from clause. */
34    protected StringBuilder fromClause = new StringBuilder();
35  
36    /** The properties clause. */
37    protected StringBuilder propertiesClause = new StringBuilder();
38  
39    /** The path clause. */
40    protected StringBuilder pathClause= new StringBuilder();
41  
42    /** The order by clause. */
43    protected StringBuilder orderByClause = new StringBuilder();
44  
45    /** The select clause. */
46    protected StringBuilder selectClause = new StringBuilder();
47  
48    /** The contains clause. */
49    protected StringBuilder containsClause = new StringBuilder();
50  
51    /** The spell check clause. */
52    protected StringBuilder spellCheckClause = new StringBuilder();
53  
54    /** The excerpt clause. */
55    protected StringBuilder excerptClause = new StringBuilder();
56  
57    /**
58     * Checks a property is null.
59     *
60     * @param propertyName the property name
61     * @param condition the condition
62     */
63    public abstract void isNull(String propertyName, LOGICAL condition);
64  
65    /**
66     * Checks a property is not null.
67     *
68     * @param propertyName the property name
69     * @param condition the condition
70     */
71    public abstract void isNotNull(String propertyName, LOGICAL condition);
72  
73    /**
74     * compare less than
75     *
76     * @param propertyName the property name
77     * @param value the value
78     * @param condition the condition
79     */
80    public abstract void lessThan(String propertyName, String value, LOGICAL condition);
81  
82    /**
83     * Greater than.
84     *
85     * @param propName the prop name
86     * @param value the value
87     * @param condition the condition
88     */
89    public abstract void greaterThan(String propName, String value, LOGICAL condition);
90  
91    /**
92     * Less than or equal.
93     *
94     * @param propName the prop name
95     * @param value the value
96     * @param condition the condition
97     */
98    public abstract void lessThanOrEqual(String propName, String value, LOGICAL condition);
99  
100   /**
101    * Greater or equal.
102    *
103    * @param propName the prop name
104    * @param value the value
105    * @param condition the condition
106    */
107   public abstract void greaterOrEqual(String propName,String value, LOGICAL condition);
108 
109   /**
110    * Equal.
111    *
112    * @param propName the prop name
113    * @param value the value
114    * @param condition the condition
115    */
116   public abstract void equal(String propName, String value , LOGICAL condition);
117 
118   /**
119    * Not equal.
120    *
121    * @param propName the prop name
122    * @param value the value
123    * @param condition the condition
124    */
125   public abstract void notEqual(String propName, String value, LOGICAL condition);
126 
127   /**
128    * Like.
129    *
130    * @param propName the prop name
131    * @param value the value
132    * @param condition the condition
133    */
134   public abstract void like(String propName, String value, LOGICAL condition);
135 
136   /**
137    * Reference.
138    *
139    * @param propName the prop name
140    * @param value the value
141    * @param condition the condition
142    */
143   public abstract void reference(String propName, String value, LOGICAL condition);
144 
145   /**
146    * Before date.
147    *
148    * @param datePropertyName the date property name
149    * @param date the date
150    * @param condition the condition
151    */
152   public abstract void beforeDate(String datePropertyName, String date, LOGICAL condition);
153 
154   /**
155    * After date.
156    *
157    * @param datePropertyName the date property name
158    * @param date the date
159    * @param condition the condition
160    */
161   public abstract void afterDate(String datePropertyName, String date, LOGICAL condition);
162 
163   /**
164    * Between dates.
165    *
166    * @param datePropertyName the date property name
167    * @param startDate the start date
168    * @param endDate the end date
169    * @param condition the condition
170    */
171   public abstract void betweenDates(String datePropertyName, String startDate, String endDate, LOGICAL condition);
172 
173   /**
174    * Sets the query path.
175    *
176    * @param path the path
177    * @param pathtype the pathtype
178    */
179   public abstract void setQueryPath(String path, PATH_TYPE pathtype);
180 
181   /**
182    * Contains.
183    *
184    * @param scope the scope
185    * @param term the term
186    * @param condition the condition
187    */
188   public abstract void contains(String scope, String term, LOGICAL condition);
189 
190   /**
191    * Not contains.
192    *
193    * @param scope the scope
194    * @param term the term
195    * @param condition the condition
196    */
197   public abstract void notContains(String scope, String term, LOGICAL condition);
198 
199   /**
200    * Excerpt.
201    *
202    * @param enable the enable
203    */
204   public abstract void excerpt(boolean enable);
205 
206   /**
207    * Spell check.
208    *
209    * @param term the term
210    */
211   public abstract void spellCheck(String term);
212 
213   /**
214    * Order by.
215    *
216    * @param properyName the propery name
217    * @param orderby the orderby
218    */
219   public abstract void orderBy(String properyName, ORDERBY orderby);
220 
221   /**
222    * Select types.
223    *
224    * @param returnTypes the return types
225    */
226   public abstract void selectTypes(String[] returnTypes);
227 
228   /**
229    * From node types.
230    *
231    * @param nodetypes the nodetypes
232    */
233   public abstract void fromNodeTypes(String[] nodetypes);
234 
235   /**
236    * Open group condition for where clause
237    *
238    * @param logical the logical
239    */
240   public abstract void openGroup(LOGICAL logical);
241 
242   /**
243    * Close group.
244    */
245   public abstract void closeGroup();
246 
247   /**
248    * Creates the query statement.
249    *
250    * @return the string
251    */
252   public abstract String createQueryStatement();
253 
254   /**
255    * Merge.
256    *
257    * @param other the other
258    */
259   public abstract void merge(AbstractQueryBuilder other);
260 
261   /**
262    * The Class QueryTermHelper.
263    */
264   public static class QueryTermHelper {
265 
266     /** The term. */
267     private String term = "";
268 
269     /**
270      * Instantiates a new query term helper.
271      */
272     public QueryTermHelper() { }
273 
274     /**
275      * Contains a word or phase
276      *
277      * @param s the s
278      *
279      * @return the query term helper
280      */
281     public QueryTermHelper contains(String s) {
282       term = term.concat(s);
283       return this;
284     }
285 
286     /**
287      * Not contains a word or phase
288      *
289      * @param s the s
290      *
291      * @return the query term helper
292      */
293     public QueryTermHelper notContains(String s) {
294       if(s.indexOf(" ")>0)
295         term = term.concat("-\\" + s + "\\");
296       else
297         term = term.concat("-"+ s);
298       return this;
299     }
300 
301     /**
302      * Builds the term.
303      *
304      * @return the string
305      */
306     public String buildTerm() { return term; }
307 
308     /**
309      * Allow fuzzy search.
310      *
311      * @return the query term helper
312      */
313     public QueryTermHelper allowFuzzySearch(double fuzzySearchIndex) {
314       term = term.concat("~").concat(String.valueOf(fuzzySearchIndex));
315       return this;
316     }
317 
318     /**
319      * Allow synonym search.
320      *
321      * @return the query term helper
322      */
323     public QueryTermHelper allowSynonymSearch() {
324       term =  "~".concat(term);
325       return this;
326     }
327 
328   }
329 
330   /**
331    * The Enum type of path constraints can be used in query statement
332    */
333   public enum PATH_TYPE { EXACT, CHILDNODES, DECENDANTS, DECENDANTS_OR_SELFT };
334 
335   /**
336    * The Enum LOGICAL function for query
337    */
338   public enum LOGICAL { AND, OR, NULL, NOT, AND_NOT, OR_NOT};
339 
340   /**
341    * The Enum ORDERBY function for query
342    */
343   public enum ORDERBY { ASC, DESC };
344   
345   /**
346    * The Enum COMPARISON_TYPE function for query
347    */
348   public enum COMPARISON_TYPE {
349     EQUAL, LIKE
350   };
351 }