KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > cmr > search > SearchParameters


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.service.cmr.search;
18
19 import java.util.ArrayList JavaDoc;
20
21 import org.alfresco.service.cmr.repository.Path;
22 import org.alfresco.service.cmr.repository.StoreRef;
23
24 /**
25  * This class provides parameters to define a search.
26  *
27  * TODO
28  * - paging of results page number and page size
29  * - paging isolation - REPEATABLE READ, READ COMMITTED, may SEE ONCE tracking node refs in previous result sets
30  * - how long repeatable read may be held
31  * - limit by the number of permission evaluations
32  *
33  * @author Andy Hind
34  */

35 public class SearchParameters extends SearchStatement
36 {
37     /*
38      * The default limit if someone asks for a limited result set but does not say how to limit....
39      */

40     private static int DEFAULT_LIMIT = 500;
41     
42     /*
43      * Standard sort definitions for sorting in document and score order.
44      */

45     public static final SortDefinition SORT_IN_DOCUMENT_ORDER_ASCENDING = new SortDefinition(SortDefinition.SortType.DOCUMENT, null, true);
46     public static final SortDefinition SORT_IN_DOCUMENT_ORDER_DESCENDING = new SortDefinition(SortDefinition.SortType.DOCUMENT, null, false);
47     public static final SortDefinition SORT_IN_SCORE_ORDER_ASCENDING = new SortDefinition(SortDefinition.SortType.SCORE, null, false);
48     public static final SortDefinition SORT_IN_SCORE_ORDER_DESCENDING = new SortDefinition(SortDefinition.SortType.SCORE, null, true);
49     
50     /**
51      * An emum defining if the default action is to "and" or "or" unspecified components in the query register.
52      * Not all search implementations will support this.
53      */

54     public enum Operator
55     {
56         OR, AND
57     }
58     
59     /*
60      * Expose as constants
61      */

62     public static final Operator OR = Operator.OR;
63     public static final Operator AND = Operator.AND;
64     
65     private ArrayList JavaDoc<StoreRef> stores = new ArrayList JavaDoc<StoreRef>(1);
66     private ArrayList JavaDoc<Path> attributePaths = new ArrayList JavaDoc<Path>(1);
67     private ArrayList JavaDoc<QueryParameterDefinition> queryParameterDefinitions = new ArrayList JavaDoc<QueryParameterDefinition>(1);
68     private boolean excludeDataInTheCurrentTransaction = false;
69     private ArrayList JavaDoc<SortDefinition> sortDefinitions = new ArrayList JavaDoc<SortDefinition>(1);
70     private Operator defaultOperator = Operator.OR;
71     
72     public SearchParameters()
73     {
74         super();
75     }
76
77     /**
78      * Set the stores to be supported - currently there can be only one.
79      * Searching across multiple stores is on the todo list.
80      *
81      * @param store
82      */

83     public void addStore(StoreRef store)
84     {
85         if(stores.size() != 0)
86         {
87             throw new IllegalStateException JavaDoc("At the moment, there can only be one store set for the search");
88         }
89         stores.add(store);
90     }
91     
92     /**
93      * Add paths for attributes in the result set.
94      *
95      * Generally this only makes sense for disconnected results sets.
96      * These atttributes/paths state what must be present in the result set, akin
97      * to the selection of columns is sql.
98      *
99      * @param attributePath
100      */

101     public void addAttrbutePath(Path attributePath)
102     {
103         attributePaths.add(attributePath);
104     }
105     
106     /**
107      * Add parameter definitions for the query - used to parameterise the query string
108      *
109      * @param queryParameterDefinition
110      */

111     public void addQueryParameterDefinition(QueryParameterDefinition queryParameterDefinition)
112     {
113         queryParameterDefinitions.add(queryParameterDefinition);
114     }
115     
116     /**
117      * If true, any data in the current transaction will be ignored in the search.
118      * You will not see anything you have added in the current transaction.
119      *
120      * By default you will see data in the current transaction.
121      * This effectively gives read committed isolation.
122      *
123      * There is a performance overhead for this, at least when using lucene.
124      * This flag may be set to avoid that performance hit if you know you do not want to find results
125      * that are yet to be committed (this includes creations, deletions and updates)
126      *
127      * @param excludeDataInTheCurrentTransaction
128      */

129     public void excludeDataInTheCurrentTransaction(boolean excludeDataInTheCurrentTransaction)
130     {
131         this.excludeDataInTheCurrentTransaction = excludeDataInTheCurrentTransaction;
132     }
133     
134     /**
135      * Add a sort to the query (for those query languages that do not support it directly)
136      *
137      * The first sort added is treated as primary, the second as secondary etc.
138      *
139      * A helper method to create SortDefinitions.
140      *
141      * @param field - this is intially a direct attribute on a node not an attribute on the parent etc
142      * TODO: It could be a relative path at some time.
143      *
144      * @param ascending - true to sort ascending, false for descending.
145      */

146     public void addSort(String JavaDoc field, boolean ascending)
147     {
148         addSort(new SortDefinition(SortDefinition.SortType.FIELD, field, ascending));
149     }
150     
151     /**
152      * Add a sort definition.
153      *
154      * @param sortDefinition - the sort definition to add. Use the static member variables
155      * for sorting in score and index order.
156      */

157     public void addSort(SortDefinition sortDefinition)
158     {
159         sortDefinitions.add(sortDefinition);
160     }
161     
162     /**
163      * A helper class for sort definition.
164      * Encapsulated using the lucene sortType, field name and a flag for ascending/descending.
165      *
166      * @author Andy Hind
167      */

168     public static class SortDefinition
169     {
170         
171         public enum SortType {FIELD, DOCUMENT, SCORE};
172         
173         SortType sortType;
174         String JavaDoc field;
175         boolean ascending;
176                 
177         SortDefinition(SortType sortType, String JavaDoc field, boolean ascending)
178         {
179             this.sortType = sortType;
180             this.field = field;
181             this.ascending = ascending;
182         }
183
184         public boolean isAscending()
185         {
186             return ascending;
187         }
188
189         public String JavaDoc getField()
190         {
191             return field;
192         }
193         
194         public SortType getSortType()
195         {
196             return sortType;
197         }
198         
199     }
200
201     /**
202      * Get the list of attribute paths that are guarenteed to be in the result set.
203      *
204      * @return
205      */

206     public ArrayList JavaDoc<Path> getAttributePaths()
207     {
208         return attributePaths;
209     }
210
211     /**
212      * Is data in the current transaction excluded from the search.
213      *
214      * @return
215      */

216     public boolean excludeDataInTheCurrentTransaction()
217     {
218         return excludeDataInTheCurrentTransaction;
219     }
220
221     /**
222      * Get the query parameters that apply to this query.
223      *
224      * @return
225      */

226     public ArrayList JavaDoc<QueryParameterDefinition> getQueryParameterDefinitions()
227     {
228         return queryParameterDefinitions;
229     }
230
231     /**
232      * Get the sort definitions that apply to this query.
233      *
234      * @return
235      */

236     public ArrayList JavaDoc<SortDefinition> getSortDefinitions()
237     {
238         return sortDefinitions;
239     }
240
241     /**
242      * Get the stores in which this query should find results.
243      *
244      * @return
245      */

246     public ArrayList JavaDoc<StoreRef> getStores()
247     {
248         return stores;
249     }
250     
251     /**
252      * Set the default operator for query elements when they are not explicit in the query.
253      *
254      * @param defaultOperator
255      */

256     public void setDefaultOperator(Operator defaultOperator)
257     {
258         this.defaultOperator = defaultOperator;
259     }
260     
261     /**
262      * Get the default operator for query elements when they are not explicit in the query.
263      *
264      * @return
265      */

266     public Operator getDefaultOperator()
267     {
268         return defaultOperator;
269     }
270     
271     private LimitBy limitBy = LimitBy.UNLIMITED;
272     
273     private PermissionEvaluationMode permissionEvaluation = PermissionEvaluationMode.EAGER;
274     
275     private int limit = DEFAULT_LIMIT;
276
277     /**
278      * Get how the result set should be limited
279      *
280      * @return
281      */

282     public LimitBy getLimitBy()
283     {
284         return limitBy;
285     }
286
287     /**
288      * Set how the result set should be limited.
289      *
290      * @param limitBy
291      */

292     public void setLimitBy(LimitBy limitBy)
293     {
294         this.limitBy = limitBy;
295     }
296
297     /**
298      * Get when permissions are evaluated.
299      *
300      * @return
301      */

302     public PermissionEvaluationMode getPermissionEvaluation()
303     {
304         return permissionEvaluation;
305     }
306
307     /**
308      * Set when permissions are evaluated.
309      *
310      * @param permissionEvaluation
311      */

312     public void setPermissionEvaluation(PermissionEvaluationMode permissionEvaluation)
313     {
314         this.permissionEvaluation = permissionEvaluation;
315     }
316
317     public int getLimit()
318     {
319         return limit;
320     }
321
322     public void setLimit(int limit)
323     {
324         this.limit = limit;
325     }
326     
327     
328 }
329
Popular Tags