Search in sources :

Example 11 with Category

use of net.jforum.entities.Category in project jforum2 by rafaelsteil.

the class ForumRepository method loadForums.

/**
	 * Loads all forums.
     * @param fm ForumDAO
     */
private void loadForums(ForumDAO fm) {
    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);
    }
    if (c != null) {
        cache.add(FQN, catId, c);
    }
    cache.add(FQN, RELATION, m);
}
Also used : Category(net.jforum.entities.Category) HashMap(java.util.HashMap) Iterator(java.util.Iterator) CategoryNotFoundException(net.jforum.exceptions.CategoryNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Forum(net.jforum.entities.Forum)

Example 12 with Category

use of net.jforum.entities.Category in project jforum2 by rafaelsteil.

the class ForumRepository method loadCategories.

/**
	 * Loads all categories.
     * @param cm CategoryDAO
     */
private void loadCategories(CategoryDAO cm) {
    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);
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) Category(net.jforum.entities.Category) CategoryOrderComparator(net.jforum.util.CategoryOrderComparator) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with Category

use of net.jforum.entities.Category in project jforum2 by rafaelsteil.

the class ForumRepository method refreshForum.

public static synchronized void refreshForum(Forum forum) {
    Category c = retrieveCategory(forum.getCategoryId());
    c.addForum(forum);
    refreshCategory(c);
}
Also used : Category(net.jforum.entities.Category)

Example 14 with Category

use of net.jforum.entities.Category in project jforum2 by rafaelsteil.

the class ForumRepository method getAllCategories.

/**
	 * Gets all categories from the cache. 
	 *
     * @param userId int
	 * @return <code>List</code> with the categories. Each entry is a <code>Category</code> object.
	 */
public static List getAllCategories(int userId) {
    PermissionControl pc = SecurityRepository.get(userId);
    List l = new ArrayList();
    Set categoriesSet = (Set) cache.get(FQN, CATEGORIES_SET);
    if (categoriesSet == null) {
        synchronized (ForumRepository.instance) {
            if (categoriesSet == null) {
                logger.warn("Categories set returned null from the cache. Trying to reload");
                try {
                    ForumRepository.instance.loadCategories(DataAccessDriver.getInstance().newCategoryDAO());
                    ForumRepository.instance.loadForums(DataAccessDriver.getInstance().newForumDAO());
                } catch (Exception e) {
                    throw new CategoryNotFoundException("Failed to get the category", e);
                }
                categoriesSet = (Set) cache.get(FQN, CATEGORIES_SET);
                if (categoriesSet == null) {
                    throw new CategoryNotFoundException("Could not find all categories. There must be a problem with the cache");
                }
            }
        }
    }
    for (Iterator iter = categoriesSet.iterator(); iter.hasNext(); ) {
        Category c = getCategory(pc, ((Category) iter.next()).getId());
        if (c != null) {
            l.add(c);
        }
    }
    return l;
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) Category(net.jforum.entities.Category) PermissionControl(net.jforum.security.PermissionControl) ArrayList(java.util.ArrayList) CategoryNotFoundException(net.jforum.exceptions.CategoryNotFoundException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseException(net.jforum.exceptions.DatabaseException) CategoryNotFoundException(net.jforum.exceptions.CategoryNotFoundException)

Example 15 with Category

use of net.jforum.entities.Category in project jforum2 by rafaelsteil.

the class ForumRepository method reloadForum.

/**
	 * 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 forumId int The forum to reload its information
	 */
public static synchronized void reloadForum(int forumId) {
    Forum f = DataAccessDriver.getInstance().newForumDAO().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);
}
Also used : Category(net.jforum.entities.Category) TreeSet(java.util.TreeSet) Set(java.util.Set) HashMap(java.util.HashMap) Map(java.util.Map) Forum(net.jforum.entities.Forum)

Aggregations

Category (net.jforum.entities.Category)20 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Forum (net.jforum.entities.Forum)7 Iterator (java.util.Iterator)6 Map (java.util.Map)6 Set (java.util.Set)6 TreeSet (java.util.TreeSet)6 HashMap (java.util.HashMap)5 CategoryNotFoundException (net.jforum.exceptions.CategoryNotFoundException)2 DatabaseException (net.jforum.exceptions.DatabaseException)2 PermissionControl (net.jforum.security.PermissionControl)2 CategoryOrderComparator (net.jforum.util.CategoryOrderComparator)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 ForumDAO (net.jforum.dao.ForumDAO)1 GroupSecurityDAO (net.jforum.dao.GroupSecurityDAO)1