RankingRepository.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * Redistribution and use in source and binary forms, 
  5.  * with or without modification, are permitted provided 
  6.  * that the following conditions are met:
  7.  * 1) Redistributions of source code must retain the above 
  8.  * copyright notice, this list of conditions and the 
  9.  * following  disclaimer.
  10.  * 2)  Redistributions in binary form must reproduce the 
  11.  * above copyright notice, this list of conditions and 
  12.  * the following disclaimer in the documentation and/or 
  13.  * other materials provided with the distribution.
  14.  * 3) Neither the name of "Rafael Steil" nor 
  15.  * the names of its contributors may be used to endorse 
  16.  * or promote products derived from this software without 
  17.  * specific prior written permission.
  18.  * 
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  20.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  21.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  22.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  24.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  25.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  27.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  28.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  30.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  31.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  32.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  33.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  34.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  35.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  36.  * 
  37.  * This file creation date: 03/09/2003 / 23:42:55
  38.  * The JForum Project
  39.  * http://www.jforum.net
  40.  */
  41. package net.jforum.repository;
  42. import java.util.Iterator;
  43. import java.util.List;
  44. import net.jforum.cache.CacheEngine;
  45. import net.jforum.cache.Cacheable;
  46. import net.jforum.dao.DataAccessDriver;
  47. import net.jforum.dao.RankingDAO;
  48. import net.jforum.entities.Ranking;
  49. import net.jforum.exceptions.RankingLoadException;
  50. /**
  51.  * @author Rafael Steil
  52.  * @version $Id: RankingRepository.java,v 1.15 2007/09/19 02:51:11 rafaelsteil Exp $
  53.  */
  54. public class RankingRepository implements Cacheable
  55. {
  56. private static CacheEngine cache;
  57. private static final String FQN = "ranking";
  58. private static final String ENTRIES = "entries";
  59. /**
  60.  * @see net.jforum.cache.Cacheable#setCacheEngine(net.jforum.cache.CacheEngine)
  61.  */
  62. public void setCacheEngine(CacheEngine engine)
  63. {
  64. cache = engine;
  65. }
  66. public static void loadRanks()
  67. {
  68. try {
  69. RankingDAO rm = DataAccessDriver.getInstance().newRankingDAO();
  70. cache.add(FQN, ENTRIES, rm.selectAll());
  71. }
  72. catch (Exception e) {
  73. throw new RankingLoadException("Error while loading the rankings: " + e);
  74. }
  75. }
  76. public static int size()
  77. {
  78. return ((List)cache.get(FQN, ENTRIES)).size();
  79. }
  80. /**
  81.  * Gets the title associated to total of messages the user have
  82.  * @param total Number of messages the user have. The ranking will be
  83.  * returned according to the range to which this total belongs to. 
  84.  * @return String with the ranking title. 
  85.  */
  86. public static String getRankTitle(int rankId, int total) 
  87. {
  88. String title = null;
  89. if (rankId > 0) {
  90. title = getRankTitleById(rankId);
  91. }
  92. if (title == null) {
  93. title = getRankTitleByPosts(total);
  94. }
  95. return title;
  96. }
  97. private static String getRankTitleByPosts(int total)
  98. {
  99. Ranking lastRank = new Ranking();
  100. List entries = (List)cache.get(FQN, ENTRIES);
  101. for (Iterator iter = entries.iterator(); iter.hasNext(); ) {
  102. Ranking r = (Ranking)iter.next();
  103. if (total == r.getMin() && !r.isSpecial()) {
  104. return r.getTitle();
  105. }
  106. else if (total > lastRank.getMin() && total < r.getMin()) {
  107. return lastRank.getTitle();
  108. }
  109. lastRank = r;
  110. }
  111. return lastRank.getTitle();
  112. }
  113. private static String getRankTitleById(int rankId)
  114. {
  115. Ranking r = new Ranking();
  116. r.setId(rankId);
  117. List l = (List)cache.get(FQN, ENTRIES);
  118. int index = l.indexOf(r);
  119. return index > -1
  120. ? ((Ranking)l.get(index)).getTitle()
  121. : null;
  122. }
  123. }