Search in sources :

Example 1 with CategoryNotFoundException

use of net.jforum.exceptions.CategoryNotFoundException 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 2 with CategoryNotFoundException

use of net.jforum.exceptions.CategoryNotFoundException 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)

Aggregations

ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Category (net.jforum.entities.Category)2 CategoryNotFoundException (net.jforum.exceptions.CategoryNotFoundException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Forum (net.jforum.entities.Forum)1 DatabaseException (net.jforum.exceptions.DatabaseException)1 PermissionControl (net.jforum.security.PermissionControl)1