KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > BaseSearchResultsMap


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.repo.template;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.error.AlfrescoRuntimeException;
24 import org.alfresco.service.ServiceRegistry;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.cmr.repository.TemplateNode;
27 import org.alfresco.service.cmr.search.ResultSet;
28 import org.alfresco.service.cmr.search.ResultSetRow;
29 import org.alfresco.service.cmr.search.SearchService;
30
31 /**
32  * Class providing the base Search Query services to execute a search returning a list of
33  * TemplateNode objects from a Lucene search string.
34  *
35  * @author Kevin Roast
36  */

37 public abstract class BaseSearchResultsMap extends BaseTemplateMap
38 {
39     /**
40      * Constructor
41      *
42      * @param parent The parent TemplateNode to execute searches from
43      * @param services The ServiceRegistry to use
44      */

45     public BaseSearchResultsMap(TemplateNode parent, ServiceRegistry services)
46     {
47         super(parent, services);
48     }
49
50     /**
51      * Perform a SearchService query with the given Lucene search string
52      */

53     protected List JavaDoc<TemplateNode> query(String JavaDoc search)
54     {
55         List JavaDoc<TemplateNode> nodes = null;
56         
57         // check if a full Lucene search string has been supplied or extracted from XML
58
if (search != null && search.length() != 0)
59         {
60             // perform the search against the repo
61
ResultSet results = null;
62             try
63             {
64                 results = this.services.getSearchService().query(
65                         this.parent.getNodeRef().getStoreRef(),
66                         SearchService.LANGUAGE_LUCENE,
67                         search);
68                 
69                 if (results.length() != 0)
70                 {
71                     nodes = new ArrayList JavaDoc<TemplateNode>(results.length());
72                     for (ResultSetRow row: results)
73                     {
74                         NodeRef nodeRef = row.getNodeRef();
75                         nodes.add(new TemplateNode(nodeRef, services, this.parent.getImageResolver()));
76                     }
77                 }
78             }
79             catch (Throwable JavaDoc err)
80             {
81                 throw new AlfrescoRuntimeException("Failed to execute search: " + search, err);
82             }
83             finally
84             {
85                 if (results != null)
86                 {
87                     results.close();
88                 }
89             }
90         }
91         
92         return nodes != null ? nodes : (List JavaDoc)Collections.emptyList();
93     }
94 }
95
Popular Tags