001: package com.openedit.hittracker;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.util.ArrayList;
006: import java.util.Collections;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: public abstract class HitTracker implements Serializable {
011: protected int fieldPage = 1;
012:
013: protected int fieldHitsPerPage = 12;
014:
015: protected int fieldCurrentHit;
016:
017: // protected String fieldQuery; //What we searched for
018: // protected String fieldUserQuery; //what the user thinks they searched for
019: // protected String fieldFriendlyQuery; //A summary of the users search in
020: // english
021: protected SearchQuery fieldSearchQuery;
022:
023: protected boolean fieldUseRandom;
024:
025: protected List fieldRandomLookup;
026:
027: protected String fieldIndexId;
028:
029: protected int fieldMaxPageListing = 10;
030:
031: public HitTracker() {
032:
033: }
034:
035: public List getPartOfPageOfHits(int inStart, int inEnd)
036: throws Exception {
037: List page = new ArrayList();
038: int count = (getPage() - 1) * getHitsPerPage(); // this is the start of
039: // the count
040: count = count + inStart; // tack on the offset
041: for (int i = inStart; i < getHitsPerPage() && i < inEnd; i++) {
042: if (count < getTotal()) {
043: page.add(get(count));
044: count++;
045: }
046: }
047: return page;
048: }
049:
050: public abstract Object get(int count) throws IOException;
051:
052: public List getPageOfHits() throws IOException {
053: int inHitsPerPage = getHitsPerPage(); // Not needed
054:
055: List page = new ArrayList(inHitsPerPage);
056: int count = (getPage() - 1) * inHitsPerPage; // pick up from here
057: for (int i = 0; i < inHitsPerPage; i++) {
058: if (count < getTotal()) {
059: page.add(get(count));
060: count++;
061: } else {
062: break;
063: }
064: }
065: return page;
066: }
067:
068: public List getPageInRows(int inColCount) throws Exception {
069: return getPageInRows(getHitsPerPage(), inColCount);
070: }
071:
072: public List getPageInRows(int inHitsPerPage, int inColCount)
073: throws Exception {
074: // first fill up a page of results from lucene
075: setHitsPerPage(inHitsPerPage);
076: List page = getPageOfHits();
077:
078: // Now break up the page into rows by dividing the count they wanted
079: double rowscount = (double) page.size() / (double) inColCount;
080:
081: List rows = new ArrayList();
082: for (int i = 0; i < rowscount; i++) {
083: int start = i * inColCount;
084: int end = i * inColCount + inColCount;
085: List sublist = page.subList(start, Math.min(page.size(),
086: end));
087: rows.add(sublist);
088: }
089: return rows;
090: }
091:
092: public int getHitsPerPage() {
093: return fieldHitsPerPage;
094: }
095:
096: public void setHitsPerPage(int inHitsPerPage) {
097: if (fieldHitsPerPage != inHitsPerPage) {
098: if (getPage() * inHitsPerPage > getTotal()) {
099: setPage(1);
100: }
101: }
102: fieldHitsPerPage = inHitsPerPage;
103: }
104:
105: public int getPage() {
106: return fieldPage;
107: }
108:
109: public void setPage(int inPage) {
110: fieldPage = inPage;
111: }
112:
113: // Use the other API
114: public abstract Iterator getAllHits();
115:
116: public Iterator iterator() {
117: return getAllHits();
118: }
119:
120: public int getCurrentHit() {
121: return fieldCurrentHit;
122: }
123:
124: public void setCurrentHit(int inCurrentHit) {
125: fieldCurrentHit = inCurrentHit;
126: }
127:
128: public abstract boolean contains(Object inHit);
129:
130: public abstract int getTotal();
131:
132: public int size() {
133: return getTotal();
134: }
135:
136: public int getSize() {
137: return getTotal();
138: }
139:
140: public int getTotalPages() {
141: double pages = (double) getTotal() / (double) getHitsPerPage();
142: if (pages % 1 > 0) {
143: pages++;
144: }
145: return (int) pages;
146: }
147:
148: public Integer nextPage() {
149: int page = getPage() + 1;
150: if (page > getTotalPages()) {
151: return null;
152: } else {
153: return new Integer(page);
154: }
155: }
156:
157: public Integer prevPage() {
158: int page = getPage() - 1;
159: if (page < 1) {
160: return null;
161: } else {
162: return new Integer(page);
163: }
164: }
165:
166: public Integer getPageStart() {
167: if (getTotal() == 0) {
168: return null;
169: }
170: int start = (getPage() - 1) * getHitsPerPage();
171: return new Integer(start + 1);
172: }
173:
174: public Integer getPageEnd() {
175: if (getTotal() == 0) {
176: return null;
177: }
178: int start = getPage() * getHitsPerPage();
179: if (start > getTotal()) {
180: return new Integer(getTotal());
181: }
182: return new Integer(start);
183:
184: }
185:
186: public SearchQuery getSearchQuery() {
187: return fieldSearchQuery;
188: }
189:
190: public String getQuery() {
191: if (getSearchQuery() != null) {
192: return getSearchQuery().toQuery();
193: }
194: return null;
195: }
196:
197: // public void setQuery(String inQuery)
198: // {
199: // fieldQuery = inQuery;
200: // }
201:
202: public List linkRange() {
203: int totalPages = getTotalPages();
204: int page = getPage();
205: int start = 1;
206:
207: if (page < getMaxPageListing() / 2) // under the first 5 records
208: {
209: start = 1;// - getMaxPageListing()/2;
210: } else if (page + getMaxPageListing() / 2 + 1 >= totalPages) // near
211: // the
212: // end +
213: // 1 for
214: // the
215: // selected
216: // one
217: {
218: start = 1 + totalPages - getMaxPageListing(); // Make it start 10
219: // from the end
220: start = Math.max(1, start); // dont go below 1
221: } else {
222: start = 1 + page - getMaxPageListing() / 2;
223: }
224:
225: int count = Math.min(totalPages, getMaxPageListing()); // what is
226: // higher the
227: // total count
228: // or 10
229: List hits = new ArrayList(count);
230: for (int i = 0; i < count; i++) {
231: hits.add(new Integer(start + i));
232: }
233: return hits;
234: }
235:
236: public List linksBefore() {
237: List range = linkRange();
238: int i = 0;
239: for (; i < range.size(); i++) {
240: Integer in = (Integer) range.get(i);
241: if (in.intValue() >= getPage()) {
242: break;
243: }
244: }
245:
246: return range.subList(0, i);
247:
248: // int totalPages = getTotalPages();
249: //
250: // int max = Math.min(totalPages, getMaxPageListing()); //what is higher
251: // the total count or 10
252: // int center = max/2;
253: // int start = getPage() - center;
254: // start = Math.max(1, start);
255: // List hits = new ArrayList(max);
256: // //are we near the end?
257: // for (int i = start; i < getPage(); i++)
258: // {
259: // hits.add( new Integer(i));
260: // }
261: // return hits;
262: }
263:
264: public List linksAfter() {
265: if (getTotalPages() == getPage()) {
266: return Collections.EMPTY_LIST;
267: }
268: List range = linkRange();
269: if (range.size() == 1) // Only one hit
270: {
271: return Collections.EMPTY_LIST;
272: }
273: int start = 0;
274: for (int i = 0; i < range.size(); i++) {
275: Integer in = (Integer) range.get(i);
276: if (in.intValue() > getPage()) {
277: start = i;
278: break;
279: }
280: }
281: return range.subList(start, range.size());
282:
283: // int max = Math.min(getTotalPages(), getMaxPageListing()); //what is
284: // higher the total count or 10
285: // List links = linksBefore();
286: // int start = 2; //Min of the selected page
287: // if( links.size() > 0)
288: // {
289: // start = getPage() + 1;
290: // }
291: //
292: // int pagesleft = getMaxPageListing() - links.size() - 1;
293: // int end = Math.min(getTotalPages(),start + pagesleft);
294: // //int count = max - before + 1;
295: // List hits = new ArrayList();
296: // //are we near the end?
297: // for (int i = start; i < end; i++)
298: // {
299: // hits.add( new Integer(i));
300: // }
301: // return hits;
302: }
303:
304: // public boolean isUseRandom()
305: // {
306: // return fieldUseRandom;
307: // }
308: // public void setUseRandom(boolean inUseRandom)
309: // {
310: // fieldUseRandom = inUseRandom;
311: // }
312: // public List getRandomLookup()
313: // {
314: // if (fieldRandomLookup == null)
315: // {
316: // fieldRandomLookup = new ArrayList(getTotal());
317: // for (int i = 0; i < getTotal(); i++)
318: // {
319: // fieldRandomLookup.add( new Integer(i));
320: // }
321: // Collections.shuffle(fieldRandomLookup );
322: // }
323: // return fieldRandomLookup;
324: // }
325: // public void setRandomLookup(List inRandomLookup)
326: // {
327: // fieldRandomLookup = inRandomLookup;
328: // }
329: /**
330: * @deprecated Use getSearchQuery().getInput("nameof field")
331: */
332: public String getUserQuery() {
333: return getSearchQuery().getInput("description");
334: }
335:
336: public String getInput(String inKey) {
337: SearchQuery query = getSearchQuery();
338: if (query != null) {
339: return query.getInput(inKey);
340: }
341: return null;
342: }
343:
344: public boolean wasInput(String inKey, String inValue) {
345: SearchQuery query = getSearchQuery();
346:
347: if (query != null && inValue != null && inKey != null) {
348: String[] inputs = query.getInputs(inKey);
349: if (inputs != null) {
350: for (int i = 0; i < inputs.length; i++) {
351: if (inputs[i] != null) {
352: if (inputs[i].equals(inValue)) {
353: return true;
354: }
355: }
356: }
357: }
358: }
359:
360: return false;
361: }
362:
363: public String getOrdering() {
364: if (getSearchQuery() == null) {
365: return null;
366: }
367: return getSearchQuery().getSortBy();
368: }
369:
370: public String getIndexId() {
371: return fieldIndexId;
372: }
373:
374: public void setIndexId(String inIndexCounter) {
375: fieldIndexId = inIndexCounter;
376: }
377:
378: public int getMaxPageListing() {
379: return fieldMaxPageListing;
380: }
381:
382: public void setMaxPageListing(int inMaxPageListing) {
383: fieldMaxPageListing = inMaxPageListing;
384: }
385:
386: public String getFriendlyQuery() {
387: SearchQuery query = getSearchQuery();
388: if (query != null) {
389: return getSearchQuery().toFriendly();
390: }
391: return null;
392: }
393:
394: public void setSearchQuery(SearchQuery inQuery) {
395:
396: fieldSearchQuery = inQuery;
397: }
398:
399: public String highlight(Object inDoc, String inField) {
400: return "Not implemented";
401: }
402:
403: public String getValue(Object inHit, String inString) {
404: return "Not implemented";
405: }
406:
407: }
|