KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > internal > model > Search


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.internal.model;
22
23
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
28 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent;
29 import org.apache.directory.ldapstudio.browser.core.internal.search.LdapSearchPageScoreComputer;
30 import org.apache.directory.ldapstudio.browser.core.model.Control;
31 import org.apache.directory.ldapstudio.browser.core.model.DN;
32 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
34 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
35 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter;
36 import org.apache.directory.ldapstudio.browser.core.model.URL;
37 import org.eclipse.search.ui.ISearchPageScoreComputer;
38
39
40 /**
41  * Default implementation of ISearch.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class Search implements ISearch
47 {
48
49     /** The serialVersionUID. */
50     private static final long serialVersionUID = -3482673086666351174L;
51
52     /** The connection. */
53     private IConnection connection;
54
55     /** The search results. */
56     private ISearchResult[] searchResults;
57
58     /** The search parameter. */
59     private SearchParameter searchParameter;
60
61     /** The count limit exceeded flag. */
62     private boolean countLimitExceeded;
63
64
65     /**
66      * Creates a new search with the following parameters:
67      * <ul>
68      * <li>searchName: current date
69      * <li>connection: null
70      * <li>empty search base
71      * <li>default filter (objectClass=*)
72      * <li>no returning attributes
73      * <li>search scope one level
74      * <li>no count limit
75      * <li>no time limit
76      * <li>never dereference aliases
77      * <li>ignore referrals
78      * <li>no initialization of hasChildren flag
79      * <li>no initialization of isAlias and isReferral flag
80      * <li>no controls
81      * <li>
82      * </ul>
83      */

84     public Search()
85     {
86         this(
87             new SimpleDateFormat JavaDoc( "yyyy-MM-dd HH-mm-ss" ).format( new Date JavaDoc() ), //$NON-NLS-1$
88
null, EMPTY_SEARCH_BASE, FILTER_TRUE, NO_ATTRIBUTES, ISearch.SCOPE_ONELEVEL, 0, 0,
89             IConnection.DEREFERENCE_ALIASES_NEVER, IConnection.HANDLE_REFERRALS_IGNORE, false, false, null );
90     }
91
92
93     /**
94      * Creates a new Search with the given connection and search parameters.
95      *
96      * @param conn the connection
97      * @param searchParameter the search parameters
98      */

99     public Search( IConnection conn, SearchParameter searchParameter )
100     {
101         this.connection = conn;
102         this.searchResults = null;
103         this.searchParameter = searchParameter;
104         this.countLimitExceeded = false;
105     }
106
107
108     /**
109      * Creates a new search with the given search parameters
110      *
111      * @param searchName
112      * the name of the search
113      * @param conn
114      * the connection of the search
115      * @param searchBase
116      * the base DN of the search, a null search base will be
117      * transformed to an empty DN.
118      * @param filter
119      * the filter to use, null or empty filters will be
120      * transformed to (objectClass=*)
121      * @param returningAttributes
122      * the attributes to return, an empty array indicates none,
123      * null will be transformed to '*' (all user attributes)
124      * @param scope
125      * the search scope, one of SCOPE_OBJECT, SCOPE_ONELEVEL,
126      * SCOPE_SUBTREE
127      * @param countLimit
128      * the count limit, 0 indicates no limit
129      * @param timeLimit
130      * the time limit in ms, 0 indicates no limit
131      * @param aliasesDereferencingMethod
132      * the aliases dereferencing method, one of IConnection.DEREFERENCE_ALIASES_NEVER,
133      * IConnection.DEREFERENCE_ALIASES_ALWAYS, IConnection.DEREFERENCE_ALIASES_FINDING
134      * or IConnection.DEREFERENCE_ALIASES_SEARCH
135      * @param referralsHandlingMethod
136      * the referrals handling method, one of IConnection.HANDLE_REFERRALS_IGNORE
137      * or IConnection.HANDLE_REFERRALS_FOLLOW
138      * @param initHasChildrenFlag
139      * the init hasChildren flag
140      * @param initAliasAndReferralsFlag
141      * the init isAlias and isReferral flag
142      */

143     public Search( String JavaDoc searchName, IConnection conn, DN searchBase, String JavaDoc filter, String JavaDoc[] returningAttributes,
144         int scope, int countLimit, int timeLimit, int aliasesDereferencingMethod, int referralsHandlingMethod,
145         boolean initHasChildrenFlag, boolean initAliasAndReferralsFlag, Control[] controls )
146     {
147         this.connection = conn;
148         this.searchResults = null;
149         this.countLimitExceeded = false;
150
151         this.searchParameter = new SearchParameter();
152         this.searchParameter.setName( searchName );
153         this.searchParameter.setSearchBase( searchBase );
154         this.searchParameter.setFilter( filter );
155         this.searchParameter.setReturningAttributes( returningAttributes );
156         this.searchParameter.setScope( scope );
157         this.searchParameter.setTimeLimit( timeLimit );
158         this.searchParameter.setCountLimit( countLimit );
159         this.searchParameter.setAliasesDereferencingMethod( aliasesDereferencingMethod );
160         this.searchParameter.setReferralsHandlingMethod( referralsHandlingMethod );
161         this.searchParameter.setInitHasChildrenFlag( initHasChildrenFlag );
162         this.searchParameter.setInitAliasAndReferralFlag( initAliasAndReferralsFlag );
163         this.searchParameter.setControls( controls );
164     }
165
166
167     /**
168      * {@inheritDoc}
169      */

170     public URL getUrl()
171     {
172         return new URL( this );
173     }
174
175
176     /**
177      * Fires a search update event if the search name is set.
178      *
179      * @param detail the SearchUpdateEvent detail
180      */

181     private void fireSearchUpdated( SearchUpdateEvent.EventDetail detail )
182     {
183         if ( this.getName() != null && !"".equals( this.getName() ) ) { //$NON-NLS-1$
184
EventRegistry.fireSearchUpdated( new SearchUpdateEvent( this, detail ), this );
185         }
186     }
187
188
189     /**
190      * {@inheritDoc}
191      */

192     public boolean isInitHasChildrenFlag()
193     {
194         return this.searchParameter.isInitHasChildrenFlag();
195     }
196
197
198     /**
199      * {@inheritDoc}
200      */

201     public boolean isInitAliasAndReferralFlag()
202     {
203         return this.searchParameter.isInitAliasAndReferralFlag();
204     }
205
206
207     /**
208      * {@inheritDoc}
209      */

210     public Control[] getControls()
211     {
212         return this.searchParameter.getControls();
213     }
214
215
216     /**
217      * {@inheritDoc}
218      */

219     public int getCountLimit()
220     {
221         return this.searchParameter.getCountLimit();
222     }
223
224
225     /**
226      * {@inheritDoc}
227      */

228     public void setCountLimit( int countLimit )
229     {
230         this.searchParameter.setCountLimit( countLimit );
231         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
232     }
233
234
235     /**
236      * {@inheritDoc}
237      */

238     public String JavaDoc getFilter()
239     {
240         return this.searchParameter.getFilter();
241     }
242
243
244     /**
245      * {@inheritDoc}
246      */

247     public void setFilter( String JavaDoc filter )
248     {
249         this.searchParameter.setFilter( filter );
250         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
251     }
252
253
254     /**
255      * {@inheritDoc}
256      */

257     public String JavaDoc[] getReturningAttributes()
258     {
259         return this.searchParameter.getReturningAttributes();
260     }
261
262
263     /**
264      * {@inheritDoc}
265      */

266     public void setReturningAttributes( String JavaDoc[] returningAttributes )
267     {
268         this.searchParameter.setReturningAttributes( returningAttributes );
269         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
270     }
271
272
273     /**
274      * {@inheritDoc}
275      */

276     public int getScope()
277     {
278         return this.searchParameter.getScope();
279     }
280
281
282     /**
283      * {@inheritDoc}
284      */

285     public void setScope( int scope )
286     {
287         this.searchParameter.setScope( scope );
288         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
289     }
290
291
292     /**
293      * {@inheritDoc}
294      */

295     public int getAliasesDereferencingMethod()
296     {
297         return this.searchParameter.getAliasesDereferencingMethod();
298     }
299
300
301     /**
302      * {@inheritDoc}
303      */

304     public void setAliasesDereferencingMethod( int aliasesDereferencingMethod )
305     {
306         this.searchParameter.setAliasesDereferencingMethod( aliasesDereferencingMethod );
307         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
308     }
309
310
311     /**
312      * {@inheritDoc}
313      */

314     public int getReferralsHandlingMethod()
315     {
316         return this.searchParameter.getReferralsHandlingMethod();
317     }
318
319
320     /**
321      * {@inheritDoc}
322      */

323     public void setReferralsHandlingMethod( int referralsHandlingMethod )
324     {
325         this.searchParameter.setReferralsHandlingMethod( referralsHandlingMethod );
326         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
327     }
328
329
330     /**
331      * {@inheritDoc}
332      */

333     public DN getSearchBase()
334     {
335         return this.searchParameter.getSearchBase();
336     }
337
338
339     /**
340      * {@inheritDoc}
341      */

342     public void setSearchBase( DN searchBase )
343     {
344         this.searchParameter.setSearchBase( searchBase );
345         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
346     }
347
348
349     /**
350      * {@inheritDoc}
351      */

352     public int getTimeLimit()
353     {
354         return this.searchParameter.getTimeLimit();
355     }
356
357
358     /**
359      * {@inheritDoc}
360      */

361     public void setTimeLimit( int timeLimit )
362     {
363         this.searchParameter.setTimeLimit( timeLimit );
364         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
365     }
366
367
368     /**
369      * {@inheritDoc}
370      */

371     public String JavaDoc getName()
372     {
373         return this.searchParameter.getName();
374     }
375
376
377     /**
378      * {@inheritDoc}
379      */

380     public void setName( String JavaDoc searchName )
381     {
382         this.searchParameter.setName( searchName );
383         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_RENAMED );
384     }
385
386
387     /**
388      * {@inheritDoc}
389      */

390     public ISearchResult[] getSearchResults()
391     {
392         return searchResults;
393     }
394
395
396     /**
397      * {@inheritDoc}
398      */

399     public void setSearchResults( ISearchResult[] searchResults )
400     {
401         this.searchResults = searchResults;
402         if ( searchResults != null )
403         {
404             this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PERFORMED );
405         }
406     }
407
408
409     /**
410      * {@inheritDoc}
411      */

412     public boolean isCountLimitExceeded()
413     {
414         return this.countLimitExceeded;
415     }
416
417
418     /**
419      * {@inheritDoc}
420      */

421     public void setCountLimitExceeded( boolean countLimitExceeded )
422     {
423         this.countLimitExceeded = countLimitExceeded;
424         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PERFORMED );
425     }
426
427
428     /**
429      * {@inheritDoc}
430      */

431     public IConnection getConnection()
432     {
433         return connection;
434     }
435
436
437     /**
438      * {@inheritDoc}
439      */

440     public void setConnection( IConnection connection )
441     {
442         this.connection = connection;
443         this.searchParameter.setCountLimit( connection.getCountLimit() );
444         this.searchParameter.setTimeLimit( connection.getTimeLimit() );
445         this.searchParameter.setAliasesDereferencingMethod( connection.getAliasesDereferencingMethod() );
446         this.searchParameter.setReferralsHandlingMethod( connection.getReferralsHandlingMethod() );
447         this.fireSearchUpdated( SearchUpdateEvent.EventDetail.SEARCH_PARAMETER_UPDATED );
448     }
449
450
451     /**
452      * {@inheritDoc}
453      */

454     public String JavaDoc toString()
455     {
456         return this.getName() + " (" + this.connection + ")"; //$NON-NLS-1$ //$NON-NLS-2$
457
}
458
459
460     /**
461      * {@inheritDoc}
462      */

463     public Object JavaDoc clone()
464     {
465         return new Search( this.getName(), this.getConnection(), this.getSearchBase(), this.getFilter(), this
466             .getReturningAttributes(), this.getScope(), this.getCountLimit(), this.getTimeLimit(), this
467             .getAliasesDereferencingMethod(), this.getReferralsHandlingMethod(), this.isInitHasChildrenFlag(), this
468             .isInitAliasAndReferralFlag(), this.getControls() );
469     }
470
471
472     /**
473      * {@inheritDoc}
474      */

475     public SearchParameter getSearchParameter()
476     {
477         return searchParameter;
478     }
479
480
481     /**
482      * {@inheritDoc}
483      */

484     public void setSearchParameter( SearchParameter searchParameter )
485     {
486         this.searchParameter = searchParameter;
487     }
488
489
490     /**
491      * {@inheritDoc}
492      */

493     public Object JavaDoc getAdapter( Class JavaDoc adapter )
494     {
495
496         Class JavaDoc<?> clazz = ( Class JavaDoc<?> ) adapter;
497         if ( clazz.isAssignableFrom( ISearchPageScoreComputer.class ) )
498         {
499             return new LdapSearchPageScoreComputer();
500         }
501         if ( clazz.isAssignableFrom( IConnection.class ) )
502         {
503             return getConnection();
504         }
505         if ( clazz.isAssignableFrom( ISearch.class ) )
506         {
507             return this;
508         }
509
510         return null;
511     }
512
513 }
514
Popular Tags