1 43 package net.jforum.repository; 44 45 import java.util.Iterator ; 46 import java.util.List ; 47 48 import net.jforum.cache.CacheEngine; 49 import net.jforum.cache.Cacheable; 50 import net.jforum.dao.DataAccessDriver; 51 import net.jforum.dao.RankingDAO; 52 import net.jforum.entities.Ranking; 53 import net.jforum.exceptions.RankingLoadException; 54 55 59 public class RankingRepository implements Cacheable 60 { 61 private static CacheEngine cache; 62 private static final String FQN = "ranking"; 63 private static final String ENTRIES = "entries"; 64 65 68 public void setCacheEngine(CacheEngine engine) 69 { 70 cache = engine; 71 } 72 73 public static void loadRanks() 74 { 75 try { 76 RankingDAO rm = DataAccessDriver.getInstance().newRankingDAO(); 77 cache.add(FQN, ENTRIES, rm.selectAll()); 78 } 79 catch (Exception e) { 80 throw new RankingLoadException("Error while loading the rankings: " + e); 81 } 82 } 83 84 public static int size() 85 { 86 return ((List )cache.get(FQN, ENTRIES)).size(); 87 } 88 89 95 public static String getRankTitle(int total) 96 { 97 Ranking lastRank = new Ranking(); 98 99 List entries = (List )cache.get(FQN, ENTRIES); 100 for (Iterator iter = entries.iterator(); iter.hasNext(); ) { 101 Ranking r = (Ranking)iter.next(); 102 103 if (total == r.getMin()) { 104 return r.getTitle(); 105 } 106 else if (total > lastRank.getMin() && total < r.getMin()) { 107 return lastRank.getTitle(); 108 } 109 110 lastRank = r; 111 } 112 113 return lastRank.getTitle(); 114 } 115 116 } 117 | Popular Tags |