KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > repo > ContentSearchContext


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.filesys.smb.server.repo;
18
19 import java.io.FileNotFoundException JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.filesys.server.filesys.FileInfo;
23 import org.alfresco.filesys.server.filesys.SearchContext;
24 import org.alfresco.filesys.smb.server.repo.pseudo.PseudoFile;
25 import org.alfresco.filesys.smb.server.repo.pseudo.PseudoFileList;
26 import org.alfresco.service.cmr.repository.NodeRef;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * Wrapper for simple XPath searche against the node service. The search is performed statically
32  * outside the context instance itself - this class merely maintains the state of the search
33  * results across client connections.
34  *
35  * @author Derek Hulley
36  */

37 public class ContentSearchContext extends SearchContext
38 {
39     private static final Log logger = LogFactory.getLog(ContentSearchContext.class);
40
41     private CifsHelper cifsHelper;
42     private List JavaDoc<NodeRef> results;
43     private int index = -1;
44
45     // Pseudo file list blended into a wildcard folder search
46

47     private PseudoFileList pseudoList;
48     private boolean donePseudoFiles = false;
49     
50     // Resume id
51

52     private int resumeId;
53     
54     /**
55      * Class constructor
56      *
57      * @param cifsHelper Filesystem helper class
58      * @param results List of file/folder nodes that match the search pattern
59      * @param searchStr Search path
60      * @param pseudoList List of pseudo files to be blended into the returned list of files
61      */

62     protected ContentSearchContext(
63             CifsHelper cifsHelper,
64             List JavaDoc<NodeRef> results,
65             String JavaDoc searchStr,
66             PseudoFileList pseudoList)
67     {
68         super();
69         super.setSearchString(searchStr);
70         this.cifsHelper = cifsHelper;
71         this.results = results;
72         this.pseudoList = pseudoList;
73     }
74     
75     /**
76      * Return the search as a string
77      *
78      * @return String
79      */

80     public String JavaDoc toString()
81     {
82         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(60);
83         sb.append("ContentSearchContext")
84           .append("[ searchStr=").append(getSearchString())
85           .append(", resultCount=").append(results.size())
86           .append("]");
87         return sb.toString();
88     }
89
90     /**
91      * Return the resume id for the current file/directory in the search.
92      *
93      * @return int
94      */

95     public int getResumeId()
96     {
97         return resumeId;
98     }
99
100     /**
101      * Determine if there are more files for the active search.
102      *
103      * @return boolean
104      */

105     public boolean hasMoreFiles()
106     {
107         // Pseudo files are returned first
108

109         if ( donePseudoFiles == false && pseudoList != null && index < (pseudoList.numberOfFiles() - 1))
110             return true;
111         return index < (results.size() - 1);
112     }
113
114     /**
115      * Return file information for the next file in the active search. Returns false if the search
116      * is complete.
117      *
118      * @param info FileInfo to return the file information.
119      * @return true if the file information is valid, else false
120      */

121     public boolean nextFileInfo(FileInfo info)
122     {
123         // Check if there is anything else to return
124

125         if (!hasMoreFiles())
126             return false;
127
128         // Increment the index and resume id
129

130         index++;
131         resumeId++;
132         
133         // If the pseudo file list is valid return the pseudo files first
134

135         if ( donePseudoFiles == false && pseudoList != null)
136         {
137             if ( index < pseudoList.numberOfFiles())
138             {
139                 PseudoFile pfile = pseudoList.getFileAt( index);
140                 if ( pfile != null)
141                 {
142                     // Get the file information for the pseudo file
143

144                     FileInfo pinfo = pfile.getFileInfo();
145                     
146                     // Copy the file information to the callers file info
147

148                     info.copyFrom( pinfo);
149                     
150                     // Check if we have finished with the pseudo file list, switch to the normal file list
151

152                     if ( index == (pseudoList.numberOfFiles() - 1))
153                     {
154                         // Switch to the main file list
155

156                         donePseudoFiles = true;
157                         index = -1;
158                     }
159                     
160                     // Indicate that the file information is valid
161

162                     return true;
163                 }
164             }
165         }
166
167         // Get the next file info from the node search
168

169         NodeRef nextNodeRef = results.get(index);
170         
171         try
172         {
173             // Get the file information and copy across to the callers file info
174

175             FileInfo nextInfo = cifsHelper.getFileInformation(nextNodeRef, "");
176             info.copyFrom(nextInfo);
177             
178             // Indicate that the file information is valid
179

180             return true;
181         }
182         catch (FileNotFoundException JavaDoc e)
183         {
184         }
185         
186         // File information is not valid
187

188         return false;
189     }
190
191     /**
192      * Return the file name of the next file in the active search. Returns null is the search is
193      * complete.
194      *
195      * @return String
196      */

197     public String JavaDoc nextFileName()
198     {
199         // Check if there is anything else to return
200

201         if (!hasMoreFiles())
202             return null;
203
204         // Increment the index and resume id
205

206         index++;
207         resumeId++;
208         
209         // If the pseudo file list is valid return the pseudo files first
210

211         if ( donePseudoFiles == false && pseudoList != null)
212         {
213             if ( index < pseudoList.numberOfFiles())
214             {
215                 PseudoFile pfile = pseudoList.getFileAt( index);
216                 if ( pfile != null)
217                 {
218                     // Get the file information for the pseudo file
219

220                     FileInfo pinfo = pfile.getFileInfo();
221                     
222                     // Copy the file information to the callers file info
223

224                     return pinfo.getFileName();
225                 }
226             }
227             else
228             {
229                 // Switch to the main file list
230

231                 donePseudoFiles = true;
232                 index = -1;
233                 
234                 if ( results == null || results.size() == 0)
235                     return null;
236             }
237         }
238
239         // Get the next file info from the node search
240

241         NodeRef nextNodeRef = results.get(index);
242         
243         try
244         {
245             // Get the file information and copy across to the callers file info
246

247             FileInfo nextInfo = cifsHelper.getFileInformation(nextNodeRef, "");
248             
249             // Indicate that the file information is valid
250

251             return nextInfo.getFileName();
252         }
253         catch (FileNotFoundException JavaDoc e)
254         {
255         }
256         
257         // No more files
258

259         return null;
260     }
261
262     /**
263      * Restart a search at the specified resume point.
264      *
265      * @param resumeId Resume point id.
266      * @return true if the search can be restarted, else false.
267      */

268     public boolean restartAt(FileInfo info)
269     {
270         // Check if the resume point is in the pseudo file list
271

272         int resId = 0;
273         
274         if (pseudoList != null)
275         {
276             while ( resId < pseudoList.numberOfFiles())
277             {
278                 // Check if the current pseudo file matches the resume file name
279

280                 PseudoFile pfile = pseudoList.getFileAt(resId);
281                 if ( pfile.getFileName().equals(info.getFileName()))
282                 {
283                     // Found the restart point
284

285                     donePseudoFiles = false;
286                     index = resId - 1;
287                     
288                     return true;
289                 }
290                 else
291                     resId++;
292             }
293         }
294         
295         // Check if the resume file is in the main file list
296

297         if ( results != null)
298         {
299             int idx = 0;
300             
301             while ( idx < results.size())
302             {
303                 // Get the file name for the node
304

305                 String JavaDoc fname = cifsHelper.getFileName( results.get( idx));
306                 if ( fname != null && fname.equals( info.getFileName()))
307                 {
308                     index = idx - 1;
309                     resumeId = resId - 1;
310                     donePseudoFiles = true;
311                     
312                     return true;
313                 }
314                 else
315                 {
316                     idx++;
317                     resId++;
318                 }
319             }
320         }
321         
322         // Failed to find resume file
323

324         return false;
325     }
326
327     /**
328      * Restart the current search at the specified file.
329      *
330      * @param info File to restart the search at.
331      * @return true if the search can be restarted, else false.
332      */

333     public boolean restartAt(int resumeId)
334     {
335         // Check if the resume point is in the pseudo file list
336

337         if (pseudoList != null)
338         {
339             if ( resumeId < pseudoList.numberOfFiles())
340             {
341                 // Resume at a pseudo file
342

343                 index = resumeId;
344                 donePseudoFiles = false;
345                 
346                 return true;
347             }
348             else
349             {
350                 // Adjust the resume id so that it is an index into the main file list
351

352                 resumeId -= pseudoList.numberOfFiles();
353             }
354         }
355         
356         // Check if the resume point is valid
357

358         if ( results != null && resumeId < results.size())
359         {
360             index = resumeId;
361             donePseudoFiles = true;
362             
363             return true;
364         }
365         
366         // Invalid resume point
367

368         return false;
369     }
370 }
371
Popular Tags