KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > hittracker > HitTracker


1 package com.openedit.hittracker;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 public abstract class HitTracker implements Serializable JavaDoc
11 {
12     protected int fieldPage = 1;
13     protected int fieldHitsPerPage = 12;
14     protected int fieldCurrentHit;
15 // protected String fieldQuery; //What we searched for
16
// protected String fieldUserQuery; //what the user thinks they searched for
17
// protected String fieldFriendlyQuery; //A summary of the users search in english
18
protected SearchQuery fieldSearchQuery;
19     
20     protected boolean fieldUseRandom;
21     protected List JavaDoc fieldRandomLookup;
22     protected String JavaDoc fieldOrdering;
23     protected String JavaDoc fieldIndexId;
24     protected int fieldMaxPageListing = 10;
25     
26     public HitTracker()
27     {
28         
29     }
30     
31     public List JavaDoc getPartOfPageOfHits(int inStart, int inEnd) throws Exception JavaDoc
32     {
33         List JavaDoc page = new ArrayList JavaDoc();
34         int count = ( getPage() -1 ) * getHitsPerPage(); //this is the start of the count
35
count = count + inStart; //tack on the offset
36
for (int i = inStart; i < getHitsPerPage() && i < inEnd; i++)
37         {
38             if ( count < getTotal())
39             {
40                 page.add(get(count));
41                 count++;
42             }
43         }
44         return page;
45     }
46     
47     public abstract Object JavaDoc get(int count) throws IOException JavaDoc;
48     
49     public List JavaDoc getPageOfHits() throws IOException JavaDoc
50     {
51         int inHitsPerPage = getHitsPerPage(); //Not needed
52

53         List JavaDoc page = new ArrayList JavaDoc(inHitsPerPage);
54         int count = ( getPage() -1 ) * inHitsPerPage; //pick up from here
55
for (int i = 0; i < inHitsPerPage; i++)
56         {
57             if ( count < getTotal())
58             {
59                 page.add(get(count));
60                 count++;
61             }
62             else
63             {
64                 break;
65             }
66         }
67         return page;
68     }
69     public List JavaDoc getPageInRows(int inColCount) throws Exception JavaDoc
70     {
71         return getPageInRows(getHitsPerPage(), inColCount);
72     }
73     
74     public List JavaDoc getPageInRows(int inHitsPerPage, int inColCount) throws Exception JavaDoc
75     {
76         // first fill up a page of results from lucene
77
setHitsPerPage(inHitsPerPage);
78         List JavaDoc page = getPageOfHits();
79         
80         //Now break up the page into rows by dividing the count they wanted
81
double rowscount = (double)page.size() / (double)inColCount;
82         
83         List JavaDoc rows = new ArrayList JavaDoc();
84         for (int i = 0; i < rowscount; i++)
85         {
86             int start = i*inColCount;
87             int end = i*inColCount + inColCount;
88             List JavaDoc sublist = page.subList(start,Math.min( page.size(),end ));
89             rows.add(sublist);
90         }
91         return rows;
92     }
93
94     public int getHitsPerPage()
95     {
96         return fieldHitsPerPage;
97     }
98     public void setHitsPerPage(int inHitsPerPage)
99     {
100         if( fieldHitsPerPage != inHitsPerPage)
101         {
102             if( getPage() * inHitsPerPage > getTotal())
103             {
104                 setPage(1);
105             }
106         }
107         fieldHitsPerPage = inHitsPerPage;
108     }
109     public int getPage()
110     {
111         return fieldPage;
112     }
113     public void setPage(int inPage)
114     {
115         fieldPage = inPage;
116     }
117     //Use the other API
118
public abstract Iterator JavaDoc getAllHits();
119
120     public Iterator JavaDoc iterator()
121     {
122         return getAllHits();
123     }
124     public int getCurrentHit()
125     {
126         return fieldCurrentHit;
127     }
128     public void setCurrentHit(int inCurrentHit)
129     {
130         fieldCurrentHit = inCurrentHit;
131     }
132     public abstract boolean contains( Object JavaDoc inHit);
133     
134     public abstract int getTotal();
135
136
137     public int size()
138     {
139         return getTotal();
140     }
141
142     public int getSize()
143     {
144         return getTotal();
145     }
146     public int getTotalPages()
147     {
148         double pages = (double)getTotal() / (double)getHitsPerPage();
149         if ( pages % 1 > 0)
150         {
151             pages++;
152         }
153         return (int)pages;
154     }
155     public Integer JavaDoc nextPage()
156     {
157         int page = getPage() + 1;
158         if ( page > getTotalPages())
159         {
160             return null;
161         }
162         else
163         {
164             return new Integer JavaDoc( page);
165         }
166     }
167     public Integer JavaDoc prevPage()
168     {
169         int page = getPage() -1;
170         if ( page < 1)
171         {
172             return null;
173         }
174         else
175         {
176             return new Integer JavaDoc( page);
177         }
178     }
179     public Integer JavaDoc getPageStart()
180     {
181         if( getTotal() == 0)
182         {
183             return null;
184         }
185         int start = (getPage()-1) * getHitsPerPage();
186         return new Integer JavaDoc(start+1);
187     }
188     public Integer JavaDoc getPageEnd()
189     {
190         if( getTotal() == 0)
191         {
192             return null;
193         }
194         int start = getPage() * getHitsPerPage();
195         if( start > getTotal())
196         {
197             return new Integer JavaDoc( getTotal() );
198         }
199         return new Integer JavaDoc(start);
200         
201     }
202     public SearchQuery getSearchQuery()
203     {
204         return fieldSearchQuery;
205     }
206     public String JavaDoc getQuery()
207     {
208         return getSearchQuery().toQuery();
209     }
210 // public void setQuery(String inQuery)
211
// {
212
// fieldQuery = inQuery;
213
// }
214

215     public List JavaDoc linkRange()
216     {
217         int totalPages = getTotalPages();
218         int page = getPage();
219         int start = 1;
220         
221         if( page < getMaxPageListing()/2) //under the first 5 records
222
{
223             start = 1;// - getMaxPageListing()/2;
224
}
225         else if( page + getMaxPageListing()/2 + 1 >= totalPages) //near the end + 1 for the selected one
226
{
227             start = 1+ totalPages - getMaxPageListing(); //Make it start 10 from the end
228
start = Math.max(1, start); //dont go below 1
229
}
230         else
231         {
232             start = 1 + page - getMaxPageListing()/2;
233         }
234         
235         int count = Math.min(totalPages, getMaxPageListing()); //what is higher the total count or 10
236
List JavaDoc hits = new ArrayList JavaDoc(count);
237         for (int i = 0; i < count; i++)
238         {
239             hits.add( new Integer JavaDoc(start+i));
240         }
241         return hits;
242     }
243     
244     public List JavaDoc linksBefore()
245     {
246         List JavaDoc range = linkRange();
247         int i = 0;
248         for (; i < range.size(); i++)
249         {
250             Integer JavaDoc in = (Integer JavaDoc)range.get(i);
251             if( in.intValue() >= getPage())
252             {
253                 break;
254             }
255         }
256         return range.subList(0,i);
257         
258 // int totalPages = getTotalPages();
259
//
260
// int max = Math.min(totalPages, getMaxPageListing()); //what is higher the total count or 10
261
// int center = max/2;
262
// int start = getPage() - center;
263
// start = Math.max(1, start);
264
// List hits = new ArrayList(max);
265
// //are we near the end?
266
// for (int i = start; i < getPage(); i++)
267
// {
268
// hits.add( new Integer(i));
269
// }
270
// return hits;
271
}
272     public List JavaDoc linksAfter()
273     {
274         if( getTotalPages() == getPage() )
275         {
276             return Collections.EMPTY_LIST;
277         }
278         List JavaDoc range = linkRange();
279         if( range.size() == 1) //Only one hit
280
{
281             return Collections.EMPTY_LIST;
282         }
283         int start = 0;
284         for (int i=0; i < range.size(); i++)
285         {
286             Integer JavaDoc in = (Integer JavaDoc)range.get(i);
287             if( in.intValue() > getPage())
288             {
289                 start = i;
290                 break;
291             }
292         }
293         return range.subList(start,range.size());
294
295         // int max = Math.min(getTotalPages(), getMaxPageListing()); //what is higher the total count or 10
296
// List links = linksBefore();
297
// int start = 2; //Min of the selected page
298
// if( links.size() > 0)
299
// {
300
// start = getPage() + 1;
301
// }
302
//
303
// int pagesleft = getMaxPageListing() - links.size() - 1;
304
// int end = Math.min(getTotalPages(),start + pagesleft);
305
// //int count = max - before + 1;
306
// List hits = new ArrayList();
307
// //are we near the end?
308
// for (int i = start; i < end; i++)
309
// {
310
// hits.add( new Integer(i));
311
// }
312
// return hits;
313
}
314
315 // public boolean isUseRandom()
316
// {
317
// return fieldUseRandom;
318
// }
319
// public void setUseRandom(boolean inUseRandom)
320
// {
321
// fieldUseRandom = inUseRandom;
322
// }
323
// public List getRandomLookup()
324
// {
325
// if (fieldRandomLookup == null)
326
// {
327
// fieldRandomLookup = new ArrayList(getTotal());
328
// for (int i = 0; i < getTotal(); i++)
329
// {
330
// fieldRandomLookup.add( new Integer(i));
331
// }
332
// Collections.shuffle(fieldRandomLookup );
333
// }
334
// return fieldRandomLookup;
335
// }
336
// public void setRandomLookup(List inRandomLookup)
337
// {
338
// fieldRandomLookup = inRandomLookup;
339
// }
340
/**
341      * @deprecated Use getSearchQuery().getInput("nameof field")
342      */

343     public String JavaDoc getUserQuery()
344     {
345         return getSearchQuery().getInput("description");
346     }
347     public String JavaDoc getInput( String JavaDoc inKey)
348     {
349         return getSearchQuery().getInput(inKey );
350     }
351     public String JavaDoc getOrdering()
352     {
353         return getSearchQuery().getSortBy();
354     }
355     public String JavaDoc getIndexId()
356     {
357         return fieldIndexId;
358     }
359     public void setIndexId(String JavaDoc inIndexCounter)
360     {
361         fieldIndexId = inIndexCounter;
362     }
363
364     public int getMaxPageListing()
365     {
366         return fieldMaxPageListing;
367     }
368
369     public void setMaxPageListing(int inMaxPageListing)
370     {
371         fieldMaxPageListing = inMaxPageListing;
372     }
373
374     public String JavaDoc getFriendlyQuery()
375     {
376         return getSearchQuery().toFriendly();
377     }
378     public void setSearchQuery(SearchQuery inQuery)
379     {
380         
381         fieldSearchQuery = inQuery;
382     }
383
384 }
385
Popular Tags