1 41 package com.mvnforum.db; 42 43 import java.util.ArrayList ; 44 45 import com.mvnforum.db.jdbc.RankDAOImplJDBC; 46 import net.myvietnam.mvncore.exception.DatabaseException; 47 import net.myvietnam.mvncore.exception.ObjectNotFoundException; 48 49 public final class RankCache { 50 51 static private RankCache instance = new RankCache(); 53 54 private ArrayList beanArray = null; 56 57 60 private RankCache() { 61 } 62 63 69 static public RankCache getInstance() { 70 return instance; 71 } 72 73 77 private void ensureNewData() throws DatabaseException { 78 if ( RankDAOImplJDBC.isDirty() || (beanArray == null) ) { 79 RankDAOImplJDBC.setDirty(false); 80 beanArray = (ArrayList )DAOFactory.getRankDAO().getRanks(); 81 } 82 } 83 84 92 public synchronized ArrayList getBeans() throws DatabaseException { 93 ensureNewData(); 94 return beanArray; 95 } 96 97 public synchronized RankBean getBean(int rankID) 98 throws DatabaseException, ObjectNotFoundException { 99 100 ensureNewData(); 101 int size = beanArray.size(); 102 for (int i = 0; i < size; i++) { 103 RankBean bean = (RankBean)beanArray.get(i); 104 if (bean.getRankID() == rankID) { 105 return bean; 106 } 107 } 108 throw new ObjectNotFoundException("Cannot find the row in table Rank where primary key = (" + rankID + ")."); 110 } 111 112 118 public synchronized void reload() throws DatabaseException { 119 RankDAOImplJDBC.setDirty(false); 120 beanArray = (ArrayList )DAOFactory.getRankDAO().getRanks(); 121 } 122 } 123 | Popular Tags |