1 16 package org.apache.cocoon.components.search; 17 18 import org.apache.lucene.document.Document; 19 import org.apache.lucene.search.Hits; 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.ListIterator ; 24 import java.util.NoSuchElementException ; 25 26 32 public class LuceneCocoonPager implements ListIterator 33 { 34 37 public final static int COUNT_OF_HITS_PER_PAGE_DEFAULT = 5; 38 39 42 public final static int HITS_INDEX_START_DEFAULT = 0; 43 44 47 int hitsIndex = HITS_INDEX_START_DEFAULT; 48 49 52 int countOfHitsPerPage = COUNT_OF_HITS_PER_PAGE_DEFAULT; 53 54 57 private Hits hits; 58 59 60 63 public LuceneCocoonPager(Hits hits) { 64 setHits(hits); 65 } 66 67 68 71 public LuceneCocoonPager() { 72 } 73 74 75 80 public void setHits(Hits hits) { 81 this.hits = hits; 82 this.hitsIndex = HITS_INDEX_START_DEFAULT; 83 } 84 85 86 91 public void setCountOfHitsPerPage(int countOfHitsPerPage) { 92 this.countOfHitsPerPage = countOfHitsPerPage; 93 if (this.countOfHitsPerPage <= 0) { 94 this.countOfHitsPerPage = 1; 95 } 96 } 97 98 99 104 public void setStartIndex(int start_index) { 105 this.hitsIndex = start_index; 106 } 107 108 109 115 public void set(Object o) { 116 throw new UnsupportedOperationException (); 117 } 118 119 120 125 public int getCountOfHits() { 126 return hits.length(); 127 } 128 129 134 public int getCountOfHitsPerPage() { 135 return this.countOfHitsPerPage; 136 } 137 138 143 public int getCountOfPages() { 144 int count_of_pages = hits.length() / this.countOfHitsPerPage; 145 int remainder = hits.length() % this.countOfHitsPerPage; 146 if (remainder != 0) { 147 count_of_pages += 1; 148 } 149 return count_of_pages; 150 } 151 152 153 158 public int getStartIndex() { 159 return this.hitsIndex; 160 } 161 162 168 public void add(Object o) throws UnsupportedOperationException { 169 throw new UnsupportedOperationException (); 170 } 171 172 178 public boolean hasNext() { 179 return hitsIndex + countOfHitsPerPage < hits.length(); 180 } 181 182 188 public boolean hasPrevious() { 189 return hitsIndex >= countOfHitsPerPage; 190 } 191 192 197 public Object next() { 198 ArrayList hitsPerPageList = new ArrayList (); 199 int endIndex = Math.min(hits.length(), hitsIndex + countOfHitsPerPage); 200 int hits_copy = hitsIndex; 202 while (hits_copy < endIndex) { 203 try { 204 HitWrapper hit = new HitWrapper(hits.score(hits_copy), hits.doc(hits_copy)); 205 hitsPerPageList.add(hit); 206 } catch (IOException ioe) { 207 throw new NoSuchElementException ("no more hits: " + ioe.getMessage()); 208 } 209 hits_copy++; 210 } 211 return hitsPerPageList; 212 } 213 214 220 public int nextIndex() { 221 return Math.min(hitsIndex + countOfHitsPerPage, hits.length() - 1); 222 } 223 224 229 public Object previous() { 230 ArrayList hitsPerPageList = new ArrayList (); 231 232 int startIndex = Math.max(0, hitsIndex - countOfHitsPerPage); 233 int endIndex = Math.min(hits.length() - 1, hitsIndex - countOfHitsPerPage); 234 235 if (startIndex < endIndex) { 236 while (startIndex < endIndex) { 237 try { 238 HitWrapper hit = new HitWrapper(hits.score(startIndex), 239 hits.doc(startIndex)); 240 hitsPerPageList.add(hit); 241 } catch (IOException ioe) { 242 throw new NoSuchElementException ("no more hits: " + ioe.getMessage()); 243 } 244 startIndex++; 245 } 246 hitsIndex = endIndex; 247 } else { 248 throw new NoSuchElementException (); 249 } 250 return hitsPerPageList; 251 } 252 253 259 public int previousIndex() { 260 return Math.max(0, hitsIndex - countOfHitsPerPage); 261 } 262 263 267 public void remove() { 268 throw new UnsupportedOperationException (); 269 } 270 271 277 public static class HitWrapper { 278 float score; 279 Document document; 280 281 287 public HitWrapper(float score, Document document) { 288 this.document = document; 289 this.score = score; 290 } 291 292 297 public Document getDocument() { 298 return document; 299 } 300 301 306 public float getScore() { 307 return score; 308 } 309 310 316 public String getField(String field) { 317 return document.get(field); 318 } 319 } 320 } 321 | Popular Tags |