1 64 65 package com.jcorporate.expresso.core.misc; 66 67 import com.jcorporate.expresso.core.controller.ControllerException; 68 import com.jcorporate.expresso.core.controller.ControllerRequest; 69 import com.jcorporate.expresso.core.dataobjects.DataObject; 70 import com.jcorporate.expresso.core.db.DBException; 71 import com.jcorporate.expresso.core.dbobj.MultiDBObject; 72 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 73 import com.jcorporate.expresso.services.dbobj.DBObjLimit; 74 75 import java.io.IOException ; 76 import java.io.ObjectInputStream ; 77 import java.io.ObjectOutputStream ; 78 import java.io.Serializable ; 79 import java.util.ArrayList ; 80 import java.util.List ; 81 82 83 92 public class RecordPaginator 93 implements Serializable { 94 95 98 private boolean moreRecords; 99 100 103 private boolean previousRecords; 104 105 108 private int pageNumber = 1; 109 110 111 private int pageLimit; 112 113 116 private int startRecordNumber; 117 118 121 private int endRecordNumber; 122 123 127 private boolean countRecords; 128 129 132 private int totalRecordCount; 133 134 public RecordPaginator() { 135 } 136 137 private void readObject(ObjectInputStream ois) 141 throws ClassNotFoundException , IOException { 142 ois.defaultReadObject(); 143 } 144 145 private void writeObject(ObjectOutputStream oos) 146 throws IOException { 147 oos.defaultWriteObject(); 148 } 149 150 public void setMoreRecords(boolean newMoreRecords) { 155 moreRecords = newMoreRecords; 156 } 157 158 public boolean isMoreRecords() { 159 return moreRecords; 160 } 161 162 public void setPreviousRecords(boolean newPreviousRecords) { 163 previousRecords = newPreviousRecords; 164 } 165 166 public boolean isPreviousRecords() { 167 return previousRecords; 168 } 169 170 public void setPageNumber(int newPageNumber) { 171 pageNumber = newPageNumber; 172 } 173 174 public int getPageNumber() { 175 return pageNumber; 176 } 177 178 public void setStartRecordNumber(int newStartRecordNumber) { 179 startRecordNumber = newStartRecordNumber; 180 } 181 182 187 public int getStartRecordNumber() { 188 if (endRecordNumber > startRecordNumber) { 189 return startRecordNumber; 190 } else { 191 return endRecordNumber; 192 } 193 } 194 195 200 public void setEndRecordNumber(int newEndRecordNumber) { 201 endRecordNumber = newEndRecordNumber; 202 } 203 204 209 public int getEndRecordNumber() { 210 return endRecordNumber; 211 } 212 213 219 public void setCountRecords(boolean newCountRecords) { 220 countRecords = newCountRecords; 221 } 222 223 228 public boolean isCountRecords() { 229 return countRecords; 230 } 231 232 237 public long getTotalRecordCount() { 238 return totalRecordCount; 239 } 240 241 246 public int getPageCount() { 247 if (pageLimit == 0) { 248 return 1; 249 } else { 250 int pageCount = (int) (this.getTotalRecordCount() / pageLimit); 251 int mod = (int) (this.getTotalRecordCount() % pageLimit); 252 if (mod != 0 || pageCount == 0) { 253 pageCount++; 254 } 255 return pageCount; 256 } 257 } 258 259 260 272 public ArrayList searchAndRetrieve(DataObject searchCriteria, String sortKey) 273 throws DBException { 274 if (isCountRecords()) { 275 totalRecordCount = searchCriteria.count(); 276 } 277 if (getPageNumber() > 1) { 278 setPreviousRecords(true); 279 } 280 281 Integer pageLimitObj = (Integer ) searchCriteria.getAttribute("pageLimit"); 282 283 if (pageLimitObj == null) { 284 this.setPageLimitAttribute(searchCriteria); 285 pageLimitObj = (Integer ) searchCriteria.getAttribute("pageLimit"); 286 } 287 288 289 if (pageLimitObj != null) { 290 pageLimit = pageLimitObj.intValue(); 291 } else { 292 pageLimit = 0; 293 } 294 295 searchCriteria.setMaxRecords(pageLimit); 296 searchCriteria.setOffsetRecord(pageLimit * (getPageNumber() - 1)); 297 298 ArrayList allRecords = (ArrayList ) searchCriteria.searchAndRetrieveList(sortKey); 299 300 if (!isCountRecords()) { 301 totalRecordCount = allRecords.size(); 302 } 303 304 startRecordNumber = (pageLimit * (getPageNumber() - 1)) + 1; 305 endRecordNumber = startRecordNumber + allRecords.size() - 1; 306 307 if (endRecordNumber >= totalRecordCount) { 308 setMoreRecords(false); 309 } else { 310 setMoreRecords(true); 311 } 312 313 return allRecords; 314 } 315 327 public List searchAndRetrieve(MultiDBObject searchCriteria, String sortKey) 328 throws DBException { 329 if (isCountRecords()) { 330 totalRecordCount = searchCriteria.count(); 331 } 332 if (getPageNumber() > 1) { 333 setPreviousRecords(true); 334 } 335 336 Integer pageLimitObj = (Integer ) searchCriteria.getAttribute("pageLimit"); 337 338 if (pageLimitObj == null) { 339 this.setPageLimitAttribute(searchCriteria); 340 pageLimitObj = (Integer ) searchCriteria.getAttribute("pageLimit"); 341 } 342 343 if (pageLimitObj != null) { 344 pageLimit = pageLimitObj.intValue(); 345 } else { 346 pageLimit = 0; 347 } 348 349 searchCriteria.setMaxRecords(pageLimit); 350 searchCriteria.setOffsetRecord(pageLimit * (getPageNumber() - 1)); 351 352 List allRecords = searchCriteria.searchAndRetrieveList(sortKey); 353 354 if (!isCountRecords()) { 355 totalRecordCount = allRecords.size(); 356 } 357 358 startRecordNumber = (pageLimit * (getPageNumber() - 1)) + 1; 359 endRecordNumber = startRecordNumber + allRecords.size() - 1; 360 361 if (endRecordNumber >= totalRecordCount) { 362 setMoreRecords(false); 363 } else { 364 setMoreRecords(true); 365 } 366 367 return allRecords; 368 } 369 370 371 377 protected void setPageLimitAttribute(DataObject dbObj) 378 throws DBException { 379 380 381 DBObjLimit dl = new DBObjLimit(SecuredDBObject.SYSTEM_ACCOUNT); 382 dl.setDataContext(dbObj.getDataContext()); 383 dl.setField("DBObjectName", ((Object ) dbObj).getClass().getName()); 384 385 int pageLimit = 0; 386 387 if (dl.find()) { 388 try { 389 pageLimit = new Integer (dl.getField("PageLimit")).intValue(); 390 } catch (NumberFormatException ne) { 391 throw new DBException("Can't use limit of '" + 392 dl.getField("PageLimit") + "' for " + 393 ((Object ) dbObj).getClass().getName()); 394 } 395 396 dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1); 397 dbObj.setAttribute("pageLimit", new Integer (pageLimit)); 398 } else { 399 400 dl.setField("DBObjectName", 402 "com.jcorporate.expresso.services.dbobj.ControllerDefault"); 403 404 if (dl.find()) { 405 try { 406 pageLimit = new Integer (dl.getField("PageLimit")).intValue(); 407 } catch (NumberFormatException ne) { 408 throw new DBException("Can't use limit of '" + 409 dl.getField("PageLimit") + "' for " + 410 ((Object ) dbObj).getClass().getName()); 411 } 412 413 dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1); 414 dbObj.setAttribute("pageLimit", new Integer (pageLimit)); 415 } else { 416 pageLimit = 0; 417 } 418 } 419 } 420 421 427 protected void setPageLimitAttribute(MultiDBObject dbObj) 428 throws DBException { 429 430 431 DBObjLimit dl = new DBObjLimit(SecuredDBObject.SYSTEM_ACCOUNT); 432 dl.setDataContext(dbObj.getDBName()); 433 dl.setField("DBObjectName", ((Object ) dbObj).getClass().getName()); 434 435 int pageLimit = 0; 436 437 if (dl.find()) { 438 try { 439 pageLimit = new Integer (dl.getField("PageLimit")).intValue(); 440 } catch (NumberFormatException ne) { 441 throw new DBException("Can't use limit of '" + 442 dl.getField("PageLimit") + "' for " + 443 ((Object ) dbObj).getClass().getName()); 444 } 445 446 dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1); 447 dbObj.setAttribute("pageLimit", new Integer (pageLimit)); 448 } else { 449 450 dl.setField("DBObjectName", 452 "com.jcorporate.expresso.services.dbobj.ControllerDefault"); 453 454 if (dl.find()) { 455 try { 456 pageLimit = new Integer (dl.getField("PageLimit")).intValue(); 457 } catch (NumberFormatException ne) { 458 throw new DBException("Can't use limit of '" + 459 dl.getField("PageLimit") + "' for " + 460 ((Object ) dbObj).getClass().getName()); 461 } 462 463 dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1); 464 dbObj.setAttribute("pageLimit", new Integer (pageLimit)); 465 } else { 466 pageLimit = 0; 467 } 468 } 469 } 470 471 480 public void setPageNumber(ControllerRequest request) 481 throws ControllerException { 482 String pg = request.getParameter("page"); 483 484 if (pg != null) { 485 try { 486 this.setPageNumber(Integer.parseInt(pg)); 487 } catch (NumberFormatException nfe) { 488 throw new ControllerException("Page number parameter is not an integer"); 489 } 490 } else { 491 setPageNumber(1); 492 } 493 } 494 } | Popular Tags |