KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > search > ContainerSearcher


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
//
15

16 package org.jahia.services.search;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.jahia.data.search.JahiaSearchResult;
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.params.ParamBean;
26 import org.jahia.registries.ServicesRegistry;
27 import org.jahia.services.sites.JahiaSite;
28 import org.jahia.services.version.EntryLoadRequest;
29
30
31 /**
32  * Handle containers search.
33  * The result for this searcher is an instance of JahiaSearchResult
34  * with a vector of matching container ids.
35  *
36  * @author Khue Nguyen <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
37  */

38
39 public class ContainerSearcher extends JahiaSearcher {
40
41     private int ctnListID = 0;
42
43     private int siteId = -1;
44
45     private int[] siteIds = new int[]{};
46
47     private String JavaDoc containerDefinitionName;
48
49     private boolean siteModeSearching = false;
50
51     private long lastSearchTime = -1;
52
53     private boolean updated = false;
54
55     private int containerLevel = 0;
56
57     private EntryLoadRequest loadRequest = EntryLoadRequest.CURRENT;
58
59     //--------------------------------------------------------------------------
60
/**
61      * Constructor for one single container list search
62      * The expected result is a JahiaSearchResult object containing a vector of matching containers.
63      * The vector contained in the JahiaSearchResult is a vector of matching ctn ids, not the containers.
64      * No right check on container, only on field.
65      *
66      * @param int ctnListID, the container list id.
67      * @param aCtnListID
68      * @param query a valid Lucene search query
69      * @param aLoadRequest
70      */

71     public ContainerSearcher (int aCtnListID, String JavaDoc query, EntryLoadRequest aLoadRequest) {
72         this.ctnListID = aCtnListID;
73         if (aLoadRequest != null) {
74             this.loadRequest = aLoadRequest;
75         }
76         setQuery(query);
77     }
78
79     //--------------------------------------------------------------------------
80
/**
81      * Constructor for one single container list search
82      * The expected result is a JahiaSearchResult object containing a vector of matching containers.
83      * The vector contained in the JahiaSearchResult is a vector of matching ctn ids, not the containers.
84      * No right check on container, only on field.
85      *
86      * @param aCtnListID
87      * @param aContainerLevel
88      * @param query
89      * @param aLoadRequest
90      */

91     public ContainerSearcher (int aCtnListID, int aContainerLevel, String JavaDoc query,
92                               EntryLoadRequest aLoadRequest) {
93         this.ctnListID = aCtnListID;
94         this.containerLevel = aContainerLevel;
95         if (aLoadRequest != null) {
96             this.loadRequest = aLoadRequest;
97         }
98         setQuery(query);
99     }
100
101     //--------------------------------------------------------------------------
102
/**
103      * Constructor for a single container list
104      *
105      * @param containerListName
106      * @param params
107      * @param query
108      * @param aLoadRequest
109      *
110      * @throws JahiaException
111      */

112     public ContainerSearcher (String JavaDoc containerListName, ParamBean params, String JavaDoc query,
113                               EntryLoadRequest aLoadRequest)
114             throws JahiaException {
115         if (containerListName != null) {
116             int clistID = ServicesRegistry.getInstance ().getJahiaContainersService ().
117                     getContainerListID (containerListName, params.getPage ().getID ());
118             if (clistID != -1) {
119                 this.ctnListID = clistID;
120             }
121         }
122         setQuery(query);
123         if (aLoadRequest != null) {
124             this.loadRequest = aLoadRequest;
125         }
126     }
127
128
129     //--------------------------------------------------------------------------
130
/**
131      * Constructor for searching containers of one Site or all Sites,
132      * and of one definition or of any definition
133      * If siteId = -1 -> search on all sites
134      * If containerDefinitionName is null -> ignore container definition type
135      * The expected result is a JahiaSearchResult object containing a vector of matching containers.
136      * The vector contained in the JahiaSearchResult is a vector of matching ctn ids, not the containers.
137      * No right check on container, only on field.
138      *
139      * @param aSiteId
140      * @param aContainerDefinitionName
141      * @param query
142      * @param aLoadRequest
143      */

144     public ContainerSearcher (int aSiteId, String JavaDoc aContainerDefinitionName,
145                               String JavaDoc query, EntryLoadRequest aLoadRequest) {
146         this.siteId = aSiteId;
147         this.containerDefinitionName = aContainerDefinitionName;
148         this.siteModeSearching = true;
149
150         if ( aSiteId == -1 ){
151             try {
152                 ServicesRegistry sReg = ServicesRegistry.getInstance();
153                 Enumeration JavaDoc sites = sReg.getJahiaSitesService().getSites();
154                 JahiaSite site = null;
155                 ArrayList JavaDoc ar = new ArrayList JavaDoc();
156                 while ( sites.hasMoreElements() ){
157                     site = (JahiaSite)sites.nextElement();
158                     ar.add(new Integer JavaDoc(site.getID()));
159                 }
160                 int[] ids = new int[ar.size()];
161                 Integer JavaDoc I = null;
162                 for ( int i=0; i<ar.size(); i++ ){
163                     I = (Integer JavaDoc)ar.get(i);
164                     ids[i]=I.intValue();
165                 }
166                 this.setSiteIds(ids);
167             } catch ( Throwable JavaDoc t) {
168             }
169         } else {
170             this.setSiteIds(new int[]{aSiteId});
171         }
172
173         if (aLoadRequest != null) {
174             this.loadRequest = aLoadRequest;
175         }
176         setQuery(query);
177     }
178
179     /**
180      * Search performed on the passed sites
181      * @param siteIDs
182      * @param aContainerDefinitionName
183      * @param query
184      * @param aLoadRequest
185      */

186     public ContainerSearcher (int[] siteIDs, String JavaDoc aContainerDefinitionName,
187                               String JavaDoc query, EntryLoadRequest aLoadRequest) {
188         if ( siteIDs != null ){
189             this.setSiteIds(siteIDs);
190         }
191         this.containerDefinitionName = aContainerDefinitionName;
192         this.siteModeSearching = true;
193
194         if (aLoadRequest != null) {
195             this.loadRequest = aLoadRequest;
196         }
197         this.setQuery(query);
198     }
199
200     //--------------------------------------------------------------------------
201
/**
202      * Return true if the search is done on an entire site ( or all sites )
203      * false, if the search is done on one container list ( using ctnListId )
204      *
205      * @return
206      */

207     public boolean isSiteModeSearching () {
208         return this.siteModeSearching;
209     }
210
211     //--------------------------------------------------------------------------
212
/**
213      * Perform the search for a given query as String.
214      * The expected result is a JahiaSearchResult object containing a vector of matching containers.
215      * The vector contained in the JahiaSearchResult is a vector of matching ctn ids, not the containers.
216      * No right check.
217      *
218      * @param String query, a valid query.
219      * @param ParamBean jParams, the param bean.
220      *
221      * @return JahiaSearchResult result, the expected result as an instance of JahiaSearchResult object
222      */

223     public JahiaSearchResult search (String JavaDoc query, ParamBean jParams)
224             throws JahiaException {
225         JahiaSearchResult result = null;
226
227         // Must set the query first.
228
setQuery (query);
229
230         // Perform the search.
231
ServicesRegistry sReg = ServicesRegistry.getInstance ();
232         result = sReg.getJahiaSearchService ().search (this, jParams);
233
234         // Store the result.
235
setResult (result);
236
237         // Set search time
238
this.lastSearchTime = System.currentTimeMillis();
239
240         this.updated = true;
241
242         return result;
243     }
244
245     //--------------------------------------------------------------------------
246
/**
247      * Return the searcher name which value is JahiaSearcher.CONTAINER_SEARCHER.
248      * The search service must return a result of JahiaSearchResult type for this searcher.
249      *
250      * @return String name, the searcher name.
251      */

252     public String JavaDoc getName () {
253         return ContainerSearcher.CONTAINER_SEARCHER;
254     }
255
256     //--------------------------------------------------------------------------
257
/**
258      * Return the container list id.
259      *
260      * @return int ctnListID, the container list id.
261      */

262     public int getCtnListID () {
263         return this.ctnListID;
264     }
265
266     //--------------------------------------------------------------------------
267
/**
268      * Return the site Id.
269      */

270     public int getSiteId () {
271         return this.siteId;
272     }
273
274     //--------------------------------------------------------------------------
275
/**
276      * Return the container definition name.
277      */

278     public String JavaDoc getContainerDefinitionName () {
279         return this.containerDefinitionName;
280     }
281
282     //--------------------------------------------------------------------------
283
/**
284      * Return the last search running time.
285      *
286      * @return int ctnListID, the container list id. -1 if never performed yet
287      */

288     public long getLastSearchTime () {
289         return this.lastSearchTime;
290     }
291
292     //--------------------------------------------------------------------------
293
/**
294      * Return the update status. Each time the doFilter method is called, this update status is set to true.
295      *
296      * @return boolean, the internal updated status value.
297      */

298     public boolean getUpdateStatus () {
299         return this.updated;
300     }
301
302     //--------------------------------------------------------------------------
303
/**
304      * Set the update status to true.
305      */

306     public void setUpdateStatus () {
307         this.updated = true;
308     }
309
310     //--------------------------------------------------------------------------
311
/**
312      * You can reset the internal update status by setting it to false
313      */

314     public void resetUpdateStatus () {
315         this.updated = false;
316     }
317
318     /**
319      * @return container list level.
320      */

321     public int getContainerLevel () {
322         return this.containerLevel;
323     }
324
325     /**
326      * @return the list of languages code to search.
327      */

328     public ArrayList JavaDoc getLanguageCodes () {
329         List JavaDoc locales = loadRequest.getLocales ();
330         ArrayList JavaDoc result = new ArrayList JavaDoc ();
331         for (int i = 0; i < locales.size (); i++) {
332             Locale JavaDoc locale = (Locale JavaDoc) locales.get (i);
333             result.add (locale.toString ());
334         }
335         return result;
336     }
337
338     public EntryLoadRequest getEntryLoadRequest () {
339         return this.loadRequest;
340     }
341
342     public int[] getSiteIds() {
343         return siteIds;
344     }
345
346     public void setSiteIds(int[] siteIDs) {
347         this.siteIds = siteIDs;
348     }
349
350     public boolean isQueryValid () {
351         return (getQuery() != null && !"".equals (getQuery().trim ()));
352     }
353
354     public String JavaDoc toString() {
355         return "Query=" + getQuery();
356     }
357
358 }
359
Popular Tags