KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > LuceneResultSet


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.search.impl.lucene;
18
19 import java.io.IOException JavaDoc;
20
21 import org.alfresco.repo.search.AbstractResultSet;
22 import org.alfresco.repo.search.ResultSetRowIterator;
23 import org.alfresco.repo.search.SearcherException;
24 import org.alfresco.repo.search.SimpleResultSetMetaData;
25 import org.alfresco.service.cmr.repository.ChildAssociationRef;
26 import org.alfresco.service.cmr.repository.NodeRef;
27 import org.alfresco.service.cmr.repository.NodeService;
28 import org.alfresco.service.cmr.repository.Path;
29 import org.alfresco.service.cmr.search.LimitBy;
30 import org.alfresco.service.cmr.search.PermissionEvaluationMode;
31 import org.alfresco.service.cmr.search.ResultSetMetaData;
32 import org.alfresco.service.cmr.search.ResultSetRow;
33 import org.alfresco.service.cmr.search.SearchParameters;
34 import org.apache.lucene.document.Document;
35 import org.apache.lucene.search.Hits;
36 import org.apache.lucene.search.Searcher;
37
38 /**
39  * Implementation of a ResultSet on top of Lucene Hits class.
40  *
41  * @author andyh
42  *
43  */

44 public class LuceneResultSet extends AbstractResultSet
45 {
46     /**
47      * The underlying hits
48      */

49     Hits hits;
50
51     private Searcher searcher;
52     
53     private NodeService nodeService;
54
55     SearchParameters searchParameters;
56     
57     /**
58      * Wrap a lucene seach result with node support
59      *
60      * @param storeRef
61      * @param hits
62      */

63     public LuceneResultSet(Hits hits, Searcher searcher, NodeService nodeService, Path[]propertyPaths, SearchParameters searchParameters)
64     {
65         super(propertyPaths);
66         this.hits = hits;
67         this.searcher = searcher;
68         this.nodeService = nodeService;
69         this.searchParameters = searchParameters;
70     }
71
72     /*
73      * ResultSet implementation
74      */

75
76     public ResultSetRowIterator iterator()
77     {
78         return new LuceneResultSetRowIterator(this);
79     }
80
81     public int length()
82     {
83         return hits.length();
84     }
85
86     public NodeRef getNodeRef(int n)
87     {
88         try
89         {
90             // We have to get the document to resolve this
91
// It is possible the store ref is also stored in the index
92
Document doc = hits.doc(n);
93             String JavaDoc id = doc.get("ID");
94             return new NodeRef(id);
95         }
96         catch (IOException JavaDoc e)
97         {
98             throw new SearcherException("IO Error reading reading node ref from the result set", e);
99         }
100     }
101
102     public float getScore(int n) throws SearcherException
103     {
104         try
105         {
106             return hits.score(n);
107         }
108         catch (IOException JavaDoc e)
109         {
110             throw new SearcherException("IO Error reading score from the result set", e);
111         }
112     }
113
114     public Document getDocument(int n)
115     {
116         try
117         {
118             Document doc = hits.doc(n);
119             return doc;
120         }
121         catch (IOException JavaDoc e)
122         {
123             throw new SearcherException("IO Error reading reading document from the result set", e);
124         }
125     }
126
127     public void close()
128     {
129         try
130         {
131             searcher.close();
132         }
133         catch (IOException JavaDoc e)
134         {
135             throw new SearcherException(e);
136         }
137     }
138
139     public NodeService getNodeService()
140     {
141         return nodeService;
142     }
143
144     public ResultSetRow getRow(int i)
145     {
146         if(i < length())
147         {
148            return new LuceneResultSetRow(this, i);
149         }
150         else
151         {
152             throw new SearcherException("Invalid row");
153         }
154     }
155
156     public ChildAssociationRef getChildAssocRef(int n)
157     {
158        return getRow(n).getChildAssocRef();
159     }
160
161     
162     public ResultSetMetaData getResultSetMetaData()
163     {
164         return new SimpleResultSetMetaData(LimitBy.UNLIMITED, PermissionEvaluationMode.EAGER, searchParameters);
165     }
166 }
167
Popular Tags