1 package com.openedit.hittracker; 2 3 import java.io.IOException ; 4 import java.io.Serializable ; 5 import java.util.ArrayList ; 6 import java.util.Collections ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 public abstract class HitTracker implements Serializable 11 { 12 protected int fieldPage = 1; 13 protected int fieldHitsPerPage = 12; 14 protected int fieldCurrentHit; 15 protected SearchQuery fieldSearchQuery; 19 20 protected boolean fieldUseRandom; 21 protected List fieldRandomLookup; 22 protected String fieldOrdering; 23 protected String fieldIndexId; 24 protected int fieldMaxPageListing = 10; 25 26 public HitTracker() 27 { 28 29 } 30 31 public List getPartOfPageOfHits(int inStart, int inEnd) throws Exception 32 { 33 List page = new ArrayList (); 34 int count = ( getPage() -1 ) * getHitsPerPage(); count = count + inStart; 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 get(int count) throws IOException ; 48 49 public List getPageOfHits() throws IOException 50 { 51 int inHitsPerPage = getHitsPerPage(); 53 List page = new ArrayList (inHitsPerPage); 54 int count = ( getPage() -1 ) * inHitsPerPage; 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 getPageInRows(int inColCount) throws Exception 70 { 71 return getPageInRows(getHitsPerPage(), inColCount); 72 } 73 74 public List getPageInRows(int inHitsPerPage, int inColCount) throws Exception 75 { 76 setHitsPerPage(inHitsPerPage); 78 List page = getPageOfHits(); 79 80 double rowscount = (double)page.size() / (double)inColCount; 82 83 List rows = new ArrayList (); 84 for (int i = 0; i < rowscount; i++) 85 { 86 int start = i*inColCount; 87 int end = i*inColCount + inColCount; 88 List 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 public abstract Iterator getAllHits(); 119 120 public Iterator 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 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 nextPage() 156 { 157 int page = getPage() + 1; 158 if ( page > getTotalPages()) 159 { 160 return null; 161 } 162 else 163 { 164 return new Integer ( page); 165 } 166 } 167 public Integer prevPage() 168 { 169 int page = getPage() -1; 170 if ( page < 1) 171 { 172 return null; 173 } 174 else 175 { 176 return new Integer ( page); 177 } 178 } 179 public Integer getPageStart() 180 { 181 if( getTotal() == 0) 182 { 183 return null; 184 } 185 int start = (getPage()-1) * getHitsPerPage(); 186 return new Integer (start+1); 187 } 188 public Integer 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 ( getTotal() ); 198 } 199 return new Integer (start); 200 201 } 202 public SearchQuery getSearchQuery() 203 { 204 return fieldSearchQuery; 205 } 206 public String getQuery() 207 { 208 return getSearchQuery().toQuery(); 209 } 210 215 public List linkRange() 216 { 217 int totalPages = getTotalPages(); 218 int page = getPage(); 219 int start = 1; 220 221 if( page < getMaxPageListing()/2) { 223 start = 1; } 225 else if( page + getMaxPageListing()/2 + 1 >= totalPages) { 227 start = 1+ totalPages - getMaxPageListing(); start = Math.max(1, start); } 230 else 231 { 232 start = 1 + page - getMaxPageListing()/2; 233 } 234 235 int count = Math.min(totalPages, getMaxPageListing()); List hits = new ArrayList (count); 237 for (int i = 0; i < count; i++) 238 { 239 hits.add( new Integer (start+i)); 240 } 241 return hits; 242 } 243 244 public List linksBefore() 245 { 246 List range = linkRange(); 247 int i = 0; 248 for (; i < range.size(); i++) 249 { 250 Integer in = (Integer )range.get(i); 251 if( in.intValue() >= getPage()) 252 { 253 break; 254 } 255 } 256 return range.subList(0,i); 257 258 } 272 public List linksAfter() 273 { 274 if( getTotalPages() == getPage() ) 275 { 276 return Collections.EMPTY_LIST; 277 } 278 List range = linkRange(); 279 if( range.size() == 1) { 281 return Collections.EMPTY_LIST; 282 } 283 int start = 0; 284 for (int i=0; i < range.size(); i++) 285 { 286 Integer in = (Integer )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 } 314 315 343 public String getUserQuery() 344 { 345 return getSearchQuery().getInput("description"); 346 } 347 public String getInput( String inKey) 348 { 349 return getSearchQuery().getInput(inKey ); 350 } 351 public String getOrdering() 352 { 353 return getSearchQuery().getSortBy(); 354 } 355 public String getIndexId() 356 { 357 return fieldIndexId; 358 } 359 public void setIndexId(String 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 getFriendlyQuery() 375 { 376 return getSearchQuery().toFriendly(); 377 } 378 public void setSearchQuery(SearchQuery inQuery) 379 { 380 381 fieldSearchQuery = inQuery; 382 } 383 384 } 385 | Popular Tags |