⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forumrepository.java

📁 个人认为是最好的Java论坛源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if (relation == null) {
			relation = new HashMap();
		}
		
		for (Iterator iter = c.getForums().iterator(); iter.hasNext(); ) {
			Forum f = (Forum)iter.next();
			relation.put(Integer.toString(f.getId()), categoryId);
		}
		
		cache.add(FQN, RELATION, relation);
	}
	
	/**
	 * Gets a specific forum from the cache.	 
	 * 
	 * @param forumId The forum's ID to get
	 * @return <code>net.jforum.Forum</code> object instance or <code>null</code>
	 * if the forum was not found or is not accessible to the user.
	 */
	public static Forum getForum(int forumId)
	{
		String categoryId = (String)((Map)cache.get(FQN, RELATION)).get(Integer.toString(forumId));
		return ((Category)cache.get(FQN, categoryId)).getForum(forumId);
	}
	
	public static boolean isForumAccessible(int forumId)
	{
		return isForumAccessible(SessionFacade.getUserSession().getUserId(), forumId);
	}
	
	public static boolean isForumAccessible(int userId, int forumId)
	{
		int categoryId = Integer.parseInt((String)((Map)cache.get(FQN, RELATION)).get(Integer.toString(forumId)));
		return isForumAccessible(userId, categoryId, forumId);
	}
	
	public static boolean isForumAccessible(int userId, int categoryId, int forumId)
	{
		return ((Category)cache.get(FQN, Integer.toString(categoryId))).getForum(userId, forumId) != null;
	}
	
	/**
	 * Adds a new forum to the cache repository.	 
	 * 
	 * @param forum The forum to add
	 */
	public synchronized static void addForum(Forum forum)
	{
		String categoryId = Integer.toString(forum.getCategoryId());

		Category c = (Category)cache.get(FQN, categoryId);
		c.addForum(forum);
		cache.add(FQN, categoryId, c);
		
		Map m = (Map)cache.get(FQN, RELATION);
		m.put(Integer.toString(forum.getId()), categoryId);
		cache.add(FQN, RELATION, m);
		
		Set s = (Set)cache.get(FQN, CATEGORIES_SET);
		cache.add(FQN, CATEGORIES_SET, s);
	}
	
	/**
	 * Removes a forum from the cache.
	 * 
	 * @param forum The forum instance to remove.
	 */
	public synchronized static void removeForum(Forum forum)
	{
		String id = Integer.toString(forum.getId());
		Map m = (Map)cache.get(FQN, RELATION);
		m.remove(id);
		cache.add(FQN, RELATION, m);

		id = Integer.toString(forum.getCategoryId());
		
		Category c = (Category)cache.get(FQN, id);
		c.removeForum(forum.getId());
		cache.add(FQN, id, c);
		
		Set s = (Set)cache.get(FQN, CATEGORIES_SET);
		cache.add(FQN, CATEGORIES_SET, s);
	}
	
	/**
	 * Reloads a forum.
	 * The forum should already be in the cache and <b>SHOULD NOT</b>
	 * have its order changed. If the forum's order was changed, 
	 * then you <b>MUST CALL</b> @link Category#changeForumOrder(Forum) <b>BEFORE</b>
	 * calling this method.
	 * 
	 * @param forum The forum to reload its information
	 * @throws Exception
	 */
	public static synchronized void reloadForum(int forumId) throws Exception
	{
		Forum f = DataAccessDriver.getInstance().newForumModel().selectById(forumId);
		
		if (((Map)cache.get(FQN, RELATION)).containsKey(Integer.toString(forumId))) {
			String id = Integer.toString(f.getCategoryId());
			Category c = (Category)cache.get(FQN, id);
			
			f.setLastPostInfo(null);
			f.setLastPostInfo(ForumRepository.getLastPostInfo(f));
			c.reloadForum(f);
			
			cache.add(FQN, id, c);
			Set s = (Set)cache.get(FQN, CATEGORIES_SET);
			cache.add(FQN, CATEGORIES_SET, s);
		}
		
		getTotalMessages(true);
	}
	
	/**
	 * Gets information about the last message posted in some forum.
	 * @param forum The forum to retrieve information
	 * @return 
	 */
	public static LastPostInfo getLastPostInfo(Forum forum) throws Exception
	{
		LastPostInfo lpi = forum.getLastPostInfo();
		
		if (lpi == null || !forum.getLastPostInfo().hasInfo()) {
			lpi = DataAccessDriver.getInstance().newForumModel().getLastPostInfo(forum.getId());
			forum.setLastPostInfo(lpi);
		}
		
		return lpi;
	}
	
	/**
	 * Gets information about the last message posted in some forum.
	 * 
	 * @param forumId The forum's id to retrieve information
	 * @return
	 * @throws Exception
	 */
	public static LastPostInfo getLastPostInfo(int forumId) throws Exception
	{
		return getLastPostInfo(getForum(forumId));
	}
	
	/**
	 * Gets the number of topics in some forum.
	 * 
	 * @param forumId The forum's id to retrieve the number of topics
	 * @param fromDb If <code>true</code>, a query to the database will be made 
	 * to get the number of topics. If <code>false</code>, the cached information
	 * will be returned
	 * @return The number of topics
	 * @throws Exception
	 * @see #getTotalTopics(int)
	 */
	public static int getTotalTopics(int forumId, boolean fromDb) throws Exception
	{
		Integer i = (Integer)cache.get(FQN_TOTAL_TOPICS, Integer.toString(forumId));
		int total = (i != null ? i.intValue() : 0);
		
		if (fromDb || total == -1) {
			total = DataAccessDriver.getInstance().newForumModel().getTotalTopics(forumId);
			cache.add(FQN_TOTAL_TOPICS, Integer.toString(forumId), new Integer(total));
		}
		
		return total;
	}
	
	/**
	 * Gets the number of topics in some forum.
	 * @param forumId The forum's id to retrieve the number of topics
	 * @return The number of topics
	 * @throws Exception
	 * @see #getTotalTopics(int, boolean)
	 */
	public static int getTotalTopics(int forumId) throws Exception
	{
		return ForumRepository.getTotalTopics(forumId, false); 
	}
	
	/**
	 * Gets the number of messages in the entire board.
	 * @return 
	 * @throws Exception
	 * @see #getTotalMessages(boolean)
	 */
	public static int getTotalMessages() throws Exception
	{
		return getTotalMessages(false);
	}

	/**
	 * Gets the number of messags in the entire board.
	 * 
	 * @param fromDb If <code>true</code>, a query to the database will
	 * be made, to retrieve the desired information. If <code>false</code>, the
	 * data will be fetched from the cache.
	 * @return The number of messages posted in the board.
	 * @throws Exception
	 * @see #getTotalMessages()
	 */
	public static int getTotalMessages(boolean fromDb) throws Exception
	{
		int total = ((Integer)cache.get(FQN, TOTAL_MESSAGES)).intValue();
		if (fromDb || total == 0) {
			total = DataAccessDriver.getInstance().newForumModel().getTotalMessages();
			cache.add(FQN, TOTAL_MESSAGES, new Integer(total));
		}
		
		return total;
	}
	
	/**
	 * Gets the number of most online users ever
	 * @return
	 */
	public static MostUsersEverOnline getMostUsersEverOnline()
	{
		return (MostUsersEverOnline)cache.get(FQN, MOST_USERS_ONLINE);
	}
	
	/**
	 * Update the value of most online users ever.
	 * 
	 * @param newValue The new value to store. Generally it
	 * will be a bigger one.
	 * @throws Exception
	 */
	public static void updateMostUsersEverOnline(MostUsersEverOnline m) throws Exception
	{
		ConfigModel cm = DataAccessDriver.getInstance().newConfigModel();
		Config config = cm.selectByName(ConfigKeys.MOST_USERS_EVER_ONLINE);
		if (config == null) {
			// Total
			config = new Config();
			config.setName(ConfigKeys.MOST_USERS_EVER_ONLINE);
			config.setValue(Integer.toString(m.getTotal()));
			
			cm.insert(config);
			
			// Date
			config.setName(ConfigKeys.MOST_USER_EVER_ONLINE_DATE);
			config.setValue(Long.toString(m.getTimeInMillis()));
			
			cm.insert(config);
		}
		else {
			// Total
			config.setValue(Integer.toString(m.getTotal()));
			cm.update(config);

			// Date
			config.setName(ConfigKeys.MOST_USER_EVER_ONLINE_DATE);
			config.setValue(Long.toString(m.getTimeInMillis()));
			cm.update(config);
		}
		
		cache.add(FQN, MOST_USERS_ONLINE, m);
	}
	
	/**
	 * Loads all forums.
	 * @throws Exception
	 */
	private void loadForums(ForumModel fm) throws Exception
	{
		List l = fm.selectAll();
		
		Map m = (Map)cache.get(FQN, RELATION);
		if (m == null) {
			m = new HashMap();
		}
		
		int lastId = 0;
		Category c = null;
		String catId = null;

		for (Iterator iter = l.iterator(); iter.hasNext(); ) {
			Forum f = (Forum)iter.next();
			
			if (f.getCategoryId() != lastId) {
				if (c != null) {
					cache.add(FQN, catId, c);
				}
				
				lastId = f.getCategoryId();
				catId = Integer.toString(f.getCategoryId());
				c = (Category)cache.get(FQN, catId);
			}
			
			if (c == null) {
				throw new CategoryNotFoundException("Category for forum #" + f.getId() + " not found");
			}
			
			String forumId = Integer.toString(f.getId());
			c.addForum(f);
			m.put(forumId, catId);
			cache.add(FQN_TOTAL_TOPICS, forumId, new Integer(-1));
		}
		
		if (c != null) {
			cache.add(FQN, catId, c);
		}
		
		cache.add(FQN, RELATION, m);
	}

	/**
	 * Loads all categories.
	 * @throws Exception 
	 */
	private void loadCategories(CategoryModel cm) throws Exception
	{
		List categories = cm.selectAll();
		Set categoriesSet = new TreeSet(new CategoryOrderComparator());
		
		for (Iterator iter = categories.iterator(); iter.hasNext(); ) {
			Category c = (Category)iter.next();
			
			cache.add(FQN, Integer.toString(c.getId()), c);
			categoriesSet.add(c);
		}
		
		cache.add(FQN, CATEGORIES_SET, categoriesSet);
	}
	
	private void loadMostUsersEverOnline(ConfigModel cm) throws Exception
	{
		Config config = cm.selectByName(ConfigKeys.MOST_USERS_EVER_ONLINE);
		MostUsersEverOnline mostUsersEverOnline = new MostUsersEverOnline();
		
		if (config != null) {
			mostUsersEverOnline.setTotal(Integer.parseInt(config.getValue()));
			
			// We're assuming that, if we have one key, the another one
			// will always exist
			config = cm.selectByName(ConfigKeys.MOST_USER_EVER_ONLINE_DATE);
			mostUsersEverOnline.setTimeInMillis(Long.parseLong(config.getValue()));
		}
		
		cache.add(FQN, MOST_USERS_ONLINE, mostUsersEverOnline);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -