001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 07/02/2005 - 10:29:14
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.repository;
044:
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Iterator;
048: import java.util.LinkedHashMap;
049: import java.util.List;
050: import java.util.Map;
051:
052: import net.jforum.cache.CacheEngine;
053: import net.jforum.cache.Cacheable;
054: import net.jforum.dao.DataAccessDriver;
055: import net.jforum.dao.PostDAO;
056: import net.jforum.entities.Post;
057: import net.jforum.util.preferences.ConfigKeys;
058: import net.jforum.util.preferences.SystemGlobals;
059: import net.jforum.view.forum.common.PostCommon;
060:
061: /**
062: * Repository for the post in the top n topics for each forum.
063: *
064: * @author Sean Mitchell
065: * @author Rafael Steil
066: * @version $Id: PostRepository.java,v 1.13 2006/08/23 02:13:48 rafaelsteil Exp $
067: */
068: public class PostRepository implements Cacheable {
069: private static final int CACHE_SIZE = SystemGlobals
070: .getIntValue(ConfigKeys.POSTS_CACHE_SIZE);
071: private static final String FQN = "posts";
072: private static CacheEngine cache;
073:
074: /**
075: * @see net.jforum.cache.Cacheable#setCacheEngine(net.jforum.cache.CacheEngine)
076: */
077: public void setCacheEngine(CacheEngine engine) {
078: cache = engine;
079: }
080:
081: public static int size() {
082: Map m = (Map) cache.get(FQN);
083: return (m != null ? m.size() : 0);
084: }
085:
086: public static int size(int topicId) {
087: List posts = (List) cache.get(FQN, Integer.toString(topicId));
088: return (posts == null ? 0 : posts.size());
089: }
090:
091: public static Collection cachedTopics() {
092: Map m = (Map) cache.get(FQN);
093: if (m == null) {
094: return new ArrayList();
095: }
096:
097: return m.keySet();
098: }
099:
100: public static List selectAllByTopicByLimit(int topicId, int start,
101: int count) {
102: String tid = Integer.toString(topicId);
103:
104: List posts = (List) cache.get(FQN, tid);
105: if (posts == null || posts.size() == 0) {
106: PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
107: posts = pm.selectAllByTopic(topicId);
108:
109: for (Iterator iter = posts.iterator(); iter.hasNext();) {
110: PostCommon.preparePostForDisplay((Post) iter.next());
111: }
112:
113: Map topics = (Map) cache.get(FQN);
114: if (topics == null || topics.size() == 0
115: || topics.size() < CACHE_SIZE) {
116: cache.add(FQN, tid, posts);
117: } else {
118: if (!(topics instanceof LinkedHashMap)) {
119: topics = new LinkedHashMap(topics) {
120: protected boolean removeEldestEntry(
121: java.util.Map.Entry eldest) {
122: return this .size() > CACHE_SIZE;
123: }
124: };
125: }
126:
127: topics.put(tid, posts);
128: cache.add(FQN, topics);
129: }
130: }
131:
132: int size = posts.size();
133: return posts.subList(start, (size < start + count) ? size
134: : start + count);
135: }
136:
137: public static void remove(int topicId, int postId) {
138: synchronized (FQN) {
139: String tid = Integer.toString(topicId);
140:
141: List posts = (List) cache.get(FQN, tid);
142:
143: if (posts != null) {
144: Post p = new Post();
145: p.setId(postId);
146: posts.remove(p);
147:
148: cache.add(FQN, tid, posts);
149: }
150: }
151: }
152:
153: public static void update(int topicId, Post p) {
154: String tid = Integer.toString(topicId);
155: List posts = (List) cache.get(FQN, tid);
156: if (posts != null && posts.contains(p)) {
157: posts.set(posts.indexOf(p), p);
158: cache.add(FQN, tid, posts);
159: }
160: }
161:
162: public static void append(int topicId, Post p) {
163: String tid = Integer.toString(topicId);
164: List posts = (List) cache.get(FQN, tid);
165: if (posts != null && !posts.contains(p)) {
166: posts.add(p);
167: cache.add(FQN, tid, posts);
168: }
169: }
170:
171: public static void clearCache(int topicId) {
172: cache.remove(FQN, Integer.toString(topicId));
173: }
174: }
|