| 1 | |
package org.paneris.bibliomania.fti; |
| 2 | |
|
| 3 | |
import java.util.Enumeration; |
| 4 | |
import java.util.Vector; |
| 5 | |
|
| 6 | |
import org.melati.util.PagedEnumeration; |
| 7 | |
|
| 8 | |
import com.sleepycat.db.DatabaseException; |
| 9 | |
|
| 10 | |
public class HitTextsEnumeration implements PagedEnumeration { |
| 11 | |
private ContextSearchResults results; |
| 12 | |
private int pageStart, pageSize, requestedPageSize; |
| 13 | |
private boolean totalCountIsMinimum; |
| 14 | |
private Enumeration us; |
| 15 | |
|
| 16 | |
public HitTextsEnumeration( |
| 17 | |
ContextSearchResults resultsP, |
| 18 | |
long startTextID, |
| 19 | |
long limitTextID, |
| 20 | |
int pageStart, |
| 21 | |
int requestedPageSize, |
| 22 | |
int hitsPerText) |
| 23 | 0 | throws FTIException { |
| 24 | |
try { |
| 25 | 0 | this.results = resultsP; |
| 26 | 0 | this.pageStart = pageStart = Math.max(pageStart, 1); |
| 27 | 0 | this.requestedPageSize = requestedPageSize; |
| 28 | |
|
| 29 | 0 | results.gotoText(startTextID); |
| 30 | |
|
| 31 | 0 | int areaCount = 0, areasInThisText = 0; |
| 32 | |
while (areaCount + 1 < pageStart |
| 33 | |
&& results.currentTextID() != -1 |
| 34 | 0 | && results.currentTextID() < limitTextID) { |
| 35 | 0 | if (results.nextArea() == null || areasInThisText >= hitsPerText) { |
| 36 | 0 | if (results.currentTextID() != -1) |
| 37 | 0 | results.gotoText(results.currentTextID() + 1); |
| 38 | 0 | areasInThisText = 0; |
| 39 | |
} else { |
| 40 | 0 | ++areaCount; |
| 41 | 0 | ++areasInThisText; |
| 42 | |
} |
| 43 | |
} |
| 44 | |
|
| 45 | 0 | Vector texts = new Vector(); |
| 46 | |
int a; |
| 47 | 0 | for (a = 0; |
| 48 | |
a < requestedPageSize |
| 49 | |
&& results.currentTextID() != -1 |
| 50 | 0 | && results.currentTextID() < limitTextID; |
| 51 | |
) { |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | if (results.currentText() != null) { |
| 57 | 0 | HitText text = |
| 58 | |
new HitText( |
| 59 | |
results, |
| 60 | |
Math.min(requestedPageSize - a, hitsPerText - areasInThisText)); |
| 61 | 0 | areasInThisText = 0; |
| 62 | 0 | texts.addElement(text); |
| 63 | 0 | a += text.areas; |
| 64 | 0 | if (results.currentTextID() != -1) |
| 65 | 0 | results.gotoText(results.currentTextID() + 1); |
| 66 | 0 | } |
| 67 | |
} |
| 68 | |
|
| 69 | 0 | totalCountIsMinimum = a == requestedPageSize; |
| 70 | 0 | pageSize = a; |
| 71 | |
|
| 72 | 0 | us = texts.elements(); |
| 73 | 0 | } catch (DatabaseException e) { |
| 74 | 0 | throw new FTIException(e); |
| 75 | 0 | } |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public boolean hasMoreElements() { |
| 85 | 0 | return us.hasMoreElements(); |
| 86 | |
} |
| 87 | |
|
| 88 | |
public Object nextElement() { |
| 89 | 0 | return us.nextElement(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public int getPageStart() { |
| 99 | 0 | return pageStart; |
| 100 | |
} |
| 101 | |
|
| 102 | |
public int getPageSize() { |
| 103 | 0 | return pageSize; |
| 104 | |
} |
| 105 | |
|
| 106 | |
public int getPageEnd() { |
| 107 | 0 | return pageStart + pageSize - 1; |
| 108 | |
} |
| 109 | |
|
| 110 | |
public int getTotalCount() { |
| 111 | 0 | return getPageEnd(); |
| 112 | |
} |
| 113 | |
|
| 114 | |
public boolean getTotalCountIsMinimum() { |
| 115 | 0 | return totalCountIsMinimum; |
| 116 | |
} |
| 117 | |
|
| 118 | |
public Integer getPrevPageStart() { |
| 119 | 0 | int it = pageStart - requestedPageSize; |
| 120 | 0 | return it < 0 ? null : new Integer(it); |
| 121 | |
} |
| 122 | |
|
| 123 | |
public Integer getNextPageStart() { |
| 124 | 0 | return totalCountIsMinimum |
| 125 | |
? new Integer(pageStart + requestedPageSize) |
| 126 | |
: null; |
| 127 | |
} |
| 128 | |
|
| 129 | |
public int getCurrentPosition() { |
| 130 | 0 | throw new RuntimeException("TODO No one else has ever called this method." + |
| 131 | |
" Do you really want to start now?"); |
| 132 | |
|
| 133 | |
} |
| 134 | |
|
| 135 | |
public int getNextPosition() { |
| 136 | 0 | throw new RuntimeException("TODO No one else has ever called this method." + |
| 137 | |
" Do you really want to start now?"); |
| 138 | |
|
| 139 | |
} |
| 140 | |
|
| 141 | |
public Vector getPages() { |
| 142 | 0 | throw new RuntimeException("TODO No one else has ever called this method." + |
| 143 | |
" Do you really want to start now?"); |
| 144 | |
|
| 145 | |
} |
| 146 | |
|
| 147 | |
public boolean nextElementOnThisPage() { |
| 148 | 0 | throw new RuntimeException("TODO No one else has ever called this method." + |
| 149 | |
" Do you really want to start now?"); |
| 150 | |
|
| 151 | |
} |
| 152 | |
} |